@@ -20,7 +20,9 @@ import (
2020 "context"
2121 "encoding/json"
2222 "fmt"
23+ "net/http"
2324 "strings"
25+ "time"
2426
2527 "github.com/SAP/sap-btp-service-operator/internal/utils/logutils"
2628 "github.com/pkg/errors"
@@ -88,12 +90,6 @@ func (r *ServiceInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Requ
8890 if utils .IsMarkedForDeletion (serviceInstance .ObjectMeta ) {
8991 return r .deleteInstance (ctx , serviceInstance )
9092 }
91- if len (serviceInstance .GetConditions ()) == 0 {
92- err := utils .InitConditions (ctx , r .Client , serviceInstance )
93- if err != nil {
94- return ctrl.Result {}, err
95- }
96- }
9793
9894 // If stored hash is MD5 (32 chars) and we're now using SHA256 (64 chars),
9995 // perform one-time migration by updating the stored hash without triggering update
@@ -105,6 +101,42 @@ func (r *ServiceInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Requ
105101 return ctrl.Result {}, utils .UpdateStatus (ctx , r .Client , serviceInstance )
106102 }
107103
104+ smClient , err := r .GetSMClient (ctx , serviceInstance )
105+ if err != nil {
106+ log .Error (err , "failed to get sm client" )
107+ return utils .HandleOperationFailure (ctx , r .Client , serviceInstance , common .Unknown , err )
108+ }
109+ if len (serviceInstance .Status .InstanceID ) > 0 {
110+ if _ , err := smClient .GetInstanceByID (serviceInstance .Status .InstanceID , nil ); err != nil {
111+ var smError * sm.ServiceManagerError
112+ if ok := errors .As (err , & smError ); ok {
113+ if smError .StatusCode == http .StatusNotFound {
114+ log .Info (fmt .Sprintf ("instance %s not found in SM" , serviceInstance .Status .InstanceID ))
115+ condition := metav1.Condition {
116+ Type : common .ConditionReady ,
117+ Status : metav1 .ConditionFalse ,
118+ ObservedGeneration : serviceInstance .Generation ,
119+ LastTransitionTime : metav1 .NewTime (time .Now ()),
120+ Reason : common .ResourceNotFound ,
121+ Message : fmt .Sprintf (common .ResourceNotFoundMessageFormat , "instance" , serviceInstance .Status .InstanceID ),
122+ }
123+ serviceInstance .Status .Conditions = []metav1.Condition {condition }
124+ serviceInstance .Status .Ready = metav1 .ConditionFalse
125+ return ctrl.Result {}, utils .UpdateStatus (ctx , r .Client , serviceInstance )
126+ }
127+ }
128+ log .Error (err , fmt .Sprintf ("failed to get instance %s from SM" , serviceInstance .Status .InstanceID ))
129+ return ctrl.Result {}, err
130+ }
131+ }
132+
133+ if len (serviceInstance .GetConditions ()) == 0 {
134+ err := utils .InitConditions (ctx , r .Client , serviceInstance )
135+ if err != nil {
136+ return ctrl.Result {}, err
137+ }
138+ }
139+
108140 if len (serviceInstance .Status .OperationURL ) > 0 {
109141 // ongoing operation - poll status from SM
110142 return r .poll (ctx , serviceInstance )
@@ -126,12 +158,6 @@ func (r *ServiceInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Requ
126158 }
127159 }
128160
129- smClient , err := r .GetSMClient (ctx , serviceInstance )
130- if err != nil {
131- log .Error (err , "failed to get sm client" )
132- return utils .HandleOperationFailure (ctx , r .Client , serviceInstance , common .Unknown , err )
133- }
134-
135161 if serviceInstance .Status .InstanceID == "" {
136162 log .Info ("Instance ID is empty, checking if instance exist in SM" )
137163 smInstance , err := r .getInstanceForRecovery (ctx , smClient , serviceInstance )
0 commit comments