diff --git a/controllers/cloud_profile.go b/controllers/cloud_profile.go index 30be5b4..9027663 100644 --- a/controllers/cloud_profile.go +++ b/controllers/cloud_profile.go @@ -4,6 +4,7 @@ package controllers import ( "context" + "encoding/json" "errors" "fmt" @@ -37,7 +38,9 @@ func (r *Reconciler) reconcileCloudProfile(ctx context.Context, log logr.Logger, var cloudProfile gardenerv1beta1.CloudProfile cloudProfile.Name = mcp.Name - _, err := controllerutil.CreateOrPatch(ctx, r.Client, &cloudProfile, func() error { + var specBefore gardenerv1beta1.CloudProfileSpec + op, err := controllerutil.CreateOrPatch(ctx, r.Client, &cloudProfile, func() error { + specBefore = *cloudProfile.Spec.DeepCopy() if err := controllerutil.SetControllerReference(mcp, &cloudProfile, r.Scheme()); err != nil { return err } @@ -60,6 +63,18 @@ func (r *Reconciler) reconcileCloudProfile(ctx context.Context, log logr.Logger, gardenerv1beta1.SetObjectDefaults_CloudProfile(&cloudProfile) return errors.Join(errs...) }) + log.Info("CloudProfile patch operation", "operation", op) + if op == controllerutil.OperationResultUpdated && log.V(1).Enabled() { + beforeJSON, err := json.Marshal(specBefore) + if err != nil { + log.Error(err, "failed to marshal spec before") + } + afterJSON, err := json.Marshal(cloudProfile.Spec) + if err != nil { + log.Error(err, "failed to marshal spec after") + } + log.V(1).Info("CloudProfile spec diff", "before", string(beforeJSON), "after", string(afterJSON)) + } if err != nil { statusErr := r.patchStatusAndCondition(ctx, mcp, v1alpha1.FailedReconcileStatus, metav1.Condition{ Type: CloudProfileAppliedConditionType, @@ -72,6 +87,7 @@ func (r *Reconciler) reconcileCloudProfile(ctx context.Context, log logr.Logger, return fmt.Errorf("failed to patch ManagedCloudProfile status: %w", statusErr) } if apierrors.IsInvalid(err) { + log.Error(err, "CloudProfile is invalid, skipping retry") return nil } return fmt.Errorf("failed to create or patch CloudProfile: %w", err) diff --git a/controllers/managedcloudprofile_controller.go b/controllers/managedcloudprofile_controller.go index 0963cb7..201083e 100644 --- a/controllers/managedcloudprofile_controller.go +++ b/controllers/managedcloudprofile_controller.go @@ -49,6 +49,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu return ctrl.Result{}, client.IgnoreNotFound(err) } + log.V(1).Info("reconcile triggered", "generation", mcp.Generation, "resourceVersion", mcp.ResourceVersion) + if err := r.reconcileCloudProfile(ctx, log, &mcp); err != nil { return ctrl.Result{}, err } @@ -61,14 +63,17 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu } func (r *Reconciler) patchStatusAndCondition(ctx context.Context, mcp *v1alpha1.ManagedCloudProfile, status v1alpha1.ReconcileStatus, cond metav1.Condition) error { + log := ctrl.LoggerFrom(ctx) original := mcp.DeepCopy() mcp.Status.Status = status if cond.Type != "" { mcp.Status.Conditions = applyCondition(mcp.Status.Conditions, cond) } if equality.Semantic.DeepEqual(original.Status, mcp.Status) { + log.V(1).Info("MCP status patch skipped, no change") return nil } + log.V(1).Info("MCP status patch issued", "status", status, "condition", cond.Type) return r.Status().Patch(ctx, mcp, client.MergeFrom(original)) }