Skip to content

Commit

Permalink
Migrate to metav1 methods for manipulating controllerRefs (#1433)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilebox authored and pmorie committed Oct 19, 2017
1 parent 96b286e commit f4cd181
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 44 deletions.
39 changes: 0 additions & 39 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
runtimeutil "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"

Expand Down Expand Up @@ -614,44 +613,6 @@ func isServiceInstanceReady(instance *v1beta1.ServiceInstance) bool {
return false
}

// TODO (nilebox): The controllerRef methods below are merged into apimachinery and will be released in 1.8:
// https://github.com/kubernetes/kubernetes/pull/48319
// Remove them after 1.8 is released and Service Catalog is migrated to it

// IsControlledBy checks if the given object has a controller ownerReference set to the given owner
func IsControlledBy(obj metav1.Object, owner metav1.Object) bool {
ref := GetControllerOf(obj)
if ref == nil {
return false
}
return ref.UID == owner.GetUID()
}

// GetControllerOf returns the controllerRef if controllee has a controller,
// otherwise returns nil.
func GetControllerOf(controllee metav1.Object) *metav1.OwnerReference {
for _, ref := range controllee.GetOwnerReferences() {
if ref.Controller != nil && *ref.Controller == true {
return &ref
}
}
return nil
}

// NewControllerRef creates an OwnerReference pointing to the given owner.
func NewControllerRef(owner metav1.Object, gvk schema.GroupVersionKind) *metav1.OwnerReference {
blockOwnerDeletion := true
isController := true
return &metav1.OwnerReference{
APIVersion: gvk.GroupVersion().String(),
Kind: gvk.Kind,
Name: owner.GetName(),
UID: owner.GetUID(),
BlockOwnerDeletion: &blockOwnerDeletion,
Controller: &isController,
}
}

// NewClientConfigurationForBroker creates a new ClientConfiguration for connecting
// to the specified Broker
func NewClientConfigurationForBroker(broker *v1beta1.ClusterServiceBroker, authConfig *osb.AuthConfig) *osb.ClientConfiguration {
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/controller_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,8 @@ func (c *controller) injectServiceBinding(binding *v1beta1.ServiceBinding, crede
existingSecret, err := secretClient.Get(binding.Spec.SecretName, metav1.GetOptions{})
if err == nil {
// Update existing secret
if !IsControlledBy(existingSecret, binding) {
controllerRef := GetControllerOf(existingSecret)
if !metav1.IsControlledBy(existingSecret, binding) {
controllerRef := metav1.GetControllerOf(existingSecret)
return fmt.Errorf(`Secret "%s/%s" is not owned by ServiceBinding, controllerRef: %v`, binding.Namespace, existingSecret.Name, controllerRef)
}
existingSecret.Data = secretData
Expand All @@ -869,7 +869,7 @@ func (c *controller) injectServiceBinding(binding *v1beta1.ServiceBinding, crede
Name: binding.Spec.SecretName,
Namespace: binding.Namespace,
OwnerReferences: []metav1.OwnerReference{
*NewControllerRef(binding, bindingControllerKind),
*metav1.NewControllerRef(binding, bindingControllerKind),
},
},
Data: secretData,
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/controller_binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,11 @@ func TestReconcileServiceBindingWithParameters(t *testing.T) {
if !ok {
t.Fatal("couldn't convert secret into a corev1.Secret")
}
controllerRef := GetControllerOf(actionSecret)
controllerRef := metav1.GetControllerOf(actionSecret)
if controllerRef == nil || controllerRef.UID != updatedServiceBinding.UID {
t.Fatalf("Secret is not owned by the ServiceBinding: %v", controllerRef)
}
if !IsControlledBy(actionSecret, updatedServiceBinding) {
if !metav1.IsControlledBy(actionSecret, updatedServiceBinding) {
t.Fatal("Secret is not owned by the ServiceBinding")
}
if e, a := testServiceBindingSecretName, actionSecret.Name; e != a {
Expand Down

0 comments on commit f4cd181

Please sign in to comment.