Skip to content

Commit

Permalink
Capi self filtering (#28)
Browse files Browse the repository at this point in the history
* Add managementClusterName to capi provider

Add currentClusterName to acd spec

* Add managementClusterRef to acd and capi to include management cluster name of cluster to be skipped

Update capi sample acd to include currentClusterRef

* 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

* Update string function of cluster to return name directly
  • Loading branch information
ranatrk authored Dec 21, 2023
1 parent 1cec06f commit 41ed04e
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 14 deletions.
21 changes: 21 additions & 0 deletions api/v1alpha1/automatedclusterdiscovery_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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 c.Name
}

// AutomatedClusterDiscoverySpec defines the desired state of AutomatedClusterDiscovery
type AutomatedClusterDiscoverySpec struct {
// Name is the name of the cluster
Expand All @@ -45,6 +63,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 Down
36 changes: 36 additions & 0 deletions 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 @@ -44,14 +44,29 @@ spec:
AutomatedClusterDiscovery
properties:
aks:
description: AKS defines the desired state of AKS
description: AKS configures discovery of AKS clusters from Azure.
properties:
subscriptionID:
description: SubscriptionID is the Azure subscription ID
type: string
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 Down
3 changes: 3 additions & 0 deletions config/samples/capi_discovery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ metadata:
spec:
type: capi
interval: 10m
capi:
currentClusterRef:
name: management-cluster
5 changes: 3 additions & 2 deletions internal/controller/automatedclusterdiscovery_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type AutomatedClusterDiscoveryReconciler struct {
EventRecorder eventRecorder

AKSProvider func(string) providers.Provider
CAPIProvider func(client.Client, string) providers.Provider
CAPIProvider func(client.Client, string, *clustersv1alpha1.Cluster) providers.Provider
}

// event emits a Kubernetes event and forwards the event to the event recorder
Expand Down Expand Up @@ -179,7 +179,8 @@ func (r *AutomatedClusterDiscoveryReconciler) reconcileResources(ctx context.Con
"name", cd.Spec.Name,
)

capiProvider := r.CAPIProvider(r.Client, cd.Namespace)
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 All @@ -942,7 +947,7 @@ func TestAutomatedClusterDiscoveryReconciler(t *testing.T) {
reconciler := &AutomatedClusterDiscoveryReconciler{
Client: k8sClient,
Scheme: scheme,
CAPIProvider: func(capiclient client.Client, namespace string) providers.Provider {
CAPIProvider: func(capiclient client.Client, namespace string, managementClusterRef *clustersv1alpha1.Cluster) providers.Provider {
return &testProvider
},
EventRecorder: &mockEventRecorder{},
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ func main() {
AKSProvider: func(subscriptionID string) providers.Provider {
return azure.NewAzureProvider(subscriptionID)
},
CAPIProvider: func(kubeclient client.Client, namespace string) providers.Provider {
return capi.NewCAPIProvider(kubeclient, namespace)
CAPIProvider: func(kubeclient client.Client, namespace string, managementClusterRef *clustersv1alpha1.Cluster) providers.Provider {
return capi.NewCAPIProvider(kubeclient, namespace, managementClusterRef)
},
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AutomatedClusterDiscovery")
Expand Down
17 changes: 10 additions & 7 deletions pkg/providers/capi/capi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@ package capi
import (
"context"

clustersv1alpha1 "github.com/weaveworks/cluster-reflector-controller/api/v1alpha1"
"github.com/weaveworks/cluster-reflector-controller/pkg/providers"
capiclusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type CAPIProvider struct {
Kubeclient client.Client
Namespace string
Kubeclient client.Client
Namespace string
ManagementClusterRef *clustersv1alpha1.Cluster
}

var _ providers.Provider = (*CAPIProvider)(nil)

// NewCAPIProvider creates and returns a CAPIProvider ready for use
func NewCAPIProvider(client client.Client, namespace string) *CAPIProvider {
func NewCAPIProvider(client client.Client, namespace string, managementClusterRef *clustersv1alpha1.Cluster) *CAPIProvider {
provider := &CAPIProvider{
Kubeclient: client,
Namespace: namespace,
Kubeclient: client,
Namespace: namespace,
ManagementClusterRef: managementClusterRef,
}
return provider
}
Expand All @@ -37,7 +40,7 @@ func (p *CAPIProvider) ListClusters(ctx context.Context) ([]*providers.ProviderC
for _, capiCluster := range capiClusters.Items {
clusters = append(clusters, &providers.ProviderCluster{
Name: capiCluster.Name,
ID: string(capiCluster.GetObjectMeta().GetUID()),
ID: capiCluster.Name,
KubeConfig: nil,
Labels: capiCluster.Labels,
})
Expand All @@ -49,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 "", nil
return p.ManagementClusterRef.String(), nil
}
4 changes: 3 additions & 1 deletion pkg/providers/capi/capi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

clustersv1alpha1 "github.com/weaveworks/cluster-reflector-controller/api/v1alpha1"
"github.com/weaveworks/cluster-reflector-controller/pkg/providers"
)

Expand All @@ -29,7 +30,7 @@ func TestClusterProvider_ListClusters(t *testing.T) {
}

client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(clusters...).Build()
provider := NewCAPIProvider(client, "default")
provider := NewCAPIProvider(client, "default", &clustersv1alpha1.Cluster{Name: "management-cluster"})

provided, err := provider.ListClusters(context.TODO())
if err != nil {
Expand All @@ -39,6 +40,7 @@ func TestClusterProvider_ListClusters(t *testing.T) {
expected := []*providers.ProviderCluster{
{
Name: "cluster-1",
ID: "cluster-1",
KubeConfig: nil,
},
}
Expand Down

0 comments on commit 41ed04e

Please sign in to comment.