From 3a879d0dcd600d6b7ba7cef82e290a8d245b60c2 Mon Sep 17 00:00:00 2001 From: Seshachalam Yerasala Venkata Date: Wed, 19 Apr 2023 11:12:09 +0530 Subject: [PATCH] Addressed review comment from Madhav about OwnerReferences. This commit addresses the review comment https://github.com/gardener/etcd-druid/pull/539#discussion_r1166278214 --- api/v1alpha1/types_etcd.go | 4 +- api/v1alpha1/types_etcd_test.go | 5 ++- pkg/component/etcd/configmap/configmap.go | 2 +- .../etcd/configmap/configmap_test.go | 2 +- pkg/component/etcd/configmap/values.go | 4 +- pkg/component/etcd/configmap/values_helper.go | 3 +- pkg/component/etcd/lease/lease_member.go | 3 +- pkg/component/etcd/lease/lease_snapshot.go | 3 +- pkg/component/etcd/lease/values.go | 4 +- pkg/component/etcd/lease/values_helper.go | 3 +- .../poddisruptionbudget.go | 4 +- .../poddisruptionbudget_test.go | 2 +- .../etcd/poddisruptionbudget/values.go | 4 +- .../etcd/poddisruptionbudget/values_helper.go | 1 + pkg/component/etcd/role/role.go | 2 +- pkg/component/etcd/role/role_test.go | 18 ++++---- pkg/component/etcd/role/values.go | 4 +- pkg/component/etcd/role/values_helper.go | 11 ++--- pkg/component/etcd/role/values_helper_test.go | 4 +- pkg/component/etcd/rolebinding/rolebinding.go | 2 +- .../etcd/rolebinding/rolebinding_test.go | 18 ++++---- pkg/component/etcd/rolebinding/values.go | 4 +- .../etcd/rolebinding/values_helper.go | 11 ++--- .../etcd/rolebinding/values_helper_test.go | 4 +- pkg/component/etcd/service/service_client.go | 3 +- pkg/component/etcd/service/service_peer.go | 3 +- pkg/component/etcd/service/service_test.go | 4 +- pkg/component/etcd/service/values.go | 4 +- pkg/component/etcd/service/values_helper.go | 3 +- .../etcd/service/values_helper_test.go | 10 ++--- .../etcd/serviceaccount/serviceaccount.go | 2 +- .../serviceaccount/serviceaccount_test.go | 44 +++++++++---------- pkg/component/etcd/serviceaccount/values.go | 4 +- .../etcd/serviceaccount/values_helper.go | 11 ++--- .../etcd/serviceaccount/values_helper_test.go | 5 +-- pkg/component/etcd/statefulset/statefulset.go | 10 ++--- .../etcd/statefulset/statefulset_test.go | 4 +- pkg/component/etcd/statefulset/values.go | 4 +- .../etcd/statefulset/values_helper.go | 3 +- 39 files changed, 108 insertions(+), 128 deletions(-) diff --git a/api/v1alpha1/types_etcd.go b/api/v1alpha1/types_etcd.go index c6685d56d..7b423456b 100644 --- a/api/v1alpha1/types_etcd.go +++ b/api/v1alpha1/types_etcd.go @@ -460,8 +460,8 @@ func (e *Etcd) GetDefaultLabels() map[string]string { } // GetAsOwnerReference returns an OwnerReference object that represents the current Etcd instance. -func (e *Etcd) GetAsOwnerReference() metav1.OwnerReference { - return metav1.OwnerReference{ +func (e *Etcd) GetAsOwnerReference() *metav1.OwnerReference { + return &metav1.OwnerReference{ APIVersion: GroupVersion.String(), Kind: "Etcd", Name: e.Name, diff --git a/api/v1alpha1/types_etcd_test.go b/api/v1alpha1/types_etcd_test.go index 9af6b79a6..c6aa9b6b6 100644 --- a/api/v1alpha1/types_etcd_test.go +++ b/api/v1alpha1/types_etcd_test.go @@ -129,7 +129,6 @@ var _ = Describe("Etcd", func() { Context("GetAsOwnerReference", func() { It("should return an OwnerReference object that represents the current Etcd instance", func() { - expected := metav1.OwnerReference{ APIVersion: GroupVersion.String(), Kind: "Etcd", @@ -138,7 +137,9 @@ var _ = Describe("Etcd", func() { Controller: pointer.Bool(true), BlockOwnerDeletion: pointer.Bool(true), } - Expect(created.GetAsOwnerReference()).To(Equal(expected)) + actual := created.GetAsOwnerReference() + Expect(actual).NotTo(BeNil()) + Expect(*created.GetAsOwnerReference()).To(Equal(expected)) }) }) diff --git a/pkg/component/etcd/configmap/configmap.go b/pkg/component/etcd/configmap/configmap.go index 901165880..58f987a71 100644 --- a/pkg/component/etcd/configmap/configmap.go +++ b/pkg/component/etcd/configmap/configmap.go @@ -165,7 +165,7 @@ func (c *component) syncConfigmap(ctx context.Context, cm *corev1.ConfigMap) err cm.Name = c.values.Name cm.Namespace = c.namespace cm.Labels = c.values.Labels - cm.OwnerReferences = c.values.OwnerReferences + cm.OwnerReferences = []metav1.OwnerReference{*c.values.OwnerReference} cm.Data = map[string]string{"etcd.conf.yaml": c.getEtcdConfigYaml()} return nil }) diff --git a/pkg/component/etcd/configmap/configmap_test.go b/pkg/component/etcd/configmap/configmap_test.go index bc590b0e9..dcb12e60e 100644 --- a/pkg/component/etcd/configmap/configmap_test.go +++ b/pkg/component/etcd/configmap/configmap_test.go @@ -194,7 +194,7 @@ var _ = Describe("Configmap", func() { func checkConfigmap(cm *corev1.ConfigMap, values *Values, namespace string) { Expect(cm.Name).To(Equal(values.Name)) - Expect(cm.OwnerReferences).To(Equal(values.OwnerReferences)) + Expect(cm.OwnerReferences).To(Equal([]metav1.OwnerReference{*values.OwnerReference})) Expect(cm.Labels).To(Equal(values.Labels)) configYML := cm.Data[etcdConfig] config := map[string]interface{}{} diff --git a/pkg/component/etcd/configmap/values.go b/pkg/component/etcd/configmap/values.go index 0645c69d9..8e40ef84e 100644 --- a/pkg/component/etcd/configmap/values.go +++ b/pkg/component/etcd/configmap/values.go @@ -59,6 +59,6 @@ type Values struct { ConfigMapChecksum string // Labels is the labels of deployed configmap Labels map[string]string - // OwnerReferences are the OwnerReferences of the Configmap. - OwnerReferences []metav1.OwnerReference + // OwnerReference are the OwnerReference of the Configmap. + OwnerReference *metav1.OwnerReference } diff --git a/pkg/component/etcd/configmap/values_helper.go b/pkg/component/etcd/configmap/values_helper.go index dab784bb7..81acb9203 100644 --- a/pkg/component/etcd/configmap/values_helper.go +++ b/pkg/component/etcd/configmap/values_helper.go @@ -20,7 +20,6 @@ import ( "strings" druidv1alpha1 "github.com/gardener/etcd-druid/api/v1alpha1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/utils/pointer" ) @@ -41,7 +40,7 @@ func GenerateValues(etcd *druidv1alpha1.Etcd) *Values { ServerPort: etcd.Spec.Etcd.ServerPort, AutoCompactionMode: etcd.Spec.Common.AutoCompactionMode, AutoCompactionRetention: etcd.Spec.Common.AutoCompactionRetention, - OwnerReferences: []metav1.OwnerReference{etcd.GetAsOwnerReference()}, + OwnerReference: etcd.GetAsOwnerReference(), Labels: etcd.GetDefaultLabels(), } return values diff --git a/pkg/component/etcd/lease/lease_member.go b/pkg/component/etcd/lease/lease_member.go index 978ccc744..0d78579eb 100644 --- a/pkg/component/etcd/lease/lease_member.go +++ b/pkg/component/etcd/lease/lease_member.go @@ -23,6 +23,7 @@ import ( "github.com/gardener/gardener/pkg/controllerutils" "github.com/gardener/gardener/pkg/utils/flow" coordinationv1 "k8s.io/api/coordination/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/sets" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -55,7 +56,7 @@ func (c *component) syncMemberLeases(ctx context.Context) error { for k, v := range labels { lease.Labels[k] = v } - lease.OwnerReferences = c.values.OwnerReferences + lease.OwnerReferences = []metav1.OwnerReference{*c.values.OwnerReference} return nil }) return err diff --git a/pkg/component/etcd/lease/lease_snapshot.go b/pkg/component/etcd/lease/lease_snapshot.go index 1e94a42a0..e1d81cf5a 100644 --- a/pkg/component/etcd/lease/lease_snapshot.go +++ b/pkg/component/etcd/lease/lease_snapshot.go @@ -20,6 +20,7 @@ import ( "github.com/gardener/gardener/pkg/controllerutils" coordinationv1 "k8s.io/api/coordination/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -32,7 +33,7 @@ func (c *component) syncSnapshotLease(ctx context.Context, lease *coordinationv1 return c.deleteSnapshotLease(ctx, lease) } _, err := controllerutils.GetAndCreateOrMergePatch(ctx, c.client, lease, func() error { - lease.OwnerReferences = c.values.OwnerReferences + lease.OwnerReferences = []metav1.OwnerReference{*c.values.OwnerReference} return nil }) return err diff --git a/pkg/component/etcd/lease/values.go b/pkg/component/etcd/lease/values.go index 8ba1ab28f..2dddfb4aa 100644 --- a/pkg/component/etcd/lease/values.go +++ b/pkg/component/etcd/lease/values.go @@ -30,6 +30,6 @@ type Values struct { Replicas int32 // Labels is the labels of deployed configmap Labels map[string]string - // OwnerReferences are the OwnerReferences of the Configmap. - OwnerReferences []metav1.OwnerReference + // OwnerReference are the OwnerReference of the Configmap. + OwnerReference *metav1.OwnerReference } diff --git a/pkg/component/etcd/lease/values_helper.go b/pkg/component/etcd/lease/values_helper.go index e39c99b4a..67d84ca22 100644 --- a/pkg/component/etcd/lease/values_helper.go +++ b/pkg/component/etcd/lease/values_helper.go @@ -16,7 +16,6 @@ package lease import ( druidv1alpha1 "github.com/gardener/etcd-druid/api/v1alpha1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // GenerateValues generates `lease.Values` for the lease component @@ -27,6 +26,6 @@ func GenerateValues(etcd *druidv1alpha1.Etcd) Values { DeltaSnapshotLeaseName: etcd.GetDeltaSnapshotLeaseName(), FullSnapshotLeaseName: etcd.GetFullSnapshotLeaseName(), Replicas: etcd.Spec.Replicas, - OwnerReferences: []metav1.OwnerReference{etcd.GetAsOwnerReference()}, + OwnerReference: etcd.GetAsOwnerReference(), } } diff --git a/pkg/component/etcd/poddisruptionbudget/poddisruptionbudget.go b/pkg/component/etcd/poddisruptionbudget/poddisruptionbudget.go index a95d7b853..da15d1066 100644 --- a/pkg/component/etcd/poddisruptionbudget/poddisruptionbudget.go +++ b/pkg/component/etcd/poddisruptionbudget/poddisruptionbudget.go @@ -90,7 +90,7 @@ func (c *component) syncPodDisruptionBudget(ctx context.Context, pdb client.Obje case *policyv1.PodDisruptionBudget: _, err := controllerutils.GetAndCreateOrMergePatch(ctx, c.client, pdb, func() error { pdb.Labels = c.values.Labels - pdb.OwnerReferences = c.values.OwnerReferences + pdb.OwnerReferences = []metav1.OwnerReference{*c.values.OwnerReference} pdb.Spec.MinAvailable = &intstr.IntOrString{ IntVal: c.values.MinAvailable, Type: intstr.Int, @@ -105,7 +105,7 @@ func (c *component) syncPodDisruptionBudget(ctx context.Context, pdb client.Obje _, err := controllerutils.GetAndCreateOrMergePatch(ctx, c.client, pdb, func() error { pdb.Annotations = c.values.Annotations pdb.Labels = c.values.Labels - pdb.OwnerReferences = c.values.OwnerReferences + pdb.OwnerReferences = []metav1.OwnerReference{*c.values.OwnerReference} pdb.Spec.MinAvailable = &intstr.IntOrString{ IntVal: c.values.MinAvailable, Type: intstr.Int, diff --git a/pkg/component/etcd/poddisruptionbudget/poddisruptionbudget_test.go b/pkg/component/etcd/poddisruptionbudget/poddisruptionbudget_test.go index 2a0b6d580..055935487 100644 --- a/pkg/component/etcd/poddisruptionbudget/poddisruptionbudget_test.go +++ b/pkg/component/etcd/poddisruptionbudget/poddisruptionbudget_test.go @@ -332,7 +332,7 @@ func checkV1PDB(pdb *policyv1.PodDisruptionBudget, values *Values, expectedNames func checkPDBMetadata(meta *metav1.ObjectMeta, values *Values, expectedNamespace string) { Expect(meta.Name).To(Equal(values.Name)) Expect(meta.Namespace).To(Equal(expectedNamespace)) - Expect(meta.OwnerReferences).To(Equal(values.OwnerReferences)) + Expect(meta.OwnerReferences).To(Equal([]metav1.OwnerReference{*values.OwnerReference})) Expect(meta.Labels).To(Equal(pdbLabels(values))) } diff --git a/pkg/component/etcd/poddisruptionbudget/values.go b/pkg/component/etcd/poddisruptionbudget/values.go index 58236f642..38b70857f 100644 --- a/pkg/component/etcd/poddisruptionbudget/values.go +++ b/pkg/component/etcd/poddisruptionbudget/values.go @@ -36,6 +36,6 @@ type Values struct { Annotations map[string]string // MinAvailable defined the minimum number of pods to be available at any point of time MinAvailable int32 - // OwnerReferences are the OwnerReferences of the PodDisruptionBudget. - OwnerReferences []metav1.OwnerReference + // OwnerReference are the OwnerReference of the PodDisruptionBudget. + OwnerReference *metav1.OwnerReference } diff --git a/pkg/component/etcd/poddisruptionbudget/values_helper.go b/pkg/component/etcd/poddisruptionbudget/values_helper.go index 2c7f9243f..3630e387b 100644 --- a/pkg/component/etcd/poddisruptionbudget/values_helper.go +++ b/pkg/component/etcd/poddisruptionbudget/values_helper.go @@ -34,6 +34,7 @@ func GenerateValues(etcd *druidv1alpha1.Etcd) Values { SelectorLabels: etcd.GetDefaultLabels(), Annotations: annotations, MinAvailable: int32(CalculatePDBMinAvailable(etcd)), + OwnerReference: etcd.GetAsOwnerReference(), } } diff --git a/pkg/component/etcd/role/role.go b/pkg/component/etcd/role/role.go index 3f5f14195..0311ee791 100644 --- a/pkg/component/etcd/role/role.go +++ b/pkg/component/etcd/role/role.go @@ -36,7 +36,7 @@ func (c component) Deploy(ctx context.Context) error { role.Name = c.values.Name role.Namespace = c.values.Namespace role.Labels = c.values.Labels - role.OwnerReferences = c.values.OwnerReferences + role.OwnerReferences = []metav1.OwnerReference{*c.values.OwnerReference} role.Rules = c.values.Rules return nil }) diff --git a/pkg/component/etcd/role/role_test.go b/pkg/component/etcd/role/role_test.go index 77034efa2..93a65a3b5 100644 --- a/pkg/component/etcd/role/role_test.go +++ b/pkg/component/etcd/role/role_test.go @@ -112,7 +112,7 @@ func verifyRoleValues(expected *rbacv1.Role, values *role.Values) { Expect(expected.Name).To(Equal(values.Name)) Expect(expected.Labels).To(Equal(values.Labels)) Expect(expected.Namespace).To(Equal(values.Namespace)) - Expect(expected.OwnerReferences).To(Equal(values.OwnerReferences)) + Expect(expected.OwnerReferences).To(Equal([]metav1.OwnerReference{*values.OwnerReference})) Expect(expected.Rules).To(MatchAllElements(testutils.RuleIterator, Elements{ "coordination.k8s.io": MatchFields(IgnoreExtras, Fields{ "APIGroups": MatchAllElements(testutils.StringArrayIterator, Elements{ @@ -187,15 +187,13 @@ func getTestRoleValues() *role.Values { Verbs: []string{"get", "list", "watch"}, }, }, - OwnerReferences: []metav1.OwnerReference{ - { - APIVersion: v1alpha1.GroupVersion.String(), - Kind: "etcd", - Name: "test-etcd", - UID: "123-456-789", - Controller: pointer.Bool(true), - BlockOwnerDeletion: pointer.Bool(true), - }, + OwnerReference: &metav1.OwnerReference{ + APIVersion: v1alpha1.GroupVersion.String(), + Kind: "etcd", + Name: "test-etcd", + UID: "123-456-789", + Controller: pointer.Bool(true), + BlockOwnerDeletion: pointer.Bool(true), }, } } diff --git a/pkg/component/etcd/role/values.go b/pkg/component/etcd/role/values.go index 65a258dad..8bf9afd4d 100644 --- a/pkg/component/etcd/role/values.go +++ b/pkg/component/etcd/role/values.go @@ -27,8 +27,8 @@ type Values struct { Namespace string // Rules holds all the PolicyRules for this Role Rules []rbacv1.PolicyRule - // OwnerReferences are the OwnerReferences of the Role. - OwnerReferences []metav1.OwnerReference + // OwnerReference are the OwnerReference of the Role. + OwnerReference *metav1.OwnerReference // Labels are the labels of the Role. Labels map[string]string } diff --git a/pkg/component/etcd/role/values_helper.go b/pkg/component/etcd/role/values_helper.go index 51cfe11b4..42ad416f1 100644 --- a/pkg/component/etcd/role/values_helper.go +++ b/pkg/component/etcd/role/values_helper.go @@ -17,18 +17,15 @@ package role import ( druidv1alpha1 "github.com/gardener/etcd-druid/api/v1alpha1" rbacv1 "k8s.io/api/rbac/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // GenerateValues generates `role.Values` for the role component with the given `etcd` object. func GenerateValues(etcd *druidv1alpha1.Etcd) *Values { return &Values{ - Name: etcd.GetRoleName(), - Namespace: etcd.Namespace, - Labels: etcd.GetDefaultLabels(), - OwnerReferences: []metav1.OwnerReference{ - etcd.GetAsOwnerReference(), - }, + Name: etcd.GetRoleName(), + Namespace: etcd.Namespace, + Labels: etcd.GetDefaultLabels(), + OwnerReference: etcd.GetAsOwnerReference(), Rules: []rbacv1.PolicyRule{ { APIGroups: []string{"coordination.k8s.io"}, diff --git a/pkg/component/etcd/role/values_helper_test.go b/pkg/component/etcd/role/values_helper_test.go index 67100ab00..1533709af 100644 --- a/pkg/component/etcd/role/values_helper_test.go +++ b/pkg/component/etcd/role/values_helper_test.go @@ -49,9 +49,7 @@ var _ = Describe("Role", func() { "name": "etcd", "instance": etcd.Name, }, - OwnerReferences: []metav1.OwnerReference{ - etcd.GetAsOwnerReference(), - }, + OwnerReference: etcd.GetAsOwnerReference(), Rules: []rbacv1.PolicyRule{ { APIGroups: []string{"coordination.k8s.io"}, diff --git a/pkg/component/etcd/rolebinding/rolebinding.go b/pkg/component/etcd/rolebinding/rolebinding.go index bab8bc6c0..c4df60802 100644 --- a/pkg/component/etcd/rolebinding/rolebinding.go +++ b/pkg/component/etcd/rolebinding/rolebinding.go @@ -36,7 +36,7 @@ func (c component) Deploy(ctx context.Context) error { roleBinding.Name = c.values.Name roleBinding.Namespace = c.values.Namespace roleBinding.Labels = c.values.Labels - roleBinding.OwnerReferences = c.values.OwnerReferences + roleBinding.OwnerReferences = []metav1.OwnerReference{*c.values.OwnerReference} roleBinding.RoleRef = rbacv1.RoleRef{ APIGroup: "rbac.authorization.k8s.io", Kind: "Role", diff --git a/pkg/component/etcd/rolebinding/rolebinding_test.go b/pkg/component/etcd/rolebinding/rolebinding_test.go index 3ad2045b1..8c77100fe 100644 --- a/pkg/component/etcd/rolebinding/rolebinding_test.go +++ b/pkg/component/etcd/rolebinding/rolebinding_test.go @@ -136,7 +136,7 @@ func verifyRoleBindingValues(expected *rbacv1.RoleBinding, values *rolebinding.V Expect(expected.Labels).To(Equal(values.Labels)) Expect(expected.Namespace).To(Equal(values.Namespace)) Expect(expected.Namespace).To(Equal(values.Namespace)) - Expect(expected.OwnerReferences).To(Equal(values.OwnerReferences)) + Expect(expected.OwnerReferences).To(Equal([]metav1.OwnerReference{*values.OwnerReference})) Expect(expected.Subjects).To(Equal([]rbacv1.Subject{ { Kind: "ServiceAccount", @@ -163,15 +163,13 @@ func getTestRoleBindingValues() *rolebinding.Values { }, RoleName: "test-role", ServiceAccountName: "test-serviceaccount", - OwnerReferences: []metav1.OwnerReference{ - { - APIVersion: v1alpha1.GroupVersion.String(), - Kind: "etcd", - Name: "test-etcd", - UID: "123-456-789", - Controller: pointer.Bool(true), - BlockOwnerDeletion: pointer.Bool(true), - }, + OwnerReference: &metav1.OwnerReference{ + APIVersion: v1alpha1.GroupVersion.String(), + Kind: "etcd", + Name: "test-etcd", + UID: "123-456-789", + Controller: pointer.Bool(true), + BlockOwnerDeletion: pointer.Bool(true), }, } } diff --git a/pkg/component/etcd/rolebinding/values.go b/pkg/component/etcd/rolebinding/values.go index 936933bf0..84f950683 100644 --- a/pkg/component/etcd/rolebinding/values.go +++ b/pkg/component/etcd/rolebinding/values.go @@ -27,8 +27,8 @@ type Values struct { // ServiceAccountName is the service account subject name for the RoleBinding. // It is assumed that the ServiceAccount exists in the namespace where the etcd custom resource is created. ServiceAccountName string - // OwnerReferences are the OwnerReferences of the RoleBinding. - OwnerReferences []metav1.OwnerReference + // OwnerReference are the OwnerReference of the RoleBinding. + OwnerReference *metav1.OwnerReference // Labels are the labels of the RoleBinding. Labels map[string]string } diff --git a/pkg/component/etcd/rolebinding/values_helper.go b/pkg/component/etcd/rolebinding/values_helper.go index a6c35a716..4a379bb44 100644 --- a/pkg/component/etcd/rolebinding/values_helper.go +++ b/pkg/component/etcd/rolebinding/values_helper.go @@ -16,18 +16,15 @@ package rolebinding import ( druidv1alpha1 "github.com/gardener/etcd-druid/api/v1alpha1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // GenerateValues generates `serviceaccount.Values` for the serviceaccount component with the given `etcd` object. func GenerateValues(etcd *druidv1alpha1.Etcd) *Values { return &Values{ - Name: etcd.GetRoleBindingName(), - Namespace: etcd.Namespace, - Labels: etcd.GetDefaultLabels(), - OwnerReferences: []metav1.OwnerReference{ - etcd.GetAsOwnerReference(), - }, + Name: etcd.GetRoleBindingName(), + Namespace: etcd.Namespace, + Labels: etcd.GetDefaultLabels(), + OwnerReference: etcd.GetAsOwnerReference(), RoleName: etcd.GetRoleName(), ServiceAccountName: etcd.GetServiceAccountName(), } diff --git a/pkg/component/etcd/rolebinding/values_helper_test.go b/pkg/component/etcd/rolebinding/values_helper_test.go index 6e8b83eff..b2283b395 100644 --- a/pkg/component/etcd/rolebinding/values_helper_test.go +++ b/pkg/component/etcd/rolebinding/values_helper_test.go @@ -46,9 +46,7 @@ var _ = Describe("RoleBinding", func() { }, RoleName: etcd.GetRoleName(), ServiceAccountName: etcd.GetServiceAccountName(), - OwnerReferences: []metav1.OwnerReference{ - etcd.GetAsOwnerReference(), - }, + OwnerReference: etcd.GetAsOwnerReference(), } ) diff --git a/pkg/component/etcd/service/service_client.go b/pkg/component/etcd/service/service_client.go index 7ce942ee3..b204f6315 100644 --- a/pkg/component/etcd/service/service_client.go +++ b/pkg/component/etcd/service/service_client.go @@ -20,6 +20,7 @@ import ( "github.com/gardener/etcd-druid/pkg/utils" "github.com/gardener/gardener/pkg/controllerutils" corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" ) @@ -27,7 +28,7 @@ func (c *component) syncClientService(ctx context.Context, svc *corev1.Service) _, err := controllerutils.GetAndCreateOrStrategicMergePatch(ctx, c.client, svc, func() error { svc.Labels = utils.MergeStringMaps(c.values.Labels, c.values.ClientServiceLabels) svc.Annotations = c.values.ClientServiceAnnotations - svc.OwnerReferences = c.values.OwnerReferences + svc.OwnerReferences = []metav1.OwnerReference{*c.values.OwnerReference} svc.Spec.Type = corev1.ServiceTypeClusterIP svc.Spec.SessionAffinity = corev1.ServiceAffinityNone svc.Spec.Selector = c.values.SelectorLabels diff --git a/pkg/component/etcd/service/service_peer.go b/pkg/component/etcd/service/service_peer.go index 8953458e1..a4189aee9 100644 --- a/pkg/component/etcd/service/service_peer.go +++ b/pkg/component/etcd/service/service_peer.go @@ -19,13 +19,14 @@ import ( "github.com/gardener/gardener/pkg/controllerutils" corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" ) func (c *component) syncPeerService(ctx context.Context, svc *corev1.Service) error { _, err := controllerutils.GetAndCreateOrStrategicMergePatch(ctx, c.client, svc, func() error { svc.Labels = c.values.Labels - svc.OwnerReferences = c.values.OwnerReferences + svc.OwnerReferences = []metav1.OwnerReference{*c.values.OwnerReference} svc.Spec.Type = corev1.ServiceTypeClusterIP svc.Spec.ClusterIP = corev1.ClusterIPNone svc.Spec.SessionAffinity = corev1.ServiceAffinityNone diff --git a/pkg/component/etcd/service/service_test.go b/pkg/component/etcd/service/service_test.go index dd778f89d..bde07ddf3 100644 --- a/pkg/component/etcd/service/service_test.go +++ b/pkg/component/etcd/service/service_test.go @@ -166,7 +166,7 @@ var _ = Describe("Service", func() { }) func checkClientService(svc *corev1.Service, values Values) { - Expect(svc.OwnerReferences).To(Equal(values.OwnerReferences)) + Expect(svc.OwnerReferences).To(Equal([]metav1.OwnerReference{*values.OwnerReference})) Expect(svc.Labels).To(Equal(utils.MergeStringMaps(values.Labels, values.ClientServiceLabels))) Expect(svc.Spec.Selector).To(Equal(values.SelectorLabels)) Expect(svc.Spec.Type).To(Equal(corev1.ServiceType("ClusterIP"))) @@ -193,7 +193,7 @@ func checkClientService(svc *corev1.Service, values Values) { } func checkPeerService(svc *corev1.Service, values Values) { - Expect(svc.OwnerReferences).To(Equal(values.OwnerReferences)) + Expect(svc.OwnerReferences).To(Equal([]metav1.OwnerReference{*values.OwnerReference})) Expect(svc.Labels).To(Equal(values.Labels)) Expect(svc.Spec.PublishNotReadyAddresses).To(BeTrue()) Expect(svc.Spec.Type).To(Equal(corev1.ServiceType("ClusterIP"))) diff --git a/pkg/component/etcd/service/values.go b/pkg/component/etcd/service/values.go index 5d6e5b7ad..7f6fd1707 100644 --- a/pkg/component/etcd/service/values.go +++ b/pkg/component/etcd/service/values.go @@ -36,8 +36,8 @@ type Values struct { PeerServiceName string // ServerPort is the port used for etcd peer communication. ServerPort int32 - // OwnerReferences are the OwnerReferences of the ETCD services. - OwnerReferences []metav1.OwnerReference + // OwnerReference are the OwnerReference of the ETCD services. + OwnerReference *metav1.OwnerReference // SelectorLabels are the labels to be used in the Service.spec selector SelectorLabels map[string]string } diff --git a/pkg/component/etcd/service/values_helper.go b/pkg/component/etcd/service/values_helper.go index fb51330c9..820432967 100644 --- a/pkg/component/etcd/service/values_helper.go +++ b/pkg/component/etcd/service/values_helper.go @@ -15,7 +15,6 @@ package service import ( - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/utils/pointer" druidv1alpha1 "github.com/gardener/etcd-druid/api/v1alpha1" @@ -48,6 +47,6 @@ func GenerateValues(etcd *druidv1alpha1.Etcd) Values { Labels: etcd.GetDefaultLabels(), PeerServiceName: etcd.GetPeerServiceName(), ServerPort: pointer.Int32Deref(etcd.Spec.Etcd.ServerPort, defaultServerPort), - OwnerReferences: []metav1.OwnerReference{etcd.GetAsOwnerReference()}, + OwnerReference: etcd.GetAsOwnerReference(), } } diff --git a/pkg/component/etcd/service/values_helper_test.go b/pkg/component/etcd/service/values_helper_test.go index 2cd4b067b..28313bf7e 100644 --- a/pkg/component/etcd/service/values_helper_test.go +++ b/pkg/component/etcd/service/values_helper_test.go @@ -109,7 +109,7 @@ var _ = Describe("#GenerateValues", func() { "PeerServiceName": Equal(fmt.Sprintf("%s-peer", etcd.Name)), "ServerPort": Equal(int32(2380)), "SelectorLabels": Equal(expectedLabels), - "OwnerReferences": Equal([]metav1.OwnerReference{etcd.GetAsOwnerReference()}), + "OwnerReference": Equal(etcd.GetAsOwnerReference()), })) }) }) @@ -136,7 +136,7 @@ var _ = Describe("#GenerateValues", func() { "ServerPort": Equal(int32(2380)), "ClientServiceAnnotations": Equal(clientServiceAnnotations), "SelectorLabels": Equal(expectedLabels), - "OwnerReferences": Equal([]metav1.OwnerReference{etcd.GetAsOwnerReference()}), + "OwnerReference": Equal(etcd.GetAsOwnerReference()), })) }) }) @@ -160,7 +160,7 @@ var _ = Describe("#GenerateValues", func() { "ServerPort": Equal(int32(2380)), "ClientServiceAnnotations": Equal(clientServiceAnnotations), "SelectorLabels": Equal(expectedLabels), - "OwnerReferences": Equal([]metav1.OwnerReference{etcd.GetAsOwnerReference()}), + "OwnerReference": Equal(etcd.GetAsOwnerReference()), })) }) }) @@ -187,7 +187,7 @@ var _ = Describe("#GenerateValues", func() { "ServerPort": Equal(int32(2380)), "ClientServiceLabels": Equal(clientServiceLabels), "SelectorLabels": Equal(expectedLabels), - "OwnerReferences": Equal([]metav1.OwnerReference{etcd.GetAsOwnerReference()}), + "OwnerReference": Equal(etcd.GetAsOwnerReference()), })) }) }) @@ -211,7 +211,7 @@ var _ = Describe("#GenerateValues", func() { "ServerPort": Equal(int32(2380)), "ClientServiceLabels": Equal(clientServiceLabels), "SelectorLabels": Equal(expectedLabels), - "OwnerReferences": Equal([]metav1.OwnerReference{etcd.GetAsOwnerReference()}), + "OwnerReference": Equal(etcd.GetAsOwnerReference()), })) }) }) diff --git a/pkg/component/etcd/serviceaccount/serviceaccount.go b/pkg/component/etcd/serviceaccount/serviceaccount.go index ca921e583..c7ce9914c 100644 --- a/pkg/component/etcd/serviceaccount/serviceaccount.go +++ b/pkg/component/etcd/serviceaccount/serviceaccount.go @@ -37,7 +37,7 @@ func (c component) Deploy(ctx context.Context) error { serviceAccount.Name = c.values.Name serviceAccount.Namespace = c.values.Namespace serviceAccount.Labels = c.values.Labels - serviceAccount.OwnerReferences = c.values.OwnerReferences + serviceAccount.OwnerReferences = []metav1.OwnerReference{*c.values.OwnerReference} serviceAccount.AutomountServiceAccountToken = pointer.Bool(!c.values.DisableAutomount) return nil }) diff --git a/pkg/component/etcd/serviceaccount/serviceaccount_test.go b/pkg/component/etcd/serviceaccount/serviceaccount_test.go index 834313425..e2acb4f03 100644 --- a/pkg/component/etcd/serviceaccount/serviceaccount_test.go +++ b/pkg/component/etcd/serviceaccount/serviceaccount_test.go @@ -51,15 +51,13 @@ var _ = Describe("ServiceAccount Component", Ordered, func() { "foo": "bar", }, DisableAutomount: true, - OwnerReferences: []metav1.OwnerReference{ - { - APIVersion: druidv1alpha1.GroupVersion.String(), - Kind: "Etcd", - Name: "test-etcd", - UID: "123-456-789", - Controller: pointer.Bool(true), - BlockOwnerDeletion: pointer.Bool(true), - }, + OwnerReference: &metav1.OwnerReference{ + APIVersion: druidv1alpha1.GroupVersion.String(), + Kind: "Etcd", + Name: "test-etcd", + UID: "123-456-789", + Controller: pointer.Bool(true), + BlockOwnerDeletion: pointer.Bool(true), }, } saComponent = New(c, values) @@ -119,15 +117,13 @@ var _ = Describe("ServiceAccount Component", Ordered, func() { "foo": "bar", }, DisableAutomount: true, - OwnerReferences: []metav1.OwnerReference{ - { - APIVersion: druidv1alpha1.GroupVersion.String(), - Kind: "Etcd", - Name: "test-etcd", - UID: "123-456-789", - Controller: pointer.Bool(true), - BlockOwnerDeletion: pointer.Bool(true), - }, + OwnerReference: &metav1.OwnerReference{ + APIVersion: druidv1alpha1.GroupVersion.String(), + Kind: "Etcd", + Name: "test-etcd", + UID: "123-456-789", + Controller: pointer.Bool(true), + BlockOwnerDeletion: pointer.Bool(true), }, } saComponent = New(c, values) @@ -161,10 +157,10 @@ func getServiceAccountKeyFromValue(value *Values) types.NamespacedName { return client.ObjectKey{Name: value.Name, Namespace: value.Namespace} } -func verifyServicAccountValues(expected *corev1.ServiceAccount, value *Values) { - Expect(expected.Name).To(Equal(value.Name)) - Expect(expected.Labels).Should(Equal(value.Labels)) - Expect(expected.Namespace).To(Equal(value.Namespace)) - Expect(expected.OwnerReferences).To(Equal(value.OwnerReferences)) - Expect(expected.AutomountServiceAccountToken).To(Equal(pointer.Bool(!value.DisableAutomount))) +func verifyServicAccountValues(expected *corev1.ServiceAccount, values *Values) { + Expect(expected.Name).To(Equal(values.Name)) + Expect(expected.Labels).Should(Equal(values.Labels)) + Expect(expected.Namespace).To(Equal(values.Namespace)) + Expect(expected.OwnerReferences).To(Equal([]metav1.OwnerReference{*values.OwnerReference})) + Expect(expected.AutomountServiceAccountToken).To(Equal(pointer.Bool(!values.DisableAutomount))) } diff --git a/pkg/component/etcd/serviceaccount/values.go b/pkg/component/etcd/serviceaccount/values.go index a998f262e..fbe9dd03c 100644 --- a/pkg/component/etcd/serviceaccount/values.go +++ b/pkg/component/etcd/serviceaccount/values.go @@ -22,8 +22,8 @@ type Values struct { Name string // Namespace is the namespace of the ServiceAccount. Namespace string - // OwnerReferences are the OwnerReferences of the ServiceAccount. - OwnerReferences []metav1.OwnerReference + // OwnerReference are the OwnerReference of the ServiceAccount. + OwnerReference *metav1.OwnerReference // Labels are the labels to apply to the ServiceAccount. Labels map[string]string // DisableAutomount defines the AutomountServiceAccountToken of the ServiceAccount. diff --git a/pkg/component/etcd/serviceaccount/values_helper.go b/pkg/component/etcd/serviceaccount/values_helper.go index d27b44a40..86122ca51 100644 --- a/pkg/component/etcd/serviceaccount/values_helper.go +++ b/pkg/component/etcd/serviceaccount/values_helper.go @@ -16,18 +16,15 @@ package serviceaccount import ( druidv1alpha1 "github.com/gardener/etcd-druid/api/v1alpha1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // GenerateValues generates `serviceaccount.Values` for the serviceaccount component for the given `etcd` object. func GenerateValues(etcd *druidv1alpha1.Etcd, disableEtcdServiceAccountAutomount bool) *Values { return &Values{ - Name: etcd.GetServiceAccountName(), - Namespace: etcd.Namespace, - Labels: etcd.GetDefaultLabels(), - OwnerReferences: []metav1.OwnerReference{ - etcd.GetAsOwnerReference(), - }, + Name: etcd.GetServiceAccountName(), + Namespace: etcd.Namespace, + Labels: etcd.GetDefaultLabels(), + OwnerReference: etcd.GetAsOwnerReference(), DisableAutomount: disableEtcdServiceAccountAutomount, } } diff --git a/pkg/component/etcd/serviceaccount/values_helper_test.go b/pkg/component/etcd/serviceaccount/values_helper_test.go index ef8e788bd..c1f935e27 100644 --- a/pkg/component/etcd/serviceaccount/values_helper_test.go +++ b/pkg/component/etcd/serviceaccount/values_helper_test.go @@ -53,9 +53,8 @@ var _ = Describe("ServiceAccount", func() { "name": "etcd", "instance": etcd.Name, }, - OwnerReferences: []metav1.OwnerReference{ - etcd.GetAsOwnerReference(), - }, + OwnerReference: etcd.GetAsOwnerReference(), + DisableAutomount: true, } }) diff --git a/pkg/component/etcd/statefulset/statefulset.go b/pkg/component/etcd/statefulset/statefulset.go index 88b0465c7..a9b85bb72 100644 --- a/pkg/component/etcd/statefulset/statefulset.go +++ b/pkg/component/etcd/statefulset/statefulset.go @@ -205,7 +205,7 @@ func (c *component) createDeployFlow(ctx context.Context) (*flow.Flow, error) { return nil, err } - flowName := fmt.Sprintf("(etcd: %s) Deploy Flow for StatefulSet %s for Namespace: %s", c.values.OwnerReferences[0].UID, c.values.Name, c.values.Namespace) + flowName := fmt.Sprintf("(etcd: %s) Deploy Flow for StatefulSet %s for Namespace: %s", c.values.OwnerReference.UID, c.values.Name, c.values.Namespace) g := flow.NewGraph(flowName) var taskID *flow.TaskID @@ -283,7 +283,7 @@ func (c *component) addImmutableFieldUpdateTask(g *flow.Graph, sts *appsv1.State Fn: func(ctx context.Context) error { return c.destroyAndWait(ctx, opName) }, Dependencies: nil, }) - c.logger.Info("added delete StatefulSet task to deploy flow due to immutable field update task", "namespace", c.values.Namespace, "name", c.values.Name, "etcdUID", c.values.OwnerReferences[0].UID) + c.logger.Info("added delete StatefulSet task to deploy flow due to immutable field update task", "namespace", c.values.Namespace, "name", c.values.Name, "etcdUID", c.values.OwnerReference.UID) return &taskID } return nil @@ -318,7 +318,7 @@ func (c *component) addCreateOrPatchTask(g *flow.Graph, originalSts *appsv1.Stat }, Dependencies: dependencies, }) - c.logger.Info("added createOrPatch StatefulSet task to the deploy flow", "taskID", taskID, "namespace", c.values.Namespace, "etcdUID", c.values.OwnerReferences[0].UID, "StatefulSetName", c.values.Name, "replicas", c.values.Replicas) + c.logger.Info("added createOrPatch StatefulSet task to the deploy flow", "taskID", taskID, "namespace", c.values.Namespace, "etcdUID", c.values.OwnerReference.UID, "StatefulSetName", c.values.Name, "replicas", c.values.Replicas) } func (c *component) getExistingSts(ctx context.Context) (*appsv1.StatefulSet, error) { @@ -376,7 +376,7 @@ func (c *component) doCreateOrUpdate(ctx context.Context, opName string, sts *ap func (c *component) destroyAndWait(ctx context.Context, opName string) error { deleteAndWait := gardenercomponent.OpDestroyAndWait(c) - c.logger.Info("deleting sts", "namespace", c.values.Namespace, "name", c.values.Name, "operation", opName, "etcdUID", c.values.OwnerReferences[0].UID) + c.logger.Info("deleting sts", "namespace", c.values.Namespace, "name", c.values.Name, "operation", opName, "etcdUID", c.values.OwnerReference.UID) if err := deleteAndWait.Destroy(ctx); err != nil { return err } @@ -553,7 +553,7 @@ func getObjectMeta(val *Values, sts *appsv1.StatefulSet, preserveAnnotations boo Namespace: val.Namespace, Labels: val.Labels, Annotations: annotations, - OwnerReferences: val.OwnerReferences, + OwnerReferences: []metav1.OwnerReference{*val.OwnerReference}, } } diff --git a/pkg/component/etcd/statefulset/statefulset_test.go b/pkg/component/etcd/statefulset/statefulset_test.go index 82fa502f9..dea154b54 100644 --- a/pkg/component/etcd/statefulset/statefulset_test.go +++ b/pkg/component/etcd/statefulset/statefulset_test.go @@ -628,7 +628,7 @@ func checkStatefulset(sts *appsv1.StatefulSet, values Values) { "VolumeSource": MatchFields(IgnoreExtras, Fields{ "ConfigMap": PointTo(MatchFields(IgnoreExtras, Fields{ "LocalObjectReference": MatchFields(IgnoreExtras, Fields{ - "Name": Equal(fmt.Sprintf("etcd-bootstrap-%s", string(values.OwnerReferences[0].UID[:6]))), + "Name": Equal(fmt.Sprintf("etcd-bootstrap-%s", string(values.OwnerReference.UID[:6]))), }), "DefaultMode": PointTo(Equal(int32(0644))), "Items": MatchAllElements(keyIterator, Elements{ @@ -714,7 +714,7 @@ func checkStatefulset(sts *appsv1.StatefulSet, values Values) { } func checkStsOwnerRefs(ors []metav1.OwnerReference, values Values) { - Expect(ors).To(Equal(values.OwnerReferences)) + Expect(ors).To(Equal([]metav1.OwnerReference{*values.OwnerReference})) } func getEtcd(name, namespace string, tlsEnabled bool, replicas int32, storageProvider *string) *druidv1alpha1.Etcd { diff --git a/pkg/component/etcd/statefulset/values.go b/pkg/component/etcd/statefulset/values.go index 57c6f57d3..b1409b7b6 100644 --- a/pkg/component/etcd/statefulset/values.go +++ b/pkg/component/etcd/statefulset/values.go @@ -29,8 +29,8 @@ type Values struct { // Namespace is the namespace of StatefulSet. Namespace string - // OwnerReferences are the OwnerReferences of the StatefulSet. - OwnerReferences []metav1.OwnerReference + // OwnerReference are the OwnerReference of the StatefulSet. + OwnerReference *metav1.OwnerReference // Replicas is the number of ETCD instance that the ETCD cluster will have. Replicas int32 diff --git a/pkg/component/etcd/statefulset/values_helper.go b/pkg/component/etcd/statefulset/values_helper.go index 8cedcd82f..1dbe4e11b 100644 --- a/pkg/component/etcd/statefulset/values_helper.go +++ b/pkg/component/etcd/statefulset/values_helper.go @@ -21,7 +21,6 @@ import ( druidv1alpha1 "github.com/gardener/etcd-druid/api/v1alpha1" "github.com/gardener/etcd-druid/pkg/utils" "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/utils/pointer" ) @@ -58,7 +57,7 @@ func GenerateValues( values := Values{ Name: etcd.Name, Namespace: etcd.Namespace, - OwnerReferences: []metav1.OwnerReference{etcd.GetAsOwnerReference()}, + OwnerReference: etcd.GetAsOwnerReference(), Replicas: etcd.Spec.Replicas, StatusReplicas: etcd.Status.Replicas, Annotations: utils.MergeStringMaps(checksumAnnotations, etcd.Spec.Annotations),