-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathinit.go
190 lines (168 loc) · 5.65 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package storage
import (
"context"
"fmt"
"regexp"
"time"
ydbCredentials "github.com/ydb-platform/ydb-go-sdk/v3/credentials"
"google.golang.org/grpc/metadata"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
. "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" //nolint:revive,stylecheck
"github.com/ydb-platform/ydb-kubernetes-operator/internal/exec"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/labels"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/resources"
)
var mismatchItemConfigGenerationRegexp = regexp.MustCompile(".*mismatch.*ItemConfigGenerationProvided# " +
"0.*ItemConfigGenerationExpected# 1.*")
func (r *Reconciler) processSkipInitPipeline(
ctx context.Context,
storage *resources.StorageClusterBuilder,
) (bool, ctrl.Result, error) {
r.Log.Info("running step processSkipInitPipeline")
r.Log.Info("Storage initialization disabled (with annotation), proceed with caution")
r.Recorder.Event(
storage,
corev1.EventTypeWarning,
"SkippingInit",
"Skipping initialization due to skip annotation present, be careful!",
)
return r.setInitStorageCompleted(
ctx,
storage,
"Storage initialization not performed because initialization is skipped",
)
}
func (r *Reconciler) setInitialStatus(
ctx context.Context,
storage *resources.StorageClusterBuilder,
) (bool, ctrl.Result, error) {
r.Log.Info("running step setInitialStatus")
// This block is special internal logic that skips all Storage initialization.
// It is needed when large clusters are migrated where `waitForStatefulSetToScale`
// does not make sense, since some nodes can be down for a long time (and it is okay, since
// database is healthy even with partial outage).
if value, ok := storage.Annotations[v1alpha1.AnnotationSkipInitialization]; ok && value == v1alpha1.AnnotationValueTrue {
if meta.FindStatusCondition(storage.Status.Conditions, StorageInitializedCondition) == nil ||
meta.IsStatusConditionFalse(storage.Status.Conditions, StorageInitializedCondition) {
return r.processSkipInitPipeline(ctx, storage)
}
return Stop, ctrl.Result{RequeueAfter: StorageInitializationRequeueDelay}, nil
}
changed := false
if meta.FindStatusCondition(storage.Status.Conditions, StorageInitializedCondition) == nil {
meta.SetStatusCondition(&storage.Status.Conditions, metav1.Condition{
Type: StorageInitializedCondition,
Status: "False",
Reason: ReasonInProgress,
Message: "Storage is not ready yet",
})
changed = true
}
if storage.Status.State == StoragePending {
storage.Status.State = StoragePreparing
changed = true
}
if changed {
return r.setState(ctx, storage)
}
return Continue, ctrl.Result{Requeue: false}, nil
}
func (r *Reconciler) setInitStorageCompleted(
ctx context.Context,
storage *resources.StorageClusterBuilder,
message string,
) (bool, ctrl.Result, error) {
meta.SetStatusCondition(&storage.Status.Conditions, metav1.Condition{
Type: StorageInitializedCondition,
Status: "True",
Reason: ReasonCompleted,
Message: message,
})
storage.Status.State = StorageReady
return r.setState(ctx, storage)
}
func (r *Reconciler) initializeStorage(
ctx context.Context,
storage *resources.StorageClusterBuilder,
creds ydbCredentials.Credentials,
) (bool, ctrl.Result, error) {
r.Log.Info("running step runInitScripts")
if storage.Status.State == StorageProvisioning {
storage.Status.State = StorageInitializing
return r.setState(ctx, storage)
}
// List Pods by label Selector
podList := &corev1.PodList{}
matchingLabels := client.MatchingLabels{}
storageLabels := labels.StorageLabels(storage.Unwrap())
for k, v := range storageLabels {
matchingLabels[k] = v
}
opts := []client.ListOption{
client.InNamespace(storage.Namespace),
matchingLabels,
}
if err := r.List(ctx, podList, opts...); err != nil || len(podList.Items) == 0 {
r.Recorder.Event(
storage,
corev1.EventTypeWarning,
"Syncing",
fmt.Sprintf("Failed to list storage pods: %s", err),
)
return Stop, ctrl.Result{RequeueAfter: DefaultRequeueDelay}, err
}
cmd := []string{
fmt.Sprintf("%s/%s", v1alpha1.BinariesDir, v1alpha1.DaemonBinaryName),
}
if storage.Spec.OperatorConnection != nil {
ydbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
token, err := creds.Token(
metadata.AppendToOutgoingContext(ydbCtx, "x-ydb-database", storage.Spec.Domain),
)
if err != nil {
r.Log.Error(err, "initializeStorage error")
return Stop, ctrl.Result{RequeueAfter: StorageInitializationRequeueDelay}, err
}
cmd = append(
cmd,
"--token",
token,
)
}
cmd = append(
cmd,
"-s",
storage.GetStorageEndpointWithProto(),
)
cmd = append(
cmd,
"admin", "blobstorage", "config", "init",
"--yaml-file",
fmt.Sprintf("%s/%s", v1alpha1.ConfigDir, v1alpha1.ConfigFileName),
)
stdout, _, err := exec.InPod(r.Scheme, r.Config, storage.Namespace, podList.Items[0].Name, "ydb-storage", cmd)
if err != nil {
if mismatchItemConfigGenerationRegexp.MatchString(stdout) {
r.Log.Info("Storage is already initialized, continuing...")
r.Recorder.Event(
storage,
corev1.EventTypeNormal,
"InitializingStorage",
"Storage initialization attempted and skipped, storage already initialized",
)
return r.setInitStorageCompleted(
ctx,
storage,
"Storage already initialized",
)
}
return Stop, ctrl.Result{RequeueAfter: StorageInitializationRequeueDelay}, err
}
return r.setInitStorageCompleted(ctx, storage, "Storage initialized successfully")
}