Skip to content

Commit 8509603

Browse files
author
Yuvaraj Kakaraparthi
committed
fix the version assumption in topology plan command
1 parent 0195739 commit 8509603

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

cmd/clusterctl/client/cluster/topology.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ func (t *topologyClient) Plan(in *TopologyPlanInput) (*TopologyPlanOutput, error
222222
// - no more than 1 cluster in the input.
223223
// - no more than 1 clusterclass in the input.
224224
func (t *topologyClient) validateInput(in *TopologyPlanInput) error {
225+
// Check if objects of the same Group.Kind are using the same version.
226+
// Note: This is because the dryrun client does not guarantee any coversions.
227+
if !hasUniqueVersionPerGroupKind(in.Objs) {
228+
return fmt.Errorf("objects of the same Group.Kind should use the same apiVersion")
229+
}
230+
225231
// Check all the objects in the input belong to the same namespace.
226232
// Note: It is okay if all the objects in the input do not have any namespace.
227233
// In such case, the list of unique namespaces will be [""].
@@ -499,6 +505,7 @@ func (t *topologyClient) generateCRDs(objs []*unstructured.Unstructured) []*apie
499505
crds := []*apiextensionsv1.CustomResourceDefinition{}
500506
crdMap := map[string]bool{}
501507
var gvk schema.GroupVersionKind
508+
502509
for _, obj := range objs {
503510
gvk = obj.GroupVersionKind()
504511
if strings.HasSuffix(gvk.Group, ".cluster.x-k8s.io") && !crdMap[gvk.String()] {
@@ -510,14 +517,16 @@ func (t *topologyClient) generateCRDs(objs []*unstructured.Unstructured) []*apie
510517
ObjectMeta: metav1.ObjectMeta{
511518
Name: fmt.Sprintf("%s.%s", flect.Pluralize(strings.ToLower(gvk.Kind)), gvk.Group),
512519
Labels: map[string]string{
513-
clusterv1.GroupVersion.String(): clusterv1.GroupVersion.Version,
520+
// Here we assume that all the versions are compatible with the Cluster API contract version.
521+
clusterv1.GroupVersion.String(): gvk.Version,
514522
},
515523
},
516524
}
517525
crds = append(crds, crd)
518526
crdMap[gvk.String()] = true
519527
}
520528
}
529+
521530
return crds
522531
}
523532

@@ -688,3 +697,16 @@ func uniqueNamespaces(objs []*unstructured.Unstructured) []string {
688697
}
689698
return ns.List()
690699
}
700+
701+
func hasUniqueVersionPerGroupKind(objs []*unstructured.Unstructured) bool {
702+
versionMap := map[string]string{}
703+
for _, obj := range objs {
704+
gvk := obj.GroupVersionKind()
705+
gk := gvk.GroupKind().String()
706+
if ver, ok := versionMap[gk]; ok && ver != gvk.Version {
707+
return false
708+
}
709+
versionMap[gk] = gvk.Version
710+
}
711+
return true
712+
}

docs/book/src/clusterctl/commands/alpha-topology-plan.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,17 @@ or validation on these objects. Defaulting and validation is only run on Cluster
424424

425425
</aside>
426426

427+
<aside class="note warning">
428+
429+
<h1>API Versions and Contract compatibility</h1>
430+
431+
All the objects in the input of the same `Group.Kind` should have the same `apiVersion`.
432+
Example: Two `InfraMachineTemplate`s with `apiVersion`s `infrastructure.cluster.x-k8s.io/v1beta1` and `infrastructure.cluster.x-k8s.io/v1alpha4` are not allowed.
433+
434+
The API version of resource in the input should be compatible with the current version of Cluster API contract.
435+
436+
</aside>
437+
427438
### `--output-directory`, `-o` (REQUIRED)
428439

429440
Information about the objects that are created and updated is written to this directory.

0 commit comments

Comments
 (0)