Skip to content

Commit f4cd181

Browse files
nileboxpmorie
authored andcommitted
Migrate to metav1 methods for manipulating controllerRefs (#1433)
1 parent 96b286e commit f4cd181

File tree

3 files changed

+5
-44
lines changed

3 files changed

+5
-44
lines changed

pkg/controller/controller.go

-39
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727

2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
"k8s.io/apimachinery/pkg/runtime"
30-
"k8s.io/apimachinery/pkg/runtime/schema"
3130
runtimeutil "k8s.io/apimachinery/pkg/util/runtime"
3231
"k8s.io/apimachinery/pkg/util/wait"
3332

@@ -614,44 +613,6 @@ func isServiceInstanceReady(instance *v1beta1.ServiceInstance) bool {
614613
return false
615614
}
616615

617-
// TODO (nilebox): The controllerRef methods below are merged into apimachinery and will be released in 1.8:
618-
// https://github.com/kubernetes/kubernetes/pull/48319
619-
// Remove them after 1.8 is released and Service Catalog is migrated to it
620-
621-
// IsControlledBy checks if the given object has a controller ownerReference set to the given owner
622-
func IsControlledBy(obj metav1.Object, owner metav1.Object) bool {
623-
ref := GetControllerOf(obj)
624-
if ref == nil {
625-
return false
626-
}
627-
return ref.UID == owner.GetUID()
628-
}
629-
630-
// GetControllerOf returns the controllerRef if controllee has a controller,
631-
// otherwise returns nil.
632-
func GetControllerOf(controllee metav1.Object) *metav1.OwnerReference {
633-
for _, ref := range controllee.GetOwnerReferences() {
634-
if ref.Controller != nil && *ref.Controller == true {
635-
return &ref
636-
}
637-
}
638-
return nil
639-
}
640-
641-
// NewControllerRef creates an OwnerReference pointing to the given owner.
642-
func NewControllerRef(owner metav1.Object, gvk schema.GroupVersionKind) *metav1.OwnerReference {
643-
blockOwnerDeletion := true
644-
isController := true
645-
return &metav1.OwnerReference{
646-
APIVersion: gvk.GroupVersion().String(),
647-
Kind: gvk.Kind,
648-
Name: owner.GetName(),
649-
UID: owner.GetUID(),
650-
BlockOwnerDeletion: &blockOwnerDeletion,
651-
Controller: &isController,
652-
}
653-
}
654-
655616
// NewClientConfigurationForBroker creates a new ClientConfiguration for connecting
656617
// to the specified Broker
657618
func NewClientConfigurationForBroker(broker *v1beta1.ClusterServiceBroker, authConfig *osb.AuthConfig) *osb.ClientConfiguration {

pkg/controller/controller_binding.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -843,8 +843,8 @@ func (c *controller) injectServiceBinding(binding *v1beta1.ServiceBinding, crede
843843
existingSecret, err := secretClient.Get(binding.Spec.SecretName, metav1.GetOptions{})
844844
if err == nil {
845845
// Update existing secret
846-
if !IsControlledBy(existingSecret, binding) {
847-
controllerRef := GetControllerOf(existingSecret)
846+
if !metav1.IsControlledBy(existingSecret, binding) {
847+
controllerRef := metav1.GetControllerOf(existingSecret)
848848
return fmt.Errorf(`Secret "%s/%s" is not owned by ServiceBinding, controllerRef: %v`, binding.Namespace, existingSecret.Name, controllerRef)
849849
}
850850
existingSecret.Data = secretData
@@ -869,7 +869,7 @@ func (c *controller) injectServiceBinding(binding *v1beta1.ServiceBinding, crede
869869
Name: binding.Spec.SecretName,
870870
Namespace: binding.Namespace,
871871
OwnerReferences: []metav1.OwnerReference{
872-
*NewControllerRef(binding, bindingControllerKind),
872+
*metav1.NewControllerRef(binding, bindingControllerKind),
873873
},
874874
},
875875
Data: secretData,

pkg/controller/controller_binding_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,11 @@ func TestReconcileServiceBindingWithParameters(t *testing.T) {
450450
if !ok {
451451
t.Fatal("couldn't convert secret into a corev1.Secret")
452452
}
453-
controllerRef := GetControllerOf(actionSecret)
453+
controllerRef := metav1.GetControllerOf(actionSecret)
454454
if controllerRef == nil || controllerRef.UID != updatedServiceBinding.UID {
455455
t.Fatalf("Secret is not owned by the ServiceBinding: %v", controllerRef)
456456
}
457-
if !IsControlledBy(actionSecret, updatedServiceBinding) {
457+
if !metav1.IsControlledBy(actionSecret, updatedServiceBinding) {
458458
t.Fatal("Secret is not owned by the ServiceBinding")
459459
}
460460
if e, a := testServiceBindingSecretName, actionSecret.Name; e != a {

0 commit comments

Comments
 (0)