From 8aa9a9db9f7246dc46ff43c100e0f4d370d599c4 Mon Sep 17 00:00:00 2001 From: Rana Tarek Hassan Date: Thu, 21 Dec 2023 13:32:09 +0200 Subject: [PATCH] Add capi to acd struct to include the CurrentClusterRef including the management cluster name Add String funct to Cluster to convert the CurrentClusterRef to string whenever needed --- .../automatedclusterdiscovery_types.go | 21 +++++++++++++--- api/v1alpha1/zz_generated.deepcopy.go | 22 +++++++++++++++- ...ave.works_automatedclusterdiscoveries.yaml | 25 +++++++++++-------- config/samples/capi_discovery.yaml | 5 ++-- .../automatedclusterdiscovery_controller.go | 4 +-- ...tomatedclusterdiscovery_controller_test.go | 5 ++++ pkg/providers/capi/capi.go | 2 +- 7 files changed, 64 insertions(+), 20 deletions(-) diff --git a/api/v1alpha1/automatedclusterdiscovery_types.go b/api/v1alpha1/automatedclusterdiscovery_types.go index b789163..010b51b 100644 --- a/api/v1alpha1/automatedclusterdiscovery_types.go +++ b/api/v1alpha1/automatedclusterdiscovery_types.go @@ -17,6 +17,8 @@ limitations under the License. package v1alpha1 import ( + "fmt" + "github.com/fluxcd/pkg/apis/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -28,12 +30,24 @@ type AKS struct { SubscriptionID string `json:"subscriptionID"` } +// CAPI defines the desired state of CAPI +type CAPI struct { + // Current Cluster name indicating the management cluster + // used to avoid choosing the cluster the controller is running in + CurrentClusterRef Cluster `json:"currentClusterRef,omitempty"` +} + type Cluster struct { // Name is the name of the cluster // +required Name string `json:"name"` } +// String returns the string representation of the Cluster +func (c Cluster) String() string { + return fmt.Sprintf("%v", c.Name) +} + // AutomatedClusterDiscoverySpec defines the desired state of AutomatedClusterDiscovery type AutomatedClusterDiscoverySpec struct { // Name is the name of the cluster @@ -51,6 +65,9 @@ type AutomatedClusterDiscoverySpec struct { // AKS configures discovery of AKS clusters from Azure. AKS *AKS `json:"aks,omitempty"` + // CAPI configures discovery of CAPI clusters + CAPI *CAPI `json:"capi,omitempty"` + // The interval at which to run the discovery // +required Interval metav1.Duration `json:"interval"` @@ -64,10 +81,6 @@ type AutomatedClusterDiscoverySpec struct { CommonLabels map[string]string `json:"commonLabels,omitempty"` // Annotations to add to all generated resources. CommonAnnotations map[string]string `json:"commonAnnotations,omitempty"` - - // Current Cluster name indicating the management cluster - // used to avoid choosing the cluster the controller is running in - CurrentClusterRef Cluster `json:"currentClusterRef,omitempty"` } // AutomatedClusterDiscoveryStatus defines the observed state of AutomatedClusterDiscovery diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 7b3f38b..b5d26f6 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -107,6 +107,11 @@ func (in *AutomatedClusterDiscoverySpec) DeepCopyInto(out *AutomatedClusterDisco *out = new(AKS) **out = **in } + if in.CAPI != nil { + in, out := &in.CAPI, &out.CAPI + *out = new(CAPI) + **out = **in + } out.Interval = in.Interval if in.CommonLabels != nil { in, out := &in.CommonLabels, &out.CommonLabels @@ -122,7 +127,6 @@ func (in *AutomatedClusterDiscoverySpec) DeepCopyInto(out *AutomatedClusterDisco (*out)[key] = val } } - out.CurrentClusterRef = in.CurrentClusterRef } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AutomatedClusterDiscoverySpec. @@ -163,6 +167,22 @@ func (in *AutomatedClusterDiscoveryStatus) DeepCopy() *AutomatedClusterDiscovery return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CAPI) DeepCopyInto(out *CAPI) { + *out = *in + out.CurrentClusterRef = in.CurrentClusterRef +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CAPI. +func (in *CAPI) DeepCopy() *CAPI { + if in == nil { + return nil + } + out := new(CAPI) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Cluster) DeepCopyInto(out *Cluster) { *out = *in diff --git a/config/crd/bases/clusters.weave.works_automatedclusterdiscoveries.yaml b/config/crd/bases/clusters.weave.works_automatedclusterdiscoveries.yaml index d85f568..6752d14 100644 --- a/config/crd/bases/clusters.weave.works_automatedclusterdiscoveries.yaml +++ b/config/crd/bases/clusters.weave.works_automatedclusterdiscoveries.yaml @@ -52,6 +52,21 @@ spec: required: - subscriptionID type: object + capi: + description: CAPI configures discovery of CAPI clusters + properties: + currentClusterRef: + description: Current Cluster name indicating the management cluster + used to avoid choosing the cluster the controller is running + in + properties: + name: + description: Name is the name of the cluster + type: string + required: + - name + type: object + type: object commonAnnotations: additionalProperties: type: string @@ -62,16 +77,6 @@ spec: type: string description: Labels to add to all generated resources. type: object - currentClusterRef: - description: Current Cluster name indicating the management cluster - used to avoid choosing the cluster the controller is running in - properties: - name: - description: Name is the name of the cluster - type: string - required: - - name - type: object disableTags: description: If DisableTags is true, labels will not be applied to the generated Clusters from the tags on the upstream Clusters. diff --git a/config/samples/capi_discovery.yaml b/config/samples/capi_discovery.yaml index f6e9eec..7b67f1d 100644 --- a/config/samples/capi_discovery.yaml +++ b/config/samples/capi_discovery.yaml @@ -6,5 +6,6 @@ metadata: spec: type: capi interval: 10m - currentClusterRef: - name: management-cluster + capi: + currentClusterRef: + name: testcapicluster diff --git a/internal/controller/automatedclusterdiscovery_controller.go b/internal/controller/automatedclusterdiscovery_controller.go index 9cc83a1..777ba92 100644 --- a/internal/controller/automatedclusterdiscovery_controller.go +++ b/internal/controller/automatedclusterdiscovery_controller.go @@ -179,8 +179,8 @@ func (r *AutomatedClusterDiscoveryReconciler) reconcileResources(ctx context.Con "name", cd.Spec.Name, ) - capiProvider := r.CAPIProvider(r.Client, cd.Namespace, &cd.Spec.CurrentClusterRef) - clusterID = cd.Spec.CurrentClusterRef.Name + capiProvider := r.CAPIProvider(r.Client, cd.Namespace, &cd.Spec.CAPI.CurrentClusterRef) + clusterID = cd.Spec.CAPI.CurrentClusterRef.String() clusters, err = capiProvider.ListClusters(ctx) if err != nil { diff --git a/internal/controller/automatedclusterdiscovery_controller_test.go b/internal/controller/automatedclusterdiscovery_controller_test.go index c23bc18..0d28c24 100644 --- a/internal/controller/automatedclusterdiscovery_controller_test.go +++ b/internal/controller/automatedclusterdiscovery_controller_test.go @@ -922,6 +922,11 @@ func TestAutomatedClusterDiscoveryReconciler(t *testing.T) { Spec: clustersv1alpha1.AutomatedClusterDiscoverySpec{ Type: "capi", Interval: metav1.Duration{Duration: time.Minute}, + CAPI: &clustersv1alpha1.CAPI{ + CurrentClusterRef: clustersv1alpha1.Cluster{ + Name: "management-cluster", + }, + }, }, } diff --git a/pkg/providers/capi/capi.go b/pkg/providers/capi/capi.go index 360e2b4..34f3719 100644 --- a/pkg/providers/capi/capi.go +++ b/pkg/providers/capi/capi.go @@ -52,5 +52,5 @@ func (p *CAPIProvider) ListClusters(ctx context.Context) ([]*providers.ProviderC // ProviderCluster has an ID to identify the cluster, but capi cluster doesn't have a Cluster ID // therefore wont't match in the case of CAPI func (p *CAPIProvider) ClusterID(ctx context.Context, kubeClient client.Reader) (string, error) { - return p.ManagementClusterRef.Name, nil + return p.ManagementClusterRef.String(), nil }