Skip to content

Commit

Permalink
chore: wip patching, throws some status errors
Browse files Browse the repository at this point in the history
Signed-off-by: AlexsJones <[email protected]>
  • Loading branch information
AlexsJones committed Jan 15, 2025
1 parent ffaf0fb commit f73ad42
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions internal/controller/mutation/mutation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ func (r *MutationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
}
// Convert the spec.targetConfiguration to an Object
// 1. Get the GVK from the Kind string
gv, err := schema.ParseGroupVersion(mutation.Spec.Result.Spec.Kind)
gv, err := schema.ParseGroupVersion(mutation.Spec.Resource.Kind)
if err != nil {
mutationControllerLog.Error(err, "unable to parse group version from kind", "kind", mutation.Kind)
return ctrl.Result{}, err
}
gvk := gv.WithKind(mutation.Spec.Result.Spec.Kind)
gvk := gv.WithKind(mutation.Spec.Resource.Kind)

// 2. Create an unstructured object
obj := &unstructured.Unstructured{}
Expand All @@ -143,6 +143,23 @@ func (r *MutationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
mutationControllerLog.Error(err, "unable to decode target configuration", "configuration", mutation.Spec.TargetConfiguration)
return ctrl.Result{RequeueAfter: 60 * time.Second}, err
}
// 4. Set the object's name and namespace (important for updates!)
obj.SetName(mutation.Spec.Resource.Name)
obj.SetNamespace(mutation.Spec.Resource.Namespace)

// 5. Apply the update using Patch
patch := client.MergeFrom(obj) // Create a patch based on the current state of the object
if err := r.Client.Patch(ctx, obj, patch); err != nil {
mutationControllerLog.Error(err, "unable to patch object", "object", obj.GetName())
return ctrl.Result{RequeueAfter: 60 * time.Second}, err
}
mutationControllerLog.Info("Successfully patched object", "object", obj.GetName())
// update status with the crazy process again
mutation.Status.Phase = corev1alpha1.AutoRemediationPhaseCompleted
if err := r.Client.Status().Update(ctx, &mutation); err != nil {
mutationControllerLog.Error(err, "unable to update mutation status")
return ctrl.Result{}, err
}
break
case corev1alpha1.AutoRemediationPhaseCompleted:
// this is when the execute/apply is completed
Expand Down

0 comments on commit f73ad42

Please sign in to comment.