Skip to content

Commit d702cc4

Browse files
committed
Finish clusterctl impl
1 parent 9f27ce5 commit d702cc4

File tree

8 files changed

+101
-40
lines changed

8 files changed

+101
-40
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: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ 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"
@@ -111,7 +112,9 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
111112

112113
addAnnotation(controlPlane, ObjectContractAnnotation, "ControlPlane")
113114
addAnnotation(controlPlane, ObjectContractVersionAnnotation, contractVersion)
114-
addControlPlane(cluster, controlPlane, tree, options)
115+
if err := addControlPlane(ctx, c, cluster, controlPlane, tree, options); err != nil {
116+
return nil, err
117+
}
115118
}
116119

117120
// Adds control plane machines.
@@ -121,7 +124,7 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
121124
}
122125
machineMap := map[string]bool{}
123126
addMachineFunc := func(parent client.Object, m *clusterv1.Machine) {
124-
_, visible := tree.Add(parent, m)
127+
_, visible := tree.Add(parent, m, GroupVersionKind(clusterv1.GroupVersion.WithKind("Machine")))
125128
machineMap[m.Name] = true
126129

127130
if visible {
@@ -205,30 +208,51 @@ func addClusterResourceSetsToObjectTree(ctx context.Context, c client.Client, cl
205208
}
206209
}
207210

208-
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 {
209212
tree.Add(cluster, controlPlane, ObjectMetaName("ControlPlane"), GroupingObject(true))
210213

211214
if options.ShowTemplates {
212215
// Add control plane infrastructure ref using spec fields guaranteed in contract
213-
infrastructureRef, found, err := unstructured.NestedMap(controlPlane.UnstructuredContent(), "spec", "machineTemplate", "infrastructureRef")
214-
if err == nil && found {
215-
infrastructureObjectRef := &corev1.ObjectReference{
216-
Kind: infrastructureRef["kind"].(string),
217-
Namespace: infrastructureRef["namespace"].(string),
218-
Name: infrastructureRef["name"].(string),
219-
APIVersion: infrastructureRef["apiVersion"].(string),
220-
}
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+
}
221220

222-
machineTemplateRefObject := ObjectReferenceObject(infrastructureObjectRef)
223-
var templateParent client.Object
224-
if options.AddTemplateVirtualNode {
225-
templateParent = addTemplateVirtualNode(tree, controlPlane, cluster.Namespace)
226-
} else {
227-
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
228226
}
229-
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
230251
}
252+
tree.Add(templateParent, machineTemplateRefObject, ObjectMetaName("MachineInfrastructureTemplate"))
231253
}
254+
255+
return nil
232256
}
233257

234258
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 {
@@ -249,6 +273,7 @@ func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, clus
249273
if !options.ShowMachineSets {
250274
addOpts = append(addOpts, GroupingObject(true))
251275
}
276+
addOpts = append(addOpts, GroupVersionKind(clusterv1.GroupVersion.WithKind("MachineDeployment")))
252277
tree.Add(workers, md, addOpts...)
253278

254279
if options.ShowTemplates {
@@ -293,7 +318,7 @@ func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, clus
293318

294319
var parent client.Object = md
295320
if options.ShowMachineSets {
296-
tree.Add(md, ms, GroupingObject(true))
321+
tree.Add(md, ms, GroupingObject(true), GroupVersionKind(clusterv1.GroupVersion.WithKind("MachineSet")))
297322
parent = ms
298323
}
299324

@@ -310,7 +335,7 @@ func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, clus
310335
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)) {
311336
for i := range machinePoolList.Items {
312337
mp := &machinePoolList.Items[i]
313-
_, visible := tree.Add(workers, mp, GroupingObject(true))
338+
_, visible := tree.Add(workers, mp, GroupingObject(true), GroupVersionKind(clusterv1.GroupVersion.WithKind("MachinePool")))
314339

315340
if visible {
316341
if machinePoolBootstrap, err := external.GetObjectFromContractVersionedRef(ctx, c, mp.Spec.Template.Spec.Bootstrap.ConfigRef, mp.Namespace); err == nil {

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.

0 commit comments

Comments
 (0)