Skip to content

Commit 53e491e

Browse files
liztiok8s-ci-robot
authored andcommitted
[0.1] set OwnerRef in more cases (#1207)
* Add HasOwner function * Adopt conditionally based on owner
1 parent d2f6ecb commit 53e491e

File tree

6 files changed

+117
-3
lines changed

6 files changed

+117
-3
lines changed

pkg/controller/machine/machine_controller.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/runtime"
2828
"k8s.io/klog"
29+
"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
2930
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
3031
controllerError "sigs.k8s.io/cluster-api/pkg/controller/error"
3132
"sigs.k8s.io/cluster-api/pkg/controller/remote"
@@ -122,7 +123,8 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul
122123
}
123124

124125
// Set the ownerRef with foreground deletion if there is a linked cluster.
125-
if cluster != nil && len(m.OwnerReferences) == 0 {
126+
if cluster != nil && shouldAdopt(m) {
127+
klog.Infof("Cluster %s/%s is adopting Machine %s", cluster.Namespace, cluster.Name, m.Name)
126128
blockOwnerDeletion := true
127129
m.OwnerReferences = append(m.OwnerReferences, metav1.OwnerReference{
128130
APIVersion: cluster.APIVersion,
@@ -384,3 +386,7 @@ func (r *ReconcileMachine) getMachinesInCluster(ctx context.Context, namespace,
384386

385387
return machines, nil
386388
}
389+
390+
func shouldAdopt(m *v1alpha1.Machine) bool {
391+
return !util.HasOwner(m.OwnerReferences, v1alpha1.SchemeGroupVersion.String(), []string{"MachineSet", "MachineDeployment", "Cluster"})
392+
}

pkg/controller/machinedeployment/machinedeployment_controller.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ func (r *ReconcileMachineDeployment) reconcile(ctx context.Context, d *v1alpha1.
173173
}
174174

175175
// Set the ownerRef with foreground deletion if there is a linked cluster.
176-
if cluster != nil && len(d.OwnerReferences) == 0 {
176+
if cluster != nil && shouldAdopt(d) {
177+
klog.Infof("Cluster %s/%s is adopting MachineDeployment %s", cluster.Namespace, cluster.Name, d.Name)
177178
blockOwnerDeletion := true
178179
d.OwnerReferences = append(d.OwnerReferences, metav1.OwnerReference{
179180
APIVersion: cluster.APIVersion,
@@ -412,3 +413,7 @@ func (r *ReconcileMachineDeployment) MachineSetToDeployments(o handler.MapObject
412413

413414
return result
414415
}
416+
417+
func shouldAdopt(md *v1alpha1.MachineDeployment) bool {
418+
return !util.HasOwner(md.OwnerReferences, v1alpha1.SchemeGroupVersion.String(), []string{"Cluster"})
419+
}

pkg/controller/machineset/machineset_controller.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"k8s.io/apimachinery/pkg/runtime"
3232
"k8s.io/client-go/tools/record"
3333
"k8s.io/klog"
34+
"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
3435
clusterv1alpha1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
3536
"sigs.k8s.io/cluster-api/pkg/util"
3637
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -170,7 +171,8 @@ func (r *ReconcileMachineSet) reconcile(ctx context.Context, machineSet *cluster
170171
}
171172

172173
// Set the ownerRef with foreground deletion if there is a linked cluster.
173-
if cluster != nil && len(machineSet.OwnerReferences) == 0 {
174+
if cluster != nil && shouldAdopt(machineSet) {
175+
klog.Infof("Cluster %s/%s is adopting MachineSet %s", cluster.Namespace, cluster.Name, machineSet.Name)
174176
blockOwnerDeletion := true
175177
machineSet.OwnerReferences = append(machineSet.OwnerReferences, metav1.OwnerReference{
176178
APIVersion: cluster.APIVersion,
@@ -494,3 +496,7 @@ func (r *ReconcileMachineSet) MachineToMachineSets(o handler.MapObject) []reconc
494496

495497
return result
496498
}
499+
500+
func shouldAdopt(ms *v1alpha1.MachineSet) bool {
501+
return !util.HasOwner(ms.OwnerReferences, v1alpha1.SchemeGroupVersion.String(), []string{"MachineDeployment", "Cluster"})
502+
}

pkg/util/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ go_test(
2727
srcs = ["util_test.go"],
2828
embed = [":go_default_library"],
2929
deps = [
30+
"//pkg/apis/cluster/v1alpha1:go_default_library",
3031
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
3132
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
3233
],

pkg/util/util.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,19 @@ func PointsTo(refs []metav1.OwnerReference, target *metav1.ObjectMeta) bool {
326326

327327
return false
328328
}
329+
330+
// HasOwner checks if any of the references in the past list match the given apiVersion and one of the given kinds
331+
func HasOwner(refList []metav1.OwnerReference, apiVersion string, kinds []string) bool {
332+
kMap := make(map[string]bool)
333+
for _, kind := range kinds {
334+
kMap[kind] = true
335+
}
336+
337+
for _, mr := range refList {
338+
if mr.APIVersion == apiVersion && kMap[mr.Kind] {
339+
return true
340+
}
341+
}
342+
343+
return false
344+
}

pkg/util/util_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525
"k8s.io/apimachinery/pkg/types"
26+
"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
2627
)
2728

2829
const validCluster = `
@@ -390,3 +391,82 @@ func TestPointsTo(t *testing.T) {
390391
})
391392
}
392393
}
394+
395+
func TestHasOwner(t *testing.T) {
396+
397+
tests := []struct {
398+
name string
399+
refList []metav1.OwnerReference
400+
expected bool
401+
}{
402+
{
403+
name: "no ownership",
404+
},
405+
{
406+
name: "owned by cluster",
407+
refList: []metav1.OwnerReference{
408+
{
409+
Kind: "Cluster",
410+
APIVersion: v1alpha1.SchemeGroupVersion.String(),
411+
},
412+
},
413+
expected: true,
414+
},
415+
416+
{
417+
name: "owned by something else",
418+
refList: []metav1.OwnerReference{
419+
{
420+
Kind: "Pod",
421+
APIVersion: "v1",
422+
},
423+
{
424+
Kind: "Deployment",
425+
APIVersion: "apps/v1",
426+
},
427+
},
428+
},
429+
{
430+
name: "owner by a deployment",
431+
refList: []metav1.OwnerReference{
432+
{
433+
Kind: "MachineDeployment",
434+
APIVersion: v1alpha1.SchemeGroupVersion.String(),
435+
},
436+
},
437+
expected: true,
438+
},
439+
{
440+
name: "right kind, wrong apiversion",
441+
refList: []metav1.OwnerReference{
442+
{
443+
Kind: "MachineDeployment",
444+
APIVersion: "wrong/v2",
445+
},
446+
},
447+
},
448+
{
449+
450+
name: "right apiversion, wrong kind",
451+
refList: []metav1.OwnerReference{
452+
{
453+
Kind: "Machine",
454+
APIVersion: v1alpha1.SchemeGroupVersion.String(),
455+
},
456+
},
457+
},
458+
}
459+
460+
for _, test := range tests {
461+
t.Run(test.name, func(t *testing.T) {
462+
result := HasOwner(
463+
test.refList,
464+
v1alpha1.SchemeGroupVersion.String(),
465+
[]string{"MachineDeployment", "Cluster"},
466+
)
467+
if test.expected != result {
468+
t.Errorf("expected hasOwner to be %v, got %v", test.expected, result)
469+
}
470+
})
471+
}
472+
}

0 commit comments

Comments
 (0)