From 3f205b7a68d80219503e8b78d171580188fe1a02 Mon Sep 17 00:00:00 2001 From: Jakub Dziechciewicz Date: Mon, 16 Dec 2019 10:35:25 +0100 Subject: [PATCH] Set OwnerReference on secrets created by controller (#39) --- controllers/oauth2client_controller.go | 9 +++++++++ .../oauth2client_controller_integration_test.go | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/controllers/oauth2client_controller.go b/controllers/oauth2client_controller.go index 5c506e5..818e96e 100644 --- a/controllers/oauth2client_controller.go +++ b/controllers/oauth2client_controller.go @@ -88,10 +88,13 @@ func (r *OAuth2ClientReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error // then lets add the finalizer and update the object. This is equivalent // registering our finalizer. if !containsString(oauth2client.ObjectMeta.Finalizers, FinalizerName) { + typeMeta := oauth2client.TypeMeta oauth2client.ObjectMeta.Finalizers = append(oauth2client.ObjectMeta.Finalizers, FinalizerName) if err := r.Update(ctx, &oauth2client); err != nil { return ctrl.Result{}, err } + // restore the TypeMeta object as it is removed during Update, but need to be accessed later + oauth2client.TypeMeta = typeMeta } } else { // The object is being deleted @@ -216,6 +219,12 @@ func (r *OAuth2ClientReconciler) registerOAuth2Client(ctx context.Context, c *hy ObjectMeta: metav1.ObjectMeta{ Name: c.Spec.SecretName, Namespace: c.Namespace, + OwnerReferences: []metav1.OwnerReference{{ + APIVersion: c.TypeMeta.APIVersion, + Kind: c.TypeMeta.Kind, + Name: c.ObjectMeta.Name, + UID: c.ObjectMeta.UID, + }}, }, Data: map[string][]byte{ ClientIDKey: []byte(*created.ClientID), diff --git a/controllers/oauth2client_controller_integration_test.go b/controllers/oauth2client_controller_integration_test.go index 407f79a..3ca563e 100644 --- a/controllers/oauth2client_controller_integration_test.go +++ b/controllers/oauth2client_controller_integration_test.go @@ -109,6 +109,7 @@ var _ = Describe("OAuth2Client Controller", func() { Expect(err).NotTo(HaveOccurred()) Expect(createdSecret.Data[controllers.ClientIDKey]).To(Equal([]byte(tstClientID))) Expect(createdSecret.Data[controllers.ClientSecretKey]).To(Equal([]byte(tstSecret))) + Expect(createdSecret.OwnerReferences).To(Equal(getOwnerReferenceTo(retrieved))) //delete instance c.Delete(context.TODO(), instance) @@ -264,6 +265,12 @@ var _ = Describe("OAuth2Client Controller", func() { Expect(*postedClient.ClientID).To(Equal(tstClientID)) Expect(*postedClient.Secret).To(Equal(tstSecret)) + // Ensure that secret doesn't have OwnerReference set + ok = client.ObjectKey{Name: tstSecretName, Namespace: tstNamespace} + err = k8sClient.Get(context.TODO(), ok, &secret) + Expect(err).To(BeNil()) + Expect(len(secret.OwnerReferences)).To(Equal(0)) + //delete instance c.Delete(context.TODO(), instance) @@ -346,6 +353,15 @@ var _ = Describe("OAuth2Client Controller", func() { }) }) +func getOwnerReferenceTo(c hydrav1alpha1.OAuth2Client) []metav1.OwnerReference { + return []metav1.OwnerReference{{ + APIVersion: c.APIVersion, + Kind: c.Kind, + Name: c.Name, + UID: c.UID, + }} +} + // add adds a new Controller to mgr with r as the reconcile.Reconciler func add(mgr manager.Manager, r reconcile.Reconciler) error { // Create a new controller