Skip to content

Commit f3f3556

Browse files
committed
Adjust to CR not setting TypeMeta anymore
1 parent 5ce07da commit f3f3556

File tree

21 files changed

+134
-116
lines changed

21 files changed

+134
-116
lines changed

cmd/clusterctl/client/cluster/upgrader_info_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,6 @@ func toSemanticVersions(versions []string) []version.Version {
491491

492492
func fakeProvider(name string, providerType clusterctlv1.ProviderType, version, targetNamespace string) clusterctlv1.Provider {
493493
return clusterctlv1.Provider{
494-
TypeMeta: metav1.TypeMeta{
495-
APIVersion: clusterctlv1.GroupVersion.String(),
496-
Kind: "Provider",
497-
},
498494
ObjectMeta: metav1.ObjectMeta{
499495
ResourceVersion: "999",
500496
Namespace: targetNamespace,

cmd/clusterctl/client/tree/discovery.go

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ package tree
1919
import (
2020
"context"
2121

22+
"github.com/pkg/errors"
2223
corev1 "k8s.io/api/core/v1"
2324
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26+
"k8s.io/apimachinery/pkg/runtime/schema"
2527
"sigs.k8s.io/controller-runtime/pkg/client"
2628

2729
addonsv1 "sigs.k8s.io/cluster-api/api/addons/v1beta2"
@@ -110,7 +112,9 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
110112

111113
addAnnotation(controlPlane, ObjectContractAnnotation, "ControlPlane")
112114
addAnnotation(controlPlane, ObjectContractVersionAnnotation, contractVersion)
113-
addControlPlane(cluster, controlPlane, tree, options)
115+
if err := addControlPlane(ctx, c, cluster, controlPlane, tree, options); err != nil {
116+
return nil, err
117+
}
114118
}
115119

116120
// Adds control plane machines.
@@ -120,7 +124,7 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
120124
}
121125
machineMap := map[string]bool{}
122126
addMachineFunc := func(parent client.Object, m *clusterv1.Machine) {
123-
_, visible := tree.Add(parent, m)
127+
_, visible := tree.Add(parent, m, GroupVersionKind(clusterv1.GroupVersion.WithKind("Machine")))
124128
machineMap[m.Name] = true
125129

126130
if visible {
@@ -204,30 +208,51 @@ func addClusterResourceSetsToObjectTree(ctx context.Context, c client.Client, cl
204208
}
205209
}
206210

207-
func addControlPlane(cluster *clusterv1.Cluster, controlPlane *unstructured.Unstructured, tree *ObjectTree, options DiscoverOptions) {
211+
func addControlPlane(ctx context.Context, c client.Client, cluster *clusterv1.Cluster, controlPlane *unstructured.Unstructured, tree *ObjectTree, options DiscoverOptions) error {
208212
tree.Add(cluster, controlPlane, ObjectMetaName("ControlPlane"), GroupingObject(true))
209213

210214
if options.ShowTemplates {
211215
// Add control plane infrastructure ref using spec fields guaranteed in contract
212-
infrastructureRef, found, err := unstructured.NestedMap(controlPlane.UnstructuredContent(), "spec", "machineTemplate", "infrastructureRef")
213-
if err == nil && found {
214-
infrastructureObjectRef := &corev1.ObjectReference{
215-
Kind: infrastructureRef["kind"].(string),
216-
Namespace: infrastructureRef["namespace"].(string),
217-
Name: infrastructureRef["name"].(string),
218-
APIVersion: infrastructureRef["apiVersion"].(string),
219-
}
216+
contractVersion, err := contract.GetContractVersionForVersion(ctx, c, controlPlane.GroupVersionKind().GroupKind(), controlPlane.GroupVersionKind().Version)
217+
if err != nil {
218+
return errors.Wrapf(err, "failed to get contract version for the ControlPlane object")
219+
}
220220

221-
machineTemplateRefObject := ObjectReferenceObject(infrastructureObjectRef)
222-
var templateParent client.Object
223-
if options.AddTemplateVirtualNode {
224-
templateParent = addTemplateVirtualNode(tree, controlPlane, cluster.Namespace)
225-
} else {
226-
templateParent = controlPlane
221+
var infrastructureObjectRef *corev1.ObjectReference
222+
if contractVersion == "v1beta1" {
223+
currentRef, err := contract.ControlPlane().MachineTemplate().InfrastructureV1Beta1Ref().Get(controlPlane)
224+
if err != nil {
225+
return nil //nolint:nilerr // intentionally ignoring the error here because infraRef in CP is optional
227226
}
228-
tree.Add(templateParent, machineTemplateRefObject, ObjectMetaName("MachineInfrastructureTemplate"))
227+
infrastructureObjectRef = currentRef
228+
} else {
229+
currentRef, err := contract.ControlPlane().MachineTemplate().InfrastructureRef().Get(controlPlane)
230+
if err != nil {
231+
return nil //nolint:nilerr // intentionally ignoring the error here because infraRef in CP is optional
232+
}
233+
apiVersion, err := contract.GetAPIVersion(ctx, c, currentRef.GroupKind())
234+
if err != nil {
235+
return err
236+
}
237+
infrastructureObjectRef = &corev1.ObjectReference{
238+
APIVersion: apiVersion,
239+
Kind: currentRef.Kind,
240+
Namespace: controlPlane.GetNamespace(),
241+
Name: currentRef.Name,
242+
}
243+
}
244+
245+
machineTemplateRefObject := ObjectReferenceObject(infrastructureObjectRef)
246+
var templateParent client.Object
247+
if options.AddTemplateVirtualNode {
248+
templateParent = addTemplateVirtualNode(tree, controlPlane, cluster.Namespace)
249+
} else {
250+
templateParent = controlPlane
229251
}
252+
tree.Add(templateParent, machineTemplateRefObject, ObjectMetaName("MachineInfrastructureTemplate"))
230253
}
254+
255+
return nil
231256
}
232257

233258
func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, cluster *clusterv1.Cluster, workers *NodeObject, machinesList *clusterv1.MachineList, tree *ObjectTree, options DiscoverOptions, addMachineFunc func(parent client.Object, m *clusterv1.Machine)) error {
@@ -248,6 +273,7 @@ func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, clus
248273
if !options.ShowMachineSets {
249274
addOpts = append(addOpts, GroupingObject(true))
250275
}
276+
addOpts = append(addOpts, GroupVersionKind(clusterv1.GroupVersion.WithKind("MachineDeployment")))
251277
tree.Add(workers, md, addOpts...)
252278

253279
if options.ShowTemplates {
@@ -286,17 +312,17 @@ func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, clus
286312
tree.Add(templateParent, machineTemplateRefObject, ObjectMetaName("MachineInfrastructureTemplate"))
287313
}
288314

289-
machineSets := selectMachinesSetsControlledBy(machineSetList, md)
315+
machineSets := selectMachinesSetsControlledBy(machineSetList, md, clusterv1.GroupVersion.WithKind("MachineDeployment").GroupKind())
290316
for i := range machineSets {
291317
ms := machineSets[i]
292318

293319
var parent client.Object = md
294320
if options.ShowMachineSets {
295-
tree.Add(md, ms, GroupingObject(true))
321+
tree.Add(md, ms, GroupingObject(true), GroupVersionKind(clusterv1.GroupVersion.WithKind("MachineSet")))
296322
parent = ms
297323
}
298324

299-
machines := selectMachinesControlledBy(machinesList, ms)
325+
machines := selectMachinesControlledBy(machinesList, ms, clusterv1.GroupVersion.WithKind("MachineSet").GroupKind())
300326
for _, w := range machines {
301327
addMachineFunc(parent, w)
302328
}
@@ -309,7 +335,7 @@ func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, clus
309335
func addMachinePoolsToObjectTree(ctx context.Context, c client.Client, workers *NodeObject, machinePoolList *clusterv1.MachinePoolList, machinesList *clusterv1.MachineList, tree *ObjectTree, addMachineFunc func(parent client.Object, m *clusterv1.Machine)) {
310336
for i := range machinePoolList.Items {
311337
mp := &machinePoolList.Items[i]
312-
_, visible := tree.Add(workers, mp, GroupingObject(true))
338+
_, visible := tree.Add(workers, mp, GroupingObject(true), GroupVersionKind(clusterv1.GroupVersion.WithKind("MachinePool")))
313339

314340
if visible {
315341
if machinePoolBootstrap, err := external.GetObjectFromContractVersionedRef(ctx, c, mp.Spec.Template.Spec.Bootstrap.ConfigRef, mp.Namespace); err == nil {
@@ -321,7 +347,7 @@ func addMachinePoolsToObjectTree(ctx context.Context, c client.Client, workers *
321347
}
322348
}
323349

324-
machines := selectMachinesControlledBy(machinesList, mp)
350+
machines := selectMachinesControlledBy(machinesList, mp, clusterv1.GroupVersion.WithKind("MachinePool").GroupKind())
325351
for _, m := range machines {
326352
addMachineFunc(mp, m)
327353
}
@@ -417,22 +443,22 @@ func selectControlPlaneMachines(machineList *clusterv1.MachineList) []*clusterv1
417443
return machines
418444
}
419445

420-
func selectMachinesSetsControlledBy(machineSetList *clusterv1.MachineSetList, controller client.Object) []*clusterv1.MachineSet {
446+
func selectMachinesSetsControlledBy(machineSetList *clusterv1.MachineSetList, controller client.Object, controllerGK schema.GroupKind) []*clusterv1.MachineSet {
421447
machineSets := []*clusterv1.MachineSet{}
422448
for i := range machineSetList.Items {
423449
m := &machineSetList.Items[i]
424-
if util.IsControlledBy(m, controller) {
450+
if util.IsControlledBy(m, controller, controllerGK) {
425451
machineSets = append(machineSets, m)
426452
}
427453
}
428454
return machineSets
429455
}
430456

431-
func selectMachinesControlledBy(machineList *clusterv1.MachineList, controller client.Object) []*clusterv1.Machine {
457+
func selectMachinesControlledBy(machineList *clusterv1.MachineList, controller client.Object, controllerGK schema.GroupKind) []*clusterv1.Machine {
432458
machines := []*clusterv1.Machine{}
433459
for i := range machineList.Items {
434460
m := &machineList.Items[i]
435-
if util.IsControlledBy(m, controller) {
461+
if util.IsControlledBy(m, controller, controllerGK) {
436462
machines = append(machines, m)
437463
}
438464
}

cmd/clusterctl/client/tree/options.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@ limitations under the License.
1616

1717
package tree
1818

19+
import (
20+
"k8s.io/apimachinery/pkg/runtime/schema"
21+
"k8s.io/utils/ptr"
22+
)
23+
1924
// AddObjectOption define an option for the ObjectTree Add operation.
2025
type AddObjectOption interface {
2126
ApplyToAdd(*addObjectOptions)
2227
}
2328

2429
type addObjectOptions struct {
25-
MetaName string
26-
GroupingObject bool
27-
NoEcho bool
28-
ZOrder int
30+
GroupVersionKind *schema.GroupVersionKind
31+
MetaName string
32+
GroupingObject bool
33+
NoEcho bool
34+
ZOrder int
2935
}
3036

3137
func (o *addObjectOptions) ApplyOptions(opts []AddObjectOption) *addObjectOptions {
@@ -35,6 +41,16 @@ func (o *addObjectOptions) ApplyOptions(opts []AddObjectOption) *addObjectOption
3541
return o
3642
}
3743

44+
// GroupVersionKind is the gvk to set on the passed in obj.
45+
// This option has to be used if obj is a typed object and
46+
// it cannot be guaranteed that gvk is set.
47+
type GroupVersionKind schema.GroupVersionKind
48+
49+
// ApplyToAdd applies the given options.
50+
func (n GroupVersionKind) ApplyToAdd(options *addObjectOptions) {
51+
options.GroupVersionKind = ptr.To(schema.GroupVersionKind(n))
52+
}
53+
3854
// The ObjectMetaName option defines the meta name that should be used for the object in the presentation layer,
3955
// e.g. control plane for KCP.
4056
type ObjectMetaName string

cmd/clusterctl/client/tree/tree.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ func (od ObjectTree) Add(parent, obj client.Object, opts ...AddObjectOption) (ad
9797
addOpts := &addObjectOptions{}
9898
addOpts.ApplyOptions(opts)
9999

100+
if addOpts.GroupVersionKind != nil {
101+
obj.GetObjectKind().SetGroupVersionKind(*addOpts.GroupVersionKind)
102+
}
103+
100104
// Get a small set of conditions that will be used to determine e.g. when grouping or when an object is just an echo of
101105
// its parent.
102106
var objReadyV1Beta1, parentReadyV1Beta1 *clusterv1.Condition

cmd/clusterctl/client/upgrade_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,6 @@ func fakeClientForUpgrade() *fakeClient {
365365

366366
func fakeProvider(name string, providerType clusterctlv1.ProviderType, version, targetNamespace string) clusterctlv1.Provider {
367367
return clusterctlv1.Provider{
368-
TypeMeta: metav1.TypeMeta{
369-
APIVersion: clusterctlv1.GroupVersion.String(),
370-
Kind: "Provider",
371-
},
372368
ObjectMeta: metav1.ObjectMeta{
373369
Namespace: targetNamespace,
374370
Name: clusterctlv1.ManifestLabel(name, providerType),

cmd/clusterctl/internal/test/fake_objects.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,12 @@ func (f *FakeControlPlane) Objs(cluster *clusterv1.Cluster) []client.Object {
358358
},
359359
Spec: fakecontrolplane.GenericControlPlaneSpec{
360360
MachineTemplate: fakecontrolplane.GenericMachineTemplate{
361-
InfrastructureRef: corev1.ObjectReference{
362-
APIVersion: controlPlaneInfrastructure.APIVersion,
363-
Kind: controlPlaneInfrastructure.Kind,
364-
Namespace: controlPlaneInfrastructure.Namespace,
365-
Name: controlPlaneInfrastructure.Name,
361+
Spec: fakecontrolplane.GenericMachineTemplateSpec{
362+
InfrastructureRef: clusterv1.ContractVersionedObjectReference{
363+
APIGroup: fakeinfrastructure.GroupVersion.Group,
364+
Kind: controlPlaneInfrastructure.Kind,
365+
Name: controlPlaneInfrastructure.Name,
366+
},
366367
},
367368
},
368369
},
@@ -1555,6 +1556,7 @@ func (f *FakeClusterClass) Objs() []client.Object {
15551556
}
15561557

15571558
clusterClass := clusterClassBuilder.Build()
1559+
clusterClass.SetGroupVersionKind(clusterv1.GroupVersion.WithKind("ClusterClass"))
15581560
objMap[clusterClass] = false
15591561

15601562
for o := range objMap {

cmd/clusterctl/internal/test/providers/controlplane/generic_types.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@ limitations under the License.
1717
package controlplane
1818

1919
import (
20-
corev1 "k8s.io/api/core/v1"
2120
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
22+
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
2223
)
2324

2425
// GenericMachineTemplate contains a generic control plane spec.
2526
type GenericMachineTemplate struct {
26-
InfrastructureRef corev1.ObjectReference `json:"infrastructureRef"`
27+
Spec GenericMachineTemplateSpec `json:"spec"`
28+
}
29+
30+
// GenericMachineTemplateSpec contains a generic control plane spec.
31+
type GenericMachineTemplateSpec struct {
32+
InfrastructureRef clusterv1.ContractVersionedObjectReference `json:"infrastructureRef"`
2733
}
2834

2935
// GenericControlPlaneSpec contains a generic control plane spec.

cmd/clusterctl/internal/test/providers/controlplane/zz_generated.deepcopy.go

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

0 commit comments

Comments
 (0)