Skip to content

Commit

Permalink
Adding '-binding' suffix to autoDetect default name and embedded name
Browse files Browse the repository at this point in the history
  • Loading branch information
navidsh committed May 15, 2020
1 parent 5454e9e commit e648d5a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
20 changes: 13 additions & 7 deletions pkg/controller/runtimecomponent/enqueue_with_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runtimecomponent

import (
"context"
"strings"

appstacksv1beta1 "github.com/application-stacks/runtime-component-operator/pkg/apis/appstacks/v1beta1"
appstacksutils "github.com/application-stacks/runtime-component-operator/pkg/utils"
Expand All @@ -22,6 +23,7 @@ var _ handler.EventHandler = &EnqueueRequestsForCustomIndexField{}
const (
indexFieldImageStreamName = "spec.applicationImage"
indexFieldBindingsResourceRef = "spec.bindings.resourceRef"
bindingSecretSuffix = "-binding"
)

// EnqueueRequestsForCustomIndexField enqueues reconcile Requests Runtime Components if the app is relying on
Expand Down Expand Up @@ -120,13 +122,17 @@ func (b *BindingSecretMatcher) Match(secret metav1.Object) ([]appstacksv1beta1.R
}
apps = append(apps, appList.Items...)

// If we are able to find an app with the secret name, add the app. This is to cover the autoDetect scenario
app := &appstacksv1beta1.RuntimeComponent{}
err = b.klient.Get(context.Background(), types.NamespacedName{Name: secret.GetName(), Namespace: secret.GetNamespace()}, app)
if err == nil {
apps = append(apps, *app)
} else if !kerrors.IsNotFound(err) {
return nil, err
if strings.HasSuffix(secret.GetName(), bindingSecretSuffix) {
appName := strings.TrimSuffix(secret.GetName(), bindingSecretSuffix)
// If we are able to find an app with the secret name, add the app. This is to cover the autoDetect scenario
app := &appstacksv1beta1.RuntimeComponent{}
err = b.klient.Get(context.Background(), types.NamespacedName{Name: appName, Namespace: secret.GetNamespace()}, app)
if err == nil {
apps = append(apps, *app)
} else if !kerrors.IsNotFound(err) {
return nil, err
}
}

return apps, nil
}
33 changes: 17 additions & 16 deletions pkg/utils/service_binding_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,18 +300,16 @@ func (r *ReconcilerBase) reconcileExternals(ba common.BaseComponent) (retRes rec
return r.requeueError(ba, err)
}
} else if ba.GetBindings() == nil || ba.GetBindings().GetAutoDetect() == nil || *ba.GetBindings().GetAutoDetect() {
bindingName := mObj.GetName()
bindingName := getDefaultServiceBindingName(ba)
key := types.NamespacedName{Name: bindingName, Namespace: mObj.GetNamespace()}

for _, gvk := range r.getServiceBindingGVK() {
// Using a unstructured object to find ServiceBinding CR since GVK might change
bindingObj := &unstructured.Unstructured{}
bindingObj.SetGroupVersionKind(gvk)
err := r.client.Get(context.Background(), key, bindingObj)
if err != nil {
if !kerrors.IsNotFound(err) {
log.Error(errors.Wrapf(err, "failed to find a service binding resource during auto-detect for GVK %q", gvk), "failed to get Service Binding CR")
}
if client.IgnoreNotFound(err) != nil {
log.Error(errors.Wrapf(err, "failed to find a service binding resource during auto-detect for GVK %q", gvk), "failed to get Service Binding CR")
continue
}

Expand All @@ -320,7 +318,7 @@ func (r *ReconcilerBase) reconcileExternals(ba common.BaseComponent) (retRes rec
if err == nil {
resolvedBindings = append(resolvedBindings, bindingName)
break
} else if err != nil && kerrors.IsNotFound(err) {
} else {
err = errors.Wrapf(err, "service binding dependency not satisfied: unable to find service binding secret for external binding %q in namespace %q", bindingName, mObj.GetNamespace())
return r.requeueError(ba, err)
}
Expand Down Expand Up @@ -424,7 +422,7 @@ func (r *ReconcilerBase) cleanUpEmbeddedBindings(ba common.BaseComponent) error
for _, gvk := range r.getServiceBindingGVK() {
bindingObj := &unstructured.Unstructured{}
bindingObj.SetGroupVersionKind(gvk)
key := types.NamespacedName{Name: mObj.GetName(), Namespace: mObj.GetNamespace()}
key := types.NamespacedName{Name: getDefaultServiceBindingName(ba), Namespace: mObj.GetNamespace()}
err := r.client.Get(context.Background(), key, bindingObj)
if err == nil && metav1.IsControlledBy(bindingObj, mObj) {
err = r.client.Delete(context.Background(), bindingObj)
Expand All @@ -438,8 +436,8 @@ func (r *ReconcilerBase) cleanUpEmbeddedBindings(ba common.BaseComponent) error
return nil
}

// reconcileEmbedded reconciles embedded blob in bindings.embedded to create or update Service Binding resource
func (r *ReconcilerBase) reconcileEmbedded(ba common.BaseComponent) (retRes reconcile.Result, retErr error) {
mObj := ba.(metav1.Object)
var resolvedBindings []string

object, err := r.toJSONFromRaw(ba.GetBindings().GetEmbedded())
Expand All @@ -465,17 +463,17 @@ func (r *ReconcilerBase) reconcileEmbedded(ba common.BaseComponent) (retRes reco

err = r.createOrUpdateEmbedded(embedded, ba)
if err != nil {
return r.requeueError(ba, errors.Wrapf(err, "failed: cannot create or update embedded Service Binding resource %q in namespace %q", mObj.GetName(), mObj.GetNamespace()))
return r.requeueError(ba, errors.Wrapf(err, "failed: cannot create or update embedded Service Binding resource %q in namespace %q", embedded.GetName(), embedded.GetNamespace()))
}

// Get binding secret and add it to status field. If binding hasn't been successful, secret won't be created and it keeps trying
key := types.NamespacedName{Name: mObj.GetName(), Namespace: mObj.GetNamespace()}
key := types.NamespacedName{Name: embedded.GetName(), Namespace: embedded.GetNamespace()}
bindingSecret := &corev1.Secret{}
err = r.GetClient().Get(context.TODO(), key, bindingSecret)
if err == nil {
resolvedBindings = append(resolvedBindings, mObj.GetName())
resolvedBindings = append(resolvedBindings, embedded.GetName())
} else {
err = errors.Wrapf(err, "service binding dependency not satisfied: unable to find service binding secret for embedded binding %q in namespace %q", mObj.GetName(), mObj.GetNamespace())
err = errors.Wrapf(err, "service binding dependency not satisfied: unable to find service binding secret for embedded binding %q in namespace %q", embedded.GetName(), embedded.GetNamespace())
return r.requeueError(ba, err)
}

Expand All @@ -500,12 +498,11 @@ func (r *ReconcilerBase) updateBindingStatus(bindings []string, ba common.BaseCo
}

func (r *ReconcilerBase) createOrUpdateEmbedded(embedded *unstructured.Unstructured, ba common.BaseComponent) error {
mObj := ba.(metav1.Object)
result := controllerutil.OperationResultNone
existing := &unstructured.Unstructured{}
existing.SetAPIVersion(embedded.GetAPIVersion())
existing.SetKind(embedded.GetKind())
key := types.NamespacedName{Name: mObj.GetName(), Namespace: mObj.GetNamespace()}
key := types.NamespacedName{Name: embedded.GetName(), Namespace: embedded.GetNamespace()}
err := r.client.Get(context.TODO(), key, existing)
if err != nil {
if kerrors.IsNotFound(err) {
Expand Down Expand Up @@ -537,7 +534,7 @@ func (r *ReconcilerBase) createOrUpdateEmbedded(embedded *unstructured.Unstructu
}
}

log.Info("Reconciled", "Kind", embedded.GetKind(), "Namespace", mObj.GetNamespace(), "Name", mObj.GetName(), "Status", result)
log.Info("Reconciled", "Kind", embedded.GetKind(), "Namespace", embedded.GetNamespace(), "Name", embedded.GetName(), "Status", result)
return nil
}

Expand All @@ -559,7 +556,7 @@ func (r *ReconcilerBase) updateEmbeddedObject(object map[string]interface{}, emb
if _, ok := object[Metadata]; ok {
return errors.New("failed: embedded Service Binding must not have a 'metadata' section")
}
embedded.SetName(mObj.GetName())
embedded.SetName(getDefaultServiceBindingName(ba))
embedded.SetNamespace(mObj.GetNamespace())
embedded.SetLabels(mObj.GetLabels())
embedded.SetAnnotations(mObj.GetAnnotations())
Expand Down Expand Up @@ -591,3 +588,7 @@ func (r *ReconcilerBase) updateEmbeddedObject(object map[string]interface{}, emb
embedded.SetAPIVersion(apiVersion.(string))
return nil
}

func getDefaultServiceBindingName(ba common.BaseComponent) string {
return (ba.(metav1.Object)).GetName() + "-bindings"
}
1 change: 1 addition & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"k8s.io/apimachinery/pkg/util/intstr"
)


// CustomizeDeployment ...
func CustomizeDeployment(deploy *appsv1.Deployment, ba common.BaseComponent) {
obj := ba.(metav1.Object)
Expand Down

0 comments on commit e648d5a

Please sign in to comment.