Skip to content

Commit

Permalink
fix issue Credential was deleted due to "Failed to update online stat…
Browse files Browse the repository at this point in the history
…us" IBM#217
  • Loading branch information
qibobo committed Oct 13, 2020
1 parent 5fa6398 commit 9618408
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
33 changes: 19 additions & 14 deletions controllers/binding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -278,7 +279,7 @@ func (r *BindingReconciler) Reconcile(request ctrl.Request) (ctrl.Result, error)
return r.updateStatusError(instance, bindingStateFailed, err)
}

return r.updateStatusOnline(session, instance, serviceInstance, serviceClassType)
return r.updateStatusOnline(session, instance)
}

// The KeyInstanceID has been set (or is still inProgress), verify that the key and secret still exist
Expand Down Expand Up @@ -313,7 +314,7 @@ func (r *BindingReconciler) Reconcile(request ctrl.Request) (ctrl.Result, error)
logt.Info("Error creating secret", instance.Name, err.Error())
return r.updateStatusError(instance, bindingStateFailed, err)
}
return r.updateStatusOnline(session, instance, serviceInstance, serviceClassType)
return r.updateStatusOnline(session, instance)
}

// The secret exists, make sure it has the right content
Expand All @@ -335,9 +336,9 @@ func (r *BindingReconciler) Reconcile(request ctrl.Request) (ctrl.Result, error)
logt.Info("Error re-creating secret", instance.Name, err.Error())
return r.updateStatusError(instance, bindingStateFailed, err)
}
return r.updateStatusOnline(session, instance, serviceInstance, serviceClassType)
return r.updateStatusOnline(session, instance)
}
return r.updateStatusOnline(session, instance, serviceInstance, serviceClassType)
return r.updateStatusOnline(session, instance)
}

func (r *BindingReconciler) getServiceInstance(instance *ibmcloudv1.Binding) (*ibmcloudv1.Service, error) {
Expand Down Expand Up @@ -507,19 +508,23 @@ func (r *BindingReconciler) createSecret(instance *ibmcloudv1.Binding, keyConten
return nil
}

func (r *BindingReconciler) updateStatusOnline(session *session.Session, instance *ibmcloudv1.Binding, serviceInstance *ibmcloudv1.Service, serviceClassType string) (ctrl.Result, error) {
instance.Status.State = bindingStateOnline
instance.Status.Message = bindingStateOnline
instance.Status.SecretName = getSecretName(instance)
err := r.Status().Update(context.Background(), instance)
if err != nil {
r.Log.Info("Failed to update online status, will delete external resource ", instance.ObjectMeta.Name, err.Error())
err = r.deleteCredentials(session, instance, serviceClassType)
func (r *BindingReconciler) updateStatusOnline(session *session.Session, instance *ibmcloudv1.Binding) (ctrl.Result, error) {
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
currentBindingInstance := &ibmcloudv1.Binding{}
err := r.Get(context.Background(), types.NamespacedName{Namespace: instance.Namespace, Name: instance.Name}, currentBindingInstance)
if err != nil {
r.Log.Info("Failed to delete external resource, operator state and external resource might be in an inconsistent state", instance.ObjectMeta.Name, err.Error())
r.Log.Error(err, "Failed to fetch binding instance", "namespace", instance.Namespace, "name", instance.Name)
return err
}
currentBindingInstance.Status.State = bindingStateOnline
currentBindingInstance.Status.Message = bindingStateOnline
currentBindingInstance.Status.SecretName = getSecretName(currentBindingInstance)
return r.Status().Update(context.Background(), currentBindingInstance)
})
if err != nil {
r.Log.Error(err, "Failed to update binding instance after retry", "namespace", instance.Namespace, "name", instance.Name)
return ctrl.Result{Requeue: true}, nil
}

return ctrl.Result{Requeue: true, RequeueAfter: config.Get().SyncPeriod}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/binding_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2277,7 +2277,7 @@ func TestBindingUpdateStatusOnlineFailed(t *testing.T) {
},
}

result, err := r.updateStatusOnline(nil, binding, service, "")
result, err := r.updateStatusOnline(nil, binding)
assert.Equal(t, ctrl.Result{
Requeue: true,
RequeueAfter: config.Get().SyncPeriod,
Expand Down

0 comments on commit 9618408

Please sign in to comment.