Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
Signed-off-by: Abhradeep Chakraborty <[email protected]>
  • Loading branch information
Abhra303 committed Aug 20, 2024
1 parent 2228b22 commit 1055eab
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 59 deletions.
23 changes: 17 additions & 6 deletions e2e/dragonfly_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,18 +478,26 @@ var _ = Describe("Dragonfly Lifecycle tests", Ordered, FlakeAttempts(3), func()
// check for affinity
Expect(pod.Spec.Affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution).To(Equal(newAffinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution))
}
// Update df to the latest
err = k8sClient.Get(ctx, types.NamespacedName{
Name: name,
Namespace: namespace,
}, &df)
Expect(err).To(BeNil())
GinkgoLogr.Info("df arg propagate phase", "phase", df.Status.Phase, "rolling-update", df.Status.IsRollingUpdate)

})

It("Check for data", func() {
stopChan := make(chan struct{}, 1)
defer close(stopChan)
rc, err := checkAndK8sPortForwardRedis(ctx, clientset, cfg, stopChan, name, namespace, password, 6395)
Expect(err).To(BeNil())

// Check for test data
data, err := rc.Get(ctx, "foo").Result()
Expect(err).To(BeNil())
Expect(data).To(Equal("bar"))
defer close(stopChan)
})

