in controllers/keycloakrealmcomponent/keycloakrealmcomponent_controller.go [299:338]
func (r *Reconcile) setComponentOwnerReference(
ctx context.Context,
component *keycloakApi.KeycloakRealmComponent,
) error {
if component.Spec.ParentRef == nil || component.Spec.ParentRef.Kind != keycloakApi.KeycloakRealmComponentKind {
return nil
}
for _, ref := range component.GetOwnerReferences() {
if ref.Kind == keycloakApi.KeycloakRealmComponentKind {
return nil
}
}
parentComponent := &keycloakApi.KeycloakRealmComponent{}
if err := r.client.Get(ctx, types.NamespacedName{Name: component.Spec.ParentRef.Name, Namespace: component.GetNamespace()}, parentComponent); err != nil {
return fmt.Errorf("unable to get parent component: %w", err)
}
gvk, err := apiutil.GVKForObject(parentComponent, r.scheme)
if err != nil {
return fmt.Errorf("unable to get gvk for parent component: %w", err)
}
ref := metav1.OwnerReference{
APIVersion: gvk.GroupVersion().String(),
Kind: gvk.Kind,
Name: parentComponent.GetName(),
UID: parentComponent.GetUID(),
BlockOwnerDeletion: pointer.Bool(true),
Controller: pointer.Bool(true),
}
component.SetOwnerReferences([]v1.OwnerReference{ref})
if err := r.client.Update(ctx, component); err != nil {
return fmt.Errorf("failed to set owner reference %s: %w", parentComponent.Name, err)
}
return nil
}