Set OwnerReference on secrets created by controller (#39)

This commit is contained in:
Jakub Dziechciewicz 2019-12-16 10:35:25 +01:00 committed by hackerman
parent 85595eb763
commit 3f205b7a68
2 changed files with 25 additions and 0 deletions

View File

@ -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 // then lets add the finalizer and update the object. This is equivalent
// registering our finalizer. // registering our finalizer.
if !containsString(oauth2client.ObjectMeta.Finalizers, FinalizerName) { if !containsString(oauth2client.ObjectMeta.Finalizers, FinalizerName) {
typeMeta := oauth2client.TypeMeta
oauth2client.ObjectMeta.Finalizers = append(oauth2client.ObjectMeta.Finalizers, FinalizerName) oauth2client.ObjectMeta.Finalizers = append(oauth2client.ObjectMeta.Finalizers, FinalizerName)
if err := r.Update(ctx, &oauth2client); err != nil { if err := r.Update(ctx, &oauth2client); err != nil {
return ctrl.Result{}, err return ctrl.Result{}, err
} }
// restore the TypeMeta object as it is removed during Update, but need to be accessed later
oauth2client.TypeMeta = typeMeta
} }
} else { } else {
// The object is being deleted // The object is being deleted
@ -216,6 +219,12 @@ func (r *OAuth2ClientReconciler) registerOAuth2Client(ctx context.Context, c *hy
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: c.Spec.SecretName, Name: c.Spec.SecretName,
Namespace: c.Namespace, 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{ Data: map[string][]byte{
ClientIDKey: []byte(*created.ClientID), ClientIDKey: []byte(*created.ClientID),

View File

@ -109,6 +109,7 @@ var _ = Describe("OAuth2Client Controller", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(createdSecret.Data[controllers.ClientIDKey]).To(Equal([]byte(tstClientID))) Expect(createdSecret.Data[controllers.ClientIDKey]).To(Equal([]byte(tstClientID)))
Expect(createdSecret.Data[controllers.ClientSecretKey]).To(Equal([]byte(tstSecret))) Expect(createdSecret.Data[controllers.ClientSecretKey]).To(Equal([]byte(tstSecret)))
Expect(createdSecret.OwnerReferences).To(Equal(getOwnerReferenceTo(retrieved)))
//delete instance //delete instance
c.Delete(context.TODO(), instance) c.Delete(context.TODO(), instance)
@ -264,6 +265,12 @@ var _ = Describe("OAuth2Client Controller", func() {
Expect(*postedClient.ClientID).To(Equal(tstClientID)) Expect(*postedClient.ClientID).To(Equal(tstClientID))
Expect(*postedClient.Secret).To(Equal(tstSecret)) 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 //delete instance
c.Delete(context.TODO(), 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 // add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error { func add(mgr manager.Manager, r reconcile.Reconciler) error {
// Create a new controller // Create a new controller