Skip to content

Commit eaa3f2a

Browse files
authored
chore: Added OwnerReferences during resource creation for EphemeralRunnerSet, EphemeralRunner, and EphemeralRunnerPod (#3575)
1 parent 3c1a323 commit eaa3f2a

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

controllers/actions.github.com/resourcebuilder.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ type ResourceBuilder struct {
7373
ExcludeLabelPropagationPrefixes []string
7474
}
7575

76+
// boolPtr returns a pointer to a bool value
77+
func boolPtr(v bool) *bool {
78+
return &v
79+
}
80+
7681
func (b *ResourceBuilder) newAutoScalingListener(autoscalingRunnerSet *v1alpha1.AutoscalingRunnerSet, ephemeralRunnerSet *v1alpha1.EphemeralRunnerSet, namespace, image string, imagePullSecrets []corev1.LocalObjectReference) (*v1alpha1.AutoscalingListener, error) {
7782
runnerScaleSetId, err := strconv.Atoi(autoscalingRunnerSet.Annotations[runnerScaleSetIdAnnotationKey])
7883
if err != nil {
@@ -284,6 +289,16 @@ func (b *ResourceBuilder) newScaleSetListenerPod(autoscalingListener *v1alpha1.A
284289
Name: autoscalingListener.Name,
285290
Namespace: autoscalingListener.Namespace,
286291
Labels: labels,
292+
OwnerReferences: []metav1.OwnerReference{
293+
{
294+
APIVersion: autoscalingListener.GetObjectKind().GroupVersionKind().GroupVersion().String(),
295+
Kind: autoscalingListener.GetObjectKind().GroupVersionKind().Kind,
296+
UID: autoscalingListener.GetUID(),
297+
Name: autoscalingListener.GetName(),
298+
Controller: boolPtr(true),
299+
BlockOwnerDeletion: boolPtr(true),
300+
},
301+
},
287302
},
288303
Spec: podSpec,
289304
}
@@ -530,6 +545,16 @@ func (b *ResourceBuilder) newEphemeralRunnerSet(autoscalingRunnerSet *v1alpha1.A
530545
Namespace: autoscalingRunnerSet.ObjectMeta.Namespace,
531546
Labels: labels,
532547
Annotations: newAnnotations,
548+
OwnerReferences: []metav1.OwnerReference{
549+
{
550+
APIVersion: autoscalingRunnerSet.GetObjectKind().GroupVersionKind().GroupVersion().String(),
551+
Kind: autoscalingRunnerSet.GetObjectKind().GroupVersionKind().Kind,
552+
UID: autoscalingRunnerSet.GetUID(),
553+
Name: autoscalingRunnerSet.GetName(),
554+
Controller: boolPtr(true),
555+
BlockOwnerDeletion: boolPtr(true),
556+
},
557+
},
533558
},
534559
Spec: v1alpha1.EphemeralRunnerSetSpec{
535560
Replicas: 0,
@@ -569,6 +594,16 @@ func (b *ResourceBuilder) newEphemeralRunner(ephemeralRunnerSet *v1alpha1.Epheme
569594
Namespace: ephemeralRunnerSet.Namespace,
570595
Labels: labels,
571596
Annotations: annotations,
597+
OwnerReferences: []metav1.OwnerReference{
598+
{
599+
APIVersion: ephemeralRunnerSet.GetObjectKind().GroupVersionKind().GroupVersion().String(),
600+
Kind: ephemeralRunnerSet.GetObjectKind().GroupVersionKind().Kind,
601+
UID: ephemeralRunnerSet.GetUID(),
602+
Name: ephemeralRunnerSet.GetName(),
603+
Controller: boolPtr(true),
604+
BlockOwnerDeletion: boolPtr(true),
605+
},
606+
},
572607
},
573608
Spec: ephemeralRunnerSet.Spec.EphemeralRunnerSpec,
574609
}
@@ -607,6 +642,16 @@ func (b *ResourceBuilder) newEphemeralRunnerPod(ctx context.Context, runner *v1a
607642
Namespace: runner.ObjectMeta.Namespace,
608643
Labels: labels,
609644
Annotations: annotations,
645+
OwnerReferences: []metav1.OwnerReference{
646+
{
647+
APIVersion: runner.GetObjectKind().GroupVersionKind().GroupVersion().String(),
648+
Kind: runner.GetObjectKind().GroupVersionKind().Kind,
649+
UID: runner.GetUID(),
650+
Name: runner.GetName(),
651+
Controller: boolPtr(true),
652+
BlockOwnerDeletion: boolPtr(true),
653+
},
654+
},
610655
}
611656

612657
newPod.ObjectMeta = objectMeta

controllers/actions.github.com/resourcebuilder_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,69 @@ func TestGitHubURLTrimLabelValues(t *testing.T) {
182182
assert.Len(t, listener.Labels[LabelKeyGitHubRepository], 0)
183183
})
184184
}
185+
186+
func TestOwnershipRelationships(t *testing.T) {
187+
// Create an AutoscalingRunnerSet
188+
autoscalingRunnerSet := v1alpha1.AutoscalingRunnerSet{
189+
ObjectMeta: metav1.ObjectMeta{
190+
Name: "test-scale-set",
191+
Namespace: "test-ns",
192+
UID: "test-autoscaling-runner-set-uid",
193+
Labels: map[string]string{
194+
LabelKeyKubernetesPartOf: labelValueKubernetesPartOf,
195+
LabelKeyKubernetesVersion: "0.2.0",
196+
},
197+
Annotations: map[string]string{
198+
runnerScaleSetIdAnnotationKey: "1",
199+
AnnotationKeyGitHubRunnerGroupName: "test-group",
200+
AnnotationKeyGitHubRunnerScaleSetName: "test-scale-set",
201+
annotationKeyValuesHash: "test-hash",
202+
},
203+
},
204+
Spec: v1alpha1.AutoscalingRunnerSetSpec{
205+
GitHubConfigUrl: "https://github.com/org/repo",
206+
},
207+
}
208+
209+
// Initialize ResourceBuilder
210+
b := ResourceBuilder{}
211+
212+
// Create EphemeralRunnerSet
213+
ephemeralRunnerSet, err := b.newEphemeralRunnerSet(&autoscalingRunnerSet)
214+
require.NoError(t, err)
215+
216+
// Test EphemeralRunnerSet ownership
217+
require.Len(t, ephemeralRunnerSet.OwnerReferences, 1, "EphemeralRunnerSet should have exactly one owner reference")
218+
ownerRef := ephemeralRunnerSet.OwnerReferences[0]
219+
assert.Equal(t, autoscalingRunnerSet.GetName(), ownerRef.Name, "Owner reference name should match AutoscalingRunnerSet name")
220+
assert.Equal(t, autoscalingRunnerSet.GetUID(), ownerRef.UID, "Owner reference UID should match AutoscalingRunnerSet UID")
221+
assert.Equal(t, true, *ownerRef.Controller, "Controller flag should be true")
222+
assert.Equal(t, true, *ownerRef.BlockOwnerDeletion, "BlockOwnerDeletion flag should be true")
223+
224+
// Create EphemeralRunner
225+
ephemeralRunner := b.newEphemeralRunner(ephemeralRunnerSet)
226+
227+
// Test EphemeralRunner ownership
228+
require.Len(t, ephemeralRunner.OwnerReferences, 1, "EphemeralRunner should have exactly one owner reference")
229+
ownerRef = ephemeralRunner.OwnerReferences[0]
230+
assert.Equal(t, ephemeralRunnerSet.GetName(), ownerRef.Name, "Owner reference name should match EphemeralRunnerSet name")
231+
assert.Equal(t, ephemeralRunnerSet.GetUID(), ownerRef.UID, "Owner reference UID should match EphemeralRunnerSet UID")
232+
assert.Equal(t, true, *ownerRef.Controller, "Controller flag should be true")
233+
assert.Equal(t, true, *ownerRef.BlockOwnerDeletion, "BlockOwnerDeletion flag should be true")
234+
235+
// Create EphemeralRunnerPod
236+
runnerSecret := &corev1.Secret{
237+
ObjectMeta: metav1.ObjectMeta{
238+
Name: "test-secret",
239+
},
240+
}
241+
pod := b.newEphemeralRunnerPod(context.TODO(), ephemeralRunner, runnerSecret)
242+
243+
// Test EphemeralRunnerPod ownership
244+
require.Len(t, pod.OwnerReferences, 1, "EphemeralRunnerPod should have exactly one owner reference")
245+
ownerRef = pod.OwnerReferences[0]
246+
assert.Equal(t, ephemeralRunner.GetName(), ownerRef.Name, "Owner reference name should match EphemeralRunner name")
247+
assert.Equal(t, ephemeralRunner.GetUID(), ownerRef.UID, "Owner reference UID should match EphemeralRunner UID")
248+
assert.Equal(t, true, *ownerRef.Controller, "Controller flag should be true")
249+
assert.Equal(t, true, *ownerRef.BlockOwnerDeletion, "BlockOwnerDeletion flag should be true")
250+
}

0 commit comments

Comments
 (0)