...
1
16
17 package utils
18
19 import (
20 "fmt"
21 "path"
22 "strings"
23
24 appsv1 "k8s.io/api/apps/v1"
25 v1 "k8s.io/api/core/v1"
26 storagev1 "k8s.io/api/storage/v1"
27 e2eframework "k8s.io/kubernetes/test/e2e/framework"
28 e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
29 )
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 func PatchCSIDeployment(f *e2eframework.Framework, o PatchCSIOptions, object interface{}) error {
53 rename := o.OldDriverName != "" && o.NewDriverName != "" &&
54 o.OldDriverName != o.NewDriverName
55
56 substKubeletRootDir := func(s string) string {
57 return strings.ReplaceAll(s, "/var/lib/kubelet/", e2eframework.TestContext.KubeletRootDir+"/")
58 }
59
60 patchVolumes := func(volumes []v1.Volume) {
61 if !rename {
62 return
63 }
64 for i := range volumes {
65 volume := &volumes[i]
66 if volume.HostPath != nil {
67
68 p := &volume.HostPath.Path
69 dir, file := path.Split(*p)
70 if file == o.OldDriverName {
71 *p = path.Join(dir, o.NewDriverName)
72 }
73
74 *p = substKubeletRootDir(*p)
75 }
76 }
77 }
78
79 patchContainers := func(containers []v1.Container) {
80 for i := range containers {
81 container := &containers[i]
82 if rename {
83 for e := range container.Args {
84
85
86 container.Args[e] = strings.Replace(container.Args[e], "/"+o.OldDriverName+"/", "/"+o.NewDriverName+"/", 1)
87 }
88 }
89
90
91 for e := range container.Args {
92 container.Args[e] = substKubeletRootDir(container.Args[e])
93 }
94 for e := range container.VolumeMounts {
95 container.VolumeMounts[e].MountPath = substKubeletRootDir(container.VolumeMounts[e].MountPath)
96 }
97
98 if len(o.Features) > 0 && len(o.Features[container.Name]) > 0 {
99 featuregateString := strings.Join(o.Features[container.Name], ",")
100 container.Args = append(container.Args, fmt.Sprintf("--feature-gates=%s", featuregateString))
101 }
102
103
104
105
106 switch container.Name {
107 case o.DriverContainerName:
108 container.Args = append(container.Args, o.DriverContainerArguments...)
109 }
110 }
111 }
112
113 patchPodSpec := func(spec *v1.PodSpec) {
114 patchContainers(spec.Containers)
115 patchVolumes(spec.Volumes)
116 if o.NodeName != "" {
117 e2epod.SetNodeSelection(spec, e2epod.NodeSelection{Name: o.NodeName})
118 }
119 }
120
121 switch object := object.(type) {
122 case *appsv1.ReplicaSet:
123 patchPodSpec(&object.Spec.Template.Spec)
124 case *appsv1.DaemonSet:
125 patchPodSpec(&object.Spec.Template.Spec)
126 case *appsv1.StatefulSet:
127 patchPodSpec(&object.Spec.Template.Spec)
128 case *appsv1.Deployment:
129 patchPodSpec(&object.Spec.Template.Spec)
130 case *storagev1.StorageClass:
131 if o.NewDriverName != "" {
132
133
134 object.Provisioner = o.NewDriverName
135 }
136 case *storagev1.CSIDriver:
137 if o.NewDriverName != "" {
138 object.Name = o.NewDriverName
139 }
140 if o.PodInfo != nil {
141 object.Spec.PodInfoOnMount = o.PodInfo
142 }
143 if o.StorageCapacity != nil {
144 object.Spec.StorageCapacity = o.StorageCapacity
145 }
146 if o.CanAttach != nil {
147 object.Spec.AttachRequired = o.CanAttach
148 }
149 if o.VolumeLifecycleModes != nil {
150 object.Spec.VolumeLifecycleModes = *o.VolumeLifecycleModes
151 }
152 if o.TokenRequests != nil {
153 object.Spec.TokenRequests = o.TokenRequests
154 }
155 if o.RequiresRepublish != nil {
156 object.Spec.RequiresRepublish = o.RequiresRepublish
157 }
158 if o.FSGroupPolicy != nil {
159 object.Spec.FSGroupPolicy = o.FSGroupPolicy
160 }
161 if o.SELinuxMount != nil {
162 object.Spec.SELinuxMount = o.SELinuxMount
163 }
164 }
165
166 return nil
167 }
168
169
170 type PatchCSIOptions struct {
171
172 OldDriverName string
173
174
175
176
177 NewDriverName string
178
179
180
181 DriverContainerName string
182
183
184 DriverContainerArguments []string
185
186
187
188 ProvisionerContainerName string
189
190
191
192 SnapshotterContainerName string
193
194 NodeName string
195
196
197
198 PodInfo *bool
199
200
201
202 CanAttach *bool
203
204
205
206 StorageCapacity *bool
207
208
209
210 VolumeLifecycleModes *[]storagev1.VolumeLifecycleMode
211
212
213
214 TokenRequests []storagev1.TokenRequest
215
216
217
218 RequiresRepublish *bool
219
220
221
222 FSGroupPolicy *storagev1.FSGroupPolicy
223
224
225
226 SELinuxMount *bool
227
228
229
230
231
232 Features map[string][]string
233 }
234
View as plain text