It("Change Service specification to LoadBalancer", func() {
Expand All @@ -512,14 +520,15 @@ var _ = Describe("Dragonfly Lifecycle tests", Ordered, FlakeAttempts(3), func()
Labels: newLabels,
}

GinkgoLogr.Info("df phase", "phase", df.Status.Phase, "rolling-update", df.Status.IsRollingUpdate)
err = k8sClient.Update(ctx, &df)
Expect(err).To(BeNil())

// Wait until Dragonfly object is marked ready
err = waitForDragonflyPhase(ctx, k8sClient, name, namespace, controller.PhaseReady, 3*time.Minute)
Expect(err).To(BeNil())
err = waitForStatefulSetReady(ctx, k8sClient, name, namespace, 3*time.Minute)
Expect(err).To(BeNil())
// // Wait until Dragonfly object is marked ready
// err = waitForDragonflyPhase(ctx, k8sClient, name, namespace, controller.PhaseReady, 1*time.Minute)
// Expect(err).To(BeNil())
// err = waitForStatefulSetReady(ctx, k8sClient, name, namespace, 3*time.Minute)
// Expect(err).To(BeNil())

var svc corev1.Service
err = k8sClient.Get(ctx, types.NamespacedName{
Expand Down Expand Up @@ -919,6 +928,8 @@ func isDragonflyInphase(ctx context.Context, c client.Client, name, namespace, p
return false, nil
}

GinkgoLogr.Info("dragonfly phase", "phase", df.Status.Phase, "update", df.Status.IsRollingUpdate)

// Ready means we also want rolling update to be false
if phase == controller.PhaseReady {
// check for replicas
Expand Down
93 changes: 40 additions & 53 deletions internal/controller/dragonfly_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ package controller
import (
"context"
"fmt"
"slices"
"time"

dfv1alpha1 "github.com/dragonflydb/dragonfly-operator/api/v1alpha1"
"github.com/dragonflydb/dragonfly-operator/internal/resources"
"github.com/samber/lo"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -117,16 +114,6 @@ func (r *DragonflyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
return ctrl.Result{}, err
}

// If df updates an immutable field, delete the statefulset with Orphan propagation policy
// and recreate the statefulset in the next loop.
if r.cantUpdateStatefulSet(&df, &statefulSet) {
var err error
if err = r.Delete(ctx, &statefulSet, client.PropagationPolicy(v1.DeletePropagationOrphan)); err != nil {
log.Error(err, fmt.Sprintf("could not delete statefulset %s/%s", statefulSet.GetNamespace(), statefulSet.GetName()))
}
return ctrl.Result{Requeue: true}, err
}

// Update all resources even if the df is in rollout state to ensure
// that newer updates don't get blocked by failed update attempts.
log.Info("updating existing resources")
Expand Down Expand Up @@ -361,46 +348,46 @@ func (r *DragonflyReconciler) getMissingResources(ctx context.Context, df *dfv1a
return missingResources, nil
}

func (r *DragonflyReconciler) isPVCSpecChanged(df *dfv1alpha1.Dragonfly, sts *appsv1.StatefulSet) bool {
dfPVCSpec := getPVCSpecFromDragonfly(df)
stsPVCSpec := getPVCSpecFromStatefulSet(sts)

return !isPVCSpecEqual(dfPVCSpec, stsPVCSpec)
}

func getPVCSpecFromDragonfly(df *dfv1alpha1.Dragonfly) *corev1.PersistentVolumeClaimSpec {
if df.Spec.Snapshot != nil {
return df.Spec.Snapshot.PersistentVolumeClaimSpec
}
return nil
}

func getPVCSpecFromStatefulSet(sts *appsv1.StatefulSet) *corev1.PersistentVolumeClaimSpec {
for i := range sts.Spec.VolumeClaimTemplates {
if sts.Spec.VolumeClaimTemplates[i].Name == "df" {
return &sts.Spec.VolumeClaimTemplates[i].Spec
}
}
return nil
}

func isPVCSpecEqual(spec1, spec2 *corev1.PersistentVolumeClaimSpec) bool {
if spec1 == nil && spec2 == nil {
return true
}
if spec1 == nil || spec2 == nil {
return false
}

// Compare essential fields
return slices.Equal(spec1.AccessModes, spec2.AccessModes) &&
lo.FromPtr(spec1.StorageClassName) == lo.FromPtr(spec2.StorageClassName) &&
spec1.Resources.Requests.Storage().Equal(*spec2.Resources.Requests.Storage())
}

func (r *DragonflyReconciler) cantUpdateStatefulSet(df *dfv1alpha1.Dragonfly, sts *appsv1.StatefulSet) bool {
return r.isPVCSpecChanged(df, sts)
}
// func (r *DragonflyReconciler) isPVCSpecChanged(df *dfv1alpha1.Dragonfly, sts *appsv1.StatefulSet) bool {
// dfPVCSpec := getPVCSpecFromDragonfly(df)
// stsPVCSpec := getPVCSpecFromStatefulSet(sts)

// return !isPVCSpecEqual(dfPVCSpec, stsPVCSpec)
// }

// func getPVCSpecFromDragonfly(df *dfv1alpha1.Dragonfly) *corev1.PersistentVolumeClaimSpec {
// if df.Spec.Snapshot != nil {
// return df.Spec.Snapshot.PersistentVolumeClaimSpec
// }
// return nil
// }

// func getPVCSpecFromStatefulSet(sts *appsv1.StatefulSet) *corev1.PersistentVolumeClaimSpec {
// for i := range sts.Spec.VolumeClaimTemplates {
// if sts.Spec.VolumeClaimTemplates[i].Name == "df" {
// return &sts.Spec.VolumeClaimTemplates[i].Spec
// }
// }
// return nil
// }

// func isPVCSpecEqual(spec1, spec2 *corev1.PersistentVolumeClaimSpec) bool {
// if spec1 == nil && spec2 == nil {
// return true
// }
// if spec1 == nil || spec2 == nil {
// return false
// }

// // Compare essential fields
// return slices.Equal(spec1.AccessModes, spec2.AccessModes) &&
// lo.FromPtr(spec1.StorageClassName) == lo.FromPtr(spec2.StorageClassName) &&
// spec1.Resources.Requests.Storage().Equal(*spec2.Resources.Requests.Storage())
// }

// func (r *DragonflyReconciler) cantUpdateStatefulSet(df *dfv1alpha1.Dragonfly, sts *appsv1.StatefulSet) bool {
// return r.isPVCSpecChanged(df, sts)
// }

// SetupWithManager sets up the controller with the Manager.
func (r *DragonflyReconciler) SetupWithManager(mgr ctrl.Manager) error {
Expand Down

0 comments on commit 1055eab

Please sign in to comment.