From 333c19aae9178cff1ad4c541fb3bbba2988996c9 Mon Sep 17 00:00:00 2001 From: Tanmay Jain Date: Tue, 31 Oct 2023 12:58:02 +0530 Subject: [PATCH] removing commented code and fixing lints. --- controllers/aerospikecluster_controller.go | 15 ++++++--------- controllers/pvc.go | 11 ++++++----- controllers/statefulset.go | 22 +++++----------------- test/cluster_helper.go | 1 + 4 files changed, 18 insertions(+), 31 deletions(-) diff --git a/controllers/aerospikecluster_controller.go b/controllers/aerospikecluster_controller.go index 254142517..ebfd878ac 100644 --- a/controllers/aerospikecluster_controller.go +++ b/controllers/aerospikecluster_controller.go @@ -48,10 +48,7 @@ type AerospikeClusterReconciler struct { } func stsPodPendingPredicate(e event.UpdateEvent) bool { - if e.ObjectOld == nil { - return false - } - if e.ObjectNew == nil { + if e.ObjectOld == nil || e.ObjectNew == nil { return false } @@ -59,6 +56,7 @@ func stsPodPendingPredicate(e event.UpdateEvent) bool { if !ok { return false } + newSTS, ok := e.ObjectNew.(*appsv1.StatefulSet) if !ok { return false @@ -76,7 +74,7 @@ type StatusPendingPredicate struct { predicate.Funcs } -// Update implements default UpdateEvent filter for validating available replicas change. +// Update implements default UpdateEvent filter for statefulSets. func (StatusPendingPredicate) Update(e event.UpdateEvent) bool { return stsPodPendingPredicate(e) } @@ -91,9 +89,7 @@ func (r *AerospikeClusterReconciler) SetupWithManager(mgr ctrl.Manager) error { CreateFunc: func(e event.CreateEvent) bool { return false }, - UpdateFunc: func(e event.UpdateEvent) bool { - return stsPodPendingPredicate(e) - }, + UpdateFunc: stsPodPendingPredicate, }, ), ). @@ -102,7 +98,8 @@ func (r *AerospikeClusterReconciler) SetupWithManager(mgr ctrl.Manager) error { MaxConcurrentReconciles: maxConcurrentReconciles, }, ). - WithEventFilter(predicate.Or(predicate.GenerationChangedPredicate{}, predicate.LabelChangedPredicate{}, StatusPendingPredicate{})). + WithEventFilter(predicate.Or(predicate.GenerationChangedPredicate{}, predicate.LabelChangedPredicate{}, + StatusPendingPredicate{})). Complete(r) } diff --git a/controllers/pvc.go b/controllers/pvc.go index c6f8fcb7a..099f132fd 100644 --- a/controllers/pvc.go +++ b/controllers/pvc.go @@ -3,12 +3,12 @@ package controllers import ( "context" "fmt" - "k8s.io/apimachinery/pkg/types" "strconv" "time" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" asdbv1 "github.com/aerospike/aerospike-kubernetes-operator/api/v1" @@ -117,17 +117,18 @@ func (r *SingleClusterReconciler) deleteLocalPVCs(pod *corev1.Pod) error { return fmt.Errorf("could not find pvc for pod %v: %v", pod.Name, err) } - for _, pvc := range pvcItems { + for idx := range pvcItems { pv := &corev1.PersistentVolume{} - pvName := types.NamespacedName{Name: pvc.Spec.VolumeName} + pvName := types.NamespacedName{Name: pvcItems[idx].Spec.VolumeName} if err := r.Client.Get(context.TODO(), pvName, pv); err != nil { return err } + if pv.Spec.Local != nil { - if err := r.Client.Delete(context.TODO(), &pvc); err != nil { + if err := r.Client.Delete(context.TODO(), &pvcItems[idx]); err != nil { return fmt.Errorf( - "could not delete pvc %s: %v", pvc.Name, err, + "could not delete pvc %s: %v", pvcItems[idx].Name, err, ) } } diff --git a/controllers/statefulset.go b/controllers/statefulset.go index 099f777ac..bcce3d6fc 100644 --- a/controllers/statefulset.go +++ b/controllers/statefulset.go @@ -598,31 +598,16 @@ func (r *SingleClusterReconciler) updateSTS( // TODO: Add validation. device, file, both should not exist in same storage class r.updateSTSStorage(statefulSet, rackState) - // Save the updated stateful set. - // Can we optimize this? Update stateful set only if there is any change - // in it. - /* currentSTS, err := r.getSTS(rackState) - if err != nil { - return err - } - - if reflect.DeepEqual(currentSTS.Spec, statefulSet.Spec) { - r.Log.Info("statefulset update not needed, no change in spec", "stsName", currentSTS.Name) - return nil - } - - */ - err := retry.RetryOnConflict(retry.DefaultRetry, func() error { found, err := r.getSTS(rackState) if err != nil { return err } - r.Log.Info("print both sts spec", "getsts", found.Spec, "statefulSet", statefulSet.Spec) + if reflect.DeepEqual(found.Spec, statefulSet.Spec) { - r.Log.Info("StatefulSet update not needed, no change in spec", "stsName", found.Name) return nil } + // Save the updated stateful set. found.Spec = statefulSet.Spec return r.Client.Update(context.TODO(), found, updateOption) }) @@ -798,12 +783,14 @@ func (r *SingleClusterReconciler) updateSTSNonPVStorage( // Add volume in statefulSet template perm := corev1.SecretVolumeSourceDefaultMode k8sVolume := createVolumeForVolumeAttachment(volume) + switch { case k8sVolume.Secret != nil: k8sVolume.Secret.DefaultMode = &perm case k8sVolume.ConfigMap != nil: k8sVolume.ConfigMap.DefaultMode = &perm } + st.Spec.Template.Spec.Volumes = append( st.Spec.Template.Spec.Volumes, k8sVolume, ) @@ -1219,6 +1206,7 @@ func getDefaultSTSVolumes( aeroCluster *asdbv1.AerospikeCluster, rackState *RackState, ) []corev1.Volume { defaultMode := corev1.SecretVolumeSourceDefaultMode + return []corev1.Volume{ { Name: confDirName, diff --git a/test/cluster_helper.go b/test/cluster_helper.go index 5e61f60d7..241dda027 100644 --- a/test/cluster_helper.go +++ b/test/cluster_helper.go @@ -1374,6 +1374,7 @@ func getStorageVolumeForAerospike(name, path string) asdbv1.VolumeSpec { func getStorageVolumeForSecret() asdbv1.VolumeSpec { perm := corev1.SecretVolumeSourceDefaultMode + return asdbv1.VolumeSpec{ Name: aerospikeConfigSecret, Source: asdbv1.VolumeSource{