Skip to content

Commit 198b08e

Browse files
authoredJan 29, 2025
Extend providers interface to support multiple cluster GVKs (#971)
* Extend providers interface to support multiple cluster GVKs * Make child cluster name configurable from environ * Find first cluster in `getCluster` and return
1 parent 063e339 commit 198b08e

15 files changed

+70
-64
lines changed
 

‎Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
NAMESPACE ?= kcm-system
2+
CLUSTER_NAME_SUFFIX ?= dev
23
VERSION ?= $(shell git describe --tags --always)
34
VERSION := $(patsubst v%,%,$(VERSION))
45
FQDN_VERSION = $(subst .,-,$(VERSION))
@@ -370,11 +371,11 @@ dev-destroy: kind-undeploy registry-undeploy ## Destroy the development environm
370371

371372
.PHONY: dev-mcluster-apply
372373
dev-mcluster-apply: envsubst ## Create dev managed cluster using 'config/dev/$(DEV_PROVIDER)-clusterdeployment.yaml'
373-
@NAMESPACE=$(NAMESPACE) $(ENVSUBST) -no-unset -i config/dev/$(DEV_PROVIDER)-clusterdeployment.yaml | $(KUBECTL) apply -f -
374+
@NAMESPACE=$(NAMESPACE) CLUSTER_NAME_SUFFIX=$(CLUSTER_NAME_SUFFIX) $(ENVSUBST) -no-unset -i config/dev/$(DEV_PROVIDER)-clusterdeployment.yaml | $(KUBECTL) apply -f -
374375

375376
.PHONY: dev-mcluster-delete
376377
dev-mcluster-delete: envsubst ## Delete dev managed cluster using 'config/dev/$(DEV_PROVIDER)-clusterdeployment.yaml'
377-
@NAMESPACE=$(NAMESPACE) $(ENVSUBST) -no-unset -i config/dev/$(DEV_PROVIDER)-clusterdeployment.yaml | $(KUBECTL) delete -f -
378+
@NAMESPACE=$(NAMESPACE) CLUSTER_NAME_SUFFIX=$(CLUSTER_NAME_SUFFIX) $(ENVSUBST) -no-unset -i config/dev/$(DEV_PROVIDER)-clusterdeployment.yaml | $(KUBECTL) delete -f -
378379

379380
.PHONY: dev-creds-apply
380381
dev-creds-apply: dev-$(DEV_PROVIDER)-creds ## Create credentials resources for $DEV_PROVIDER

‎config/dev/adopted-clusterdeployment.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: k0rdent.mirantis.com/v1alpha1
22
kind: ClusterDeployment
33
metadata:
4-
name: adopted-dev
4+
name: adopted-${CLUSTER_NAME_SUFFIX}
55
namespace: ${NAMESPACE}
66
spec:
77
template: adopted-cluster-0-0-2

‎config/dev/aks-clusterdeployment.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: k0rdent.mirantis.com/v1alpha1
22
kind: ClusterDeployment
33
metadata:
4-
name: azure-aks-dev
4+
name: azure-aks-${CLUSTER_NAME_SUFFIX}
55
namespace: ${NAMESPACE}
66
spec:
77
template: azure-aks-0-0-2

‎config/dev/aws-clusterdeployment.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: k0rdent.mirantis.com/v1alpha1
22
kind: ClusterDeployment
33
metadata:
4-
name: aws-dev
4+
name: aws-${CLUSTER_NAME_SUFFIX}
55
namespace: ${NAMESPACE}
66
spec:
77
template: aws-standalone-cp-0-0-6

‎config/dev/azure-clusterdeployment.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: k0rdent.mirantis.com/v1alpha1
22
kind: ClusterDeployment
33
metadata:
4-
name: azure-dev
4+
name: azure-${CLUSTER_NAME_SUFFIX}
55
namespace: ${NAMESPACE}
66
spec:
77
template: azure-standalone-cp-0-0-6

‎config/dev/eks-clusterdeployment.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: k0rdent.mirantis.com/v1alpha1
22
kind: ClusterDeployment
33
metadata:
4-
name: eks-dev
4+
name: eks-${CLUSTER_NAME_SUFFIX}
55
namespace: ${NAMESPACE}
66
spec:
77
template: aws-eks-0-0-4

‎config/dev/openstack-clusterdeployment.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: k0rdent.mirantis.com/v1alpha1
22
kind: ClusterDeployment
33
metadata:
4-
name: openstack-dev
4+
name: openstack-${CLUSTER_NAME_SUFFIX}
55
namespace: ${NAMESPACE}
66
spec:
77
template: openstack-standalone-cp-0-0-5

‎config/dev/vsphere-clusterdeployment.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: k0rdent.mirantis.com/v1alpha1
22
kind: ClusterDeployment
33
metadata:
4-
name: vsphere-dev
4+
name: vsphere-${CLUSTER_NAME_SUFFIX}
55
namespace: ${NAMESPACE}
66
spec:
77
template: vsphere-standalone-cp-0-0-6

‎internal/controller/clusterdeployment_controller.go

+21-17
Original file line numberDiff line numberDiff line change
@@ -649,12 +649,12 @@ func (r *ClusterDeploymentReconciler) releaseCluster(ctx context.Context, namesp
649649

650650
// Associate the provider with it's GVK
651651
for _, provider := range providers {
652-
gvk := providersloader.GetClusterGVK(provider)
653-
if !gvk.Empty() {
652+
gvks := providersloader.GetClusterGVKs(provider)
653+
if len(gvks) == 0 {
654654
continue
655655
}
656656

657-
cluster, err := r.getCluster(ctx, namespace, name, gvk)
657+
cluster, err := r.getCluster(ctx, namespace, name, gvks...)
658658
if err != nil {
659659
if provider == "aws" && apierrors.IsNotFound(err) {
660660
return nil
@@ -665,7 +665,7 @@ func (r *ClusterDeploymentReconciler) releaseCluster(ctx context.Context, namesp
665665

666666
found, err := r.objectsAvailable(ctx, namespace, cluster.Name, gvkMachine)
667667
if err != nil {
668-
return err
668+
continue
669669
}
670670

671671
if !found {
@@ -697,21 +697,25 @@ func (r *ClusterDeploymentReconciler) getInfraProvidersNames(ctx context.Context
697697
return ips[:len(ips):len(ips)], nil
698698
}
699699

700-
func (r *ClusterDeploymentReconciler) getCluster(ctx context.Context, namespace, name string, gvk schema.GroupVersionKind) (*metav1.PartialObjectMetadata, error) {
701-
opts := &client.ListOptions{
702-
LabelSelector: labels.SelectorFromSet(map[string]string{kcm.FluxHelmChartNameKey: name}),
703-
Namespace: namespace,
704-
}
705-
itemsList := &metav1.PartialObjectMetadataList{}
706-
itemsList.SetGroupVersionKind(gvk)
707-
if err := r.Client.List(ctx, itemsList, opts); err != nil {
708-
return nil, err
709-
}
710-
if len(itemsList.Items) == 0 {
711-
return nil, fmt.Errorf("%s with name %s was not found", gvk.Kind, name)
700+
func (r *ClusterDeploymentReconciler) getCluster(ctx context.Context, namespace, name string, gvks ...schema.GroupVersionKind) (*metav1.PartialObjectMetadata, error) {
701+
for _, gvk := range gvks {
702+
opts := &client.ListOptions{
703+
LabelSelector: labels.SelectorFromSet(map[string]string{kcm.FluxHelmChartNameKey: name}),
704+
Namespace: namespace,
705+
}
706+
itemsList := &metav1.PartialObjectMetadataList{}
707+
itemsList.SetGroupVersionKind(gvk)
708+
709+
if err := r.Client.List(ctx, itemsList, opts); err != nil {
710+
return nil, fmt.Errorf("failed to list %s in namespace %s: %w", gvk.Kind, namespace, err)
711+
}
712+
713+
if len(itemsList.Items) > 0 {
714+
return &itemsList.Items[0], nil
715+
}
712716
}
713717

714-
return &itemsList.Items[0], nil
718+
return nil, fmt.Errorf("no cluster found with name %s in namespace %s for any of the provided GroupVersionKinds", name, namespace)
715719
}
716720

717721
func (r *ClusterDeploymentReconciler) removeClusterFinalizer(ctx context.Context, cluster *metav1.PartialObjectMetadata) error {

‎internal/providers/providers.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ var (
5050
type ProviderModule interface {
5151
// GetName returns the short name of the provider
5252
GetName() string
53-
// GetClusterGVK returns the GroupVersionKind for the provider's cluster resource
54-
GetClusterGVK() schema.GroupVersionKind
53+
// GetClusterGVKs returns the GroupVersionKind for the provider's cluster resource
54+
GetClusterGVKs() []schema.GroupVersionKind
5555
// GetClusterIdentityKinds returns a list of supported cluster identity kinds
5656
GetClusterIdentityKinds() []string
5757
}
@@ -85,17 +85,17 @@ func List() []kcm.Provider {
8585
return slices.Clone(providers)
8686
}
8787

88-
// GetClusterGVK returns the GroupVersionKind for a provider's cluster resource
89-
func GetClusterGVK(shortName string) schema.GroupVersionKind {
88+
// GetClusterGVKs returns the GroupVersionKind for a provider's cluster resource
89+
func GetClusterGVKs(shortName string) []schema.GroupVersionKind {
9090
mu.RLock()
9191
defer mu.RUnlock()
9292

9393
module, ok := registry[shortName]
9494
if !ok {
95-
return schema.GroupVersionKind{}
95+
return nil
9696
}
9797

98-
return module.GetClusterGVK()
98+
return module.GetClusterGVKs()
9999
}
100100

101101
// GetClusterIdentityKinds returns the supported identity kinds for a given infrastructure provider

‎internal/providers/yaml.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,11 @@ import (
2424
"k8s.io/apimachinery/pkg/runtime/schema"
2525
)
2626

27-
// GVK represents the GroupVersionKind structure in YAML.
28-
type GVK struct {
29-
Group string `yaml:"group"`
30-
Version string `yaml:"version"`
31-
Kind string `yaml:"kind"`
32-
}
33-
3427
// YAMLProviderDefinition represents a YAML-based provider configuration.
3528
type YAMLProviderDefinition struct {
36-
Name string `yaml:"name"`
37-
ClusterGVK GVK `yaml:"clusterGVK"`
38-
ClusterIdentityKinds []string `yaml:"clusterIdentityKinds"`
29+
Name string `yaml:"name"`
30+
ClusterGVKs []schema.GroupVersionKind `yaml:"clusterGVKs"`
31+
ClusterIdentityKinds []string `yaml:"clusterIdentityKinds"`
3932
}
4033

4134
var _ ProviderModule = (*YAMLProviderDefinition)(nil)
@@ -44,12 +37,20 @@ func (p *YAMLProviderDefinition) GetName() string {
4437
return p.Name
4538
}
4639

47-
func (p *YAMLProviderDefinition) GetClusterGVK() schema.GroupVersionKind {
48-
return schema.GroupVersionKind{
49-
Group: p.ClusterGVK.Group,
50-
Version: p.ClusterGVK.Version,
51-
Kind: p.ClusterGVK.Kind,
40+
func (p *YAMLProviderDefinition) GetClusterGVKs() []schema.GroupVersionKind {
41+
if len(p.ClusterGVKs) == 0 {
42+
return nil
5243
}
44+
45+
result := make([]schema.GroupVersionKind, 0, len(p.ClusterGVKs))
46+
47+
for _, gvk := range p.ClusterGVKs {
48+
if !gvk.Empty() {
49+
result = append(result, gvk)
50+
}
51+
}
52+
53+
return result
5354
}
5455

5556
func (p *YAMLProviderDefinition) GetClusterIdentityKinds() []string {

‎providers/aws.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
# limitations under the License.
1414

1515
name: aws
16-
clusterGVK:
17-
group: infrastructure.cluster.x-k8s.io
18-
version: v1beta2
19-
kind: AWSCluster
16+
clusterGVKs:
17+
- group: infrastructure.cluster.x-k8s.io
18+
version: v1beta2
19+
kind: AWSCluster
2020
clusterIdentityKinds:
2121
- AWSClusterStaticIdentity
2222
- AWSClusterRoleIdentity
23-
- AWSClusterControllerIdentity
23+
- AWSClusterControllerIdentity

‎providers/azure.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
# limitations under the License.
1414

1515
name: azure
16-
clusterGVK:
17-
group: infrastructure.cluster.x-k8s.io
18-
version: v1beta1
19-
kind: AzureCluster
16+
clusterGVKs:
17+
- group: infrastructure.cluster.x-k8s.io
18+
version: v1beta1
19+
kind: AzureCluster
2020
clusterIdentityKinds:
2121
- AzureClusterIdentity
2222
- Secret

‎providers/openstack.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
# limitations under the License.
1414

1515
name: openstack
16-
clusterGVK:
17-
group: infrastructure.cluster.x-k8s.io
18-
version: v1beta1
19-
kind: OpenStackCluster
16+
clusterGVKs:
17+
- group: infrastructure.cluster.x-k8s.io
18+
version: v1beta1
19+
kind: OpenStackCluster
2020
clusterIdentityKinds:
2121
- Secret

‎providers/vsphere.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
# limitations under the License.
1414

1515
name: vsphere
16-
clusterGVK:
17-
group: infrastructure.cluster.x-k8s.io
18-
version: v1beta1
19-
kind: VSphereCluster
16+
clusterGVKs:
17+
- group: infrastructure.cluster.x-k8s.io
18+
version: v1beta1
19+
kind: VSphereCluster
2020
clusterIdentityKinds:
2121
- VSphereClusterIdentity

0 commit comments

Comments
 (0)
Please sign in to comment.