-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
ceph_installer.go
1060 lines (932 loc) · 37.5 KB
/
ceph_installer.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 2016 The Rook Authors. All rights reserved.
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 installer
import (
"context"
"flag"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path"
"testing"
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/daemon/ceph/client"
"github.com/rook/rook/pkg/operator/ceph/cluster"
"github.com/rook/rook/tests/framework/utils"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
kerrors "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"
)
const (
// test with the latest octopus build
octopusTestImage = "quay.io/ceph/ceph:v15"
// test with the latest pacific build
pacificTestImage = "quay.io/ceph/ceph:v16"
// test with the current development version of Pacific
pacificDevelTestImage = "quay.io/ceph/daemon-base:latest-pacific-devel"
octopusDevelTestImage = "quay.io/ceph/daemon-base:latest-octopus-devel"
// test with the latest master image
masterTestImage = "quay.io/ceph/daemon-base:latest-master-devel"
cephOperatorLabel = "app=rook-ceph-operator"
defaultclusterName = "test-cluster"
clusterCustomSettings = `
[global]
osd_pool_default_size = 1
bdev_flock_retry = 20
`
volumeReplicationVersion = "v0.1.0"
)
var (
OctopusVersion = cephv1.CephVersionSpec{Image: octopusTestImage}
OctopusDevelVersion = cephv1.CephVersionSpec{Image: octopusDevelTestImage}
PacificVersion = cephv1.CephVersionSpec{Image: pacificTestImage}
PacificDevelVersion = cephv1.CephVersionSpec{Image: pacificDevelTestImage}
MasterVersion = cephv1.CephVersionSpec{Image: masterTestImage, AllowUnsupported: true}
volumeReplicationBaseURL = fmt.Sprintf("https://raw.githubusercontent.com/csi-addons/volume-replication-operator/%s/config/crd/bases/", volumeReplicationVersion)
volumeReplicationCRDURL = volumeReplicationBaseURL + "replication.storage.openshift.io_volumereplications.yaml"
volumeReplicationClassCRDURL = volumeReplicationBaseURL + "replication.storage.openshift.io_volumereplicationclasses.yaml"
)
// CephInstaller wraps installing and uninstalling rook on a platform
type CephInstaller struct {
settings *TestCephSettings
Manifests CephManifests
k8shelper *utils.K8sHelper
hostPathToDelete string
helmHelper *utils.HelmHelper
k8sVersion string
changeHostnames bool
T func() *testing.T
}
func ReturnCephVersion() cephv1.CephVersionSpec {
switch os.Getenv("CEPH_SUITE_VERSION") {
case "master":
return MasterVersion
case "pacific-devel":
return PacificDevelVersion
default:
return PacificVersion
}
}
// CreateCephOperator creates rook-operator via kubectl
func (h *CephInstaller) CreateCephOperator() (err error) {
// creating rook resources
logger.Info("Creating Rook CRDs")
resources := h.Manifests.GetCRDs(h.k8shelper)
if _, err = h.k8shelper.KubectlWithStdin(resources, createFromStdinArgs...); err != nil {
return err
}
if h.changeHostnames {
// give nodes a hostname that is different from its k8s node name to confirm that all the daemons will be initialized properly
err = h.k8shelper.ChangeHostnames()
assert.NoError(h.T(), err)
}
// The operator namespace needs to be created explicitly, while the cluster namespace is created with the common.yaml
if err := h.k8shelper.CreateNamespace(h.settings.OperatorNamespace); err != nil {
return err
}
// Create the namespace and RBAC before starting the operator
_, err = h.k8shelper.KubectlWithStdin(h.Manifests.GetCommon(), createFromStdinArgs...)
if err != nil {
return errors.Errorf("Failed to create rook-operator pod: %v ", err)
}
err = h.startAdmissionController()
if err != nil {
return errors.Errorf("Failed to start admission controllers: %v", err)
}
if err := h.CreateVolumeReplicationCRDs(); err != nil {
return errors.Wrap(err, "failed to create volume replication CRDs")
}
_, err = h.k8shelper.KubectlWithStdin(h.Manifests.GetOperator(), createFromStdinArgs...)
if err != nil {
return errors.Errorf("Failed to create rook-operator pod: %v", err)
}
logger.Infof("Rook operator started")
return nil
}
func (h *CephInstaller) CreateVolumeReplicationCRDs() (err error) {
if !h.Manifests.Settings().EnableVolumeReplication {
logger.Info("volume replication CRDs skipped")
return nil
}
if !h.k8shelper.VersionAtLeast("v1.16.0") {
logger.Info("volume replication CRDs skipped on older than k8s 1.16")
return nil
}
logger.Info("Creating volume replication CRDs")
if _, err := h.k8shelper.KubectlWithStdin(readManifestFromURL(volumeReplicationCRDURL), createFromStdinArgs...); err != nil {
return errors.Wrap(err, "failed to create volumereplication CRD")
}
if _, err := h.k8shelper.KubectlWithStdin(readManifestFromURL(volumeReplicationClassCRDURL), createFromStdinArgs...); err != nil {
return errors.Wrap(err, "failed to create volumereplicationclass CRD")
}
return nil
}
func (h *CephInstaller) startAdmissionController() error {
if !h.k8shelper.VersionAtLeast("v1.16.0") {
logger.Info("skipping the admission controller on K8s version older than v1.16")
return nil
}
if !h.settings.EnableAdmissionController {
logger.Info("skipping admission controller for this test suite")
return nil
}
if utils.IsPlatformOpenShift() {
logger.Info("skipping the admission controller on OpenShift")
return nil
}
rootPath, err := utils.FindRookRoot()
if err != nil {
return errors.Errorf("failed to find rook root. %v", err)
}
userHome, err := os.UserHomeDir()
if err != nil {
return errors.Errorf("failed to find user home directory. %v", err)
}
scriptPath := path.Join(rootPath, "tests/scripts/deploy_admission_controller_test.sh")
err = h.k8shelper.MakeContext().Executor.ExecuteCommandWithEnv([]string{fmt.Sprintf("NAMESPACE=%s", h.settings.OperatorNamespace), fmt.Sprintf("HOME=%s", userHome)}, "bash", scriptPath)
if err != nil {
return err
}
return nil
}
func (h *CephInstaller) WaitForToolbox(namespace string) error {
if err := h.k8shelper.WaitForLabeledPodsToRun("app=rook-ceph-tools", namespace); err != nil {
return errors.Wrap(err, "Rook Toolbox couldn't start")
}
logger.Infof("Rook Toolbox started")
podNames, err := h.k8shelper.GetPodNamesForApp("rook-ceph-tools", namespace)
assert.NoError(h.T(), err)
for _, podName := range podNames {
// All e2e tests should run ceph commands in the toolbox since we are not inside a container
logger.Infof("found active toolbox pod: %q", podName)
client.RunAllCephCommandsInToolboxPod = podName
return nil
}
return errors.Errorf("could not find toolbox pod")
}
// CreateRookToolbox creates rook-ceph-tools via kubectl
func (h *CephInstaller) CreateRookToolbox(manifests CephManifests) (err error) {
logger.Infof("Starting Rook toolbox")
_, err = h.k8shelper.KubectlWithStdin(manifests.GetToolbox(), createFromStdinArgs...)
if err != nil {
return errors.Wrap(err, "failed to create rook-toolbox pod")
}
return h.WaitForToolbox(manifests.Settings().Namespace)
}
// Execute a command in the ceph toolbox
func (h *CephInstaller) Execute(command string, parameters []string, namespace string) (error, string) {
clusterInfo := client.AdminTestClusterInfo(namespace)
cmd, args := client.FinalizeCephCommandArgs(command, clusterInfo, parameters, h.k8shelper.MakeContext().ConfigDir)
result, err := h.k8shelper.MakeContext().Executor.ExecuteCommandWithOutput(cmd, args...)
if err != nil {
logger.Warningf("Error executing command %q: <%v>", command, err)
return err, result
}
return nil, result
}
// CreateCephCluster creates rook cluster via kubectl
func (h *CephInstaller) CreateCephCluster() error {
ctx := context.TODO()
var err error
h.settings.DataDirHostPath, err = h.initTestDir(h.settings.Namespace)
if err != nil {
return errors.Errorf("failed to create test dir. %+v", err)
}
logger.Infof("Creating cluster with settings: %+v", h.settings)
logger.Infof("Creating custom ceph.conf settings")
customSettings := map[string]string{"config": clusterCustomSettings}
customCM := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "rook-config-override",
Namespace: h.settings.Namespace,
},
Data: customSettings,
}
if _, err := h.k8shelper.Clientset.CoreV1().ConfigMaps(h.settings.Namespace).Create(ctx, customCM, metav1.CreateOptions{}); err != nil {
return errors.Errorf("failed to create custom ceph.conf. %+v", err)
}
logger.Info("Starting Rook Cluster")
rookCluster := h.Manifests.GetCephCluster()
logger.Info(rookCluster)
maxTry := 10
for i := 0; i < maxTry; i++ {
_, err := h.k8shelper.KubectlWithStdin(rookCluster, createFromStdinArgs...)
if err == nil {
break
}
if i == maxTry-1 {
return errors.Errorf("failed to create rook cluster. %v", err)
}
logger.Infof("failed to create rook cluster, trying again... %v", err)
time.Sleep(5 * time.Second)
}
return nil
}
func (h *CephInstaller) waitForCluster() error {
if err := h.k8shelper.WaitForPodCount("app=rook-ceph-mon", h.settings.Namespace, h.settings.Mons); err != nil {
return err
}
if err := h.k8shelper.WaitForPodCount("app=rook-ceph-mgr", h.settings.Namespace, 1); err != nil {
return err
}
if !h.settings.SkipOSDCreation {
if err := h.k8shelper.WaitForPodCount("app=rook-ceph-osd", h.settings.Namespace, 1); err != nil {
return err
}
}
if h.settings.UseCrashPruner {
if err := h.k8shelper.WaitForCronJob("rook-ceph-crashcollector-pruner", h.settings.Namespace); err != nil {
return err
}
}
logger.Infof("Rook Cluster started")
if !h.settings.SkipOSDCreation {
return h.k8shelper.WaitForLabeledPodsToRun("app=rook-ceph-osd", h.settings.Namespace)
}
return nil
}
// CreateRookExternalCluster creates rook external cluster via kubectl
func (h *CephInstaller) CreateRookExternalCluster(externalManifests CephManifests) error {
var err error
externalSettings := externalManifests.Settings()
externalSettings.DataDirHostPath, err = h.initTestDir(externalSettings.Namespace)
if err != nil {
return errors.Errorf("failed to create test dir. %+v", err)
}
logger.Infof("Creating external cluster %q with core storage namespace %q", externalSettings.Namespace, h.settings.Namespace)
logger.Infof("Creating external cluster roles")
roles := externalManifests.GetCommonExternal()
if _, err := h.k8shelper.KubectlWithStdin(roles, createFromStdinArgs...); err != nil {
return errors.Wrap(err, "failed to create cluster roles")
}
// Inject connection information from the first cluster
logger.Info("Injecting cluster connection information")
err = h.injectRookExternalClusterInfo(externalSettings)
if err != nil {
return errors.Wrap(err, "failed to inject cluster information into the external cluster")
}
// Start the external cluster
logger.Infof("Starting Rook External Cluster with yaml")
rookCluster := externalManifests.GetExternalCephCluster()
if _, err := h.k8shelper.KubectlWithStdin(rookCluster, createFromStdinArgs...); err != nil {
return errors.Wrap(err, "failed to create rook external cluster")
}
logger.Infof("Running toolbox on external namespace %q", externalSettings.Namespace)
if err := h.CreateRookToolbox(externalManifests); err != nil {
return errors.Wrap(err, "failed to start toolbox on external cluster")
}
var clusterStatus cephv1.ClusterStatus
for i := 0; i < 8; i++ {
ctx := context.TODO()
clusterResource, err := h.k8shelper.RookClientset.CephV1().CephClusters(externalSettings.Namespace).Get(ctx, externalSettings.ClusterName, metav1.GetOptions{})
if err != nil {
logger.Warningf("failed to get external cluster CR, retrying. %v", err)
time.Sleep(time.Second * 5)
continue
}
clusterStatus = clusterResource.Status
clusterPhase := string(clusterResource.Status.Phase)
if clusterPhase != "Connected" {
logger.Warningf("failed to start external cluster, retrying, state: %v", clusterResource.Status)
time.Sleep(time.Second * 5)
} else if clusterPhase == "Connected" {
logger.Info("Rook external cluster connected")
return nil
}
}
return errors.Errorf("failed to start external cluster, state: %v", clusterStatus)
}
// InjectRookExternalClusterInfo inject connection information for an external cluster
func (h *CephInstaller) injectRookExternalClusterInfo(externalSettings *TestCephSettings) error {
ctx := context.TODO()
// get config map
cm, err := h.GetRookExternalClusterMonConfigMap()
if err != nil {
return errors.Errorf("failed to get configmap. %v", err)
}
// create config map
_, err = h.k8shelper.Clientset.CoreV1().ConfigMaps(externalSettings.Namespace).Create(ctx, cm, metav1.CreateOptions{})
if err != nil {
return errors.Errorf("failed to create configmap. %v", err)
}
// get secret
secret, err := h.GetRookExternalClusterMonSecret()
if err != nil {
return errors.Errorf("failed to get secret. %v", err)
}
// create secret
_, err = h.k8shelper.Clientset.CoreV1().Secrets(externalSettings.Namespace).Create(ctx, secret, metav1.CreateOptions{})
if err != nil {
return errors.Errorf("failed to create secret. %v", err)
}
return nil
}
// GetRookExternalClusterMonConfigMap gets the monitor kubernetes configmap of the external cluster
func (h *CephInstaller) GetRookExternalClusterMonConfigMap() (*v1.ConfigMap, error) {
ctx := context.TODO()
configMapName := "rook-ceph-mon-endpoints"
externalCM, err := h.k8shelper.Clientset.CoreV1().ConfigMaps(h.settings.Namespace).Get(ctx, configMapName, metav1.GetOptions{})
if err != nil {
return nil, errors.Errorf("failed to get secret. %v", err)
}
newCM := &v1.ConfigMap{}
newCM.Name = externalCM.Name
newCM.Data = externalCM.Data
return newCM, nil
}
// GetRookExternalClusterMonSecret gets the monitor kubernetes secret of the external cluster
func (h *CephInstaller) GetRookExternalClusterMonSecret() (*v1.Secret, error) {
ctx := context.TODO()
secretName := "rook-ceph-mon" //nolint:gosec // We safely suppress gosec in tests file
externalSecret, err := h.k8shelper.Clientset.CoreV1().Secrets(h.settings.Namespace).Get(ctx, secretName, metav1.GetOptions{})
if err != nil {
return nil, errors.Errorf("failed to get secret. %v", err)
}
newSecret := &v1.Secret{}
newSecret.Name = externalSecret.Name
newSecret.Data = externalSecret.Data
return newSecret, nil
}
func (h *CephInstaller) initTestDir(namespace string) (string, error) {
val, err := baseTestDir()
if err != nil {
return "", err
}
h.hostPathToDelete = path.Join(val, "rook-test")
testDir := path.Join(h.hostPathToDelete, namespace)
// skip the test dir creation if we are not running under "/data"
if val != "/data" {
// Create the test dir on the local host
if err := os.MkdirAll(testDir, 0777); err != nil {
return "", err
}
var err error
if testDir, err = ioutil.TempDir(testDir, "test-"); err != nil {
return "", err
}
} else {
// Compose a random test directory name without actually creating it since not running on the localhost
r := rand.Int() //nolint:gosec // We safely suppress gosec in tests file
testDir = path.Join(testDir, fmt.Sprintf("test-%d", r))
}
return testDir, nil
}
// GetNodeHostnames returns the list of nodes in the k8s cluster
func (h *CephInstaller) GetNodeHostnames() ([]string, error) {
ctx := context.TODO()
nodes, err := h.k8shelper.Clientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, errors.Errorf("failed to get k8s nodes. %+v", err)
}
var names []string
for _, node := range nodes.Items {
names = append(names, node.Labels[v1.LabelHostname])
}
return names, nil
}
func (h *CephInstaller) installRookOperator() (bool, error) {
ctx := context.TODO()
var err error
startDiscovery := h.settings.EnableDiscovery
h.k8shelper.CreateAnonSystemClusterBinding()
// Create rook operator
logger.Infof("Starting Rook Operator")
if h.settings.UseHelm {
// enable the discovery daemonset with the helm chart
startDiscovery = true
err := h.CreateRookOperatorViaHelm(map[string]interface{}{
"enableDiscoveryDaemon": true,
"image": map[string]interface{}{"tag": LocalBuildTag},
})
if err != nil {
return false, errors.Wrap(err, "failed to configure helm")
}
} else {
err := h.CreateCephOperator()
if err != nil {
return false, errors.Wrap(err, "failed to configure ceph operator")
}
}
if !h.k8shelper.IsPodInExpectedState("rook-ceph-operator", h.settings.OperatorNamespace, "Running") {
logger.Error("rook-ceph-operator is not running")
h.k8shelper.GetLogsFromNamespace(h.settings.OperatorNamespace, "test-setup", utils.TestEnvName())
logger.Error("rook-ceph-operator is not Running, abort!")
return false, err
}
discovery, err := h.k8shelper.Clientset.AppsV1().DaemonSets(h.settings.OperatorNamespace).Get(ctx, "rook-discover", metav1.GetOptions{})
if startDiscovery {
assert.NoError(h.T(), err)
assert.NotNil(h.T(), discovery)
} else {
assert.Error(h.T(), err)
assert.True(h.T(), kerrors.IsNotFound(err))
}
return true, nil
}
func (h *CephInstaller) InstallRook() (bool, error) {
if h.settings.RookVersion != LocalBuildTag {
// make sure we have the images from a previous release locally so the test doesn't hit a timeout
assert.NoError(h.T(), h.k8shelper.GetDockerImage("rook/ceph:"+h.settings.RookVersion))
}
assert.NoError(h.T(), h.k8shelper.GetDockerImage(h.settings.CephVersion.Image))
k8sversion := h.k8shelper.GetK8sServerVersion()
logger.Infof("Installing rook on K8s %s", k8sversion)
success, err := h.installRookOperator()
if err != nil {
return false, err
}
if !success {
return false, nil
}
if h.settings.UseHelm {
err = h.CreateRookCephClusterViaHelm(map[string]interface{}{
"image": "rook/ceph:" + LocalBuildTag,
})
if err != nil {
return false, errors.Wrap(err, "failed to install ceph cluster using Helm")
}
} else {
// Create rook cluster
err = h.CreateCephCluster()
if err != nil {
logger.Errorf("Cluster %q install failed. %v", h.settings.Namespace, err)
return false, err
}
}
logger.Info("Waiting for Rook Cluster")
if err := h.waitForCluster(); err != nil {
return false, err
}
if h.settings.UseHelm {
err := h.WaitForToolbox(h.settings.Namespace)
if err != nil {
return false, err
}
} else {
err = h.CreateRookToolbox(h.Manifests)
if err != nil {
return false, errors.Wrapf(err, "failed to install toolbox in cluster %s", h.settings.Namespace)
}
}
const loopCount = 20
for i := 0; i < loopCount; i++ {
_, err = client.Status(h.k8shelper.MakeContext(), client.AdminTestClusterInfo(h.settings.Namespace))
if err == nil {
logger.Infof("toolbox ready")
break
}
logger.Infof("toolbox is not ready")
if i == loopCount-1 {
return false, errors.Errorf("toolbox cannot connect to cluster")
}
time.Sleep(5 * time.Second)
}
if h.settings.UseHelm {
logger.Infof("Confirming ceph cluster installed correctly")
if err := h.ConfirmHelmClusterInstalledCorrectly(); err != nil {
return false, errors.Wrap(err, "the ceph cluster storage CustomResources did not install correctly")
}
if !h.settings.RetainHelmDefaultStorageCRs {
err = h.RemoveRookCephClusterHelmDefaultCustomResources()
if err != nil {
return false, errors.Wrap(err, "failed to remove the default helm CustomResources")
}
}
}
logger.Infof("installed rook operator and cluster %s on k8s %s", h.settings.Namespace, h.k8sVersion)
return true, nil
}
// UninstallRook uninstalls rook from k8s
func (h *CephInstaller) UninstallRook() {
h.UninstallRookFromMultipleNS(h.Manifests)
}
// UninstallRookFromMultipleNS uninstalls rook from multiple namespaces in k8s
func (h *CephInstaller) UninstallRookFromMultipleNS(manifests ...CephManifests) {
ctx := context.TODO()
var clusterNamespaces []string
for _, manifest := range manifests {
clusterNamespaces = append(clusterNamespaces, manifest.Settings().Namespace)
}
// Gather logs after status checks
h.GatherAllRookLogs(h.T().Name(), append([]string{h.settings.OperatorNamespace}, clusterNamespaces...)...)
// If test failed do not teardown and leave the cluster in the state it is
if h.T().Failed() {
logger.Info("one of the tests failed, leaving the cluster in its bad shape for investigation")
return
}
logger.Infof("Uninstalling Rook")
var err error
skipOperatorCleanup := false
for _, manifest := range manifests {
namespace := manifest.Settings().Namespace
clusterName := manifest.Settings().ClusterName
if manifest.Settings().SkipCleanupPolicy && manifest.Settings().SkipClusterCleanup {
logger.Infof("SKIPPING ALL CLEANUP for namespace %q", namespace)
skipOperatorCleanup = true
continue
}
testCleanupPolicy := !h.settings.UseHelm && !manifest.Settings().IsExternal && !manifest.Settings().SkipCleanupPolicy
if testCleanupPolicy {
// Add cleanup policy to the core ceph cluster
err = h.addCleanupPolicy(namespace, clusterName)
if err != nil {
assert.NoError(h.T(), err)
// no need to check for cleanup policy later if it already failed
testCleanupPolicy = false
}
// if the test passed, check that the ceph status is HEALTH_OK before we tear the cluster down
if !h.T().Failed() {
// Only check the Ceph status for the core cluster
// The check won't work for an external cluster since the core cluster is already gone
h.checkCephHealthStatus()
}
}
// The pool CRs should already be removed by the tests that created them
pools, err := h.k8shelper.RookClientset.CephV1().CephBlockPools(namespace).List(ctx, metav1.ListOptions{})
assert.NoError(h.T(), err, "failed to retrieve pool CRs")
for _, pool := range pools.Items {
logger.Infof("found pools: %v", pools)
assert.Fail(h.T(), fmt.Sprintf("pool %q still exists", pool.Name))
// Get the operator log
h.GatherAllRookLogs(h.T().Name()+"poolcheck", h.settings.OperatorNamespace)
}
if h.settings.UseHelm {
// helm rook-ceph-cluster cleanup
if h.settings.RetainHelmDefaultStorageCRs {
err = h.RemoveRookCephClusterHelmDefaultCustomResources()
if err != nil {
assert.Fail(h.T(), "failed to remove the default helm CustomResources")
}
}
err = h.helmHelper.DeleteLocalRookHelmChart(namespace, CephClusterChartName)
checkError(h.T(), err, fmt.Sprintf("cannot uninstall helm chart %s", CephClusterChartName))
} else {
err = h.k8shelper.DeleteResourceAndWait(false, "-n", namespace, "cephcluster", clusterName)
checkError(h.T(), err, fmt.Sprintf("cannot remove cluster %s", namespace))
clusterDeleteRetries := 0
crdCheckerFunc := func() error {
_, err := h.k8shelper.RookClientset.CephV1().CephClusters(namespace).Get(ctx, clusterName, metav1.GetOptions{})
clusterDeleteRetries++
if clusterDeleteRetries > 10 {
// If the operator really isn't going to remove the finalizer, just force remove it
h.removeClusterFinalizers(namespace, clusterName)
}
return err
}
err = h.k8shelper.WaitForCustomResourceDeletion(namespace, clusterName, crdCheckerFunc)
checkError(h.T(), err, fmt.Sprintf("failed to wait for cluster crd %s deletion", namespace))
}
if testCleanupPolicy {
err = h.waitForCleanupJobs(namespace)
if err != nil {
assert.NoError(h.T(), err)
h.GatherAllRookLogs(h.T().Name()+"cleanup-job", append([]string{h.settings.OperatorNamespace}, clusterNamespaces...)...)
}
}
// helm operator cleanup
if h.settings.UseHelm {
err = h.helmHelper.DeleteLocalRookHelmChart(namespace, OperatorChartName)
checkError(h.T(), err, fmt.Sprintf("cannot uninstall helm chart %s", OperatorChartName))
// delete the entire namespace (in non-helm installs it's removed with the common.yaml)
err = h.k8shelper.DeleteResourceAndWait(false, "namespace", namespace)
checkError(h.T(), err, fmt.Sprintf("cannot delete namespace %s", namespace))
continue
}
// Skip the remainder of cluster cleanup if desired
if manifest.Settings().SkipClusterCleanup {
logger.Infof("SKIPPING CLUSTER CLEANUP")
skipOperatorCleanup = true
continue
}
// non-helm cleanup
if manifest.Settings().IsExternal {
logger.Infof("Deleting all the resources in the common external manifest")
_, err = h.k8shelper.KubectlWithStdin(manifest.GetCommonExternal(), deleteFromStdinArgs...)
if err != nil {
logger.Errorf("failed to remove common external resources. %v", err)
} else {
logger.Infof("done deleting all the resources in the common external manifest")
}
} else {
h.k8shelper.PrintResources(namespace, "cephblockpools.ceph.rook.io")
h.k8shelper.PrintResources(namespace, "cephclients.ceph.rook.io")
h.k8shelper.PrintResources(namespace, "cephclusters.ceph.rook.io")
h.k8shelper.PrintResources(namespace, "cephfilesystemmirrors.ceph.rook.io")
h.k8shelper.PrintResources(namespace, "cephfilesystems.ceph.rook.io")
h.k8shelper.PrintResources(namespace, "cephnfses.ceph.rook.io")
h.k8shelper.PrintResources(namespace, "cephobjectrealms.ceph.rook.io")
h.k8shelper.PrintResources(namespace, "cephobjectstores.ceph.rook.io")
h.k8shelper.PrintResources(namespace, "cephobjectstoreusers.ceph.rook.io")
h.k8shelper.PrintResources(namespace, "cephobjectzonegroups.ceph.rook.io")
h.k8shelper.PrintResources(namespace, "cephobjectzones.ceph.rook.io")
h.k8shelper.PrintResources(namespace, "cephrbdmirrors.ceph.rook.io")
h.k8shelper.PrintResources(namespace, "objectbucketclaims.ceph.rook.io")
h.k8shelper.PrintResources(namespace, "objectbuckets.ceph.rook.io")
h.k8shelper.PrintPodStatus(namespace)
h.k8shelper.PrintPVs(true)
logger.Infof("Deleting all the resources in the common manifest")
_, err = h.k8shelper.KubectlWithStdin(h.Manifests.GetCommon(), deleteFromStdinArgs...)
if err != nil {
logger.Errorf("failed to remove common manifest. %v", err)
} else {
logger.Infof("done deleting all the resources in the common manifest")
}
}
}
// Skip the remainder of cluster cleanup if desired
if skipOperatorCleanup {
logger.Infof("SKIPPING OPERATOR CLEANUP")
return
}
if !h.settings.UseHelm {
logger.Infof("Deleting all the resources in the operator manifest")
_, err = h.k8shelper.KubectlWithStdin(h.Manifests.GetOperator(), deleteFromStdinArgs...)
if err != nil {
logger.Errorf("failed to remove operator resources. %v", err)
} else {
logger.Infof("done deleting all the resources in the operator manifest")
}
}
logger.Info("removing the CRDs")
_, err = h.k8shelper.KubectlWithStdin(h.Manifests.GetCRDs(h.k8shelper), deleteFromStdinArgs...)
if err != nil {
logger.Errorf("failed to remove CRDS. %v", err)
} else {
logger.Infof("done deleting all the CRDs")
}
err = h.k8shelper.DeleteResourceAndWait(false, "namespace", h.settings.OperatorNamespace)
checkError(h.T(), err, fmt.Sprintf("cannot delete operator namespace %s", h.settings.OperatorNamespace))
err = h.k8shelper.Clientset.AdmissionregistrationV1().ValidatingWebhookConfigurations().Delete(ctx, "rook-ceph-webhook", metav1.DeleteOptions{})
checkError(h.T(), err, "failed to delete webhook configuration")
logger.Infof("done removing the operator from namespace %s", h.settings.OperatorNamespace)
logger.Infof("removing host data dir %s", h.hostPathToDelete)
// removing data dir if exists
if h.hostPathToDelete != "" {
nodes, err := h.GetNodeHostnames()
checkError(h.T(), err, "cannot get node names")
for _, node := range nodes {
err = h.verifyDirCleanup(node, h.hostPathToDelete)
logger.Infof("verified cleanup of %s from node %s", h.hostPathToDelete, node)
assert.NoError(h.T(), err)
}
}
if h.changeHostnames {
// revert the hostname labels for the test
_, err = h.k8shelper.RestoreHostnames()
assert.NoError(h.T(), err)
}
// wait a bit longer for the system namespace to be cleaned up after their deletion
for i := 0; i < 15; i++ {
_, err := h.k8shelper.Clientset.CoreV1().Namespaces().Get(ctx, h.settings.OperatorNamespace, metav1.GetOptions{})
if err != nil && kerrors.IsNotFound(err) {
logger.Infof("operator namespace %q removed", h.settings.OperatorNamespace)
break
}
logger.Infof("operator namespace %q still found...", h.settings.OperatorNamespace)
time.Sleep(5 * time.Second)
}
}
func (h *CephInstaller) removeClusterFinalizers(namespace, clusterName string) {
ctx := context.TODO()
// Get the latest cluster instead of using the same instance in case it has been changed
cluster, err := h.k8shelper.RookClientset.CephV1().CephClusters(namespace).Get(ctx, clusterName, metav1.GetOptions{})
if err != nil {
logger.Errorf("failed to remove finalizer. failed to get cluster. %+v", err)
return
}
objectMeta := &cluster.ObjectMeta
if len(objectMeta.Finalizers) == 0 {
logger.Infof("no finalizers to remove from cluster %s", namespace)
return
}
objectMeta.Finalizers = nil
_, err = h.k8shelper.RookClientset.CephV1().CephClusters(cluster.Namespace).Update(ctx, cluster, metav1.UpdateOptions{})
if err != nil {
logger.Errorf("failed to remove finalizers from cluster %s. %+v", objectMeta.Name, err)
return
}
logger.Infof("removed finalizers from cluster %s", objectMeta.Name)
}
func (h *CephInstaller) checkCephHealthStatus() {
ctx := context.TODO()
clusterResource, err := h.k8shelper.RookClientset.CephV1().CephClusters(h.settings.Namespace).Get(ctx, h.settings.ClusterName, metav1.GetOptions{})
assert.Nil(h.T(), err)
clusterPhase := string(clusterResource.Status.Phase)
if clusterPhase != "Ready" && clusterPhase != "Connected" && clusterPhase != "Progressing" {
assert.Equal(h.T(), "Ready", string(clusterResource.Status.Phase))
}
// Depending on the tests, the health may be fluctuating with different components being started or stopped.
// If needed, give it a few seconds to settle and check the status again.
logger.Infof("checking ceph cluster health in namespace %q", h.settings.Namespace)
if clusterResource.Status.CephStatus.Health != "HEALTH_OK" {
time.Sleep(10 * time.Second)
clusterResource, err = h.k8shelper.RookClientset.CephV1().CephClusters(h.settings.Namespace).Get(ctx, h.settings.ClusterName, metav1.GetOptions{})
assert.Nil(h.T(), err)
}
// The health status is not stable enough for the integration tests to rely on.
// We should enable this check if we can get the ceph status to be stable despite all the changing configurations performed by rook.
//assert.Equal(h.T(), "HEALTH_OK", clusterResource.Status.CephStatus.Health)
assert.NotEqual(h.T(), "", clusterResource.Status.CephStatus.LastChecked)
// Print the details if the health is not ok
if clusterResource.Status.CephStatus.Health != "HEALTH_OK" {
logger.Errorf("Ceph health status: %s", clusterResource.Status.CephStatus.Health)
for name, message := range clusterResource.Status.CephStatus.Details {
logger.Errorf("Ceph health message: %s. %s: %s", name, message.Severity, message.Message)
}
}
}
func (h *CephInstaller) verifyDirCleanup(node, dir string) error {
resources := h.GetCleanupVerificationPod(node, dir)
_, err := h.k8shelper.KubectlWithStdin(resources, createFromStdinArgs...)
return err
}
func (h *CephInstaller) CollectOperatorLog(suiteName, testName string) {
if !h.T().Failed() && TestLogCollectionLevel() != "all" {
return
}
name := fmt.Sprintf("%s_%s", suiteName, testName)
h.k8shelper.CollectPodLogsFromLabel(cephOperatorLabel, h.settings.OperatorNamespace, name, utils.TestEnvName())
}
func (h *CephInstaller) GatherAllRookLogs(testName string, namespaces ...string) {
if !h.T().Failed() && TestLogCollectionLevel() != "all" {
return
}
logger.Infof("gathering all logs from the test")
for _, namespace := range namespaces {
h.k8shelper.GetLogsFromNamespace(namespace, testName, utils.TestEnvName())
h.k8shelper.GetPodDescribeFromNamespace(namespace, testName, utils.TestEnvName())
h.k8shelper.GetEventsFromNamespace(namespace, testName, utils.TestEnvName())
}
}
// NewCephInstaller creates new instance of CephInstaller
func NewCephInstaller(t func() *testing.T, clientset *kubernetes.Clientset, settings *TestCephSettings) *CephInstaller {
// By default set a cluster name that is different from the namespace so we don't rely on the namespace
// in expected places
if settings.ClusterName == "" {
settings.ClusterName = defaultclusterName
}
version, err := clientset.ServerVersion()
if err != nil {
logger.Infof("failed to get kubectl server version. %+v", err)
}
k8shelp, err := utils.CreateK8sHelper(t)
if err != nil {
panic("failed to get kubectl client :" + err.Error())
}
logger.Infof("Rook Version: %s", settings.RookVersion)
logger.Infof("Ceph Version: %s", settings.CephVersion.Image)
h := &CephInstaller{
settings: settings,
Manifests: NewCephManifests(settings),
k8shelper: k8shelp,
helmHelper: utils.NewHelmHelper(testHelmPath()),
k8sVersion: version.String(),
changeHostnames: settings.ChangeHostName,
T: t,
}
flag.Parse()
return h
}
// GetCleanupPod gets a cleanup Pod that cleans up the dataDirHostPath
func (h *CephInstaller) GetCleanupPod(node, removalDir string) string {
return `apiVersion: batch/v1
kind: Job
metadata:
name: rook-cleanup-` + uuid.Must(uuid.NewRandom()).String() + `
spec:
template:
spec:
restartPolicy: Never
containers:
- name: rook-cleaner
image: rook/ceph:` + LocalBuildTag + `
securityContext:
privileged: true
volumeMounts:
- name: cleaner
mountPath: /scrub
command:
- "sh"
- "-c"
- "rm -rf /scrub/*"
nodeSelector:
kubernetes.io/hostname: ` + node + `
volumes:
- name: cleaner
hostPath:
path: ` + removalDir
}
// GetCleanupVerificationPod verifies that the dataDirHostPath is empty
func (h *CephInstaller) GetCleanupVerificationPod(node, hostPathDir string) string {
return `apiVersion: batch/v1
kind: Job
metadata:
name: rook-verify-cleanup-` + uuid.Must(uuid.NewRandom()).String() + `
spec:
template:
spec:
restartPolicy: Never
containers:
- name: rook-cleaner
image: rook/ceph:` + LocalBuildTag + `
securityContext:
privileged: true
volumeMounts:
- name: cleaner
mountPath: /scrub
command:
- "sh"
- "-c"
- |
set -xEeuo pipefail
#Assert dataDirHostPath is empty
if [ "$(ls -A /scrub/)" ]; then
exit 1
fi
nodeSelector:
kubernetes.io/hostname: ` + node + `
volumes:
- name: cleaner
hostPath:
path: ` + hostPathDir
}
func (h *CephInstaller) addCleanupPolicy(namespace, clusterName string) error {
// Retry updating the CR a few times in case of random failure
var returnErr error
for i := 0; i < 3; i++ {
ctx := context.TODO()
cluster, err := h.k8shelper.RookClientset.CephV1().CephClusters(namespace).Get(ctx, clusterName, metav1.GetOptions{})