Skip to content

Commit

Permalink
test: add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
raffis committed Dec 12, 2023
1 parent b6b2775 commit 9c4bea4
Show file tree
Hide file tree
Showing 23 changed files with 1,018 additions and 532 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.dylib
bin
testbin/*
manager

# Test binary, build with `go test -c`
*.test
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ build: generate fmt vet tidy ## Build manager binary.

.PHONY: run
run: manifests generate fmt vet tidy ## Run a controller from your host.
go run ./main.go
go run ./main.go --log-level=debug

# Find or download gen-crd-api-reference-docs
GEN_CRD_API_REFERENCE_DOCS = $(GOBIN)/gen-crd-api-reference-docs
Expand Down
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ rely on.
If no pods are running matching either `app: backend` or `app: another-rds-client` the aws instance named `rds-myname` will be terminated after
a grace period of 5 minutes.

**Note**: If no grace period is set the instance will be terminated immediately after the condition ScaledToZero is set to `True`.

```yaml
kind: AWSRDSInstance
metadata:
Expand Down Expand Up @@ -88,7 +90,9 @@ type: Opaque

Each resource reports various conditions in `.status.condtions` which will give the necessary insight about the
current state of the resource.
Namely there are three conditions which are `Ready`, `Reconciling` and `PodsReady`.
Namely there are the conditions `Ready` and `ScaledToZero`.
ScaledToZero will give insigh whether the target rules have pods elected as up or down and Ready gives insight about
the reconcililation status itself.

```yaml
status:
Expand All @@ -99,12 +103,12 @@ status:
reason: ReconciliationFailed
status: "False"
type: Ready
- lastTransitionTime: "2023-11-24T09:46:42Z"
message: ""
observedGeneration: 32
reason: Progressing
status: "True"
type: Reconciling
- lastTransitionTime: "2023-12-11T14:03:31Z"
message: selector matches at least one running pod
observedGeneration: 3
reason: PodsRunning
status: "False"
type: ScaledToZero
```

## Installation
Expand Down
16 changes: 12 additions & 4 deletions api/v1beta1/awsrdsinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ type AWSRDSInstance struct {
}

type AWSRDSInstanceSpec struct {
Options `json:",inline"`
Secret LocalObjectReference `json:"localObkjectReference,omitempty"`
Region string `json:"region,omitempty"`
InstanceName string `json:"instanceName,omitempty"`
Suspend bool `json:"suspend,omitempty"`
ScaleToZero []metav1.LabelSelector `json:"scaleToZero,omitempty"`
GracePeriod metav1.Duration `json:"gracePeriod,omitempty"`
Interval metav1.Duration `json:"interval,omitempty"`
Secret LocalObjectReference `json:"secret,omitempty"`
Region string `json:"region,omitempty"`
InstanceName string `json:"instanceName,omitempty"`
}

// KeycloakClusterList contains a list of KeycloakCluster.
Expand Down Expand Up @@ -51,6 +54,11 @@ func AWSRDSInstanceReady(set AWSRDSInstance, status metav1.ConditionStatus, reas
return set
}

func AWSRDSInstanceScaledToZero(set AWSRDSInstance, status metav1.ConditionStatus, reason, message string) AWSRDSInstance {
setResourceCondition(&set, ConditionScaledToZero, status, reason, message, set.ObjectMeta.Generation)
return set
}

// GetStatusConditions returns a pointer to the Status.Conditions slice
func (in *AWSRDSInstance) GetStatusConditions() *[]metav1.Condition {
return &in.Status.Conditions
Expand Down
16 changes: 12 additions & 4 deletions api/v1beta1/mongodbatlascluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ type MongoDBAtlasCluster struct {
}

type MongoDBAtlasClusterSpec struct {
Options `json:",inline"`
GroupID string `json:"groupID,omitempty"`
Secret LocalObjectReference `json:"localObkjectReference,omitempty"`
ClusterName string `json:"clusterName,omitempty"`
Suspend bool `json:"suspend,omitempty"`
ScaleToZero []metav1.LabelSelector `json:"scaleToZero,omitempty"`
GracePeriod metav1.Duration `json:"gracePeriod,omitempty"`
Interval metav1.Duration `json:"interval,omitempty"`
GroupID string `json:"groupID,omitempty"`
Secret LocalObjectReference `json:"secret,omitempty"`
ClusterName string `json:"clusterName,omitempty"`
}

// KeycloakClusterList contains a list of KeycloakCluster.
Expand Down Expand Up @@ -51,6 +54,11 @@ func MongoDBAtlasClusterReady(set MongoDBAtlasCluster, status metav1.ConditionSt
return set
}

func MongoDBAtlasClusterScaledToZero(set MongoDBAtlasCluster, status metav1.ConditionStatus, reason, message string) MongoDBAtlasCluster {
setResourceCondition(&set, ConditionScaledToZero, status, reason, message, set.ObjectMeta.Generation)
return set
}

// GetStatusConditions returns a pointer to the Status.Conditions slice
func (in *MongoDBAtlasCluster) GetStatusConditions() *[]metav1.Condition {
return &in.Status.Conditions
Expand Down
15 changes: 4 additions & 11 deletions api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,15 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type Options struct {
Suspend bool `json:"suspend,omitempty"`
ScaleToZero []metav1.LabelSelector `json:"scaleToZero,omitempty"`
GracePeriod metav1.Duration `json:"gracePeriod,omitempty"`
Interval metav1.Duration `json:"interval,omitempty"`
}

type LocalObjectReference struct {
Name string `json:"name,omitempty"`
}

const (
Finalizer = "finalizers.doodle.com"
ConditionReady = "Ready"
ConditionReconciling = "Reconciling"
ConditionPodsReady = "PodsReady"
Finalizer = "finalizers.doodle.com"
ConditionReady = "Ready"
ConditionReconciling = "Reconciling"
ConditionScaledToZero = "ScaledToZero"
)

// ConditionalResource is a resource with conditions
Expand Down
44 changes: 18 additions & 26 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ spec:
type: string
interval:
type: string
localObkjectReference:
properties:
name:
type: string
type: object
region:
type: string
scaleToZero:
Expand Down Expand Up @@ -95,6 +90,11 @@ spec:
type: object
x-kubernetes-map-type: atomic
type: array
secret:
properties:
name:
type: string
type: object
suspend:
type: boolean
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ spec:
type: string
interval:
type: string
localObkjectReference:
properties:
name:
type: string
type: object
scaleToZero:
items:
description: A label selector is a label query over a set of resources.
Expand Down Expand Up @@ -95,6 +90,11 @@ spec:
type: object
x-kubernetes-map-type: atomic
type: array
secret:
properties:
name:
type: string
type: object
suspend:
type: boolean
type: object
Expand Down
Loading

0 comments on commit 9c4bea4

Please sign in to comment.