Skip to content

Commit 1d39b90

Browse files
authored
Merge pull request #6270 from ykakarap/topology-plan-contract-fix
🐛 fix the version assumption in topology plan command
2 parents 4afdea8 + 8509603 commit 1d39b90

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 [""].
@@ -501,6 +507,7 @@ func (t *topologyClient) generateCRDs(objs []*unstructured.Unstructured) []*apie
501507
crds := []*apiextensionsv1.CustomResourceDefinition{}
502508
crdMap := map[string]bool{}
503509
var gvk schema.GroupVersionKind
510+
504511
for _, obj := range objs {
505512
gvk = obj.GroupVersionKind()
506513
if strings.HasSuffix(gvk.Group, ".cluster.x-k8s.io") && !crdMap[gvk.String()] {
@@ -512,14 +519,16 @@ func (t *topologyClient) generateCRDs(objs []*unstructured.Unstructured) []*apie
512519
ObjectMeta: metav1.ObjectMeta{
513520
Name: fmt.Sprintf("%s.%s", flect.Pluralize(strings.ToLower(gvk.Kind)), gvk.Group),
514521
Labels: map[string]string{
515-
clusterv1.GroupVersion.String(): clusterv1.GroupVersion.Version,
522+
// Here we assume that all the versions are compatible with the Cluster API contract version.
523+
clusterv1.GroupVersion.String(): gvk.Version,
516524
},
517525
},
518526
}
519527
crds = append(crds, crd)
520528
crdMap[gvk.String()] = true
521529
}
522530
}
531+
523532
return crds
524533
}
525534

@@ -698,3 +707,16 @@ func uniqueNamespaces(objs []*unstructured.Unstructured) []string {
698707
}
699708
return ns.List()
700709
}
710+
711+
func hasUniqueVersionPerGroupKind(objs []*unstructured.Unstructured) bool {
712+
versionMap := map[string]string{}
713+
for _, obj := range objs {
714+
gvk := obj.GroupVersionKind()
715+
gk := gvk.GroupKind().String()
716+
if ver, ok := versionMap[gk]; ok && ver != gvk.Version {
717+
return false
718+
}
719+
versionMap[gk] = gvk.Version
720+
}
721+
return true
722+
}

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)