Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capi self filtering #28

Merged
merged 4 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One idea here is to implement Stringer this way in CAPIProvider.ClusterID you could just call p.ManagementClusterRef.String() if/when Cluster grows a Namespace, that code remains the same?

// 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"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻


// 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
Loading