Skip to content

Commit

Permalink
Add managed-by label to generated resources.
Browse files Browse the repository at this point in the history
This also adds "origin" labels to indicate which cluster-discovery
resource created the clusters and secrets.
  • Loading branch information
bigkevmcd committed Oct 12, 2023
1 parent 3c2aa74 commit 77ac51b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 90 deletions.
14 changes: 14 additions & 0 deletions internal/controller/automatedclusterdiscovery_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import (
"k8s.io/client-go/tools/clientcmd/api"
)

const k8sManagedByLabel = "app.kubernetes.io/managed-by"

// AutomatedClusterDiscoveryReconciler reconciles a AutomatedClusterDiscovery object
type AutomatedClusterDiscoveryReconciler struct {
client.Client
Expand Down Expand Up @@ -159,6 +161,7 @@ func (r *AutomatedClusterDiscoveryReconciler) reconcileClusters(ctx context.Cont
if err := controllerutil.SetOwnerReference(cd, gitopsCluster, r.Scheme); err != nil {
return inventoryResources, fmt.Errorf("failed to set ownership on created GitopsCluster: %w", err)
}
gitopsCluster.SetLabels(labelsForResource(*cd))
if err := r.Client.Create(ctx, gitopsCluster); err != nil {
return inventoryResources, err
}
Expand All @@ -182,6 +185,8 @@ func (r *AutomatedClusterDiscoveryReconciler) reconcileClusters(ctx context.Cont
if err := controllerutil.SetOwnerReference(cd, secret, r.Scheme); err != nil {
return inventoryResources, fmt.Errorf("failed to set ownership on created Secret: %w", err)
}
secret.SetLabels(labelsForResource(*cd))

if err := r.Client.Create(ctx, secret); err != nil {
return inventoryResources, err
}
Expand Down Expand Up @@ -321,3 +326,12 @@ func clustersToMapping(clusters []*providers.ProviderCluster) map[string]*provid

return names
}

func labelsForResource(acd clustersv1alpha1.AutomatedClusterDiscovery) map[string]string {
return map[string]string{
k8sManagedByLabel: "cluster-reflector-controller",
"clusters.weave.works/origin-name": acd.GetName(),
"clusters.weave.works/origin-namespace": acd.GetNamespace(),
"clusters.weave.works/origin-type": acd.Spec.Type,
}
}
21 changes: 21 additions & 0 deletions internal/controller/automatedclusterdiscovery_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,25 @@ func TestAutomatedClusterDiscoveryReconciler(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, ctrl.Result{RequeueAfter: time.Minute}, result)

wantLabels := map[string]string{
"app.kubernetes.io/managed-by": "cluster-reflector-controller",
"clusters.weave.works/origin-name": "test-aks",
"clusters.weave.works/origin-namespace": "default",
"clusters.weave.works/origin-type": "aks",
}

gitopsCluster := &gitopsv1alpha1.GitopsCluster{}
err = k8sClient.Get(ctx, types.NamespacedName{Name: "cluster-1", Namespace: aksCluster.Namespace}, gitopsCluster)
assert.NoError(t, err)
assert.Equal(t, gitopsv1alpha1.GitopsClusterSpec{
SecretRef: &meta.LocalObjectReference{Name: "cluster-1-kubeconfig"},
}, gitopsCluster.Spec)
assertHasLabels(t, gitopsCluster, wantLabels)

secret := &corev1.Secret{}
err = k8sClient.Get(ctx, types.NamespacedName{Name: "cluster-1-kubeconfig", Namespace: aksCluster.Namespace}, secret)
assert.NoError(t, err)
assertHasLabels(t, secret, wantLabels)

value, err := clientcmd.Write(*testProvider.response[0].KubeConfig)
assert.NoError(t, err)
Expand Down Expand Up @@ -350,6 +359,18 @@ func assertHasOwnerReference(t *testing.T, obj metav1.Object, ownerRef metav1.Ow
t.Fatalf("%s %s does not have OwnerReference %s", obj.GetResourceVersion(), obj.GetName(), &ownerRef)
}

func assertHasLabels(t *testing.T, o client.Object, want map[string]string) {
labels := o.GetLabels()
for k, v := range want {
kv, ok := labels[k]
if !ok {
t.Errorf("%s %s/%s is missing label %q with value %q", o.GetObjectKind().GroupVersionKind().Kind, o.GetNamespace(), o.GetName(), k, v)
continue
}
assert.Equal(t, v, kv)
}
}

func isOwnerReferenceEqual(a, b metav1.OwnerReference) bool {
return (a.APIVersion == b.APIVersion) &&
(a.Kind == b.Kind) &&
Expand Down
90 changes: 0 additions & 90 deletions internal/controller/suite_test.go

This file was deleted.

0 comments on commit 77ac51b

Please sign in to comment.