Skip to content

Commit 628f443

Browse files
committed
Adjust to CR not setting TypeMeta anymore
1 parent ee9d41a commit 628f443

File tree

9 files changed

+25
-34
lines changed

9 files changed

+25
-34
lines changed

cmd/clusterctl/client/tree/discovery.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
corev1 "k8s.io/api/core/v1"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25+
"k8s.io/apimachinery/pkg/runtime/schema"
2526
"sigs.k8s.io/controller-runtime/pkg/client"
2627

2728
addonsv1 "sigs.k8s.io/cluster-api/api/addons/v1beta2"
@@ -286,7 +287,7 @@ func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, clus
286287
tree.Add(templateParent, machineTemplateRefObject, ObjectMetaName("MachineInfrastructureTemplate"))
287288
}
288289

289-
machineSets := selectMachinesSetsControlledBy(machineSetList, md)
290+
machineSets := selectMachinesSetsControlledBy(machineSetList, md, clusterv1.GroupVersion.WithKind("MachineDeployment").GroupKind())
290291
for i := range machineSets {
291292
ms := machineSets[i]
292293

@@ -296,7 +297,7 @@ func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, clus
296297
parent = ms
297298
}
298299

299-
machines := selectMachinesControlledBy(machinesList, ms)
300+
machines := selectMachinesControlledBy(machinesList, ms, clusterv1.GroupVersion.WithKind("MachineSet").GroupKind())
300301
for _, w := range machines {
301302
addMachineFunc(parent, w)
302303
}
@@ -321,7 +322,7 @@ func addMachinePoolsToObjectTree(ctx context.Context, c client.Client, workers *
321322
}
322323
}
323324

324-
machines := selectMachinesControlledBy(machinesList, mp)
325+
machines := selectMachinesControlledBy(machinesList, mp, clusterv1.GroupVersion.WithKind("MachinePool").GroupKind())
325326
for _, m := range machines {
326327
addMachineFunc(mp, m)
327328
}
@@ -417,22 +418,22 @@ func selectControlPlaneMachines(machineList *clusterv1.MachineList) []*clusterv1
417418
return machines
418419
}
419420

