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 2 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
10 changes: 10 additions & 0 deletions api/v1alpha1/automatedclusterdiscovery_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ type AKS struct {
SubscriptionID string `json:"subscriptionID"`
}

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?

// AutomatedClusterDiscoverySpec defines the desired state of AutomatedClusterDiscovery
type AutomatedClusterDiscoverySpec struct {
// Name is the name of the cluster
Expand Down Expand Up @@ -58,6 +64,10 @@ 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"`
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we add a CAPI field and configure this within that?

Given that we only use this for the CAPI Provider?

}

// AutomatedClusterDiscoveryStatus defines the observed state of AutomatedClusterDiscovery
Expand Down
16 changes: 16 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,7 +44,7 @@ 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
Expand All @@ -62,6 +62,16 @@ 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
2 changes: 2 additions & 0 deletions config/samples/capi_discovery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ metadata:
spec:
type: capi
interval: 10m
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.CurrentClusterRef)
clusterID = cd.Spec.CurrentClusterRef.Name

clusters, err = capiProvider.ListClusters(ctx)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,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.Name, 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