-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsq.go
1121 lines (930 loc) · 42.3 KB
/
nsq.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2019 The NSQ-Operator Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
import (
"context"
"fmt"
"github.com/andyxning/nsq-operator/pkg/apis/nsqio/v1alpha1"
pkgcommon "github.com/andyxning/nsq-operator/pkg/common"
"github.com/andyxning/nsq-operator/pkg/constant"
"github.com/andyxning/nsq-operator/pkg/generated/clientset/versioned"
"github.com/andyxning/nsq-operator/pkg/sdk/v1alpha1/types"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/klog"
)
func CreateCluster(kubeClient *kubernetes.Clientset, nsqClient *versioned.Clientset, ncr *types.NsqCreateRequest) error {
// Create nsqlookupd configmap
nsqLookupdCM := ncr.AssembleNsqLookupdConfigMap()
klog.Infof("Create nsqlookupd configmap %s/%s", nsqLookupdCM.Namespace, nsqLookupdCM.Name)
_, err := kubeClient.CoreV1().ConfigMaps(ncr.NsqdConfig.Namespace).Create(nsqLookupdCM)
if errors.IsAlreadyExists(err) {
klog.Infof("Nsqlookupd configmap %s/%s exists. Update it", nsqLookupdCM.Namespace, nsqLookupdCM.Name)
var old *corev1.ConfigMap
if klog.V(4) {
old, err = kubeClient.CoreV1().ConfigMaps(ncr.NsqdConfig.Namespace).Get(nsqLookupdCM.Name, metav1.GetOptions{})
if err != nil {
klog.Errorf("Get old nsqlookupd configmap %s/%s error: %v", nsqLookupdCM.Namespace, nsqLookupdCM.Name, err)
return err
}
klog.V(4).Infof("Old nsqlookupd configmap %s/%s: %#v", nsqLookupdCM.Namespace, nsqLookupdCM.Name, *old)
}
oldCopy := old.DeepCopy()
oldCopy.Data = nsqLookupdCM.Data
klog.V(4).Infof("New nsqlookupd configmap %s/%s: %#v", nsqLookupdCM.Namespace, nsqLookupdCM.Name, *oldCopy)
_, err = kubeClient.CoreV1().ConfigMaps(ncr.NsqdConfig.Namespace).Update(oldCopy)
}
if err != nil {
klog.Errorf("Create nsqlookupd configmap error: %v", err)
return err
}
// Create nsqlookupd
nsqLookupd := ncr.AssembleNsqLookupd()
klog.Infof("Create nsqlookupd %s/%s", nsqLookupd.Namespace, nsqLookupd.Name)
_, err = nsqClient.NsqV1alpha1().NsqLookupds(ncr.NsqdConfig.Namespace).Create(nsqLookupd)
if errors.IsAlreadyExists(err) {
klog.Infof("Nsqlookupd %s/%s exists. Update it", nsqLookupd.Namespace, nsqLookupd.Name)
var old *v1alpha1.NsqLookupd
if klog.V(4) {
old, err = nsqClient.NsqV1alpha1().NsqLookupds(ncr.NsqdConfig.Namespace).Get(nsqLookupd.Name, metav1.GetOptions{})
if err != nil {
klog.Errorf("Get old nsqlookupd %s/%s error: %v", nsqLookupd.Namespace, nsqLookupd.Name, err)
return err
}
klog.V(4).Infof("Old nsqlookupd %s/%s: %#v", nsqLookupd.Namespace, nsqLookupd.Name, *old)
}
oldCopy := old.DeepCopy()
oldCopy.Spec = nsqLookupd.Spec
klog.V(4).Infof("New nsqlookupd %s/%s: %#v", nsqLookupd.Namespace, nsqLookupd.Name, *oldCopy)
_, err = nsqClient.NsqV1alpha1().NsqLookupds(ncr.NsqdConfig.Namespace).Update(oldCopy)
}
if err != nil {
klog.Errorf("Create nsqlookupd error: %v", err)
return err
}
// Wait for nsqlookupd to reach its spec
klog.V(2).Infof("Waiting for nsqlookupd %s/%s reach its spec", nsqLookupd.Namespace, nsqLookupd.Name)
ctx, cancel := context.WithTimeout(context.Background(), *ncr.NsqdConfig.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
nsqLookupd, err := nsqClient.NsqV1alpha1().NsqLookupds(ncr.NsqdConfig.Namespace).Get(ncr.NsqdConfig.Name, metav1.GetOptions{})
if err != nil {
klog.Warningf("Get nsqlookupd error: %v", err)
return
}
if nsqLookupd.Spec.Replicas != nsqLookupd.Status.AvailableReplicas {
klog.V(2).Infof("Spec and status does not match for nsqlookupd %s/%s. Spec: %#v, Status: %#v",
ncr.NsqdConfig.Namespace, ncr.NsqdConfig.Name, nsqLookupd.Spec, nsqLookupd.Status)
return
}
// Spec and status matches
cancel()
}, constant.NsqLookupdStatusCheckPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Timeout for waiting nsqlookupd resource to reach its spec")
return ctx.Err()
}
// Check for existence of nsqadmin configmap
klog.V(2).Infof("Waiting for nsqadmin configmap %s/%s reach its spec", ncr.NsqdConfig.Namespace, pkgcommon.NsqAdminConfigMapName(ncr.NsqdConfig.Name))
ctx, cancel = context.WithTimeout(context.Background(), *ncr.NsqdConfig.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
nsqadminCM, err := kubeClient.CoreV1().ConfigMaps(ncr.NsqdConfig.Namespace).Get(pkgcommon.NsqAdminConfigMapName(ncr.NsqdConfig.Name), metav1.GetOptions{})
if err != nil {
klog.Warningf("Get nsqadmin configmap error: %v", err)
return
}
if nsqadminCM.Data != nil && len(nsqadminCM.Data) != 0 {
// nsqadmin configmap exists
cancel()
}
}, constant.ConfigMapStatusCheckPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Timeout for waiting nsqadmin configmap to be ready")
return ctx.Err()
}
// Create nsqadmin
nsqadmin := ncr.AssembleNsqAdmin()
klog.Infof("Create nsqadmin %s/%s", nsqadmin.Namespace, nsqadmin.Name)
_, err = nsqClient.NsqV1alpha1().NsqAdmins(ncr.NsqdConfig.Namespace).Create(nsqadmin)
if errors.IsAlreadyExists(err) {
klog.Infof("Nsqadmin %s/%s exists. Update it", nsqadmin.Namespace, nsqadmin.Name)
var old *v1alpha1.NsqAdmin
if klog.V(4) {
old, err = nsqClient.NsqV1alpha1().NsqAdmins(ncr.NsqdConfig.Namespace).Get(nsqadmin.Name, metav1.GetOptions{})
if err != nil {
klog.Errorf("Get old nsqadmin %s/%s error: %v", nsqadmin.Namespace, nsqadmin.Name, err)
return err
}
klog.V(4).Infof("Old nsqadmin %s/%s: %#v", nsqadmin.Namespace, nsqadmin.Name, *old)
}
oldCopy := old.DeepCopy()
oldCopy.Spec = nsqadmin.Spec
klog.V(4).Infof("New nsqadmin %s/%s: %#v", nsqadmin.Namespace, nsqadmin.Name, *oldCopy)
_, err = nsqClient.NsqV1alpha1().NsqAdmins(ncr.NsqdConfig.Namespace).Update(oldCopy)
}
if err != nil {
klog.Errorf("Create nsqadmin error: %v", err)
return err
}
// Wait for nsqadmin to reach its spec
klog.V(2).Infof("Waiting for nsqadmin %s/%s reach its spec", nsqadmin.Namespace, nsqadmin.Name)
ctx, cancel = context.WithTimeout(context.Background(), *ncr.NsqdConfig.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
nsqAdmin, err := nsqClient.NsqV1alpha1().NsqAdmins(ncr.NsqdConfig.Namespace).Get(ncr.NsqdConfig.Name, metav1.GetOptions{})
if err != nil {
klog.Warningf("Get nsqadmin error: %v", err)
return
}
if nsqAdmin.Spec.Replicas != nsqAdmin.Status.AvailableReplicas {
klog.V(2).Infof("Spec and status does not match for nsqadmin %s/%s. Spec: %#v, Status: %#v",
ncr.NsqdConfig.Namespace, ncr.NsqdConfig.Name, nsqAdmin.Spec, nsqAdmin.Status)
return
}
// Spec and status matches
cancel()
}, constant.NsqAdminStatusCheckPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Timeout for waiting nsqadmin resource to reach its spec")
return ctx.Err()
}
// Create nsqd configmap
labelSelector := &metav1.LabelSelector{MatchLabels: map[string]string{"cluster": pkgcommon.NsqLookupdDeploymentName(ncr.NsqdConfig.Name)}}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
if err != nil {
klog.Errorf("Gen label selector for nsqlookupd %s/%s pods error: %v", ncr.NsqdConfig.Namespace, ncr.NsqdConfig.Name, err)
return err
}
podList, err := kubeClient.CoreV1().Pods(ncr.NsqdConfig.Namespace).List(metav1.ListOptions{
LabelSelector: selector.String(),
})
if err != nil {
klog.Errorf("List nsqlookupd %s/%s pods error: %v", ncr.NsqdConfig.Namespace, ncr.NsqdConfig.Name, err)
return err
}
var addresses []string
for _, pod := range podList.Items {
addresses = append(addresses, fmt.Sprintf("%s:%v", pod.Status.PodIP, constant.NsqLookupdTcpPort))
}
nsqdCM := ncr.NsqdConfig.AssembleNsqdConfigMap(addresses)
klog.Infof("Create nsqd configmap %s/%s", nsqdCM.Namespace, nsqdCM.Name)
_, err = kubeClient.CoreV1().ConfigMaps(ncr.NsqdConfig.Namespace).Create(nsqdCM)
if errors.IsAlreadyExists(err) {
klog.Infof("Nsqd configmap %s/%s exists. Update it", nsqdCM.Namespace, nsqdCM.Name)
var old *corev1.ConfigMap
if klog.V(4) {
old, err = kubeClient.CoreV1().ConfigMaps(ncr.NsqdConfig.Namespace).Get(nsqdCM.Name, metav1.GetOptions{})
if err != nil {
klog.Errorf("Get old nsqd configmap %s/%s error: %v", nsqdCM.Namespace, nsqdCM.Name, err)
return err
}
klog.V(4).Infof("Old nsqd configmap %s/%s: %#v", nsqdCM.Namespace, nsqdCM.Name, *old)
}
oldCopy := old.DeepCopy()
oldCopy.Data = nsqdCM.Data
klog.V(4).Infof("New nsqd configmap %s/%s: %#v", nsqdCM.Namespace, nsqdCM.Name, *oldCopy)
_, err = kubeClient.CoreV1().ConfigMaps(ncr.NsqdConfig.Namespace).Update(oldCopy)
}
if err != nil {
klog.Errorf("Create nsqd configmap error: %v", err)
return err
}
// Create nsqd
nsqd := ncr.AssembleNsqd()
klog.Infof("Create nsqd %s/%s", nsqd.Namespace, nsqd.Name)
_, err = nsqClient.NsqV1alpha1().Nsqds(ncr.NsqdConfig.Namespace).Create(nsqd)
if errors.IsAlreadyExists(err) {
klog.Infof("Nsqd %s/%s exists. Update it", nsqd.Namespace, nsqd.Name)
var old *v1alpha1.Nsqd
if klog.V(4) {
old, err = nsqClient.NsqV1alpha1().Nsqds(ncr.NsqdConfig.Namespace).Get(nsqd.Name, metav1.GetOptions{})
if err != nil {
klog.Errorf("Get old nsqd %s/%s error: %v", nsqd.Namespace, nsqd.Name, err)
return err
}
klog.V(4).Infof("Old nsqd %s/%s: %#v", nsqd.Namespace, nsqd.Name, *old)
}
oldCopy := old.DeepCopy()
oldCopy.Spec = nsqd.Spec
klog.V(4).Infof("New nsqd %s/%s: %#v", nsqd.Namespace, nsqd.Name, *oldCopy)
_, err = nsqClient.NsqV1alpha1().Nsqds(ncr.NsqdConfig.Namespace).Update(oldCopy)
}
if err != nil {
klog.Errorf("Create nsqd error: %v", err)
return err
}
// Wait for nsqd to reach its spec
klog.V(2).Infof("Waiting for nsqd %s/%s reach its spec", nsqd.Namespace, nsqd.Name)
ctx, cancel = context.WithTimeout(context.Background(), *ncr.NsqdConfig.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
nsqd, err := nsqClient.NsqV1alpha1().Nsqds(ncr.NsqdConfig.Namespace).Get(ncr.NsqdConfig.Name, metav1.GetOptions{})
if err != nil {
klog.Warningf("Get nsqd error: %v", err)
return
}
if nsqd.Spec.Replicas != nsqd.Status.AvailableReplicas {
klog.V(2).Infof("Spec and status does not match for nsqd %s/%s. Spec: %#v, Status: %#v",
ncr.NsqdConfig.Namespace, ncr.NsqdConfig.Name, nsqd.Spec, nsqd.Status)
return
}
// Spec and status matches
cancel()
}, constant.NsqdStatusCheckPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Timeout for waiting nsqd resource to reach its spec")
return ctx.Err()
}
// Create nsqdscale
nsqdScale := ncr.AssembleNsqdScale()
klog.Infof("Create nsqdscale %s/%s: %+v", nsqdScale.Namespace, nsqdScale.Name, nsqdScale)
_, err = nsqClient.NsqV1alpha1().NsqdScales(nsqdScale.Namespace).Create(nsqdScale)
if errors.IsAlreadyExists(err) {
klog.Infof("Nsqdscale %s/%s exists. Update it", nsqdScale.Namespace, nsqdScale.Name)
var old *v1alpha1.NsqdScale
old, err = nsqClient.NsqV1alpha1().NsqdScales(nsqdScale.Namespace).Get(nsqdScale.Name, metav1.GetOptions{})
if err != nil {
klog.Errorf("Get old nsqdscale %s/%s error: %v", nsqdScale.Namespace, nsqdScale.Name, err)
return err
}
klog.V(4).Infof("Old nsqdscale %s/%s: %#v", nsqdScale.Namespace, nsqdScale.Name, *old)
oldCopy := old.DeepCopy()
oldCopy.Spec = nsqdScale.Spec
klog.V(4).Infof("New nsqdscale %s/%s: %#v", nsqdScale.Namespace, nsqdScale.Name, *oldCopy)
_, err = nsqClient.NsqV1alpha1().NsqdScales(nsqdScale.Namespace).Update(oldCopy)
}
if err != nil {
klog.Errorf("Create nsqdscale error: %v", err)
return err
}
return nil
}
func DeleteCluster(kubeClient *kubernetes.Clientset, nsqClient *versioned.Clientset, ndr *types.NsqDeleteRequest) error {
err := nsqClient.NsqV1alpha1().NsqLookupds(ndr.Namespace).Delete(ndr.Name, &metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
klog.Warningf("Delete nsqlookupd %s/%s error: %v", ndr.Namespace, ndr.Name, err)
return err
}
klog.Infof("Waiting for nsqlookupd %s/%s to be deleted", ndr.Namespace, ndr.Name)
ctx, cancel := context.WithTimeout(context.Background(), *ndr.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
labelSelector := &metav1.LabelSelector{MatchLabels: map[string]string{"cluster": pkgcommon.NsqLookupdDeploymentName(ndr.Name)}}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
if err != nil {
klog.Warningf("Assemble nsqlookupd %s/%s selector error: %v", ndr.Namespace, ndr.Name, err)
return
}
pl, err := kubeClient.CoreV1().Pods(ndr.Namespace).List(metav1.ListOptions{
LabelSelector: selector.String(),
})
if err != nil {
klog.Warningf("List nsqlookupd %s/%s pods error: %v", ndr.Namespace, ndr.Name, err)
return
}
if len(pl.Items) != 0 {
klog.Infof("There are still %d nsqlookupd for %s/%s", len(pl.Items), ndr.Namespace, ndr.Name)
return
}
cancel()
klog.Infof("Delete nsqlookupd %s/%s success", ndr.Namespace, ndr.Name)
}, constant.NsqLookupdStatusCheckPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Delete nsqlookupd %s/%s error: %v", ndr.Namespace, ndr.Name, ctx.Err())
return ctx.Err()
}
err = nsqClient.NsqV1alpha1().NsqAdmins(ndr.Namespace).Delete(ndr.Name, &metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
klog.Errorf("Delete nsqadmin %s/%s error: %v", ndr.Namespace, ndr.Name, err)
return err
}
klog.Infof("Waiting for nsqadmin %s/%s to be deleted", ndr.Namespace, ndr.Name)
ctx, cancel = context.WithTimeout(context.Background(), *ndr.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
labelSelector := &metav1.LabelSelector{MatchLabels: map[string]string{"cluster": pkgcommon.NsqAdminDeploymentName(ndr.Name)}}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
if err != nil {
klog.Warningf("Assemble nsqadmin %s/%s selector error: %v", ndr.Namespace, ndr.Name, err)
return
}
pl, err := kubeClient.CoreV1().Pods(ndr.Namespace).List(metav1.ListOptions{
LabelSelector: selector.String(),
})
if err != nil {
klog.Warningf("List nsqadmin %s/%s pods error: %v", ndr.Namespace, ndr.Name, err)
return
}
if len(pl.Items) != 0 {
klog.Infof("There are still %d nsqadmin for %s/%s", len(pl.Items), ndr.Namespace, ndr.Name)
return
}
cancel()
klog.Infof("Delete nsqadmin %s/%s success", ndr.Namespace, ndr.Name)
}, constant.NsqAdminStatusCheckPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Delete nsqadmin %s/%s error: %v", ndr.Namespace, ndr.Name, ctx.Err())
return ctx.Err()
}
err = nsqClient.NsqV1alpha1().Nsqds(ndr.Namespace).Delete(ndr.Name, &metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
klog.Errorf("Delete nsqd %s/%s error: %v", ndr.Namespace, ndr.Name, err)
return err
}
klog.Infof("Waiting for nsqd %s/%s to be deleted", ndr.Namespace, ndr.Name)
ctx, cancel = context.WithTimeout(context.Background(), *ndr.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
labelSelector := &metav1.LabelSelector{MatchLabels: map[string]string{"cluster": pkgcommon.NsqdStatefulSetName(ndr.Name)}}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
if err != nil {
klog.Warningf("Assemble nsqd %s/%s selector error: %v", ndr.Namespace, ndr.Name, err)
return
}
pl, err := kubeClient.CoreV1().Pods(ndr.Namespace).List(metav1.ListOptions{
LabelSelector: selector.String(),
})
if err != nil {
klog.Warningf("List nsqd %s/%s pods error: %v", ndr.Namespace, ndr.Name, err)
return
}
if len(pl.Items) != 0 {
klog.Infof("There are still %d nsqd for %s/%s", len(pl.Items), ndr.Namespace, ndr.Name)
return
}
cancel()
klog.Infof("Delete nsqd %s/%s success", ndr.Namespace, ndr.Name)
}, constant.NsqdStatusCheckPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Delete nsqd %s/%s error: %v", ndr.Namespace, ndr.Name, ctx.Err())
return ctx.Err()
}
err = kubeClient.CoreV1().ConfigMaps(ndr.Namespace).Delete(pkgcommon.NsqLookupdConfigMapName(ndr.Name), &metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
klog.Errorf("Delete nsqlookupd configmap %s/%s error: %v", ndr.Namespace, pkgcommon.NsqLookupdConfigMapName(ndr.Name), err)
return err
}
klog.Infof("Waiting for nsqlookupd configmap %s/%s to be deleted", ndr.Namespace, pkgcommon.NsqLookupdConfigMapName(ndr.Name))
ctx, cancel = context.WithTimeout(context.Background(), *ndr.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
_, err := kubeClient.CoreV1().ConfigMaps(ndr.Namespace).Get(pkgcommon.NsqLookupdConfigMapName(ndr.Name), metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
klog.Warningf("Get nsqlookupd configmap %s/%s error: %v", ndr.Namespace, pkgcommon.NsqLookupdConfigMapName(ndr.Name), err)
return
}
cancel()
klog.Infof("Delete nsqlookupd configmap %s/%s success", ndr.Namespace, pkgcommon.NsqLookupdConfigMapName(ndr.Name))
}, constant.ResourceUpdateRetryPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Delete nsqlookupd configmap %s/%s error: %v", ndr.Namespace, pkgcommon.NsqLookupdConfigMapName(ndr.Name), ctx.Err())
return ctx.Err()
}
err = kubeClient.CoreV1().ConfigMaps(ndr.Namespace).Delete(pkgcommon.NsqAdminConfigMapName(ndr.Name), &metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
klog.Errorf("Delete nsqadmin configmap %s/%s error: %v", ndr.Namespace, pkgcommon.NsqAdminConfigMapName(ndr.Name), err)
return err
}
klog.Infof("Waiting for nsqadmin configmap %s/%s to be deleted", ndr.Namespace, pkgcommon.NsqAdminConfigMapName(ndr.Name))
ctx, cancel = context.WithTimeout(context.Background(), *ndr.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
_, err := kubeClient.CoreV1().ConfigMaps(ndr.Namespace).Get(pkgcommon.NsqAdminConfigMapName(ndr.Name), metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
klog.Warningf("Get nsqadmin configmap %s/%s error: %v", ndr.Namespace, pkgcommon.NsqAdminConfigMapName(ndr.Name), err)
return
}
cancel()
klog.Infof("Delete nsqadmin configmap %s/%s success", ndr.Namespace, pkgcommon.NsqAdminConfigMapName(ndr.Name))
}, constant.ResourceUpdateRetryPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Delete nsqadmin configmap %s/%s error: %v", ndr.Namespace, pkgcommon.NsqAdminConfigMapName(ndr.Name), ctx.Err())
return ctx.Err()
}
err = kubeClient.CoreV1().ConfigMaps(ndr.Namespace).Delete(pkgcommon.NsqdConfigMapName(ndr.Name), &metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
klog.Errorf("Delete nsqd configmap %s/%s error: %v", ndr.Namespace, pkgcommon.NsqdConfigMapName(ndr.Name), err)
return err
}
klog.Infof("Waiting for nsqd configmap %s/%s to be deleted", ndr.Namespace, pkgcommon.NsqdConfigMapName(ndr.Name))
ctx, cancel = context.WithTimeout(context.Background(), *ndr.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
_, err := kubeClient.CoreV1().ConfigMaps(ndr.Namespace).Get(pkgcommon.NsqdConfigMapName(ndr.Name), metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
klog.Warningf("Get nsqd configmap %s/%s error: %v", ndr.Namespace, pkgcommon.NsqdConfigMapName(ndr.Name), err)
return
}
cancel()
klog.Infof("Delete nsqd configmap %s/%s success", ndr.Namespace, pkgcommon.NsqdConfigMapName(ndr.Name))
}, constant.ResourceUpdateRetryPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Delete nsqd configmap %s/%s error: %v", ndr.Namespace, pkgcommon.NsqdConfigMapName(ndr.Name), ctx.Err())
return ctx.Err()
}
err = nsqClient.NsqV1alpha1().NsqdScales(ndr.Namespace).Delete(ndr.Name, &metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
klog.Errorf("Delete nsqdscale %s/%s error: %v", ndr.Namespace, ndr.Name, err)
return err
}
klog.Infof("Waiting for nsqdscale %s/%s to be deleted", ndr.Namespace, ndr.Name)
ctx, cancel = context.WithTimeout(context.Background(), *ndr.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
_, err := nsqClient.NsqV1alpha1().NsqdScales(ndr.Namespace).Get(ndr.Name, metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
klog.Warningf("Get nsqdscale %s/%s error: %v", ndr.Namespace, ndr.Name, err)
return
}
cancel()
klog.Infof("Delete nsqdscale %s/%s success", ndr.Namespace, ndr.Name)
}, constant.ResourceUpdateRetryPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Delete nsqdscale %s/%s error: %v", ndr.Namespace, ndr.Name, ctx.Err())
return ctx.Err()
}
return nil
}
func ScaleNsqAdmin(nsqClient *versioned.Clientset, nasr *types.NsqAdminReplicaUpdateRequest) error {
klog.Infof("Set nsqadmin %s/%s replicas to %d", nasr.Namespace, nasr.Name, nasr.Replicas)
ctx, cancel := context.WithTimeout(context.Background(), *nasr.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
nsqAdmin, err := nsqClient.NsqV1alpha1().NsqAdmins(nasr.Namespace).Get(nasr.Name, metav1.GetOptions{})
if err != nil {
klog.Warningf("Get nsqadmin %s/%s error: %v", nasr.Namespace, nasr.Name, err)
return
}
nsqAdminCopy := nsqAdmin.DeepCopy()
nsqAdminCopy.Spec.Replicas = nasr.Replicas
_, err = nsqClient.NsqV1alpha1().NsqAdmins(nasr.Namespace).Update(nsqAdminCopy)
if err != nil {
klog.Errorf("Update nsqadmin %s/%s error: %v", nasr.Namespace, nasr.Name, err)
return
}
cancel()
}, constant.ResourceUpdateRetryPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Timeout for setting nsqadmin %s/%s replicas", nasr.Namespace, nasr.Name)
return ctx.Err()
}
klog.Infof("Waiting for nsqadmin %s/%s replicas to %d", nasr.Namespace, nasr.Name, nasr.Replicas)
ctx, cancel = context.WithTimeout(context.Background(), *nasr.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
nsqAdmin, err := nsqClient.NsqV1alpha1().NsqAdmins(nasr.Namespace).Get(nasr.Name, metav1.GetOptions{})
if err != nil {
klog.Warningf("Get nsqadmin %s/%s error: %v", nasr.Namespace, nasr.Name, err)
return
}
if nsqAdmin.Status.AvailableReplicas != nasr.Replicas {
klog.V(2).Infof("Spec and status replicas does not match for nsqadmin %s/%s. Old replicas: %v, New replicas: %v",
nasr.Namespace, nasr.Name, nsqAdmin.Status.AvailableReplicas, nasr.Replicas)
return
}
cancel()
}, constant.NsqAdminStatusCheckPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Timeout for waiting nsqadmin %s/%s reach its replicas %d", nasr.Namespace, nasr.Name, nasr.Replicas)
return ctx.Err()
}
return nil
}
func ScaleNsqLookupd(nsqClient *versioned.Clientset, nlsr *types.NsqLookupdReplicaUpdateRequest) error {
klog.Infof("Set nsqlookupd %s/%s replicas to %d", nlsr.Namespace, nlsr.Name, nlsr.Replicas)
ctx, cancel := context.WithTimeout(context.Background(), *nlsr.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
nsqLookupd, err := nsqClient.NsqV1alpha1().NsqLookupds(nlsr.Namespace).Get(nlsr.Name, metav1.GetOptions{})
if err != nil {
klog.Warningf("Get nsqLookupd %s/%s error: %v", nlsr.Namespace, nlsr.Name, err)
return
}
nsqLookupdCopy := nsqLookupd.DeepCopy()
nsqLookupdCopy.Spec.Replicas = nlsr.Replicas
_, err = nsqClient.NsqV1alpha1().NsqLookupds(nlsr.Namespace).Update(nsqLookupdCopy)
if err != nil {
klog.Errorf("Update nsqlookupd %s/%s error: %v", nlsr.Namespace, nlsr.Name, err)
return
}
cancel()
}, constant.ResourceUpdateRetryPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Timeout for setting nsqlookupd %s/%s replicas", nlsr.Namespace, nlsr.Name)
return ctx.Err()
}
klog.Infof("Waiting for nsqlookupd %s/%s replicas to %d", nlsr.Namespace, nlsr.Name, nlsr.Replicas)
ctx, cancel = context.WithTimeout(context.Background(), *nlsr.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
nsqLookupd, err := nsqClient.NsqV1alpha1().NsqLookupds(nlsr.Namespace).Get(nlsr.Name, metav1.GetOptions{})
if err != nil {
klog.Warningf("Get nsqlookupd %s/%s error: %v", nlsr.Namespace, nlsr.Name, err)
return
}
if nsqLookupd.Status.AvailableReplicas != nlsr.Replicas {
klog.V(2).Infof("Spec and status replicas does not match for nsqlookupd %s/%s. Old replicas: %v, New replicas: %v",
nlsr.Namespace, nlsr.Name, nsqLookupd.Status.AvailableReplicas, nlsr.Replicas)
return
}
cancel()
}, constant.NsqLookupdStatusCheckPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Timeout for waiting nsqlookupd %s/%s reach its replicas %d", nlsr.Namespace, nlsr.Name, nlsr.Replicas)
return ctx.Err()
}
return nil
}
func ScaleNsqd(nsqClient *versioned.Clientset, ndsr *types.NsqdReplicaUpdateRequest) error {
klog.Infof("Set nsqd %s/%s replicas to %d", ndsr.Namespace, ndsr.Name, ndsr.Replicas)
ctx, cancel := context.WithTimeout(context.Background(), *ndsr.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
nsqd, err := nsqClient.NsqV1alpha1().Nsqds(ndsr.Namespace).Get(ndsr.Name, metav1.GetOptions{})
if err != nil {
klog.Warningf("Get nsqd %s/%s error: %v", ndsr.Namespace, ndsr.Name, err)
return
}
nsqdCopy := nsqd.DeepCopy()
nsqdCopy.Spec.Replicas = ndsr.Replicas
_, err = nsqClient.NsqV1alpha1().Nsqds(ndsr.Namespace).Update(nsqdCopy)
if err != nil {
klog.Errorf("Update nsqd %s/%s error: %v", ndsr.Namespace, ndsr.Name, err)
return
}
cancel()
}, constant.ResourceUpdateRetryPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Timeout for setting nsqd %s/%s replicas", ndsr.Namespace, ndsr.Name)
return ctx.Err()
}
klog.Infof("Waiting for nsqd %s/%s replicas to %d", ndsr.Namespace, ndsr.Name, ndsr.Replicas)
ctx, cancel = context.WithTimeout(context.Background(), *ndsr.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
nsqd, err := nsqClient.NsqV1alpha1().Nsqds(ndsr.Namespace).Get(ndsr.Name, metav1.GetOptions{})
if err != nil {
klog.Warningf("Get nsqd %s/%s error: %v", ndsr.Namespace, ndsr.Name, err)
return
}
if nsqd.Status.AvailableReplicas != ndsr.Replicas {
klog.V(2).Infof("Spec and status replicas does not match for nsqd %s/%s. Old replicas: %v, New replicas: %v",
ndsr.Namespace, ndsr.Name, nsqd.Status.AvailableReplicas, ndsr.Replicas)
return
}
cancel()
}, constant.NsqdStatusCheckPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Timeout for waiting nsqd %s/%s reach its replicas %d", ndsr.Namespace, ndsr.Name, ndsr.Replicas)
return ctx.Err()
}
return nil
}
func UpdateNsqAdminImage(kubeClient *kubernetes.Clientset, nsqClient *versioned.Clientset, nauir *types.NsqAdminUpdateImageRequest) error {
klog.Infof("Set nsqadmin %s/%s image to %s", nauir.Namespace, nauir.Name, nauir.Image)
ctx, cancel := context.WithTimeout(context.Background(), *nauir.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
nsqAdmin, err := nsqClient.NsqV1alpha1().NsqAdmins(nauir.Namespace).Get(nauir.Name, metav1.GetOptions{})
if err != nil {
klog.Warningf("Get nsqadmin %s/%s error: %v", nauir.Namespace, nauir.Name, err)
return
}
nsqAdminCopy := nsqAdmin.DeepCopy()
nsqAdminCopy.Spec.Image = nauir.Image
_, err = nsqClient.NsqV1alpha1().NsqAdmins(nauir.Namespace).Update(nsqAdminCopy)
if err != nil {
klog.Errorf("Update nsqadmin %s/%s error: %v", nauir.Namespace, nauir.Name, err)
return
}
cancel()
}, constant.ResourceUpdateRetryPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Timeout for setting nsqadmin %s/%s image", nauir.Namespace, nauir.Name)
return ctx.Err()
}
klog.Infof("Waiting for nsqadmin %s/%s image to %s", nauir.Namespace, nauir.Name, nauir.Image)
ctx, cancel = context.WithTimeout(context.Background(), *nauir.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
labelSelector := &metav1.LabelSelector{MatchLabels: map[string]string{"cluster": pkgcommon.NsqAdminDeploymentName(nauir.Name)}}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
if err != nil {
klog.Errorf("Failed to generate label selector for nsqadmin %s/%s: %v", nauir.Namespace, nauir.Name, err)
return
}
podList, err := kubeClient.CoreV1().Pods(nauir.Namespace).List(metav1.ListOptions{
LabelSelector: selector.String(),
})
if err != nil {
klog.Errorf("Failed to list nsqadmin %s/%s pods: %v", nauir.Namespace, nauir.Name, err)
return
}
for _, pod := range podList.Items {
if pod.Spec.Containers[0].Image != nauir.Image {
klog.Infof("Spec and status image does not match for nsqadmin %s/%s. Pod: %v. Spec image: %v, new image: %v",
nauir.Namespace, nauir.Name, pod.Name, pod.Spec.Containers[0].Image, nauir.Image)
return
}
}
nsqAdminDep, err := kubeClient.AppsV1().Deployments(nauir.Namespace).Get(pkgcommon.NsqAdminDeploymentName(nauir.Name), metav1.GetOptions{})
if err != nil {
klog.Errorf("Failed to get nsqadmin %s/%s deployment", nauir.Namespace, nauir.Name)
return
}
if !(nsqAdminDep.Status.ReadyReplicas == *nsqAdminDep.Spec.Replicas && nsqAdminDep.Status.Replicas == nsqAdminDep.Status.ReadyReplicas) {
klog.Errorf("Waiting for nsqadmin %s/%s pods ready", nauir.Namespace, nauir.Name)
return
}
cancel()
}, constant.NsqAdminStatusCheckPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Timeout for waiting nsqadmin %s/%s reach image %s", nauir.Namespace, nauir.Name, nauir.Image)
return ctx.Err()
}
return nil
}
func UpdateNsqLookupdImage(kubeClient *kubernetes.Clientset, nsqClient *versioned.Clientset, nluir *types.NsqLookupdUpdateImageRequest) error {
klog.Infof("Set nsqlookupd %s/%s image to %s", nluir.Namespace, nluir.Name, nluir.Image)
ctx, cancel := context.WithTimeout(context.Background(), *nluir.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
nsqLookupd, err := nsqClient.NsqV1alpha1().NsqLookupds(nluir.Namespace).Get(nluir.Name, metav1.GetOptions{})
if err != nil {
klog.Warningf("Get nsqlookupd %s/%s error: %v", nluir.Namespace, nluir.Name, err)
return
}
nsqLookupdCopy := nsqLookupd.DeepCopy()
nsqLookupdCopy.Spec.Image = nluir.Image
_, err = nsqClient.NsqV1alpha1().NsqLookupds(nluir.Namespace).Update(nsqLookupdCopy)
if err != nil {
klog.Errorf("Update nsqlookupd %s/%s error: %v", nluir.Namespace, nluir.Name, err)
return
}
cancel()
}, constant.ResourceUpdateRetryPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Timeout for setting nsqlookupd %s/%s image", nluir.Namespace, nluir.Name)
return ctx.Err()
}
klog.Infof("Waiting for nsqlookupd %s/%s image to %s", nluir.Namespace, nluir.Name, nluir.Image)
ctx, cancel = context.WithTimeout(context.Background(), *nluir.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
labelSelector := &metav1.LabelSelector{MatchLabels: map[string]string{"cluster": pkgcommon.NsqLookupdDeploymentName(nluir.Name)}}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
if err != nil {
klog.Errorf("Failed to generate label selector for nsqlookupd %s/%s: %v", nluir.Namespace, nluir.Name, err)
return
}
podList, err := kubeClient.CoreV1().Pods(nluir.Namespace).List(metav1.ListOptions{
LabelSelector: selector.String(),
})
if err != nil {
klog.Errorf("Failed to list nsqlookupd %s/%s pods: %v", nluir.Namespace, nluir.Name, err)
return
}
for _, pod := range podList.Items {
if pod.Spec.Containers[0].Image != nluir.Image {
klog.Infof("Spec and status image does not match for nsqlookupd %s/%s. Pod: %v. Spec image: %v, new image: %v",
nluir.Namespace, nluir.Name, pod.Name, pod.Spec.Containers[0].Image, nluir.Image)
return
}
}
nsqLookupdDep, err := kubeClient.AppsV1().Deployments(nluir.Namespace).Get(pkgcommon.NsqLookupdDeploymentName(nluir.Name), metav1.GetOptions{})
if err != nil {
klog.Errorf("Failed to get nsqlookupd %s/%s deployment", nluir.Namespace, nluir.Name)
return
}
if !(nsqLookupdDep.Status.ReadyReplicas == *nsqLookupdDep.Spec.Replicas && nsqLookupdDep.Status.Replicas == nsqLookupdDep.Status.ReadyReplicas) {
klog.Errorf("Waiting for nsqlookupd %s/%s pods ready", nluir.Namespace, nluir.Name)
return
}
cancel()
}, constant.NsqLookupdStatusCheckPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Timeout for waiting nsqlookupd %s/%s reach image %s", nluir.Namespace, nluir.Name, nluir.Image)
return ctx.Err()
}
return nil
}
func UpdateNsqdImage(kubeClient *kubernetes.Clientset, nsqClient *versioned.Clientset, nduir *types.NsqdUpdateImageRequest) error {
klog.Infof("Set nsqd %s/%s image to %s", nduir.Namespace, nduir.Name, nduir.Image)
ctx, cancel := context.WithTimeout(context.Background(), *nduir.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
nsqd, err := nsqClient.NsqV1alpha1().Nsqds(nduir.Namespace).Get(nduir.Name, metav1.GetOptions{})
if err != nil {
klog.Warningf("Get nsqd %s/%s error: %v", nduir.Namespace, nduir.Name, err)
return
}
nsqdCopy := nsqd.DeepCopy()
nsqdCopy.Spec.Image = nduir.Image
_, err = nsqClient.NsqV1alpha1().Nsqds(nduir.Namespace).Update(nsqdCopy)
if err != nil {
klog.Errorf("Update nsqd %s/%s error: %v", nduir.Namespace, nduir.Name, err)
return
}
cancel()
}, constant.ResourceUpdateRetryPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Timeout for setting nsqd %s/%s image", nduir.Namespace, nduir.Name)
return ctx.Err()
}
klog.Infof("Waiting for nsqd %s/%s image to %s", nduir.Namespace, nduir.Name, nduir.Image)
ctx, cancel = context.WithTimeout(context.Background(), *nduir.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
labelSelector := &metav1.LabelSelector{MatchLabels: map[string]string{"cluster": pkgcommon.NsqdStatefulSetName(nduir.Name)}}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
if err != nil {
klog.Errorf("Failed to generate label selector for nsqd %s/%s: %v", nduir.Namespace, nduir.Name, err)
return
}
podList, err := kubeClient.CoreV1().Pods(nduir.Namespace).List(metav1.ListOptions{
LabelSelector: selector.String(),
})
if err != nil {
klog.Errorf("Failed to list nsqd %s/%s pods: %v", nduir.Namespace, nduir.Name, err)
return
}
for _, pod := range podList.Items {
if pod.Spec.Containers[0].Image != nduir.Image {
klog.Infof("Spec and status image does not match for nsqd %s/%s. Pod: %v. Spec image: %v, new image: %v",
nduir.Namespace, nduir.Name, pod.Name, pod.Spec.Containers[0].Image, nduir.Image)
return
}
}
nsqdSs, err := kubeClient.AppsV1().StatefulSets(nduir.Namespace).Get(pkgcommon.NsqdStatefulSetName(nduir.Name), metav1.GetOptions{})
if err != nil {
klog.Errorf("Failed to get nsqd %s/%s statefulset", nduir.Namespace, nduir.Name)
return
}
if nsqdSs.Status.ReadyReplicas != *nsqdSs.Spec.Replicas {
klog.Errorf("Waiting for nsqd %s/%s pods ready", nduir.Namespace, nduir.Name)
return
}
cancel()
}, constant.NsqdStatusCheckPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Timeout for waiting nsqd %s/%s reach image %s", nduir.Namespace, nduir.Name, nduir.Image)
return ctx.Err()
}
return nil
}
func AdjustNsqdConfig(kubeClient *kubernetes.Clientset, ndcr *types.NsqdConfigRequest) error {
klog.Infof("Update nsqd %s/%s config to %#v", ndcr.Namespace, ndcr.Name, ndcr.AssembleNsqdConfigMap([]string{}).Data)
ctx, cancel := context.WithTimeout(context.Background(), *ndcr.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
// Create nsqd configmap
labelSelector := &metav1.LabelSelector{MatchLabels: map[string]string{"cluster": pkgcommon.NsqLookupdDeploymentName(ndcr.Name)}}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
if err != nil {
klog.Errorf("Gen label selector for nsqlookupd %s/%s pods error: %v", ndcr.Namespace, ndcr.Name, err)
return
}
podList, err := kubeClient.CoreV1().Pods(ndcr.Namespace).List(metav1.ListOptions{
LabelSelector: selector.String(),
})
if err != nil {
klog.Errorf("List nsqlookupd %s/%s pods error: %v", ndcr.Namespace, ndcr.Name, err)
return
}
var addresses []string
for _, pod := range podList.Items {
addresses = append(addresses, fmt.Sprintf("%s:%v", pod.Status.PodIP, constant.NsqLookupdTcpPort))
}
nsqdCMWanted := ndcr.AssembleNsqdConfigMap(addresses)
nsqdCM, err := kubeClient.CoreV1().ConfigMaps(ndcr.Namespace).Get(pkgcommon.NsqdConfigMapName(ndcr.Name), metav1.GetOptions{})
if err != nil {
klog.Warningf("Get nsqd %s/%s configmap error: %v", ndcr.Namespace, ndcr.Name, err)
return
}
nsqdCMNew := nsqdCM.DeepCopy()
nsqdCMNew.Data = nsqdCMWanted.Data
_, err = kubeClient.CoreV1().ConfigMaps(ndcr.Namespace).Update(nsqdCMNew)
if err != nil {
klog.Errorf("Update nsqd %s/%s configmap error: %v", ndcr.Namespace, ndcr.Name, err)
}
klog.Infof("Update nsqd %s/%s configmap success", ndcr.Namespace, ndcr.Name)
cancel()
}, constant.ConfigMapStatusCheckPeriod)
if ctx.Err() != context.Canceled {
klog.Errorf("Timeout for waiting nsqd %s/%s configmap to be ready", ndcr.Namespace, ndcr.Name)
return ctx.Err()
}
configmap, err := kubeClient.CoreV1().ConfigMaps(ndcr.Namespace).Get(pkgcommon.NsqdConfigMapName(ndcr.Name), metav1.GetOptions{})
if err != nil {
klog.Errorf("Get configmap for nsqd %s/%s error: %v", ndcr.Namespace, ndcr.Name, err)
return err
}
configmapHash, err := pkgcommon.Hash(configmap.Data)
if err != nil {
klog.Errorf("Hash configmap data for nsqd %s/%s error: %v", ndcr.Namespace, ndcr.Name, err)
return err
}
ctx, cancel = context.WithTimeout(context.Background(), *ndcr.WaitTimeout)
wait.UntilWithContext(ctx, func(ctx context.Context) {
labelSelector := &metav1.LabelSelector{MatchLabels: map[string]string{"cluster": pkgcommon.NsqdStatefulSetName(ndcr.Name)}}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
if err != nil {
klog.Errorf("Failed to generate label selector for nsqd %s/%s: %v", ndcr.Namespace, ndcr.Name, err)
return
}
podList, err := kubeClient.CoreV1().Pods(ndcr.Namespace).List(metav1.ListOptions{
LabelSelector: selector.String(),
})
if err != nil {
klog.Errorf("Failed to list nsqd %s/%s pods: %v", ndcr.Namespace, ndcr.Name, err)
return