@@ -27,7 +27,22 @@ const (
27
27
HealthcheckRequeueDelay = 30 * time .Second
28
28
StorageInitializationRequeueDelay = 30 * time .Second
29
29
30
- StorageInitializedCondition = "StorageInitialized"
30
+ StorageInitializedCondition = "StorageInitialized"
31
+ StorageInitializedReasonInProgress = "InProgres"
32
+ StorageInitializedReasonCompleted = "Completed"
33
+
34
+ InitStorageStepCondition = "InitStorageStep"
35
+ InitStorageStepReasonInProgress = "InProgres"
36
+ InitStorageStepReasonCompleted = "Completed"
37
+
38
+ InitRootStorageStepCondition = "InitRootStorageStep"
39
+ InitRootStorageStepReasonInProgress = "InProgres"
40
+ InitRootStorageStepReasonNotRequired = "NotRequired"
41
+ InitRootStorageStepReasonCompleted = "Completed"
42
+
43
+ InitCMSStepCondition = "InitCMSStep"
44
+ InitCMSStepReasonInProgress = "InProgres"
45
+ InitCMSStepReasonCompleted = "Completed"
31
46
)
32
47
33
48
func (r * StorageReconciler ) Sync (ctx context.Context , cr * ydbv1alpha1.Storage ) (ctrl.Result , error ) {
@@ -53,6 +68,10 @@ func (r *StorageReconciler) Sync(ctx context.Context, cr *ydbv1alpha1.Storage) (
53
68
}
54
69
55
70
if ! meta .IsStatusConditionTrue (storage .Status .Conditions , StorageInitializedCondition ) {
71
+ result , err = r .setInitialStatus (ctx , & storage )
72
+ if err != nil || ! result .IsZero () {
73
+ return result , err
74
+ }
56
75
result , err = r .runInitScripts (ctx , & storage )
57
76
if err != nil || ! result .IsZero () {
58
77
return result , err
@@ -65,31 +84,63 @@ func (r *StorageReconciler) Sync(ctx context.Context, cr *ydbv1alpha1.Storage) (
65
84
func (r * StorageReconciler ) runInitScripts (ctx context.Context , storage * resources.StorageClusterBuilder ) (ctrl.Result , error ) {
66
85
podName := fmt .Sprintf ("%s-0" , storage .Name )
67
86
68
- cmd := []string {"/bin/bash" , "/opt/kikimr/cfg/init_storage.bash" }
69
- _ , _ , err := exec .ExecInPod (r .Scheme , r .Config , storage .Namespace , podName , "ydb-storage" , cmd )
70
- if err != nil {
71
- return controllers .RequeueAfter (StorageInitializationRequeueDelay , err )
87
+ if ! meta .IsStatusConditionTrue (storage .Status .Conditions , InitStorageStepCondition ) {
88
+ cmd := []string {"/bin/bash" , "/opt/kikimr/cfg/init_storage.bash" }
89
+ _ , _ , err := exec .ExecInPod (r .Scheme , r .Config , storage .Namespace , podName , "ydb-storage" , cmd )
90
+ if err != nil {
91
+ return controllers .RequeueAfter (StorageInitializationRequeueDelay , err )
92
+ }
93
+ meta .SetStatusCondition (& storage .Status .Conditions , metav1.Condition {
94
+ Type : InitStorageStepCondition ,
95
+ Status : "True" ,
96
+ Reason : InitStorageStepReasonCompleted ,
97
+ Message : "InitStorageStep completed successfully" ,
98
+ })
99
+ if _ , err := r .setState (ctx , storage ); err != nil {
100
+ return controllers .NoRequeue (err )
101
+ }
72
102
}
73
103
74
- cmd = []string {"/bin/bash" , "-c" , "test -s /opt/kikimr/cfg/init_root_storage.bash && source /opt/kikimr/cfg/init_root_storage.bash" }
75
- _ , _ , err = exec .ExecInPod (r .Scheme , r .Config , storage .Namespace , podName , "ydb-storage" , cmd )
76
- if err != nil {
77
- return controllers .RequeueAfter (StorageInitializationRequeueDelay , err )
104
+ if ! meta .IsStatusConditionTrue (storage .Status .Conditions , InitRootStorageStepCondition ) {
105
+ cmd := []string {"/bin/bash" , "/opt/kikimr/cfg/init_root_storage.bash" }
106
+ _ , _ , err := exec .ExecInPod (r .Scheme , r .Config , storage .Namespace , podName , "ydb-storage" , cmd )
107
+ if err != nil {
108
+ return controllers .RequeueAfter (StorageInitializationRequeueDelay , err )
109
+ }
110
+ meta .SetStatusCondition (& storage .Status .Conditions , metav1.Condition {
111
+ Type : InitRootStorageStepCondition ,
112
+ Status : "True" ,
113
+ Reason : InitRootStorageStepReasonCompleted ,
114
+ Message : "InitRootStorageStep completed successfully" ,
115
+ })
116
+ if _ , err := r .setState (ctx , storage ); err != nil {
117
+ return controllers .NoRequeue (err )
118
+ }
78
119
}
79
120
80
- cmd = []string {"/bin/bash" , "/opt/kikimr/cfg/init_cms.bash" }
81
- _ , _ , err = exec .ExecInPod (r .Scheme , r .Config , storage .Namespace , podName , "ydb-storage" , cmd )
82
- if err != nil {
83
- return controllers .RequeueAfter (StorageInitializationRequeueDelay , err )
121
+ if ! meta .IsStatusConditionTrue (storage .Status .Conditions , InitCMSStepCondition ) {
122
+ cmd := []string {"/bin/bash" , "/opt/kikimr/cfg/init_cms.bash" }
123
+ _ , _ , err := exec .ExecInPod (r .Scheme , r .Config , storage .Namespace , podName , "ydb-storage" , cmd )
124
+ if err != nil {
125
+ return controllers .RequeueAfter (StorageInitializationRequeueDelay , err )
126
+ }
127
+ meta .SetStatusCondition (& storage .Status .Conditions , metav1.Condition {
128
+ Type : InitCMSStepCondition ,
129
+ Status : "True" ,
130
+ Reason : InitCMSStepReasonCompleted ,
131
+ Message : "InitCMSStep completed successfully" ,
132
+ })
133
+ if _ , err := r .setState (ctx , storage ); err != nil {
134
+ return controllers .NoRequeue (err )
135
+ }
84
136
}
85
137
86
- resourcesProvided := metav1.Condition {
138
+ meta . SetStatusCondition ( & storage . Status . Conditions , metav1.Condition {
87
139
Type : StorageInitializedCondition ,
88
140
Status : "True" ,
89
- Reason : "StorageInitialized" ,
141
+ Reason : StorageInitializedReasonCompleted ,
90
142
Message : "Storage initialized successfully" ,
91
- }
92
- meta .SetStatusCondition (& storage .Status .Conditions , resourcesProvided )
143
+ })
93
144
if _ , err := r .setState (ctx , storage ); err != nil {
94
145
return controllers .NoRequeue (err )
95
146
}
@@ -173,6 +224,85 @@ func (r *StorageReconciler) waitForStatefulSetToScale(ctx context.Context, stora
173
224
return controllers .Ok ()
174
225
}
175
226
227
+ func (r * StorageReconciler ) setInitialStatus (ctx context.Context , storage * resources.StorageClusterBuilder ) (ctrl.Result , error ) {
228
+ meta .SetStatusCondition (& storage .Status .Conditions , metav1.Condition {
229
+ Type : StorageInitializedCondition ,
230
+ Status : "False" ,
231
+ Reason : StorageInitializedReasonInProgress ,
232
+ Message : "Storage is not initialized" ,
233
+ })
234
+ if _ , err := r .setState (ctx , storage ); err != nil {
235
+ return controllers .NoRequeue (err )
236
+ }
237
+
238
+ configMapName := storage .Name
239
+ if storage .Spec .ClusterConfig != "" {
240
+ configMapName = storage .Spec .ClusterConfig
241
+ }
242
+
243
+ configMap := & corev1.ConfigMap {}
244
+ err := r .Get (ctx , types.NamespacedName {
245
+ Name : configMapName ,
246
+ Namespace : storage .Namespace ,
247
+ }, configMap )
248
+
249
+ if err != nil && errors .IsNotFound (err ) {
250
+ return controllers .Ok ()
251
+ } else if err != nil {
252
+ r .Recorder .Event (
253
+ storage ,
254
+ corev1 .EventTypeNormal ,
255
+ "Syncing" ,
256
+ fmt .Sprintf ("Failed to get ConfigMap: %s" , err ),
257
+ )
258
+ return controllers .NoRequeue (err )
259
+ }
260
+
261
+ meta .SetStatusCondition (& storage .Status .Conditions , metav1.Condition {
262
+ Type : InitStorageStepCondition ,
263
+ Status : "False" ,
264
+ Reason : InitStorageStepReasonInProgress ,
265
+ Message : "InitStorageStep is required" ,
266
+ })
267
+ if _ , err := r .setState (ctx , storage ); err != nil {
268
+ return controllers .NoRequeue (err )
269
+ }
270
+
271
+ if _ , ok := configMap .Data ["init_root_storage.bash" ]; ok {
272
+ meta .SetStatusCondition (& storage .Status .Conditions , metav1.Condition {
273
+ Type : InitRootStorageStepCondition ,
274
+ Status : "False" ,
275
+ Reason : InitRootStorageStepReasonInProgress ,
276
+ Message : fmt .Sprintf ("InitRootStorageStep (init_root_storage.bash script) is specified in ConfigMap: %s" , configMapName ),
277
+ })
278
+ if _ , err := r .setState (ctx , storage ); err != nil {
279
+ return controllers .NoRequeue (err )
280
+ }
281
+ } else {
282
+ meta .SetStatusCondition (& storage .Status .Conditions , metav1.Condition {
283
+ Type : InitRootStorageStepCondition ,
284
+ Status : "True" ,
285
+ Reason : InitRootStorageStepReasonNotRequired ,
286
+ Message : "InitRootStorageStep is not required" ,
287
+ })
288
+ if _ , err := r .setState (ctx , storage ); err != nil {
289
+ return controllers .NoRequeue (err )
290
+ }
291
+ }
292
+
293
+ meta .SetStatusCondition (& storage .Status .Conditions , metav1.Condition {
294
+ Type : InitCMSStepCondition ,
295
+ Status : "False" ,
296
+ Reason : InitCMSStepReasonInProgress ,
297
+ Message : "InitCMSStep is required" ,
298
+ })
299
+ if _ , err := r .setState (ctx , storage ); err != nil {
300
+ return controllers .NoRequeue (err )
301
+ }
302
+
303
+ return controllers .Ok ()
304
+ }
305
+
176
306
func (r * StorageReconciler ) waitForHealthCheck (ctx context.Context , storage * resources.StorageClusterBuilder ) (ctrl.Result , error ) {
177
307
err := healthcheck .CheckBootstrapHealth (ctx , storage )
178
308
0 commit comments