From edc0405bb632b637b6e837046fda9990bdf3b58e Mon Sep 17 00:00:00 2001 From: Alessandro Cattapan Date: Fri, 11 Oct 2024 21:00:53 +0200 Subject: [PATCH 1/4] Added keys for optional labels at deployment and pod level --- .../hazelcast/templates/clustering-service-deployment.yaml | 7 +++++++ .../charts/hazelcast/templates/pega-hz-deployment.yaml | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/charts/pega/charts/hazelcast/templates/clustering-service-deployment.yaml b/charts/pega/charts/hazelcast/templates/clustering-service-deployment.yaml index 5a593bdfc..70a1c9734 100644 --- a/charts/pega/charts/hazelcast/templates/clustering-service-deployment.yaml +++ b/charts/pega/charts/hazelcast/templates/clustering-service-deployment.yaml @@ -4,6 +4,10 @@ apiVersion: apps/v1 metadata: name: {{ template "clusteringServiceName" . }} namespace: {{ .Release.Namespace }} + labels: +{{- if .Values.deploymentLabels }} +{{ toYaml .Values.deploymentLabels | indent 4 }} +{{- end }} spec: selector: matchLabels: @@ -17,6 +21,9 @@ spec: app: {{ template "clusteringServiceName" . }} component: "Hazelcast" ops.identifier: "hazelcast" +{{- if .Values.podLabels }} +{{ toYaml .Values.podLabels | indent 8 }} +{{- end }} {{- include "generatedClusteringServicePodLabels" . | indent 8 }} annotations: {{- include "generatedClusteringServicePodAnnotations" . | indent 8 }} diff --git a/charts/pega/charts/hazelcast/templates/pega-hz-deployment.yaml b/charts/pega/charts/hazelcast/templates/pega-hz-deployment.yaml index 494c116ca..a8c5a7a83 100644 --- a/charts/pega/charts/hazelcast/templates/pega-hz-deployment.yaml +++ b/charts/pega/charts/hazelcast/templates/pega-hz-deployment.yaml @@ -4,6 +4,10 @@ apiVersion: apps/v1 metadata: name: {{ template "hazelcastName" . }} namespace: {{ .Release.Namespace }} + labels: +{{- if .Values.deploymentLabels }} +{{ toYaml .Values.deploymentLabels | indent 4 }} +{{- end }} spec: selector: matchLabels: @@ -17,6 +21,9 @@ spec: app: {{ template "hazelcastName" . }} component: "Hazelcast" ops.identifier: "hazelcast" +{{- if .Values.podLabels }} +{{ toYaml .Values.podLabels | indent 8 }} +{{- end }} {{- include "generatedHazelcastServicePodLabels" . | indent 8 }} annotations: {{- include "generatedHazelcastServicePodAnnotations" . | indent 8 }} From 5bc0ac54b2d0a5a7bad21c3a738a6633e21dfb1d Mon Sep 17 00:00:00 2001 From: Alessandro Cattapan Date: Fri, 11 Oct 2024 21:08:39 +0200 Subject: [PATCH 2/4] Updated the documentation --- charts/pega/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/charts/pega/README.md b/charts/pega/README.md index 4dc6b5b42..49565e036 100644 --- a/charts/pega/README.md +++ b/charts/pega/README.md @@ -1247,6 +1247,8 @@ Parameter | Description | Default value `hazelcast.password` | Configures the password to be used in a client-server Hazelcast model for authentication between the nodes in the Pega deployment and the nodes in the Hazelcast cluster. This parameter configures the password credential in Hazelcast cluster and your Pega nodes so authentication occurs automatically. | `""` `hazelcast.external_secret_name` | If you configured a secret in an external secrets operator, enter the secret name. For details, see [this section](#optional-support-for-providing-credentialscertificates-using-external-secrets-operator). | `""` `hazelcast.affinity` | Configures policy to assign the pods to the nodes. See the official [Kubernetes Documentation](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/). | `""` +`hazelcast.podLabels` | Provide custom labels for Pods as metadata to be consumed by other tools and libraries. | `""` +`hazelcast.deploymentLabels` | Provide custom labels for the deployment (more precisely the StatefulSet) as metadata to be consumed by other tools and libraries. | `""` #### Example ```yaml From d2a7515fd4bc9cb005dc9c33b7bc07d6cdb6bafd Mon Sep 17 00:00:00 2001 From: Alessandro Cattapan Date: Fri, 11 Oct 2024 21:24:15 +0200 Subject: [PATCH 3/4] Added Unit Tests --- .../clustering-service-deployment_test.go | 3 + .../pega/clustering-service-migration_test.go | 57 +++++++++---------- .../src/test/pega/pega-hz-deployment_test.go | 9 ++- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/terratest/src/test/pega/clustering-service-deployment_test.go b/terratest/src/test/pega/clustering-service-deployment_test.go index 271f92ecd..99667dd19 100644 --- a/terratest/src/test/pega/clustering-service-deployment_test.go +++ b/terratest/src/test/pega/clustering-service-deployment_test.go @@ -30,6 +30,7 @@ func TestClusteringServiceDeployment(t *testing.T) { "global.provider": vendor, "global.actions.execute": operation, "hazelcast.clusteringServiceEnabled": "true", + "hazelcast.podLabels.key1": "value1", }, } @@ -46,6 +47,8 @@ func VerifyClusteringServiceDeployment(t *testing.T, yamlContent string) { for index, statefulInfo := range statefulSlice { if index >= 1 { UnmarshalK8SYaml(t, statefulInfo, &statefulsetObj) + require.Empty(t, statefulsetObj.Labels) + require.Equal(t, statefulsetObj.Spec.Template.Labels["key1"], "value1") require.Equal(t, *statefulsetObj.Spec.Replicas, int32(3)) require.Equal(t, statefulsetObj.Spec.ServiceName, "clusteringservice-service") statefulsetSpec := statefulsetObj.Spec.Template.Spec diff --git a/terratest/src/test/pega/clustering-service-migration_test.go b/terratest/src/test/pega/clustering-service-migration_test.go index 05bcf7b3a..08f2b8e82 100644 --- a/terratest/src/test/pega/clustering-service-migration_test.go +++ b/terratest/src/test/pega/clustering-service-migration_test.go @@ -1,45 +1,46 @@ package pega import ( + "fmt" + "path/filepath" + "strings" + "testing" + "github.com/gruntwork-io/terratest/modules/helm" "github.com/stretchr/testify/require" - "path/filepath" k8sbatch "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" k8srbac "k8s.io/api/rbac/v1" - "testing" - "strings" - "fmt" ) func TestClusteringServiceMigration(t *testing.T) { var supportedVendors = []string{"k8s", "openshift", "eks", "gke", "aks", "pks"} - var supportedOperations = []string{"deploy","install-deploy"} + var supportedOperations = []string{"deploy", "install-deploy"} helmChartPath, err := filepath.Abs(PegaHelmChartPath) require.NoError(t, err) - for _,vendor := range supportedVendors{ + for _, vendor := range supportedVendors { - for _,operation := range supportedOperations{ + for _, operation := range supportedOperations { - fmt.Println(vendor + "-" + operation) + fmt.Println(vendor + "-" + operation) var options = &helm.Options{ SetValues: map[string]string{ - "global.provider": vendor, - "global.actions.execute": operation, - "hazelcast.migration.initiateMigration": "true", - "hazelcast.clusteringServiceEnabled": "true", + "global.provider": vendor, + "global.actions.execute": operation, + "hazelcast.migration.initiateMigration": "true", + "hazelcast.clusteringServiceEnabled": "true", }, } yamlContent := RenderTemplate(t, options, helmChartPath, []string{"charts/hazelcast/templates/clustering-service-migration.yaml"}) yamlSplit := strings.Split(yamlContent, "---") assertServiceAccount(t, yamlSplit[1], options) - assertRole(t, yamlSplit[2], options) - assertRoleBinding(t, yamlSplit[3], options) + assertRole(t, yamlSplit[2], options) + assertRoleBinding(t, yamlSplit[3], options) assertMigrationJob(t, yamlSplit[4], options) } @@ -57,27 +58,27 @@ func assertRole(t *testing.T, roleYaml string, options *helm.Options) { var roleObj k8srbac.Role UnmarshalK8SYaml(t, roleYaml, &roleObj) - require.Equal(t, roleObj.ObjectMeta.Name, "clusteringservice-migration-role") - require.Equal(t, roleObj.ObjectMeta.Namespace, "default") + require.Equal(t, roleObj.ObjectMeta.Name, "clusteringservice-migration-role") + require.Equal(t, roleObj.ObjectMeta.Namespace, "default") require.Equal(t, roleObj.Rules[0].APIGroups, []string{""}) require.Equal(t, roleObj.Rules[0].Resources, []string{"pods"}) require.Equal(t, roleObj.Rules[0].Verbs, []string{"get", "list"}) - require.Equal(t, roleObj.Rules[1].APIGroups, []string{""}) - require.Equal(t, roleObj.Rules[1].Resources, []string{"pods/exec"}) - require.Equal(t, roleObj.Rules[1].Verbs, []string{"create"}) + require.Equal(t, roleObj.Rules[1].APIGroups, []string{""}) + require.Equal(t, roleObj.Rules[1].Resources, []string{"pods/exec"}) + require.Equal(t, roleObj.Rules[1].Verbs, []string{"create"}) } func assertRoleBinding(t *testing.T, roleBinding string, options *helm.Options) { var roleBindingObj k8srbac.RoleBinding UnmarshalK8SYaml(t, roleBinding, &roleBindingObj) - require.Equal(t, roleBindingObj.ObjectMeta.Name, "clusteringservice-migration-role-binding") - require.Equal(t, roleBindingObj.ObjectMeta.Namespace, "default") + require.Equal(t, roleBindingObj.ObjectMeta.Name, "clusteringservice-migration-role-binding") + require.Equal(t, roleBindingObj.ObjectMeta.Namespace, "default") require.Equal(t, roleBindingObj.Subjects[0].Kind, "ServiceAccount") require.Equal(t, roleBindingObj.Subjects[0].Name, "clusteringservice-migration-sa") - require.Equal(t, roleBindingObj.RoleRef.APIGroup, "rbac.authorization.k8s.io") - require.Equal(t, roleBindingObj.RoleRef.Kind, "Role") - require.Equal(t, roleBindingObj.RoleRef.Name, "clusteringservice-migration-role") + require.Equal(t, roleBindingObj.RoleRef.APIGroup, "rbac.authorization.k8s.io") + require.Equal(t, roleBindingObj.RoleRef.Kind, "Role") + require.Equal(t, roleBindingObj.RoleRef.Name, "clusteringservice-migration-role") } func assertMigrationJob(t *testing.T, jobYaml string, options *helm.Options) { @@ -86,10 +87,8 @@ func assertMigrationJob(t *testing.T, jobYaml string, options *helm.Options) { jobSpec := jobObj.Spec.Template.Spec - require.Equal(t, jobObj.ObjectMeta.Name, "clusteringservice-migration-job") - require.Equal(t, jobObj.ObjectMeta.Namespace, "default") - require.Equal(t, jobObj.Spec.Template.ObjectMeta.Name, "clusteringservice-migration-job") + require.Equal(t, jobObj.ObjectMeta.Name, "clusteringservice-migration-job") + require.Equal(t, jobObj.ObjectMeta.Namespace, "default") + require.Equal(t, jobObj.Spec.Template.ObjectMeta.Name, "clusteringservice-migration-job") require.Equal(t, jobSpec.ServiceAccountName, "clusteringservice-migration-sa") } - - diff --git a/terratest/src/test/pega/pega-hz-deployment_test.go b/terratest/src/test/pega/pega-hz-deployment_test.go index 172b5dd4a..35a80264c 100644 --- a/terratest/src/test/pega/pega-hz-deployment_test.go +++ b/terratest/src/test/pega/pega-hz-deployment_test.go @@ -24,9 +24,10 @@ func TestHazelcastDeployment(t *testing.T) { var options = &helm.Options{ SetValues: map[string]string{ - "global.provider": vendor, - "global.actions.execute": operation, - "hazelcast.enabled": "true", + "global.provider": vendor, + "global.actions.execute": operation, + "hazelcast.enabled": "true", + "hazelcast.podLabels.key1": "value1", }, } @@ -43,6 +44,8 @@ func VerifyHazelcastDeployment(t *testing.T, yamlContent string) { for index, statefulInfo := range statefulSlice { if index >= 1 { UnmarshalK8SYaml(t, statefulInfo, &statefulsetObj) + require.Empty(t, statefulsetObj.Labels) + require.Equal(t, statefulsetObj.Spec.Template.Labels["key1"], "value1") require.Equal(t, *statefulsetObj.Spec.Replicas, int32(3)) require.Equal(t, statefulsetObj.Spec.ServiceName, "pega-hazelcast-service") statefulsetSpec := statefulsetObj.Spec.Template.Spec From 3d8022e68a6b4fcba91426a74294eed3895c1d85 Mon Sep 17 00:00:00 2001 From: Alessandro Cattapan Date: Fri, 11 Oct 2024 22:42:41 +0200 Subject: [PATCH 4/4] Changed the name holding the labels of the deployment --- charts/pega/README.md | 2 +- .../hazelcast/templates/clustering-service-deployment.yaml | 4 ++-- .../pega/charts/hazelcast/templates/pega-hz-deployment.yaml | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/charts/pega/README.md b/charts/pega/README.md index 49565e036..9bc2146b2 100644 --- a/charts/pega/README.md +++ b/charts/pega/README.md @@ -1248,7 +1248,7 @@ Parameter | Description | Default value `hazelcast.external_secret_name` | If you configured a secret in an external secrets operator, enter the secret name. For details, see [this section](#optional-support-for-providing-credentialscertificates-using-external-secrets-operator). | `""` `hazelcast.affinity` | Configures policy to assign the pods to the nodes. See the official [Kubernetes Documentation](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/). | `""` `hazelcast.podLabels` | Provide custom labels for Pods as metadata to be consumed by other tools and libraries. | `""` -`hazelcast.deploymentLabels` | Provide custom labels for the deployment (more precisely the StatefulSet) as metadata to be consumed by other tools and libraries. | `""` +`hazelcast.deployment.labels` | Provide custom labels for the deployment (more precisely the StatefulSet) as metadata to be consumed by other tools and libraries. | `""` #### Example ```yaml diff --git a/charts/pega/charts/hazelcast/templates/clustering-service-deployment.yaml b/charts/pega/charts/hazelcast/templates/clustering-service-deployment.yaml index 70a1c9734..9db23f153 100644 --- a/charts/pega/charts/hazelcast/templates/clustering-service-deployment.yaml +++ b/charts/pega/charts/hazelcast/templates/clustering-service-deployment.yaml @@ -5,8 +5,8 @@ metadata: name: {{ template "clusteringServiceName" . }} namespace: {{ .Release.Namespace }} labels: -{{- if .Values.deploymentLabels }} -{{ toYaml .Values.deploymentLabels | indent 4 }} +{{- if and (.Values.deployment) (.Values.deployment.labels) }} +{{ toYaml .Values.deployment.labels | indent 4 }} {{- end }} spec: selector: diff --git a/charts/pega/charts/hazelcast/templates/pega-hz-deployment.yaml b/charts/pega/charts/hazelcast/templates/pega-hz-deployment.yaml index a8c5a7a83..de861ca27 100644 --- a/charts/pega/charts/hazelcast/templates/pega-hz-deployment.yaml +++ b/charts/pega/charts/hazelcast/templates/pega-hz-deployment.yaml @@ -5,8 +5,8 @@ metadata: name: {{ template "hazelcastName" . }} namespace: {{ .Release.Namespace }} labels: -{{- if .Values.deploymentLabels }} -{{ toYaml .Values.deploymentLabels | indent 4 }} +{{- if and (.Values.deployment) (.Values.deployment.labels) }} +{{ toYaml .Values.deployment.labels | indent 4 }} {{- end }} spec: selector: