@@ -26,16 +26,14 @@ import (
2626
2727 smTypes "github.com/Peripli/service-manager/pkg/types"
2828 "github.com/Peripli/service-manager/pkg/web"
29- servicesv1alpha1 "github.com/SAP/sap-btp-service-operator/api/v1alpha1"
29+ "github.com/SAP/sap-btp-service-operator/api/v1alpha1"
3030 "github.com/SAP/sap-btp-service-operator/client/sm/types"
3131 "github.com/go-logr/logr"
3232 apierrors "k8s.io/apimachinery/pkg/api/errors"
3333 ctrl "sigs.k8s.io/controller-runtime"
3434 "sigs.k8s.io/controller-runtime/pkg/client"
3535)
3636
37- const instanceFinalizerName string = "services.cloud.sap.com/instance-finalizer"
38-
3937// ServiceInstanceReconciler reconciles a ServiceInstance object
4038type ServiceInstanceReconciler struct {
4139 * BaseReconciler
@@ -48,7 +46,7 @@ type ServiceInstanceReconciler struct {
4846func (r * ServiceInstanceReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
4947 log := r .Log .WithValues ("serviceinstance" , req .NamespacedName )
5048
51- serviceInstance := & servicesv1alpha1 .ServiceInstance {}
49+ serviceInstance := & v1alpha1 .ServiceInstance {}
5250 if err := r .Get (ctx , req .NamespacedName , serviceInstance ); err != nil {
5351 if ! apierrors .IsNotFound (err ) {
5452 log .Error (err , "unable to fetch ServiceInstance" )
@@ -60,6 +58,13 @@ func (r *ServiceInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Requ
6058 }
6159 serviceInstance = serviceInstance .DeepCopy ()
6260
61+ if len (serviceInstance .GetConditions ()) == 0 {
62+ err := r .init (ctx , log , serviceInstance )
63+ if err != nil {
64+ return ctrl.Result {}, err
65+ }
66+ }
67+
6368 smClient , err := r .getSMClient (ctx , log , serviceInstance )
6469 if err != nil {
6570 setFailureConditions (smTypes .CREATE , err .Error (), serviceInstance )
@@ -77,13 +82,6 @@ func (r *ServiceInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Requ
7782 if isDelete (serviceInstance .ObjectMeta ) {
7883 return r .deleteInstance (ctx , smClient , serviceInstance , log )
7984 }
80- // The object is not being deleted, so if it does not have our finalizer,
81- // then lets init it
82- if ! controllerutil .ContainsFinalizer (serviceInstance , instanceFinalizerName ) {
83- if err := r .init (ctx , instanceFinalizerName , log , serviceInstance ); err != nil {
84- return ctrl.Result {}, err
85- }
86- }
8785
8886 if serviceInstance .Generation == serviceInstance .Status .ObservedGeneration && ! isInProgress (serviceInstance ) {
8987 log .Info (fmt .Sprintf ("Spec is not changed - ignoring... Generation is - %v" , serviceInstance .Generation ))
@@ -119,14 +117,14 @@ func (r *ServiceInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Requ
119117 return r .updateInstance (ctx , smClient , serviceInstance , log )
120118}
121119
122- func (r * ServiceInstanceReconciler ) poll (ctx context.Context , smClient sm.Client , serviceInstance * servicesv1alpha1 .ServiceInstance , log logr.Logger ) (ctrl.Result , error ) {
120+ func (r * ServiceInstanceReconciler ) poll (ctx context.Context , smClient sm.Client , serviceInstance * v1alpha1 .ServiceInstance , log logr.Logger ) (ctrl.Result , error ) {
123121 log .Info (fmt .Sprintf ("resource is in progress, found operation url %s" , serviceInstance .Status .OperationURL ))
124122 status , statusErr := smClient .Status (serviceInstance .Status .OperationURL , nil )
125123 if statusErr != nil {
126124 log .Info (fmt .Sprintf ("failed to fetch operation, got error from SM: %s" , statusErr .Error ()), "operationURL" , serviceInstance .Status .OperationURL )
127125 setFailureConditions (serviceInstance .Status .OperationType , statusErr .Error (), serviceInstance )
128126 // if failed to read operation status we cleanup the status to trigger re-sync from SM
129- freshStatus := servicesv1alpha1 .ServiceInstanceStatus {Conditions : serviceInstance .GetConditions ()}
127+ freshStatus := v1alpha1 .ServiceInstanceStatus {Conditions : serviceInstance .GetConditions ()}
130128 if isDelete (serviceInstance .ObjectMeta ) {
131129 freshStatus .InstanceID = serviceInstance .Status .InstanceID
132130 }
@@ -161,7 +159,7 @@ func (r *ServiceInstanceReconciler) poll(ctx context.Context, smClient sm.Client
161159 setSuccessConditions (smTypes .OperationCategory (status .Type ), serviceInstance )
162160 if serviceInstance .Status .OperationType == smTypes .DELETE {
163161 // delete was successful - remove our finalizer from the list and update it.
164- if err := r .removeFinalizer (ctx , serviceInstance , instanceFinalizerName , log ); err != nil {
162+ if err := r .removeFinalizer (ctx , serviceInstance , v1alpha1 . FinalizerName , log ); err != nil {
165163 return ctrl.Result {}, err
166164 }
167165 }
@@ -173,7 +171,7 @@ func (r *ServiceInstanceReconciler) poll(ctx context.Context, smClient sm.Client
173171 return ctrl.Result {}, r .updateStatusWithRetries (ctx , serviceInstance , log )
174172}
175173
176- func (r * ServiceInstanceReconciler ) createInstance (ctx context.Context , smClient sm.Client , serviceInstance * servicesv1alpha1 .ServiceInstance , log logr.Logger ) (ctrl.Result , error ) {
174+ func (r * ServiceInstanceReconciler ) createInstance (ctx context.Context , smClient sm.Client , serviceInstance * v1alpha1 .ServiceInstance , log logr.Logger ) (ctrl.Result , error ) {
177175 log .Info ("Creating instance in SM" )
178176 _ , instanceParameters , err := buildParameters (r .Client , serviceInstance .Namespace , serviceInstance .Spec .ParametersFrom , serviceInstance .Spec .Parameters )
179177 if err != nil {
@@ -221,7 +219,7 @@ func (r *ServiceInstanceReconciler) createInstance(ctx context.Context, smClient
221219 return ctrl.Result {}, r .updateStatusWithRetries (ctx , serviceInstance , log )
222220}
223221
224- func (r * ServiceInstanceReconciler ) updateInstance (ctx context.Context , smClient sm.Client , serviceInstance * servicesv1alpha1 .ServiceInstance , log logr.Logger ) (ctrl.Result , error ) {
222+ func (r * ServiceInstanceReconciler ) updateInstance (ctx context.Context , smClient sm.Client , serviceInstance * v1alpha1 .ServiceInstance , log logr.Logger ) (ctrl.Result , error ) {
225223 var err error
226224
227225 log .Info (fmt .Sprintf ("updating instance %s in SM" , serviceInstance .Status .InstanceID ))
@@ -261,8 +259,8 @@ func (r *ServiceInstanceReconciler) updateInstance(ctx context.Context, smClient
261259 return ctrl.Result {}, r .updateStatusWithRetries (ctx , serviceInstance , log )
262260}
263261
264- func (r * ServiceInstanceReconciler ) deleteInstance (ctx context.Context , smClient sm.Client , serviceInstance * servicesv1alpha1 .ServiceInstance , log logr.Logger ) (ctrl.Result , error ) {
265- if controllerutil .ContainsFinalizer (serviceInstance , instanceFinalizerName ) {
262+ func (r * ServiceInstanceReconciler ) deleteInstance (ctx context.Context , smClient sm.Client , serviceInstance * v1alpha1 .ServiceInstance , log logr.Logger ) (ctrl.Result , error ) {
263+ if controllerutil .ContainsFinalizer (serviceInstance , v1alpha1 . FinalizerName ) {
266264 if len (serviceInstance .Status .InstanceID ) == 0 {
267265 log .Info ("No instance id found validating instance does not exists in SM before removing finalizer" )
268266
@@ -277,7 +275,7 @@ func (r *ServiceInstanceReconciler) deleteInstance(ctx context.Context, smClient
277275 return ctrl.Result {}, r .updateStatusWithRetries (ctx , serviceInstance , log )
278276 }
279277 log .Info ("instance does not exists in SM, removing finalizer" )
280- return ctrl.Result {}, r .removeFinalizer (ctx , serviceInstance , instanceFinalizerName , log )
278+ return ctrl.Result {}, r .removeFinalizer (ctx , serviceInstance , v1alpha1 . FinalizerName , log )
281279 }
282280
283281 log .Info (fmt .Sprintf ("Deleting instance with id %v from SM" , serviceInstance .Status .InstanceID ))
@@ -314,7 +312,7 @@ func (r *ServiceInstanceReconciler) deleteInstance(ctx context.Context, smClient
314312 }
315313
316314 // remove our finalizer from the list and update it.
317- if err := r .removeFinalizer (ctx , serviceInstance , instanceFinalizerName , log ); err != nil {
315+ if err := r .removeFinalizer (ctx , serviceInstance , v1alpha1 . FinalizerName , log ); err != nil {
318316 return ctrl.Result {}, err
319317 }
320318
@@ -325,7 +323,7 @@ func (r *ServiceInstanceReconciler) deleteInstance(ctx context.Context, smClient
325323 return ctrl.Result {}, nil
326324}
327325
328- func (r * ServiceInstanceReconciler ) resyncInstanceStatus (k8sInstance * servicesv1alpha1 .ServiceInstance , smInstance * types.ServiceInstance ) {
326+ func (r * ServiceInstanceReconciler ) resyncInstanceStatus (k8sInstance * v1alpha1 .ServiceInstance , smInstance * types.ServiceInstance ) {
329327 //set observed generation to 0 because we dont know which generation the current state in SM represents,
330328 //unless the generation is 1 and SM is in the same state as operator
331329 if k8sInstance .Generation == 1 {
@@ -353,11 +351,11 @@ func (r *ServiceInstanceReconciler) resyncInstanceStatus(k8sInstance *servicesv1
353351
354352func (r * ServiceInstanceReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
355353 return ctrl .NewControllerManagedBy (mgr ).
356- For (& servicesv1alpha1 .ServiceInstance {}).
354+ For (& v1alpha1 .ServiceInstance {}).
357355 Complete (r )
358356}
359357
360- func (r * ServiceInstanceReconciler ) getInstanceForRecovery (smClient sm.Client , serviceInstance * servicesv1alpha1 .ServiceInstance , log logr.Logger ) (* types.ServiceInstance , error ) {
358+ func (r * ServiceInstanceReconciler ) getInstanceForRecovery (smClient sm.Client , serviceInstance * v1alpha1 .ServiceInstance , log logr.Logger ) (* types.ServiceInstance , error ) {
361359 parameters := sm.Parameters {
362360 FieldQuery : []string {
363361 fmt .Sprintf ("name eq '%s'" , serviceInstance .Spec .ExternalName ),
0 commit comments