99 "hash/fnv"
1010
1111 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+ "k8s.io/apimachinery/pkg/runtime"
1213 ctrl "sigs.k8s.io/controller-runtime"
1314 "sigs.k8s.io/controller-runtime/pkg/client"
1415 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -28,6 +29,10 @@ type PersonalOrganizationController struct {
2829 Client client.Client
2930
3031 Config PersonalOrganizationControllerConfig
32+
33+ // The scheme is used to set the controller reference on the personal
34+ // organization.
35+ Scheme * runtime.Scheme
3136}
3237
3338// +kubebuilder:rbac:groups=iam.datumapis.com,resources=users,verbs=get;list;watch
@@ -59,15 +64,8 @@ func (r *PersonalOrganizationController) Reconcile(ctx context.Context, req ctrl
5964 _ , err := controllerutil .CreateOrUpdate (ctx , r .Client , personalOrg , func () error {
6065 logger .Info ("Creating or updating personal organization" , "organization" , personalOrg .Name )
6166 metav1 .SetMetaDataAnnotation (& personalOrg .ObjectMeta , "kubernetes.io/display-name" , fmt .Sprintf ("%s %s's Personal Org" , user .Spec .GivenName , user .Spec .FamilyName ))
62- personalOrg .ObjectMeta .OwnerReferences = []metav1.OwnerReference {
63- // The owner reference is used to ensure that the personal organization
64- // is deleted when the user is deleted.
65- {
66- APIVersion : iamv1alpha1 .SchemeGroupVersion .String (),
67- Kind : "User" ,
68- Name : user .Name ,
69- UID : user .UID ,
70- },
67+ if err := controllerutil .SetControllerReference (user , personalOrg , r .Scheme ); err != nil {
68+ return fmt .Errorf ("failed to set controller reference: %w" , err )
7169 }
7270 personalOrg .Spec .Type = "Personal"
7371 return nil
0 commit comments