Skip to content

Commit

Permalink
Add capi to acd struct to include the CurrentClusterRef including the…
Browse files Browse the repository at this point in the history
… management cluster name

Add String funct to Cluster to convert the CurrentClusterRef to string whenever needed
  • Loading branch information
ranatrk committed Dec 21, 2023
1 parent 92b7c14 commit 8aa9a9d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 20 deletions.
21 changes: 17 additions & 4 deletions api/v1alpha1/automatedclusterdiscovery_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand All @@ -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"`
Expand All @@ -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
Expand Down
22 changes: 21 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions config/samples/capi_discovery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ metadata:
spec:
type: capi
interval: 10m
currentClusterRef:
name: management-cluster
capi:
currentClusterRef:
name: testcapicluster
4 changes: 2 additions & 2 deletions internal/controller/automatedclusterdiscovery_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
},
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/providers/capi/capi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 8aa9a9d

Please sign in to comment.