Skip to content

Commit 1bbfc55

Browse files
committed
validation to reject multiples replicas when disk storage enabled
1 parent b29301a commit 1bbfc55

9 files changed

+66
-3
lines changed

.github/workflows/test.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ jobs:
1919
go-version: 1.20.x
2020
id: go
2121
- uses: actions/checkout@v3
22+
- name: Create k8s Kind Cluster
23+
uses: helm/[email protected]
24+
with:
25+
version: v0.20.0
26+
config: utils/kind-cluster.yaml
27+
cluster_name: limitador-local
28+
wait: 120s
29+
- name: Check cluster info
30+
run: |
31+
kubectl cluster-info dump
2232
- name: Run the tests
2333
run: make test
2434
- name: Upload coverage reports to Codecov

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ vet: ## Run go vet against code.
199199
go vet ./...
200200

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

204204
##@ Build
205205

api/v1alpha1/limitador_types.go

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ type Limitador struct {
9191
metav1.TypeMeta `json:",inline"`
9292
metav1.ObjectMeta `json:"metadata,omitempty"`
9393

94+
// +kubebuilder:validation:Optional
95+
// +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"
9496
Spec LimitadorSpec `json:"spec,omitempty"`
9597
Status LimitadorStatus `json:"status,omitempty"`
9698
}

bundle/manifests/limitador-operator.clusterserviceversion.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ metadata:
3636
capabilities: Basic Install
3737
categories: Integration & Delivery
3838
containerImage: quay.io/kuadrant/limitador-operator:latest
39-
createdAt: "2023-09-04T14:22:55Z"
39+
createdAt: "2023-09-08T08:41:16Z"
4040
operators.operatorframework.io/builder: operator-sdk-v1.28.1
4141
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
4242
repository: https://github.com/Kuadrant/limitador-operator

bundle/manifests/limitador.kuadrant.io_limitadors.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@ spec:
349349
version:
350350
type: string
351351
type: object
352+
x-kubernetes-validations:
353+
- message: disk storage does not allow multiple replicas
354+
rule: (!has(self.storage) || !has(self.storage.disk)) || (!has(self.replicas)
355+
|| self.replicas < 2)
352356
status:
353357
description: LimitadorStatus defines the observed state of Limitador
354358
properties:

config/crd/bases/limitador.kuadrant.io_limitadors.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ spec:
350350
version:
351351
type: string
352352
type: object
353+
x-kubernetes-validations:
354+
- message: disk storage does not allow multiple replicas
355+
rule: (!has(self.storage) || !has(self.storage.disk)) || (!has(self.replicas)
356+
|| self.replicas < 2)
353357
status:
354358
description: LimitadorStatus defines the observed state of Limitador
355359
properties:

controllers/limitador_controller_test.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ var _ = Describe("Limitador controller", func() {
171171
Equal("/home/limitador/etc/limitador-config.yaml"),
172172
)
173173
Expect(createdLimitadorDeployment.Spec.Template.Spec.Containers[0].VolumeMounts[0].MountPath).Should(
174-
Equal("/home/limitador/etc/"),
174+
Equal("/home/limitador/etc"),
175175
)
176176
Expect(createdLimitadorDeployment.Spec.Template.Spec.Volumes[0].VolumeSource.ConfigMap.Name).Should(
177177
Equal(limitador.LimitsConfigMapName(limitadorObj)),
@@ -579,4 +579,36 @@ var _ = Describe("Limitador controller", func() {
579579

580580
})
581581
})
582+
583+
// This test requires actual k8s cluster
584+
// It's testing implementation based on CRD x-kubernetes-validations extentions
585+
// used to alidate custom resources using Common Expression Language (CEL)
586+
// https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation-rules
587+
Context("Disk storage does not allow multiple replicas", func() {
588+
AfterEach(func() {
589+
limitadorObj := limitadorWithInvalidDiskReplicas()
590+
err := k8sClient.Delete(context.TODO(), limitadorObj, deletePropagationPolicy)
591+
Expect(err == nil || errors.IsNotFound(err))
592+
})
593+
594+
It("resource is rejected", func() {
595+
limitadorObj := limitadorWithInvalidDiskReplicas()
596+
err := k8sClient.Create(context.TODO(), limitadorObj)
597+
Expect(err).To(HaveOccurred())
598+
Expect(errors.IsInvalid(err)).To(BeTrue())
599+
})
600+
})
582601
})
602+
603+
func limitadorWithInvalidDiskReplicas() *limitadorv1alpha1.Limitador {
604+
return &limitadorv1alpha1.Limitador{
605+
TypeMeta: metav1.TypeMeta{Kind: "Limitador", APIVersion: "limitador.kuadrant.io/v1alpha1"},
606+
ObjectMeta: metav1.ObjectMeta{Name: "limitador-with-invalid-disk-replicas", Namespace: "default"},
607+
Spec: limitadorv1alpha1.LimitadorSpec{
608+
Replicas: &[]int{2}[0],
609+
Storage: &limitadorv1alpha1.Storage{
610+
Disk: &limitadorv1alpha1.DiskSpec{},
611+
},
612+
},
613+
}
614+
}

controllers/suite_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ var _ = BeforeSuite(func() {
5858
testEnv = &envtest.Environment{
5959
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
6060
ErrorIfCRDPathMissing: true,
61+
UseExistingCluster: &[]bool{true}[0],
6162
}
6263

6364
cfg, err := testEnv.Start()

doc/development.md

+10
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ make local-cleanup
169169

170170
## Run tests
171171

172+
You need an active session open to a kubernetes cluster.
173+
174+
Optionally, run kind and deploy kuadrant deps
175+
176+
```sh
177+
make local-env-setup
178+
```
179+
180+
Run tests
181+
172182
```sh
173183
make test
174184
```

0 commit comments

Comments
 (0)