-
Notifications
You must be signed in to change notification settings - Fork 283
/
vspherecsi.go
148 lines (132 loc) Β· 4.2 KB
/
vspherecsi.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
package framework
import (
"context"
"fmt"
"strings"
"time"
v1 "k8s.io/api/storage/v1"
"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
"github.com/aws/eks-anywhere/pkg/constants"
"github.com/aws/eks-anywhere/pkg/retrier"
)
const (
csiDeployment = "vsphere-csi-controller"
csiDaemonSet = "vsphere-csi-node"
csiStorageClassName = "standard"
csiStorageClassProvisioner = "csi.vsphere.vmware.com"
kubeSystemNameSpace = "kube-system"
)
const defaultMaxRetries = 5
// ValidateVSphereCSI checks whether vsphere csi exists as expected or not.
func (e *ClusterE2ETest) ValidateVSphereCSI(installed bool) {
ctx := context.Background()
maxRetries := defaultMaxRetries
if !installed {
maxRetries = 1
}
err := e.getDeployment(ctx, maxRetries)
if err != nil {
handleError(e.T, installed, err)
}
err = e.getDaemonSet(ctx, maxRetries)
if err != nil {
handleError(e.T, installed, err)
}
storageclass, err := e.getStorageClass(ctx, maxRetries)
if err != nil {
handleError(e.T, installed, err)
}
if installed && storageclass.Provisioner != csiStorageClassProvisioner {
e.T.Fatalf("provisioners don't match. got: %v, want: %v", storageclass.Provisioner, csiStorageClassProvisioner)
}
err = e.getClusterResourceSet(ctx, maxRetries)
if err != nil {
handleError(e.T, installed, err)
}
}
func handleError(t T, installed bool, err error) {
if installed || !strings.Contains(err.Error(), "not found") {
t.Fatal(err)
}
}
// DeleteVSphereCSI removes the vsphere csi from the cluster.
func (e *ClusterE2ETest) DeleteVSphereCSI() {
ctx := context.Background()
e.deleteVsphereCSIResources(ctx)
csiClusterResourceSetName := fmt.Sprintf("%s-csi", e.ClusterName)
opts := &kubernetes.KubectlDeleteOptions{
Name: csiClusterResourceSetName,
Namespace: constants.EksaSystemNamespace,
}
err := e.KubectlClient.Delete(ctx, "clusterresourceset", e.Cluster().KubeconfigFile, opts)
if err != nil {
e.T.Fatal(err)
}
}
// DeleteWorkloadVsphereCSI removes the vsphere CSI from a workload cluster.
func (w *WorkloadCluster) DeleteWorkloadVsphereCSI() {
ctx := context.Background()
w.deleteVsphereCSIResources(ctx)
}
func (e *ClusterE2ETest) deleteVsphereCSIResources(ctx context.Context) {
opts := &kubernetes.KubectlDeleteOptions{
Name: csiDeployment,
Namespace: kubeSystemNameSpace,
}
err := e.KubectlClient.Delete(ctx, "deployment", e.Cluster().KubeconfigFile, opts)
if err != nil {
e.T.Fatal(err)
}
opts = &kubernetes.KubectlDeleteOptions{
Name: csiDaemonSet,
Namespace: kubeSystemNameSpace,
}
err = e.KubectlClient.Delete(ctx, "daemonset", e.Cluster().KubeconfigFile, opts)
if err != nil {
e.T.Fatal(err)
}
err = e.KubectlClient.DeleteClusterObject(ctx, "storageclass", csiStorageClassName, e.Cluster().KubeconfigFile)
if err != nil {
e.T.Fatal(err)
}
}
func (e *ClusterE2ETest) getDeployment(ctx context.Context, retries int) error {
return retrier.Retry(retries, time.Second*5, func() error {
_, err := e.KubectlClient.GetDeployment(ctx, csiDeployment, kubeSystemNameSpace, e.Cluster().KubeconfigFile)
if err != nil {
return err
}
return nil
})
}
func (e *ClusterE2ETest) getDaemonSet(ctx context.Context, retries int) error {
return retrier.Retry(retries, time.Second*5, func() error {
_, err := e.KubectlClient.GetDaemonSet(ctx, csiDaemonSet, kubeSystemNameSpace, e.Cluster().KubeconfigFile)
if err != nil {
return err
}
return nil
})
}
func (e *ClusterE2ETest) getStorageClass(ctx context.Context, retries int) (*v1.StorageClass, error) {
var storageclass *v1.StorageClass
err := retrier.Retry(retries, time.Second*5, func() error {
s, err := e.KubectlClient.GetStorageClass(ctx, csiStorageClassName, e.Cluster().KubeconfigFile)
if err != nil {
return err
}
storageclass = s
return nil
})
return storageclass, err
}
func (e *ClusterE2ETest) getClusterResourceSet(ctx context.Context, retries int) error {
return retrier.Retry(retries, time.Second*5, func() error {
csiClusterResourceSetName := fmt.Sprintf("%s-csi", e.ClusterName)
_, err := e.KubectlClient.GetClusterResourceSet(ctx, e.Cluster().KubeconfigFile, csiClusterResourceSetName, constants.EksaSystemNamespace)
if err != nil {
return err
}
return nil
})
}