From 9c2e09babcc5b9e68ecff3ddb45f8a061e762bcf Mon Sep 17 00:00:00 2001 From: Kajol Asabe <114986456+kasabe28@users.noreply.github.com> Date: Fri, 30 Aug 2024 19:31:00 +0530 Subject: [PATCH] Change workerpool hash generation (#619) * change workpool hash generation * Modify test case to pass additional data for workpool hash generation --- pkg/controller/worker/machines.go | 25 ++++++++++++++++++++++++- pkg/controller/worker/machines_test.go | 8 +++++--- pkg/controller/worker/suite_test.go | 26 ++++++++++++++++++++------ 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/pkg/controller/worker/machines.go b/pkg/controller/worker/machines.go index 139bec24..58d3ed11 100644 --- a/pkg/controller/worker/machines.go +++ b/pkg/controller/worker/machines.go @@ -7,6 +7,7 @@ import ( "context" "encoding/json" "fmt" + "strconv" "github.com/gardener/gardener/extensions/pkg/controller/worker" genericworkeractuator "github.com/gardener/gardener/extensions/pkg/controller/worker/genericactuator" @@ -190,5 +191,27 @@ func (w *workerDelegate) generateMachineClassAndSecrets() ([]*machinecontrollerv func (w *workerDelegate) generateHashForWorkerPool(pool v1alpha1.WorkerPool) (string, error) { // Generate the worker pool hash. - return worker.WorkerPoolHash(pool, w.cluster, nil, nil) + return worker.WorkerPoolHash(pool, w.cluster, computeAdditionalHashDataV1(pool), nil) +} + +func computeAdditionalHashDataV1(pool v1alpha1.WorkerPool) []string { + var additionalData []string + + if pool.Volume != nil && pool.Volume.Encrypted != nil { + additionalData = append(additionalData, strconv.FormatBool(*pool.Volume.Encrypted)) + } + + for _, dv := range pool.DataVolumes { + additionalData = append(additionalData, dv.Name, dv.Size) + + if dv.Type != nil { + additionalData = append(additionalData, *dv.Type) + } + + if dv.Encrypted != nil { + additionalData = append(additionalData, strconv.FormatBool(*dv.Encrypted)) + } + } + + return additionalData } diff --git a/pkg/controller/worker/machines_test.go b/pkg/controller/worker/machines_test.go index 6fe9835e..9fbe0e5f 100644 --- a/pkg/controller/worker/machines_test.go +++ b/pkg/controller/worker/machines_test.go @@ -6,6 +6,7 @@ package worker import ( "encoding/json" "fmt" + "strconv" "github.com/gardener/gardener/extensions/pkg/controller/worker" genericworkeractuator "github.com/gardener/gardener/extensions/pkg/controller/worker/genericactuator" @@ -53,8 +54,8 @@ var _ = Describe("Machines", func() { err = workerDelegate.DeployMachineClasses(ctx) Expect(err).NotTo(HaveOccurred()) - // TODO: Fix machine pool hashing - workerPoolHash, err := worker.WorkerPoolHash(pool, testCluster, nil, nil) + additionalData := []string{strconv.FormatBool(volumeEncrypted), datVolumeName, volumeSize, volumeType, strconv.FormatBool(volumeEncrypted)} + workerPoolHash, err := worker.WorkerPoolHash(pool, testCluster, additionalData, nil) Expect(err).NotTo(HaveOccurred()) By("ensuring that the machine class for each pool has been deployed") @@ -120,7 +121,8 @@ var _ = Describe("Machines", func() { It("should generate the machine deployments", func(ctx SpecContext) { By("creating a worker delegate") - workerPoolHash, err := worker.WorkerPoolHash(pool, testCluster, nil, nil) + additionalData := []string{strconv.FormatBool(volumeEncrypted), datVolumeName, volumeSize, volumeType, strconv.FormatBool(volumeEncrypted)} + workerPoolHash, err := worker.WorkerPoolHash(pool, testCluster, additionalData, nil) Expect(err).NotTo(HaveOccurred()) var ( deploymentName1 = fmt.Sprintf("%s-%s-z%d", w.Namespace, pool.Name, 1) diff --git a/pkg/controller/worker/suite_test.go b/pkg/controller/worker/suite_test.go index d87558cf..75bfe4a1 100644 --- a/pkg/controller/worker/suite_test.go +++ b/pkg/controller/worker/suite_test.go @@ -120,6 +120,14 @@ var _ = BeforeSuite(func() { komega.SetClient(k8sClient) }) +var ( + volumeName = "test-volume" + volumeSize = "10Gi" + volumeType = "fast" + volumeEncrypted = true + datVolumeName = "volume-1" +) + func SetupTest() (*corev1.Namespace, *gardener.ChartApplier) { var ( chartApplier gardener.ChartApplier @@ -139,9 +147,6 @@ func SetupTest() (*corev1.Namespace, *gardener.ChartApplier) { chartApplier, err = gardener.NewChartApplierForConfig(cfg) Expect(err).NotTo(HaveOccurred()) - volumeName := "test-volume" - volumeType := "fast" - // define test resources pool = gardenerextensionv1alpha1.WorkerPool{ MachineType: "foo", @@ -158,9 +163,18 @@ func SetupTest() (*corev1.Namespace, *gardener.ChartApplier) { Name: "pool", UserData: []byte("some-data"), Volume: &gardenerextensionv1alpha1.Volume{ - Name: &volumeName, - Type: &volumeType, - Size: "10Gi", + Name: &volumeName, + Type: &volumeType, + Size: volumeSize, + Encrypted: &volumeEncrypted, + }, + DataVolumes: []gardenerextensionv1alpha1.DataVolume{ + { + Name: datVolumeName, + Type: &volumeType, + Size: volumeSize, + Encrypted: &volumeEncrypted, + }, }, Zones: []string{"zone1", "zone2"}, Architecture: ptr.To[string]("amd64"),