From b86e2efeaaece6e0a27ebe272546acbd432d2d71 Mon Sep 17 00:00:00 2001 From: PuneetPunamiya Date: Mon, 20 Jun 2022 12:18:03 +0530 Subject: [PATCH] Adds Tekton Hub ownerRef for Hub components Currently, there is no ownerRef added for secrets and configMap of db, api and ui This patch adds an ownerRef of `TektonHub` when Hub instance is created Note: If the secrets of db and api are created by user then those one's are not deleted Signed-off-by: Puneet Punamiya ppunamiy@redhat.com --- pkg/reconciler/kubernetes/tektonhub/tektonhub.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/reconciler/kubernetes/tektonhub/tektonhub.go b/pkg/reconciler/kubernetes/tektonhub/tektonhub.go index ec9db24ac4..3996bbf5b8 100644 --- a/pkg/reconciler/kubernetes/tektonhub/tektonhub.go +++ b/pkg/reconciler/kubernetes/tektonhub/tektonhub.go @@ -386,7 +386,7 @@ func (r *Reconciler) validateOrCreateDBSecrets(ctx context.Context, th *v1alpha1 dbSecret, err := r.getSecret(ctx, dbSecretName, namespace, dbKeys) if err != nil { - newDbSecret := createDbSecret(dbSecretName, namespace, dbSecret) + newDbSecret := createDbSecret(dbSecretName, namespace, dbSecret, th) if apierrors.IsNotFound(err) { _, err = r.kubeClientSet.CoreV1().Secrets(namespace).Create(ctx, newDbSecret, metav1.CreateOptions{}) if err != nil { @@ -496,6 +496,7 @@ func createUiConfigMap(name, namespace string, th *v1alpha1.TektonHub) *corev1.C Labels: map[string]string{ "ui": "tektonhub-ui", }, + OwnerReferences: []metav1.OwnerReference{getOwnerRef(th)}, }, Data: map[string]string{ "API_URL": th.Status.ApiRouteUrl, @@ -946,6 +947,7 @@ func createApiConfigMap(name, namespace string, th *v1alpha1.TektonHub) *corev1. Labels: map[string]string{ "app": "api", }, + OwnerReferences: []metav1.OwnerReference{getOwnerRef(th)}, }, Data: map[string]string{ "CONFIG_FILE_URL": th.Spec.Api.HubConfigUrl, @@ -987,7 +989,7 @@ func (r *Reconciler) getHubManifest(ctx context.Context, th *v1alpha1.TektonHub, return manifest, nil } -func createDbSecret(name, namespace string, existingSecret *corev1.Secret) *corev1.Secret { +func createDbSecret(name, namespace string, existingSecret *corev1.Secret, th *v1alpha1.TektonHub) *corev1.Secret { s := &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ Name: name, @@ -995,6 +997,7 @@ func createDbSecret(name, namespace string, existingSecret *corev1.Secret) *core Labels: map[string]string{ "app": "db", }, + OwnerReferences: []metav1.OwnerReference{getOwnerRef(th)}, }, Type: corev1.SecretTypeOpaque, } @@ -1028,6 +1031,11 @@ func createDbSecret(name, namespace string, existingSecret *corev1.Secret) *core return s } +// Get an ownerRef of TektonHub +func getOwnerRef(th *v1alpha1.TektonHub) metav1.OwnerReference { + return *metav1.NewControllerRef(th, th.GroupVersionKind()) +} + func (r *Reconciler) targetNamespaceCheck(ctx context.Context, th *v1alpha1.TektonHub) error { _, err := r.kubeClientSet.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{}) if err != nil {