Skip to content

Commit

Permalink
Allow external updates to Operator managed resources (#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
thegridman authored Oct 9, 2023
1 parent e80e86e commit 01553e8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
12 changes: 4 additions & 8 deletions controllers/job/job_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ func (in *ReconcileJob) createJob(ctx context.Context, deployment coh.CoherenceR
}

func (in *ReconcileJob) updateJob(ctx context.Context, deployment coh.CoherenceResource, job *batchv1.Job, storage utils.Storage, logger logr.Logger) (reconcile.Result, error) {
logger.Info("Updating job")

// get the desired resource state from the store
resource, found := storage.GetLatest().GetResource(coh.ResourceTypeJob, job.Name)
if !found {
Expand All @@ -231,6 +229,10 @@ func (in *ReconcileJob) updateJob(ctx context.Context, deployment coh.CoherenceR
// Patch the Job if required, returning a bool to indicate whether a patch was applied.
func (in *ReconcileJob) patchJob(ctx context.Context, deployment coh.CoherenceResource, job, desired *batchv1.Job, storage utils.Storage, logger logr.Logger) (reconcile.Result, error) {
hashMatches := in.HashLabelsMatch(job, storage)
if hashMatches {
return reconcile.Result{}, nil
}

resource, _ := storage.GetPrevious().GetResource(coh.ResourceTypeJob, job.GetName())
original := &batchv1.Job{}

Expand Down Expand Up @@ -320,8 +322,6 @@ func (in *ReconcileJob) patchJob(ctx context.Context, deployment coh.CoherenceRe
return reconcile.Result{}, nil
}

logger.Info("Updating Job")

// now apply the patch
patched, err := in.ApplyThreeWayPatchWithCallback(ctx, current.GetName(), current, patch, data, callback)

Expand All @@ -334,10 +334,6 @@ func (in *ReconcileJob) patchJob(ctx context.Context, deployment coh.CoherenceRe
return reconcile.Result{Requeue: true}, nil
}

if patched && hashMatches {
logger.Info("Patch applied to job even though hashes matched (possible external update)")
}

return reconcile.Result{}, nil
}

Expand Down
13 changes: 8 additions & 5 deletions controllers/reconciler/base_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func (in *CommonReconciler) ApplyThreeWayPatchWithCallback(ctx context.Context,
callback()
}

in.GetLog().WithValues().Info(fmt.Sprintf("Patching %s/%s with %s", kind, name, string(data)))
in.GetLog().WithValues().Info(fmt.Sprintf("Patching %s/%s", kind, name), "Patch", string(data))
err := in.GetManager().GetClient().Patch(ctx, current, patch)
if err != nil {
return false, errors.Wrapf(err, "failed to patch %s/%s with %s", kind, name, string(data))
Expand All @@ -461,7 +461,7 @@ func (in *CommonReconciler) CreateThreeWayPatch(name string, original, desired,

// log the patch
kind := current.GetObjectKind().GroupVersionKind().Kind
in.GetLog().Info(fmt.Sprintf("Created patch for %s/%s %s", kind, name, string(data)))
in.GetLog().Info(fmt.Sprintf("Created patch for %s/%s", kind, name), "Patch", string(data))

return client.RawPatch(in.patchType, data), data, nil
}
Expand Down Expand Up @@ -877,9 +877,12 @@ func (in *ReconcileSecondaryResource) Delete(ctx context.Context, namespace, nam

// Update possibly updates the resource if the current state differs from the desired state.
func (in *ReconcileSecondaryResource) Update(ctx context.Context, name string, current client.Object, storage utils.Storage, logger logr.Logger) error {
logger.Info(fmt.Sprintf("Updating %v", in.Kind))

hashMatches := in.HashLabelsMatch(current, storage)
if hashMatches {
logger.Info(fmt.Sprintf("Nothing to update for %v, hashes match", in.Kind))
return nil
}

original, _ := storage.GetPrevious().GetResource(in.Kind, name)
desired, found := storage.GetLatest().GetResource(in.Kind, name)
if !found {
Expand All @@ -889,7 +892,7 @@ func (in *ReconcileSecondaryResource) Update(ctx context.Context, name string, c

patched, err := in.ThreeWayPatch(ctx, name, current, original.Spec, desired.Spec)
if patched && hashMatches {
logger.Info(fmt.Sprintf("Patch applied to %v even though hashes matched (possible external update)", in.Kind))
logger.Info(fmt.Sprintf("Patch applied to %v", in.Kind))
}
return err
}
Expand Down
8 changes: 6 additions & 2 deletions controllers/servicemonitor/servicemonitor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,13 @@ func (in *ReconcileServiceMonitor) CreateServiceMonitor(ctx context.Context, nam

// UpdateServiceMonitor possibly updates the ServiceMonitor if the current state differs from the desired state.
func (in *ReconcileServiceMonitor) UpdateServiceMonitor(ctx context.Context, namespace, name string, current *monitoring.ServiceMonitor, storage utils.Storage, logger logr.Logger) error {
logger.Info(fmt.Sprintf("Updating %v", in.Kind))

hashMatches := in.HashLabelsMatch(current, storage)
if hashMatches {
logger.Info(fmt.Sprintf("Nothing to update for %v, hashes match", in.Kind))
return nil
}

logger.Info(fmt.Sprintf("Updating %v", in.Kind))
original, _ := storage.GetPrevious().GetResource(in.Kind, name)
desired, found := storage.GetLatest().GetResource(in.Kind, name)
if !found {
Expand Down
12 changes: 4 additions & 8 deletions controllers/statefulset/statefulset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,6 @@ func (in *ReconcileStatefulSet) createStatefulSet(ctx context.Context, deploymen
}

func (in *ReconcileStatefulSet) updateStatefulSet(ctx context.Context, deployment coh.CoherenceResource, current *appsv1.StatefulSet, storage utils.Storage, logger logr.Logger) (reconcile.Result, error) {
logger.Info("Updating statefulSet")

var err error
result := reconcile.Result{}

Expand Down Expand Up @@ -350,6 +348,10 @@ func (in *ReconcileStatefulSet) updateStatefulSet(ctx context.Context, deploymen
// Patch the StatefulSet if required, returning a bool to indicate whether a patch was applied.
func (in *ReconcileStatefulSet) patchStatefulSet(ctx context.Context, deployment coh.CoherenceResource, current, desired *appsv1.StatefulSet, storage utils.Storage, logger logr.Logger) (reconcile.Result, error) {
hashMatches := in.HashLabelsMatch(current, storage)
if hashMatches {
return reconcile.Result{}, nil
}

resource, _ := storage.GetPrevious().GetResource(coh.ResourceTypeStatefulSet, current.GetName())
original := &appsv1.StatefulSet{}

Expand Down Expand Up @@ -452,8 +454,6 @@ func (in *ReconcileStatefulSet) patchStatefulSet(ctx context.Context, deployment
return reconcile.Result{}, nil
}

logger.Info("Updating StatefulSet")

if deploymentSpec.CheckHABeforeUpdate() {
// Check we have the expected number of ready replicas
if readyReplicas != currentReplicas {
Expand Down Expand Up @@ -498,10 +498,6 @@ func (in *ReconcileStatefulSet) patchStatefulSet(ctx context.Context, deployment
return reconcile.Result{Requeue: true}, nil
}

if patched && hashMatches {
logger.Info("Patch applied to statefulSet even though hashes matched (possible external update)")
}

return reconcile.Result{}, nil
}

Expand Down

0 comments on commit 01553e8

Please sign in to comment.