Skip to content

Commit 41ed04e

Browse files
authored
Capi self filtering (#28)
* 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
1 parent 1cec06f commit 41ed04e

File tree

9 files changed

+100
-14
lines changed

9 files changed

+100
-14
lines changed

api/v1alpha1/automatedclusterdiscovery_types.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ type AKS struct {
2828
SubscriptionID string `json:"subscriptionID"`
2929
}
3030

31+
// CAPI defines the desired state of CAPI
32+
type CAPI struct {
33+
// Current Cluster name indicating the management cluster
34+
// used to avoid choosing the cluster the controller is running in
35+
CurrentClusterRef Cluster `json:"currentClusterRef,omitempty"`
36+
}
37+
38+
type Cluster struct {
39+
// Name is the name of the cluster
40+
// +required
41+
Name string `json:"name"`
42+
}
43+
44+
// String returns the string representation of the Cluster
45+
func (c Cluster) String() string {
46+
return c.Name
47+
}
48+
3149
// AutomatedClusterDiscoverySpec defines the desired state of AutomatedClusterDiscovery
3250
type AutomatedClusterDiscoverySpec struct {
3351
// Name is the name of the cluster
@@ -45,6 +63,9 @@ type AutomatedClusterDiscoverySpec struct {
4563
// AKS configures discovery of AKS clusters from Azure.
4664
AKS *AKS `json:"aks,omitempty"`
4765

66+
// CAPI configures discovery of CAPI clusters
67+
CAPI *CAPI `json:"capi,omitempty"`
68+
4869
// The interval at which to run the discovery
4970
// +required
5071
Interval metav1.Duration `json:"interval"`

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/clusters.weave.works_automatedclusterdiscoveries.yaml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,29 @@ spec:
4444
AutomatedClusterDiscovery
4545
properties:
4646
aks:
47-
description: AKS defines the desired state of AKS
47+
description: AKS configures discovery of AKS clusters from Azure.
4848
properties:
4949
subscriptionID:
5050
description: SubscriptionID is the Azure subscription ID
5151
type: string
5252
required:
5353
- subscriptionID
5454
type: object
55+
capi:
56+
description: CAPI configures discovery of CAPI clusters
57+
properties:
58+
currentClusterRef:
59+
description: Current Cluster name indicating the management cluster
60+
used to avoid choosing the cluster the controller is running
61+
in
62+
properties:
63+
name:
64+
description: Name is the name of the cluster
65+
type: string
66+
required:
67+
- name
68+
type: object
69+
type: object
5570
commonAnnotations:
5671
additionalProperties:
5772
type: string

config/samples/capi_discovery.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ metadata:
66
spec:
77
type: capi
88
interval: 10m
9+
capi:
10+
currentClusterRef:
11+
name: management-cluster

internal/controller/automatedclusterdiscovery_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type AutomatedClusterDiscoveryReconciler struct {
5555
EventRecorder eventRecorder
5656

5757
AKSProvider func(string) providers.Provider
58-
CAPIProvider func(client.Client, string) providers.Provider
58+
CAPIProvider func(client.Client, string, *clustersv1alpha1.Cluster) providers.Provider
5959
}
6060

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

182-
capiProvider := r.CAPIProvider(r.Client, cd.Namespace)
182+
capiProvider := r.CAPIProvider(r.Client, cd.Namespace, &cd.Spec.CAPI.CurrentClusterRef)
183+
clusterID = cd.Spec.CAPI.CurrentClusterRef.String()
183184

184185
clusters, err = capiProvider.ListClusters(ctx)
185186
if err != nil {

internal/controller/automatedclusterdiscovery_controller_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,11 @@ func TestAutomatedClusterDiscoveryReconciler(t *testing.T) {
922922
Spec: clustersv1alpha1.AutomatedClusterDiscoverySpec{
923923
Type: "capi",
924924
Interval: metav1.Duration{Duration: time.Minute},
925+
CAPI: &clustersv1alpha1.CAPI{
926+
CurrentClusterRef: clustersv1alpha1.Cluster{
927+
Name: "management-cluster",
928+
},
929+
},
925930
},
926931
}
927932

@@ -942,7 +947,7 @@ func TestAutomatedClusterDiscoveryReconciler(t *testing.T) {
942947
reconciler := &AutomatedClusterDiscoveryReconciler{
943948
Client: k8sClient,
944949
Scheme: scheme,
945-
CAPIProvider: func(capiclient client.Client, namespace string) providers.Provider {
950+
CAPIProvider: func(capiclient client.Client, namespace string, managementClusterRef *clustersv1alpha1.Cluster) providers.Provider {
946951
return &testProvider
947952
},
948953
EventRecorder: &mockEventRecorder{},

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ func main() {
114114
AKSProvider: func(subscriptionID string) providers.Provider {
115115
return azure.NewAzureProvider(subscriptionID)
116116
},
117-
CAPIProvider: func(kubeclient client.Client, namespace string) providers.Provider {
118-
return capi.NewCAPIProvider(kubeclient, namespace)
117+
CAPIProvider: func(kubeclient client.Client, namespace string, managementClusterRef *clustersv1alpha1.Cluster) providers.Provider {
118+
return capi.NewCAPIProvider(kubeclient, namespace, managementClusterRef)
119119
},
120120
}).SetupWithManager(mgr); err != nil {
121121
setupLog.Error(err, "unable to create controller", "controller", "AutomatedClusterDiscovery")

pkg/providers/capi/capi.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,26 @@ package capi
33
import (
44
"context"
55

6+
clustersv1alpha1 "github.com/weaveworks/cluster-reflector-controller/api/v1alpha1"
67
"github.com/weaveworks/cluster-reflector-controller/pkg/providers"
78
capiclusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
89
"sigs.k8s.io/controller-runtime/pkg/client"
910
)
1011

1112
type CAPIProvider struct {
12-
Kubeclient client.Client
13-
Namespace string
13+
Kubeclient client.Client
14+
Namespace string
15+
ManagementClusterRef *clustersv1alpha1.Cluster
1416
}
1517

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

1820
// NewCAPIProvider creates and returns a CAPIProvider ready for use
19-
func NewCAPIProvider(client client.Client, namespace string) *CAPIProvider {
21+
func NewCAPIProvider(client client.Client, namespace string, managementClusterRef *clustersv1alpha1.Cluster) *CAPIProvider {
2022
provider := &CAPIProvider{
21-
Kubeclient: client,
22-
Namespace: namespace,
23+
Kubeclient: client,
24+
Namespace: namespace,
25+
ManagementClusterRef: managementClusterRef,
2326
}
2427
return provider
2528
}
@@ -37,7 +40,7 @@ func (p *CAPIProvider) ListClusters(ctx context.Context) ([]*providers.ProviderC
3740
for _, capiCluster := range capiClusters.Items {
3841
clusters = append(clusters, &providers.ProviderCluster{
3942
Name: capiCluster.Name,
40-
ID: string(capiCluster.GetObjectMeta().GetUID()),
43+
ID: capiCluster.Name,
4144
KubeConfig: nil,
4245
Labels: capiCluster.Labels,
4346
})
@@ -49,5 +52,5 @@ func (p *CAPIProvider) ListClusters(ctx context.Context) ([]*providers.ProviderC
4952
// ProviderCluster has an ID to identify the cluster, but capi cluster doesn't have a Cluster ID
5053
// therefore wont't match in the case of CAPI
5154
func (p *CAPIProvider) ClusterID(ctx context.Context, kubeClient client.Reader) (string, error) {
52-
return "", nil
55+
return p.ManagementClusterRef.String(), nil
5356
}

pkg/providers/capi/capi_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"sigs.k8s.io/controller-runtime/pkg/client"
1212
"sigs.k8s.io/controller-runtime/pkg/client/fake"
1313

14+
clustersv1alpha1 "github.com/weaveworks/cluster-reflector-controller/api/v1alpha1"
1415
"github.com/weaveworks/cluster-reflector-controller/pkg/providers"
1516
)
1617

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

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

3435
provided, err := provider.ListClusters(context.TODO())
3536
if err != nil {
@@ -39,6 +40,7 @@ func TestClusterProvider_ListClusters(t *testing.T) {
3940
expected := []*providers.ProviderCluster{
4041
{
4142
Name: "cluster-1",
43+
ID: "cluster-1",
4244
KubeConfig: nil,
4345
},
4446
}

0 commit comments

Comments
 (0)