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

Add managed-by label to generated resources. #9

Merged
merged 1 commit into from
Oct 12, 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
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.

Loading