-
Notifications
You must be signed in to change notification settings - Fork 649
/
odfWorkerReplace.go
426 lines (383 loc) · 14.7 KB
/
odfWorkerReplace.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package softwaredefinedstorage
import (
"context"
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"time"
v2 "github.com/IBM-Cloud/bluemix-go/api/container/containerv2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
templatev1 "github.com/openshift/api/template/v1"
templatev1client "github.com/openshift/client-go/template/clientset/versioned/typed/template/v1"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
corev1 "k8s.io/api/core/v1"
policy "k8s.io/api/policy/v1beta1"
apierror "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var deploymentList []string
// ODF Struct Defined
type odf struct{}
func NewSdsOdf() Sds {
return &odf{}
}
// Steps before Worker Replace for ODF
func (o odf) PreWorkerReplace(worker v2.Worker) error {
log.Println("Inside preWorkerReplace for ODF")
workerName := worker.NetworkInterfaces[0].IpAddress
log.Println("This is the Worker to be replaced", workerName)
// Scale Deployments to 0
for label, id := range AppLabelId {
err := scalePods(workerName, app, label, id, 0)
if err != nil {
return err
}
}
// Check if Replica Set is 0
for _, v := range deploymentList {
_, err := waitForOdfDeploymentStatus(0, v)
if err != nil {
return err
}
}
log.Println("Deployments have been successfully scaled!")
node := workerName
// Cordon the node
_, err := waitForNodeCordonStatus(node)
if err != nil {
return err
}
//Drain the node
pods, err := clientSet.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{FieldSelector: "spec.nodeName=" + workerName})
if err != nil {
return fmt.Errorf("[ERROR] Error getting Pods from worker node %s - %s", workerName, err)
}
// Evict the pods from the given node
for _, pod := range pods.Items {
evictPod(pod.Name, pod.Namespace)
log.Printf("Pod %s has been evicted\n", pod.Name)
}
log.Printf("Node %s has been drained\n", node)
return nil
}
func evictPod(name, namespace string) error {
return clientSet.PolicyV1beta1().Evictions(namespace).Evict(context.TODO(), &policy.Eviction{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace},
})
}
// Function to Scale the deployments based on the pods present in the current worker node
func scalePods(workerName string, appLabel string, appLabelValue string, idLabel string, replicas int32) error {
labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{appLabel: appLabelValue}}
listOptions := metav1.ListOptions{
LabelSelector: labels.Set(labelSelector.MatchLabels).String(),
FieldSelector: "spec.nodeName=" + workerName}
pods, err := clientSet.CoreV1().Pods(odfNamespace).List(context.TODO(), listOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error getting Pods in the openshift-storage namespace: %s", err)
}
if len(pods.Items) == 0 {
return nil
}
for _, pod := range pods.Items {
log.Printf("Pod name: %v\n", pod.Name)
deploymentName := pod.Labels[appLabel] + "-" + pod.Labels[idLabel]
deploymentsClient := clientSet.AppsV1().Deployments(odfNamespace)
deploymentList = append(deploymentList, deploymentName)
result, err := deploymentsClient.Get(context.TODO(), deploymentName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("[ERROR] Failed to get latest version of deployment: %v - %v", deploymentName, err)
}
result.Spec.Replicas = int32Ptr(replicas) // reduce replica count
scaleDeployment, err := deploymentsClient.Update(context.TODO(), result, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("[ERROR] Error updating scale object in deployment: %v - %v", deploymentName, err)
}
log.Printf("Successfully scaled deployment %s to %d replicas", deploymentName, *scaleDeployment.Spec.Replicas)
}
return nil
}
// Steps after worker replace has been complete
func (o odf) PostWorkerReplace(worker v2.Worker) error {
log.Println("In Post Worker Replace")
deploymentsClient := clientSet.AppsV1().Deployments(odfNamespace)
log.Println("Scaling Up Deployments")
// Scale Up Deployments that have been Brought down in pre worker replace
for _, v := range deploymentList {
if !strings.Contains(v, crashcollectorLabel) {
result, err := deploymentsClient.Get(context.TODO(), v, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("[ERROR] Failed to get latest version of deployment: %v - %v", v, err)
}
result.Spec.Replicas = int32Ptr(1) // increase replica count
scaleDeployment, err := deploymentsClient.Update(context.TODO(), result, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("[Error] Error updating scale object in deployment: %v - %v", v, err)
}
log.Printf("Successfully scaled deployment %s to %d replicas", v, *scaleDeployment.Spec.Replicas)
}
}
log.Println("Checking if Number of Replicas is 1")
// Check if the deployment replicas are 1
for _, v := range deploymentList {
if !strings.Contains(v, crashcollectorLabel) {
_, err := waitForOdfDeploymentStatus(1, v)
if err != nil {
return err
}
}
}
log.Println("Deployments have been successfully scaled")
deploymentList = nil
workerName := worker.NetworkInterfaces[0].IpAddress
log.Println("This is the New Worker Name", workerName)
// Check the status of the OSD pods, if failed then run job
labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{app: osdLabel}}
listOptions := metav1.ListOptions{
LabelSelector: labels.Set(labelSelector.MatchLabels).String(),
FieldSelector: "spec.nodeName=" + workerName}
pods, err := clientSet.CoreV1().Pods(odfNamespace).List(context.TODO(), listOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error getting Pods in the openshift-storage namespace %v", err)
}
osdID := ""
for _, pod := range pods.Items {
log.Printf("Pod Name %s is %s/n", pod.Name, pod.Status.Phase)
if !(pod.Status.Phase == "Running" || pod.Status.Phase == "Pending") {
osdID = osdID + pod.Labels["ceph-osd-id"] + ","
}
}
osdID = strings.TrimRight(osdID, ",")
if len(osdID) > 0 {
err := executeTemplate(osdID)
if err != nil {
return err
}
}
// Check Ceph Status if HEALTH OK then return success!
log.Println("Fetching Ceph Cluster Status")
_, err = waitForCephClusterStatus()
if err != nil {
return err
}
return nil
}
func int32Ptr(i int32) *int32 { return &i }
// To execute a given template using the openshift go client
func executeTemplate(osdID string) error {
// Create an OpenShift template/v1 client.
templateclient, err := templatev1client.NewForConfig(restConfig)
if err != nil {
return fmt.Errorf("[ERROR] Error creating templateClient %s", err)
}
// Get the "ocs-osd-removal" Template from the "openshift-storage" Namespace.
template, err := templateclient.Templates(odfNamespace).Get(context.Background(),
"ocs-osd-removal", metav1.GetOptions{})
if err != nil {
return fmt.Errorf("[ERROR] Error getting ocs-osd-removal template %s", err)
}
// INSTANTIATE THE TEMPLATE.
// To set Template parameters, create a Secret holding overridden parameters
// and their values.
secret, err := clientSet.CoreV1().Secrets(odfNamespace).Create(context.Background(), &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "parameters",
},
StringData: map[string]string{
"FAILED_OSD_IDS": osdID,
},
}, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("[ERROR] Error creating secret for ocs-osd-removal template %s", err)
}
// Create a TemplateInstance object, linking the Template and a reference to
// the Secret object created above.
templateInstance, err := templateclient.TemplateInstances(odfNamespace).Create(context.Background(),
&templatev1.TemplateInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "templateinstance",
},
Spec: templatev1.TemplateInstanceSpec{
Template: *template,
Secret: &corev1.LocalObjectReference{
Name: secret.Name,
},
},
}, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("[ERROR] Error deploying ocs-osd-removal template %s", err)
}
_, err = waitForTemplateInstanceStatus(templateInstance)
if err != nil {
return err
}
// DELETE THE INSTANTIATED TEMPLATE.
// We use the foreground propagation policy to ensure that the garbage
// collector removes all instantiated objects before the TemplateInstance
// itself disappears.
foreground := metav1.DeletePropagationForeground
deleteOptions := metav1.DeleteOptions{PropagationPolicy: &foreground}
err = templateclient.TemplateInstances(odfNamespace).Delete(context.TODO(), templateInstance.Name,
deleteOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error deleting TemplateInstance resource %s", err)
}
// Finally delete the "parameters" Secret.
err = clientSet.CoreV1().Secrets(odfNamespace).Delete(context.TODO(), secret.Name,
metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("[ERROR] Error deleting parameter Secret resource %s", err)
}
return nil
}
func waitForOdfDeploymentStatus(replicas int32, deploymentName string) (interface{}, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{"NotReady"},
Target: []string{"Ready"},
Refresh: odfDeploymentRefreshFunc(replicas, deploymentName),
Timeout: time.Duration(sdsTimeout),
Delay: 5 * time.Second,
MinTimeout: 5 * time.Second,
NotFoundChecks: 100,
}
return stateConf.WaitForState()
}
func odfDeploymentRefreshFunc(replicas int32, deploymentName string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
log.Println("Checking Deployment Status....")
deploymentsClient := clientSet.AppsV1().Deployments(odfNamespace)
result, err := deploymentsClient.Get(context.TODO(), deploymentName, metav1.GetOptions{})
if err != nil {
return nil, "NotReady", fmt.Errorf("[ERROR] Failed to get latest version of deployment: %v - %v", deploymentName, err)
}
log.Printf("Deployment Name:%s Replica Number:%d\n", deploymentName, result.Status.ReadyReplicas)
if result.Status.ReadyReplicas == replicas {
log.Println("Deployment has been successfully scaled")
return true, "Ready", nil
}
return nil, "NotReady", nil
}
}
func waitForCephClusterStatus() (interface{}, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{"NotReady"},
Target: []string{"Ready"},
Refresh: cephClusterRefreshFunc(),
Timeout: time.Duration(sdsTimeout),
Delay: 5 * time.Second,
MinTimeout: 5 * time.Second,
NotFoundChecks: 100,
}
return stateConf.WaitForState()
}
func cephClusterRefreshFunc() resource.StateRefreshFunc {
return func() (interface{}, string, error) {
scheme := runtime.NewScheme()
utilruntime.Must(cephv1.AddToScheme(scheme))
cntrlClient, err := client.New(restConfig, client.Options{Scheme: scheme})
if err != nil {
return nil, "NotReady", fmt.Errorf("[ERROR] Error getting config")
}
cephcluster := &cephv1.CephCluster{}
var CephclusterGet []string
CephclusterGet = append(CephclusterGet, "\n Cephcluster Details \n \n", "NAME DATADIRHOSTPATH MONCOUNT AGE PHASE MESSAGE HEALTH\n")
err = cntrlClient.Get(context.Background(), types.NamespacedName{Name: "ocs-storagecluster-cephcluster", Namespace: odfNamespace}, cephcluster)
if err != nil && !apierror.IsNotFound(err) {
return nil, "NotReady", err
} else {
if apierror.IsNotFound(err) {
return nil, "NotReady", fmt.Errorf("[ERROR] Cephcluster not found")
} else {
_, err := json.MarshalIndent(cephcluster, "", " ")
if err != nil {
return nil, "NotReady", err
}
}
CephclusterGet = append(CephclusterGet, "\n", cephcluster.Name, cephcluster.Spec.DataDirHostPath, strconv.Itoa(cephcluster.Spec.Mon.Count), time.Now().Sub(cephcluster.CreationTimestamp.Time).String(), string(cephcluster.Status.Phase), cephcluster.Status.Message, cephcluster.Status.CephStatus.Health, "\n")
if err != nil {
return nil, "NotReady", err
}
}
log.Println("Ceph Cluster Status", CephclusterGet[9])
if CephclusterGet[9] == "HEALTH_OK" {
log.Println("Ceph Cluster is OK, Worker Replace Done!")
return true, "Ready", nil
}
return nil, "NotReady", nil
}
}
func waitForNodeCordonStatus(node string) (interface{}, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{"NotReady"},
Target: []string{"Ready"},
Refresh: nodeCordonRefreshFunc(node),
Timeout: time.Duration(sdsTimeout),
Delay: 10 * time.Second,
MinTimeout: 10 * time.Second,
NotFoundChecks: 100,
}
return stateConf.WaitForState()
}
func nodeCordonRefreshFunc(node string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
n, err := clientSet.CoreV1().Nodes().Get(context.Background(), node, metav1.GetOptions{})
if err != nil {
return nil, "NotReady", fmt.Errorf("[ERROR] Node %s not found\n", node)
}
n.Spec.Unschedulable = true
_, err = clientSet.CoreV1().Nodes().Update(context.Background(), n, metav1.UpdateOptions{})
if err != nil {
return nil, "NotReady", fmt.Errorf("[ERROR] Unable to update the node %s\n", node)
}
n, err = clientSet.CoreV1().Nodes().Get(context.Background(), node, metav1.GetOptions{})
if err != nil {
return nil, "NotReady", fmt.Errorf("[ERROR] Unable to cordon node %s\n", node)
}
if n.Spec.Unschedulable {
log.Println("Node has been successfully Cordoned")
return true, "Ready", nil
}
return nil, "NotReady", nil
}
}
func waitForTemplateInstanceStatus(templateInstance *templatev1.TemplateInstance) (interface{}, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{"NotReady"},
Target: []string{"Ready"},
Refresh: templateInstanceRefreshFunc(templateInstance),
Timeout: time.Duration(sdsTimeout),
Delay: 10 * time.Second,
MinTimeout: 10 * time.Second,
NotFoundChecks: 100,
}
return stateConf.WaitForState()
}
func templateInstanceRefreshFunc(templateInstance *templatev1.TemplateInstance) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
for _, cond := range templateInstance.Status.Conditions {
// If the TemplateInstance contains a status condition
// Ready == True, stop watching.
if cond.Type == templatev1.TemplateInstanceReady &&
cond.Status == corev1.ConditionTrue {
return true, "Ready", nil
}
// If the TemplateInstance contains a status condition
// InstantiateFailure == True, indicate failure.
if cond.Type ==
templatev1.TemplateInstanceInstantiateFailure &&
cond.Status == corev1.ConditionTrue {
return nil, "NotReady", fmt.Errorf("[ERROR] Error TemplateInstance instantiation failed")
}
}
return nil, "NotReady", nil
}
}