Skip to content

Commit 1b61488

Browse files
committed
Update status patcher util and interface for newer controller-runtime compatibiility
1 parent 1623a23 commit 1b61488

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

controllers/actions.github.com/autoscalingrunnerset_controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func (r *AutoscalingRunnerSetReconciler) Reconcile(ctx context.Context, req ctrl
219219

220220
// Update the status of autoscaling runner set.
221221
if latestRunnerSet.Status.CurrentReplicas != autoscalingRunnerSet.Status.CurrentRunners {
222-
if err := patch(ctx, r.Status(), autoscalingRunnerSet, func(obj *v1alpha1.AutoscalingRunnerSet) {
222+
if err := patchSubResource(ctx, r.Status(), autoscalingRunnerSet, func(obj *v1alpha1.AutoscalingRunnerSet) {
223223
obj.Status.CurrentRunners = latestRunnerSet.Status.CurrentReplicas
224224
}); err != nil {
225225
log.Error(err, "Failed to update autoscaling runner set status with current runner count")

controllers/actions.github.com/clientutil.go

+10
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,13 @@ func patch[T object[T]](ctx context.Context, client patcher, obj T, update func(
2020
update(obj)
2121
return client.Patch(ctx, obj, kclient.MergeFrom(original))
2222
}
23+
24+
type subResourcePatcher interface {
25+
Patch(ctx context.Context, obj kclient.Object, patch kclient.Patch, opts ...kclient.SubResourcePatchOption) error
26+
}
27+
28+
func patchSubResource[T object[T]](ctx context.Context, client subResourcePatcher, obj T, update func(obj T)) error {
29+
original := obj.DeepCopy()
30+
update(obj)
31+
return client.Patch(ctx, obj, kclient.MergeFrom(original))
32+
}

controllers/actions.github.com/ephemeralrunner_controller.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ func (r *EphemeralRunnerReconciler) cleanupRunnerLinkedSecrets(ctx context.Conte
369369

370370
func (r *EphemeralRunnerReconciler) markAsFailed(ctx context.Context, ephemeralRunner *v1alpha1.EphemeralRunner, log logr.Logger) error {
371371
log.Info("Updating ephemeral runner status to Failed")
372-
if err := patch(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) {
372+
if err := patchSubResource(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) {
373373
obj.Status.Phase = corev1.PodFailed
374374
obj.Status.Reason = "TooManyPodFailures"
375375
obj.Status.Message = "Pod has failed to start more than 5 times"
@@ -388,7 +388,7 @@ func (r *EphemeralRunnerReconciler) markAsFailed(ctx context.Context, ephemeralR
388388

389389
func (r *EphemeralRunnerReconciler) markAsFinished(ctx context.Context, ephemeralRunner *v1alpha1.EphemeralRunner, log logr.Logger) error {
390390
log.Info("Updating ephemeral runner status to Finished")
391-
if err := patch(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) {
391+
if err := patchSubResource(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) {
392392
obj.Status.Phase = corev1.PodSucceeded
393393
}); err != nil {
394394
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
409409
}
410410

411411
log.Info("Updating ephemeral runner status to track the failure count")
412-
if err := patch(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) {
412+
if err := patchSubResource(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) {
413413
if obj.Status.Failures == nil {
414414
obj.Status.Failures = make(map[string]bool)
415415
}
@@ -487,7 +487,7 @@ func (r *EphemeralRunnerReconciler) updateStatusWithRunnerConfig(ctx context.Con
487487
log.Info("Created ephemeral runner JIT config", "runnerId", jitConfig.Runner.Id)
488488

489489
log.Info("Updating ephemeral runner status with runnerId and runnerJITConfig")
490-
err = patch(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) {
490+
err = patchSubResource(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) {
491491
obj.Status.RunnerId = jitConfig.Runner.Id
492492
obj.Status.RunnerName = jitConfig.Runner.Name
493493
obj.Status.RunnerJITConfig = jitConfig.EncodedJITConfig
@@ -556,7 +556,7 @@ func (r *EphemeralRunnerReconciler) updateRunStatusFromPod(ctx context.Context,
556556
}
557557

558558
log.Info("Updating ephemeral runner status with pod phase", "phase", pod.Status.Phase, "reason", pod.Status.Reason, "message", pod.Status.Message)
559-
err := patch(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) {
559+
err := patchSubResource(ctx, r.Status(), ephemeralRunner, func(obj *v1alpha1.EphemeralRunner) {
560560
obj.Status.Phase = pod.Status.Phase
561561
obj.Status.Ready = obj.Status.Ready || (pod.Status.Phase == corev1.PodRunning)
562562
obj.Status.Reason = pod.Status.Reason

controllers/actions.github.com/ephemeralrunnerset_controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (r *EphemeralRunnerSetReconciler) Reconcile(ctx context.Context, req ctrl.R
182182
// Update the status if needed.
183183
if ephemeralRunnerSet.Status.CurrentReplicas != total {
184184
log.Info("Updating status with current runners count", "count", total)
185-
if err := patch(ctx, r.Status(), ephemeralRunnerSet, func(obj *v1alpha1.EphemeralRunnerSet) {
185+
if err := patchSubResource(ctx, r.Status(), ephemeralRunnerSet, func(obj *v1alpha1.EphemeralRunnerSet) {
186186
obj.Status.CurrentReplicas = total
187187
}); err != nil {
188188
log.Error(err, "Failed to update status with current runners count")

0 commit comments

Comments
 (0)