Skip to content

Commit 2d2ecf7

Browse files
committed
validation to reject multiples replicas when disk storage enabled
1 parent acfd3b2 commit 2d2ecf7

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
@@ -94,6 +94,8 @@ type Limitador struct {
9494
metav1.TypeMeta `json:",inline"`
9595
metav1.ObjectMeta `json:"metadata,omitempty"`
9696

97+
// +kubebuilder:validation:Optional
98+
// +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"
9799
Spec LimitadorSpec `json:"spec,omitempty"`
98100
Status LimitadorStatus `json:"status,omitempty"`
99101
}

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
@@ -1177,6 +1177,10 @@ spec:
11771177
version:
11781178
type: string
11791179
type: object
1180+
x-kubernetes-validations:
1181+
- message: disk storage does not allow multiple replicas
1182+
rule: (!has(self.storage) || !has(self.storage.disk)) || (!has(self.replicas)
1183+
|| self.replicas < 2)
11801184
status:
11811185
description: LimitadorStatus defines the observed state of Limitador
11821186
properties:

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

+4
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,10 @@ spec:
11781178
version:
11791179
type: string
11801180
type: object
1181+
x-kubernetes-validations:
1182+
- message: disk storage does not allow multiple replicas
1183+
rule: (!has(self.storage) || !has(self.storage.disk)) || (!has(self.replicas)
1184+
|| self.replicas < 2)
11811185
status:
11821186
description: LimitadorStatus defines the observed state of Limitador
11831187
properties:

controllers/limitador_controller_test.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ var _ = Describe("Limitador controller", func() {
189189
Equal("/home/limitador/etc/limitador-config.yaml"),
190190
)
191191
Expect(createdLimitadorDeployment.Spec.Template.Spec.Containers[0].VolumeMounts[0].MountPath).Should(
192-
Equal("/home/limitador/etc/"),
192+
Equal("/home/limitador/etc"),
193193
)
194194
Expect(createdLimitadorDeployment.Spec.Template.Spec.Volumes[0].VolumeSource.ConfigMap.Name).Should(
195195
Equal(limitador.LimitsConfigMapName(limitadorObj)),
@@ -640,4 +640,36 @@ var _ = Describe("Limitador controller", func() {
640640

641641
})
642642
})
643+
644+
// This test requires actual k8s cluster
645+
// It's testing implementation based on CRD x-kubernetes-validations extentions
646+
// used to alidate custom resources using Common Expression Language (CEL)
647+
// https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation-rules
648+
Context("Disk storage does not allow multiple replicas", func() {
649+
AfterEach(func() {
650+
limitadorObj := limitadorWithInvalidDiskReplicas()
651+
err := k8sClient.Delete(context.TODO(), limitadorObj, deletePropagationPolicy)
652+
Expect(err == nil || errors.IsNotFound(err))
653+
})
654+
655+
It("resource is rejected", func() {
656+
limitadorObj := limitadorWithInvalidDiskReplicas()
657+
err := k8sClient.Create(context.TODO(), limitadorObj)
658+
Expect(err).To(HaveOccurred())
659+
Expect(errors.IsInvalid(err)).To(BeTrue())
660+
})
661+
})
643662
})
663+
664+
func limitadorWithInvalidDiskReplicas() *limitadorv1alpha1.Limitador {
665+
return &limitadorv1alpha1.Limitador{
666+
TypeMeta: metav1.TypeMeta{Kind: "Limitador", APIVersion: "limitador.kuadrant.io/v1alpha1"},
667+
ObjectMeta: metav1.ObjectMeta{Name: "limitador-with-invalid-disk-replicas", Namespace: "default"},
668+
Spec: limitadorv1alpha1.LimitadorSpec{
669+
Replicas: &[]int{2}[0],
670+
Storage: &limitadorv1alpha1.Storage{
671+
Disk: &limitadorv1alpha1.DiskSpec{},
672+
},
673+
},
674+
}
675+
}

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)