420-
func selectMachinesSetsControlledBy(machineSetList *clusterv1.MachineSetList, controller client.Object) []*clusterv1.MachineSet {
421+
func selectMachinesSetsControlledBy(machineSetList *clusterv1.MachineSetList, controller client.Object, controllerGK schema.GroupKind) []*clusterv1.MachineSet {
421422
machineSets := []*clusterv1.MachineSet{}
422423
for i := range machineSetList.Items {
423424
m := &machineSetList.Items[i]
424-
if util.IsControlledBy(m, controller) {
425+
if util.IsControlledBy(m, controller, controllerGK) {
425426
machineSets = append(machineSets, m)
426427
}
427428
}
428429
return machineSets
429430
}
430431

431-
func selectMachinesControlledBy(machineList *clusterv1.MachineList, controller client.Object) []*clusterv1.Machine {
432+
func selectMachinesControlledBy(machineList *clusterv1.MachineList, controller client.Object, controllerGK schema.GroupKind) []*clusterv1.Machine {
432433
machines := []*clusterv1.Machine{}
433434
for i := range machineList.Items {
434435
m := &machineList.Items[i]
435-
if util.IsControlledBy(m, controller) {
436+
if util.IsControlledBy(m, controller, controllerGK) {
436437
machines = append(machines, m)
437438
}
438439
}

controlplane/kubeadm/internal/controllers/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ func (r *KubeadmControlPlaneReconciler) adoptOwnedSecrets(ctx context.Context, k
13871387

13881388
for i := range secrets.Items {
13891389
s := secrets.Items[i]
1390-
if !util.IsOwnedByObject(&s, currentOwner) {
1390+
if !util.IsOwnedByObject(&s, currentOwner, bootstrapv1.GroupVersion.WithKind("KubeadmConfig").GroupKind()) {
13911391
continue
13921392
}
13931393
// avoid taking ownership of the bootstrap data secret

controlplane/kubeadm/internal/controllers/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (r *KubeadmControlPlaneReconciler) reconcileKubeconfig(ctx context.Context,
9494
}
9595

9696
// only do rotation on owned secrets
97-
if !util.IsControlledBy(configSecret, controlPlane.KCP) {
97+
if !util.IsControlledBy(configSecret, controlPlane.KCP, controlplanev1.GroupVersion.WithKind("KubeadmControlPlane").GroupKind()) {
9898
return ctrl.Result{}, nil
9999
}
100100

controlplane/kubeadm/internal/workload_cluster_coredns_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,10 +1139,6 @@ func (m *fakeMigrator) Migrate(_, _, _ string, _ bool) (string, error) {
11391139

11401140
func newCoreDNSInfoDeploymentWithimage(image string) *appsv1.Deployment {
11411141
return &appsv1.Deployment{
1142-
TypeMeta: metav1.TypeMeta{
1143-
Kind: "Deployment",
1144-
APIVersion: "apps/v1",
1145-
},
11461142
ObjectMeta: metav1.ObjectMeta{
11471143
Name: coreDNSKey,
11481144
Namespace: metav1.NamespaceSystem,

internal/controllers/cluster/cluster_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ func (c *clusterDescendants) filterOwnedDescendants(cluster *clusterv1.Cluster)
631631
return nil //nolint:nilerr // We don't want to exit the EachListItem loop, just continue
632632
}
633633

634-
if util.IsOwnedByObject(acc, cluster) {
634+
if util.IsOwnedByObject(acc, cluster, clusterv1.GroupVersion.WithKind("Cluster").GroupKind()) {
635635
ownedDescendants = append(ownedDescendants, obj)
636636
}
637637

internal/controllers/cluster/cluster_controller_status.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func setWorkersReplicas(_ context.Context, cluster *clusterv1.Cluster, machinePo
246246
}
247247

248248
for _, ms := range machineSets.Items {
249-
if !util.IsOwnedByObject(&ms, cluster) {
249+
if !util.IsOwnedByObject(&ms, cluster, clusterv1.GroupVersion.WithKind("Cluster").GroupKind()) {
250250
continue
251251
}
252252
if ms.Spec.Replicas != nil {
@@ -267,7 +267,7 @@ func setWorkersReplicas(_ context.Context, cluster *clusterv1.Cluster, machinePo
267267
}
268268

269269
for _, m := range workerMachines.UnsortedList() {
270-
if !util.IsOwnedByObject(m, cluster) {
270+
if !util.IsOwnedByObject(m, cluster, clusterv1.GroupVersion.WithKind("Cluster").GroupKind()) {
271271
continue
272272
}
273273
currentReplicas = ptr.To(ptr.Deref(currentReplicas, 0) + 1)
@@ -973,7 +973,7 @@ func setScalingUpCondition(ctx context.Context, cluster *clusterv1.Cluster, cont
973973
ws = append(ws, aggregationWrapper{md: &md})
974974
}
975975
for _, ms := range machineSets.Items {
976-
if !util.IsOwnedByObject(&ms, cluster) {
976+
if !util.IsOwnedByObject(&ms, cluster, clusterv1.GroupVersion.WithKind("Cluster").GroupKind()) {
977977
continue
978978
}
979979
ws = append(ws, aggregationWrapper{ms: &ms})
@@ -1050,7 +1050,7 @@ func setScalingDownCondition(ctx context.Context, cluster *clusterv1.Cluster, co
10501050
ws = append(ws, aggregationWrapper{md: &md})
10511051
}
10521052
for _, ms := range machineSets.Items {
1053-
if !util.IsOwnedByObject(&ms, cluster) {
1053+
if !util.IsOwnedByObject(&ms, cluster, clusterv1.GroupVersion.WithKind("Cluster").GroupKind()) {
10541054
continue
10551055
}
10561056
ws = append(ws, aggregationWrapper{ms: &ms})

util/collections/machine_filters.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424
"k8s.io/apimachinery/pkg/labels"
2525
"k8s.io/apimachinery/pkg/selection"
26-
"sigs.k8s.io/controller-runtime/pkg/client"
2726

2827
controlplanev1 "sigs.k8s.io/cluster-api/api/controlplane/kubeadm/v1beta2"
2928
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
@@ -101,12 +100,12 @@ func InFailureDomains(failureDomains ...string) Func {
101100

102101
// OwnedMachines returns a filter to find all machines owned by specified owner.
103102
// Usage: GetFilteredMachinesForCluster(ctx, client, cluster, OwnedMachines(controlPlane)).
104-
func OwnedMachines(owner client.Object) func(machine *clusterv1.Machine) bool {
103+
func OwnedMachines(owner *controlplanev1.KubeadmControlPlane) func(machine *clusterv1.Machine) bool {
105104
return func(machine *clusterv1.Machine) bool {
106105
if machine == nil {
107106
return false
108107
}
109-
return util.IsOwnedByObject(machine, owner)
108+
return util.IsOwnedByObject(machine, owner, controlplanev1.GroupVersion.WithKind("KubeadmControlPlane").GroupKind())
110109
}
111110
}
112111

util/util.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,22 +359,22 @@ func indexOwnerRef(ownerReferences []metav1.OwnerReference, ref metav1.OwnerRefe
359359

360360
// IsOwnedByObject returns true if any of the owner references point to the given target.
361361
// It matches the object based on the Group, Kind and Name.
362-
func IsOwnedByObject(obj metav1.Object, target client.Object) bool {
362+
func IsOwnedByObject(obj metav1.Object, target client.Object, targetGK schema.GroupKind) bool {
363363
for _, ref := range obj.GetOwnerReferences() {
364-
if refersTo(&ref, target) {
364+
if refersTo(&ref, target, targetGK) {
365365
return true
366366
}
367367
}
368368
return false
369369
}
370370

371371
// IsControlledBy differs from metav1.IsControlledBy. This function matches on Group, Kind and Name. The metav1.IsControlledBy function matches on UID only.
372-
func IsControlledBy(obj metav1.Object, owner client.Object) bool {
372+
func IsControlledBy(obj metav1.Object, owner client.Object, ownerGK schema.GroupKind) bool {
373373
controllerRef := metav1.GetControllerOfNoCopy(obj)
374374
if controllerRef == nil {
375375
return false
376376
}
377-
return refersTo(controllerRef, owner)
377+
return refersTo(controllerRef, owner, ownerGK)
378378
}
379379

380380
// Returns true if a and b point to the same object based on Group, Kind and Name.
@@ -393,14 +393,13 @@ func referSameObject(a, b metav1.OwnerReference) bool {
393393
}
394394

395395
// Returns true if ref refers to obj based on Group, Kind and Name.
396-
func refersTo(ref *metav1.OwnerReference, obj client.Object) bool {
396+
func refersTo(ref *metav1.OwnerReference, obj client.Object, objGK schema.GroupKind) bool {
397397
refGv, err := schema.ParseGroupVersion(ref.APIVersion)
398398
if err != nil {
399399
return false
400400
}
401401

402-
gvk := obj.GetObjectKind().GroupVersionKind()
403-
return refGv.Group == gvk.Group && ref.Kind == gvk.Kind && ref.Name == obj.GetName()
402+
return refGv.Group == objGK.Group && ref.Kind == objGK.Kind && ref.Name == obj.GetName()
404403
}
405404

406405
// UnstructuredUnmarshalField is a wrapper around json and unstructured objects to decode and copy a specific field

util/util_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,9 @@ func TestIsOwnedByObject(t *testing.T) {
345345
targetName := "fri3ndsh1p"
346346

347347
meta := fakeMeta{
348-
metav1.ObjectMeta{
348+
ObjectMeta: metav1.ObjectMeta{
349349
Name: targetName,
350350
},
351-
metav1.TypeMeta{
352-
APIVersion: "ponies.info/v1",
353-
Kind: targetKind,
354-
},
355351
}
356352

357353
tests := []struct {
@@ -437,7 +433,7 @@ func TestIsOwnedByObject(t *testing.T) {
437433
OwnerReferences: test.refs,
438434
}
439435

440-
g.Expect(IsOwnedByObject(pointer, &meta)).To(Equal(test.expected), "Could not find a ref to %+v in %+v", meta, test.refs)
436+
g.Expect(IsOwnedByObject(pointer, &meta, schema.GroupKind{Group: "ponies.info", Kind: targetKind})).To(Equal(test.expected), "Could not find a ref to %+v in %+v", meta, test.refs)
441437
})
442438
}
443439
}

0 commit comments

Comments
 (0)