Set OwnerReference on secrets created by controller (#39)
This commit is contained in:
parent
85595eb763
commit
3f205b7a68
@ -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),
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user