4
4
"context"
5
5
"fmt"
6
6
7
- "github.com/ydb-platform/ydb-go-sdk/v3"
7
+ ydb "github.com/ydb-platform/ydb-go-sdk/v3"
8
8
corev1 "k8s.io/api/core/v1"
9
9
apierrors "k8s.io/apimachinery/pkg/api/errors"
10
10
"k8s.io/apimachinery/pkg/api/meta"
@@ -72,62 +72,71 @@ func (r *Reconciler) setInitDatabaseCompleted(
72
72
return r .updateStatus (ctx , database , StatusUpdateRequeueDelay )
73
73
}
74
74
75
- func (r * Reconciler ) checkCreateTenantOperation (
75
+ func (r * Reconciler ) checkCreateDatabaseOperation (
76
76
ctx context.Context ,
77
77
database * resources.DatabaseBuilder ,
78
78
tenant * cms.Tenant ,
79
79
ydbOptions ydb.Option ,
80
80
) (bool , ctrl.Result , error ) {
81
81
condition := meta .FindStatusCondition (database .Status .Conditions , CreateDatabaseOperationCondition )
82
- if condition == nil || len (condition .Message ) == 0 {
82
+ if len (condition .Message ) == 0 {
83
83
// Something is wrong with the condition where we save operation id
84
84
// retry create tenant
85
85
meta .SetStatusCondition (& database .Status .Conditions , metav1.Condition {
86
86
Type : CreateDatabaseOperationCondition ,
87
- Status : metav1 .ConditionTrue ,
88
- Reason : ReasonNotRequired ,
87
+ Status : metav1 .ConditionFalse ,
88
+ Reason : ReasonFailed ,
89
89
})
90
90
return r .updateStatus (ctx , database , DatabaseInitializationRequeueDelay )
91
91
}
92
- operationID := condition .Message
93
- finished , operationErr , err := tenant .CheckCreateOperation (ctx , operationID , ydbOptions )
92
+
93
+ operation := & cms.Operation {
94
+ StorageEndpoint : tenant .StorageEndpoint ,
95
+ Domain : tenant .Domain ,
96
+ Id : condition .Message ,
97
+ }
98
+ response , err := operation .GetOperation (ctx , ydbOptions )
94
99
if err != nil {
95
100
r .Recorder .Event (
96
101
database ,
97
102
corev1 .EventTypeWarning ,
98
103
"InitializingFailed" ,
99
- fmt .Sprintf ("Error creating tenant %s: %s" , tenant . Path , err ),
104
+ fmt .Sprintf ("Failed to check creation operation, operationID %s: %s" , operation . Id , err ),
100
105
)
101
106
return Stop , ctrl.Result {RequeueAfter : DatabaseInitializationRequeueDelay }, err
102
107
}
103
- if operationErr != nil {
104
- // Creation operation failed - retry Create Tenant
108
+
109
+ finished , operationID , err := operation .CheckGetOperationResponse (ctx , response )
110
+ if err != nil {
105
111
r .Recorder .Event (
106
112
database ,
107
113
corev1 .EventTypeWarning ,
108
114
"InitializingFailed" ,
109
- fmt .Sprintf ("Error creating tenant %s: %s" , tenant .Path , operationErr ),
115
+ fmt .Sprintf ("Error creating tenant %s: %s" , tenant .Path , err ),
110
116
)
111
117
meta .SetStatusCondition (& database .Status .Conditions , metav1.Condition {
112
- Type : CreateDatabaseOperationCondition ,
113
- Status : metav1 .ConditionTrue ,
114
- Reason : ReasonNotRequired ,
118
+ Type : CreateDatabaseOperationCondition ,
119
+ Status : metav1 .ConditionFalse ,
120
+ Reason : ReasonFailed ,
121
+ Message : fmt .Sprintf ("Failed to create tenant %s" , tenant .Path ),
115
122
})
116
- return r . updateStatus ( ctx , database , DatabaseInitializationRequeueDelay )
123
+ return Stop , ctrl. Result { RequeueAfter : DatabaseInitializationRequeueDelay }, err
117
124
}
125
+
118
126
if ! finished {
119
127
r .Recorder .Event (
120
128
database ,
121
129
corev1 .EventTypeWarning ,
122
- "Pending" ,
130
+ string ( DatabaseInitializing ) ,
123
131
fmt .Sprintf ("Tenant creation operation is not completed, operationID: %s" , operationID ),
124
132
)
125
133
return Stop , ctrl.Result {RequeueAfter : DatabaseInitializationRequeueDelay }, nil
126
134
}
135
+
127
136
r .Recorder .Event (
128
137
database ,
129
138
corev1 .EventTypeNormal ,
130
- "Initialized" ,
139
+ string ( DatabaseInitializing ) ,
131
140
fmt .Sprintf ("Tenant %s created" , tenant .Path ),
132
141
)
133
142
return r .setInitDatabaseCompleted (ctx , database , "Database initialized successfully" )
@@ -239,9 +248,10 @@ func (r *Reconciler) initializeTenant(
239
248
ydbOpts := ydb .MergeOptions (ydb .WithCredentials (creds ), tlsOptions )
240
249
241
250
if meta .IsStatusConditionFalse (database .Status .Conditions , CreateDatabaseOperationCondition ) {
242
- return r .checkCreateTenantOperation (ctx , database , tenant , ydbOpts )
251
+ return r .checkCreateDatabaseOperation (ctx , database , tenant , ydbOpts )
243
252
}
244
- operationID , err := tenant .Create (ctx , ydb .WithCredentials (creds ), tlsOptions )
253
+
254
+ response , err := tenant .CreateDatabase (ctx , ydbOpts )
245
255
if err != nil {
246
256
r .Recorder .Event (
247
257
database ,
@@ -251,11 +261,23 @@ func (r *Reconciler) initializeTenant(
251
261
)
252
262
return Stop , ctrl.Result {RequeueAfter : DatabaseInitializationRequeueDelay }, err
253
263
}
254
- if len (operationID ) > 0 {
264
+
265
+ finished , operationID , err := tenant .CheckCreateDatabaseResponse (ctx , response )
266
+ if err != nil {
267
+ r .Recorder .Event (
268
+ database ,
269
+ corev1 .EventTypeWarning ,
270
+ "InitializingFailed" ,
271
+ fmt .Sprintf ("Failed %s: %s" , tenant .Path , err ),
272
+ )
273
+ return Stop , ctrl.Result {RequeueAfter : DatabaseInitializationRequeueDelay }, err
274
+ }
275
+
276
+ if ! finished {
255
277
r .Recorder .Event (
256
278
database ,
257
279
corev1 .EventTypeWarning ,
258
- "Pending" ,
280
+ string ( DatabaseInitializing ) ,
259
281
fmt .Sprintf ("Tenant creation operation in progress, operationID: %s" , operationID ),
260
282
)
261
283
meta .SetStatusCondition (& database .Status .Conditions , metav1.Condition {
@@ -269,7 +291,7 @@ func (r *Reconciler) initializeTenant(
269
291
r .Recorder .Event (
270
292
database ,
271
293
corev1 .EventTypeNormal ,
272
- "Initialized" ,
294
+ string ( DatabaseInitializing ) ,
273
295
fmt .Sprintf ("Tenant %s created" , tenant .Path ),
274
296
)
275
297
0 commit comments