Skip to content

Commit

Permalink
Incorporate requested changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwalantmodi05 committed Jan 7, 2025
1 parent f6e7766 commit 197565a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 42 deletions.
11 changes: 6 additions & 5 deletions api/v1beta1/aerospikebackupservice_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,24 @@ func (r *AerospikeBackupService) validateBackupServiceSecrets() error {
}

func (r *AerospikeBackupService) validateServicePodSpec() (admission.Warnings, error) {
if err := validateObjectMeta(&r.Spec.PodSpec.ObjectMeta); err != nil {
if err := validatePodObjectMeta(&r.Spec.PodSpec.ObjectMeta); err != nil {
return nil, err
}

return r.validateResources()
}

func (r *AerospikeBackupService) validateResources() (admission.Warnings, error) {
var warn admission.Warnings

if r.Spec.Resources != nil && r.Spec.PodSpec.ServiceContainerSpec.Resources != nil {
if !reflect.DeepEqual(r.Spec.Resources, r.Spec.PodSpec.ServiceContainerSpec.Resources) {
return nil, fmt.Errorf("resources mismatch, different resources requirements found in " +
"spec.resources and spec.podSpec.serviceContainer.resources")
} else {
warn = []string{"spec.resources field is deprecated, " +
"resources field is now part of spec.podSpec.serviceContainer"}
}

warn = []string{"spec.resources field is deprecated, " +
"resources field is now part of spec.podSpec.serviceContainer"}
}

if r.Spec.PodSpec.ServiceContainerSpec.Resources != nil {
Expand All @@ -181,7 +182,7 @@ func (r *AerospikeBackupService) validateResources() (admission.Warnings, error)
return warn, nil
}

func validateObjectMeta(objectMeta *AerospikeObjectMeta) error {
func validatePodObjectMeta(objectMeta *AerospikeObjectMeta) error {
for label := range objectMeta.Labels {
if label == asdbv1.AerospikeAppLabel || label == asdbv1.AerospikeCustomResourceLabel {
return fmt.Errorf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ spec:
- description: |-
Resources defines the requests and limits for the backup service container.
Resources.Limits should be more than Resources.Requests.
Deprecated: Resources field is now part of podSpec.serviceContainer
Deprecated: Resources field is now part of spec.podSpec.serviceContainer
displayName: Resources
path: resources
- description: SecretMounts is the list of secret to be mounted in the backup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ webhooks:
resources:
- aerospikeclusters
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
service:
name: aerospike-operator-webhook-service
namespace: {{ .Release.Namespace }}
path: /mutate-asdb-aerospike-com-v1beta1-aerospikebackupservice
failurePolicy: Fail
name: maerospikebackupservice.kb.io
rules:
- apiGroups:
- asdb.aerospike.com
apiVersions:
- v1beta1
operations:
- CREATE
- UPDATE
resources:
- aerospikebackupservices
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
Expand Down
61 changes: 26 additions & 35 deletions test/backup_service/backup_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ var _ = Describe(

})

It("Should add custom annotations and labels in the backup service deployement pods", func() {
It("Should add/update custom annotations and labels in the backup service deployement pods", func() {
labels := map[string]string{"label-test-1": "test-1"}
annotations := map[string]string{"annotation-test-1": "test-1"}

Expand All @@ -292,22 +292,7 @@ var _ = Describe(
err = DeployBackupService(k8sClient, backupService)
Expect(err).ToNot(HaveOccurred())

deploy, dErr := getBackupServiceDeployment(k8sClient, name, namespace)
Expect(dErr).ToNot(HaveOccurred())

By("Validating Annotations")
actual := deploy.Spec.Template.ObjectMeta.Annotations
valid := validateLabelsOrAnnotations(actual, annotations)
Expect(valid).To(
BeTrue(), "Unable to find annotations",
)

By("Validating Labels")
actual = deploy.Spec.Template.ObjectMeta.Labels
valid = validateLabelsOrAnnotations(actual, labels)
Expect(valid).To(
BeTrue(), "Unable to find labels",
)
validatePodObjectMeta(annotations, labels)

By("Updating custom annotations and labels")
updatedLabels := map[string]string{"label-test-2": "test-2", "label-test-3": "test-3"}
Expand All @@ -320,22 +305,7 @@ var _ = Describe(
err = updateBackupService(k8sClient, backupService)
Expect(err).ToNot(HaveOccurred())

deploy, dErr = getBackupServiceDeployment(k8sClient, name, namespace)
Expect(dErr).ToNot(HaveOccurred())

By("Validating Annotations")
actual = deploy.Spec.Template.ObjectMeta.Annotations
valid = validateLabelsOrAnnotations(actual, updatedAnnotations)
Expect(valid).To(
BeTrue(), "Unable to find annotations",
)

By("Validating Labels")
actual = deploy.Spec.Template.ObjectMeta.Labels
valid = validateLabelsOrAnnotations(actual, updatedLabels)
Expect(valid).To(
BeTrue(), "Unable to find labels",
)
validatePodObjectMeta(updatedAnnotations, updatedLabels)
})

It("Should add SchedulingPolicy in the backup service deployement pods", func() {
Expand Down Expand Up @@ -380,8 +350,8 @@ var _ = Describe(
err = DeployBackupService(k8sClient, backupService)
Expect(err).ToNot(HaveOccurred())

requestMem := resource.MustParse("1Gi")
requestCPU := resource.MustParse("200m")
requestMem := resource.MustParse("100Mi")
requestCPU := resource.MustParse("20m")
limitMem := resource.MustParse("2Gi")
limitCPU := resource.MustParse("300m")

Expand Down Expand Up @@ -470,3 +440,24 @@ func validateBackupServiceSecurityContext(k8sClient client.Client, sc *corev1.Se

return nil
}

func validatePodObjectMeta(annotations, labels map[string]string) {
deploy, dErr := getBackupServiceDeployment(k8sClient, name, namespace)
Expect(dErr).ToNot(HaveOccurred())

By("Validating Annotations")

actual := deploy.Spec.Template.ObjectMeta.Annotations
valid := validateLabelsOrAnnotations(actual, annotations)
Expect(valid).To(
BeTrue(), "Annotations mismatch. expected %s, found %s", annotations, actual,
)

By("Validating Labels")

actual = deploy.Spec.Template.ObjectMeta.Labels
valid = validateLabelsOrAnnotations(actual, labels)
Expect(valid).To(
BeTrue(), "Labels mismatch. expected %s, found %s", labels, actual,
)
}
1 change: 0 additions & 1 deletion test/backup_service/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ func DeleteBackupService(
return nil
}

//nolint:unparam // name and namespace is intentionally kept for future use
func getBackupServiceDeployment(k8sClient client.Client, name, namespace string) (*app.Deployment, error) {
deployment := &app.Deployment{}
if err := k8sClient.Get(context.TODO(), types.NamespacedName{
Expand Down

0 comments on commit 197565a

Please sign in to comment.