Skip to content

Commit

Permalink
validation to reject multiples replicas when disk storage enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed Sep 8, 2023
1 parent b29301a commit 1bbfc55
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ jobs:
go-version: 1.20.x
id: go
- uses: actions/checkout@v3
- name: Create k8s Kind Cluster
uses: helm/kind-action@v1.2.0
with:
version: v0.20.0
config: utils/kind-cluster.yaml
cluster_name: limitador-local
wait: 120s
- name: Check cluster info
run: |
kubectl cluster-info dump
- name: Run the tests
run: make test
- name: Upload coverage reports to Codecov
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ vet: ## Run go vet against code.
go vet ./...

test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) $(ARCH_PARAM) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) $(ARCH_PARAM) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out -v

##@ Build

Expand Down
2 changes: 2 additions & 0 deletions api/v1alpha1/limitador_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ type Limitador struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

// +kubebuilder:validation:Optional
// +kubebuilder:validation:XValidation:rule="(!has(self.storage) || !has(self.storage.disk)) || (!has(self.replicas) || self.replicas < 2)",message="disk storage does not allow multiple replicas"
Spec LimitadorSpec `json:"spec,omitempty"`
Status LimitadorStatus `json:"status,omitempty"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ metadata:
capabilities: Basic Install
categories: Integration & Delivery
containerImage: quay.io/kuadrant/limitador-operator:latest
createdAt: "2023-09-04T14:22:55Z"
createdAt: "2023-09-08T08:41:16Z"
operators.operatorframework.io/builder: operator-sdk-v1.28.1
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
repository: https://github.com/Kuadrant/limitador-operator
Expand Down
4 changes: 4 additions & 0 deletions bundle/manifests/limitador.kuadrant.io_limitadors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ spec:
version:
type: string
type: object
x-kubernetes-validations:
- message: disk storage does not allow multiple replicas
rule: (!has(self.storage) || !has(self.storage.disk)) || (!has(self.replicas)
|| self.replicas < 2)
status:
description: LimitadorStatus defines the observed state of Limitador
properties:
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/limitador.kuadrant.io_limitadors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ spec:
version:
type: string
type: object
x-kubernetes-validations:
- message: disk storage does not allow multiple replicas
rule: (!has(self.storage) || !has(self.storage.disk)) || (!has(self.replicas)
|| self.replicas < 2)
status:
description: LimitadorStatus defines the observed state of Limitador
properties:
Expand Down
34 changes: 33 additions & 1 deletion controllers/limitador_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ var _ = Describe("Limitador controller", func() {
Equal("/home/limitador/etc/limitador-config.yaml"),
)
Expect(createdLimitadorDeployment.Spec.Template.Spec.Containers[0].VolumeMounts[0].MountPath).Should(
Equal("/home/limitador/etc/"),
Equal("/home/limitador/etc"),
)
Expect(createdLimitadorDeployment.Spec.Template.Spec.Volumes[0].VolumeSource.ConfigMap.Name).Should(
Equal(limitador.LimitsConfigMapName(limitadorObj)),
Expand Down Expand Up @@ -579,4 +579,36 @@ var _ = Describe("Limitador controller", func() {

})
})

// This test requires actual k8s cluster
// It's testing implementation based on CRD x-kubernetes-validations extentions
// used to alidate custom resources using Common Expression Language (CEL)
// https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation-rules
Context("Disk storage does not allow multiple replicas", func() {
AfterEach(func() {
limitadorObj := limitadorWithInvalidDiskReplicas()
err := k8sClient.Delete(context.TODO(), limitadorObj, deletePropagationPolicy)
Expect(err == nil || errors.IsNotFound(err))
})

It("resource is rejected", func() {
limitadorObj := limitadorWithInvalidDiskReplicas()
err := k8sClient.Create(context.TODO(), limitadorObj)
Expect(err).To(HaveOccurred())
Expect(errors.IsInvalid(err)).To(BeTrue())
})
})
})

func limitadorWithInvalidDiskReplicas() *limitadorv1alpha1.Limitador {
return &limitadorv1alpha1.Limitador{
TypeMeta: metav1.TypeMeta{Kind: "Limitador", APIVersion: "limitador.kuadrant.io/v1alpha1"},
ObjectMeta: metav1.ObjectMeta{Name: "limitador-with-invalid-disk-replicas", Namespace: "default"},
Spec: limitadorv1alpha1.LimitadorSpec{
Replicas: &[]int{2}[0],
Storage: &limitadorv1alpha1.Storage{
Disk: &limitadorv1alpha1.DiskSpec{},
},
},
}
}
1 change: 1 addition & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var _ = BeforeSuite(func() {
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,
UseExistingCluster: &[]bool{true}[0],
}

cfg, err := testEnv.Start()
Expand Down
10 changes: 10 additions & 0 deletions doc/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ make local-cleanup

## Run tests

You need an active session open to a kubernetes cluster.

Optionally, run kind and deploy kuadrant deps

```sh
make local-env-setup
```

Run tests

```sh
make test
```
Expand Down

0 comments on commit 1bbfc55

Please sign in to comment.