From 1b614886ec1c945a0e74079ad235dce55bc8dc80 Mon Sep 17 00:00:00 2001 From: Yusuke Kuoka Date: Sun, 22 Jan 2023 01:30:59 +0000 Subject: [PATCH] Update status patcher util and interface for newer controller-runtime compatibiility --- .../autoscalingrunnerset_controller.go | 2 +- controllers/actions.github.com/clientutil.go | 10 ++++++++++ .../actions.github.com/ephemeralrunner_controller.go | 10 +++++----- .../ephemeralrunnerset_controller.go | 2 +- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/controllers/actions.github.com/autoscalingrunnerset_controller.go b/controllers/actions.github.com/autoscalingrunnerset_controller.go index 2be67c0ef8..a268d560d9 100644 --- a/controllers/actions.github.com/autoscalingrunnerset_controller.go +++ b/controllers/actions.github.com/autoscalingrunnerset_controller.go @@ -219,7 +219,7 @@ func (r *AutoscalingRunnerSetReconciler) Reconcile(ctx context.Context, req ctrl // Update the status of autoscaling runner set. if latestRunnerSet.Status.CurrentReplicas != autoscalingRunnerSet.Status.CurrentRunners { - if err := patch(ctx, r.Status(), autoscalingRunnerSet, func(obj *v1alpha1.AutoscalingRunnerSet) { + if err := patchSubResource(ctx, r.Status(), autoscalingRunnerSet, func(obj *v1alpha1.AutoscalingRunnerSet) { obj.Status.CurrentRunners = latestRunnerSet.Status.CurrentReplicas }); err != nil { log.Error(err, "Failed to update autoscaling runner set status with current runner count") diff --git a/controllers/actions.github.com/clientutil.go b/controllers/actions.github.com/clientutil.go index e3dfbebb4f..876d8dfd43 100644 --- a/controllers/actions.github.com/clientutil.go +++ b/controllers/actions.github.com/clientutil.go @@ -20,3 +20,13 @@ func patch[T object[T]](ctx context.Context, client patcher, obj T, update func( update(obj) return client.Patch(ctx, obj, kclient.MergeFrom(original)) } + +type subResourcePatcher interface { + Patch(ctx context.Context, obj kclient.Object, patch kclient.Patch, opts ...kclient.SubResourcePatchOption) error +} + +func patchSubResource[T object[T]](ctx context.Context, client subResourcePatcher, obj T, update func(obj T)) error { + original := obj.DeepCopy() + update(obj) + return client.Patch(ctx, obj, kclient.MergeFrom(original)) +} diff --git a/controllers/actions.github.com/ephemeralrunner_controller.go b/controllers/actions.github.com/ephemeralrunner_controller.go index b6945d2986..7ee546cb07 100644 --- a/controllers/actions.github.com/ephemeralrunner_controller.go +++ b/controllers/actions.github.com/ephemeralrunner_controller.go @@ -369,7 +369,7 @@ func (r *EphemeralRunnerReconciler) cleanupRunnerLinkedSecrets(ctx context.Conte func (r *EphemeralRunnerReconciler) markAsFailed(ctx context.Context, ephemeralRunner *v1alpha1.EphemeralRunner, log logr.Logger) error { log.Info("Updating ephemeral runner status to Failed") - if err := patch(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) { + if err := patchSubResource(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) { obj.Status.Phase = corev1.PodFailed obj.Status.Reason = "TooManyPodFailures" obj.Status.Message = "Pod has failed to start more than 5 times" @@ -388,7 +388,7 @@ func (r *EphemeralRunnerReconciler) markAsFailed(ctx context.Context, ephemeralR func (r *EphemeralRunnerReconciler) markAsFinished(ctx context.Context, ephemeralRunner *v1alpha1.EphemeralRunner, log logr.Logger) error { log.Info("Updating ephemeral runner status to Finished") - if err := patch(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) { + if err := patchSubResource(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) { obj.Status.Phase = corev1.PodSucceeded }); err != nil { return fmt.Errorf("failed to update ephemeral runner with status finished: %v", err) @@ -409,7 +409,7 @@ func (r *EphemeralRunnerReconciler) deletePodAsFailed(ctx context.Context, ephem } log.Info("Updating ephemeral runner status to track the failure count") - if err := patch(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) { + if err := patchSubResource(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) { if obj.Status.Failures == nil { obj.Status.Failures = make(map[string]bool) } @@ -487,7 +487,7 @@ func (r *EphemeralRunnerReconciler) updateStatusWithRunnerConfig(ctx context.Con log.Info("Created ephemeral runner JIT config", "runnerId", jitConfig.Runner.Id) log.Info("Updating ephemeral runner status with runnerId and runnerJITConfig") - err = patch(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) { + err = patchSubResource(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) { obj.Status.RunnerId = jitConfig.Runner.Id obj.Status.RunnerName = jitConfig.Runner.Name obj.Status.RunnerJITConfig = jitConfig.EncodedJITConfig @@ -556,7 +556,7 @@ func (r *EphemeralRunnerReconciler) updateRunStatusFromPod(ctx context.Context, } log.Info("Updating ephemeral runner status with pod phase", "phase", pod.Status.Phase, "reason", pod.Status.Reason, "message", pod.Status.Message) - err := patch(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) { + err := patchSubResource(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) { obj.Status.Phase = pod.Status.Phase obj.Status.Ready = obj.Status.Ready || (pod.Status.Phase == corev1.PodRunning) obj.Status.Reason = pod.Status.Reason diff --git a/controllers/actions.github.com/ephemeralrunnerset_controller.go b/controllers/actions.github.com/ephemeralrunnerset_controller.go index ed0776a9be..6744e1bad2 100644 --- a/controllers/actions.github.com/ephemeralrunnerset_controller.go +++ b/controllers/actions.github.com/ephemeralrunnerset_controller.go @@ -182,7 +182,7 @@ func (r *EphemeralRunnerSetReconciler) Reconcile(ctx context.Context, req ctrl.R // Update the status if needed. if ephemeralRunnerSet.Status.CurrentReplicas != total { log.Info("Updating status with current runners count", "count", total) - if err := patch(ctx, r.Status(), ephemeralRunnerSet, func(obj *v1alpha1.EphemeralRunnerSet) { + if err := patchSubResource(ctx, r.Status(), ephemeralRunnerSet, func(obj *v1alpha1.EphemeralRunnerSet) { obj.Status.CurrentReplicas = total }); err != nil { log.Error(err, "Failed to update status with current runners count")