deployment.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package utils
  14. import (
  15. "path"
  16. "strconv"
  17. "strings"
  18. appsv1 "k8s.io/api/apps/v1"
  19. v1 "k8s.io/api/core/v1"
  20. storagev1 "k8s.io/api/storage/v1"
  21. "k8s.io/kubernetes/test/e2e/framework"
  22. )
  23. // PatchCSIDeployment modifies the CSI driver deployment:
  24. // - replaces the provisioner name
  25. // - forces pods onto a specific host
  26. //
  27. // All of that is optional, see PatchCSIOptions. Just beware
  28. // that not renaming the CSI driver deployment can be problematic:
  29. // - when multiple tests deploy the driver, they need
  30. // to run sequentially
  31. // - might conflict with manual deployments
  32. //
  33. // This function is written so that it works for CSI driver deployments
  34. // that follow these conventions:
  35. // - driver and provisioner names are identical
  36. // - the driver binary accepts a --drivername parameter
  37. // - the provisioner binary accepts a --provisioner parameter
  38. // - the paths inside the container are either fixed
  39. // and don't need to be patch (for example, --csi-address=/csi/csi.sock is
  40. // okay) or are specified directly in a parameter (for example,
  41. // --kubelet-registration-path=/var/lib/kubelet/plugins/csi-hostpath/csi.sock)
  42. //
  43. // Driver deployments that are different will have to do the patching
  44. // without this function, or skip patching entirely.
  45. //
  46. // TODO (?): the storage.csi.image.version and storage.csi.image.registry
  47. // settings are ignored. We could patch the image definitions or deprecate
  48. // those options.
  49. func PatchCSIDeployment(f *framework.Framework, o PatchCSIOptions, object interface{}) error {
  50. rename := o.OldDriverName != "" && o.NewDriverName != "" &&
  51. o.OldDriverName != o.NewDriverName
  52. patchVolumes := func(volumes []v1.Volume) {
  53. if !rename {
  54. return
  55. }
  56. for i := range volumes {
  57. volume := &volumes[i]
  58. if volume.HostPath != nil {
  59. // Update paths like /var/lib/kubelet/plugins/<provisioner>.
  60. p := &volume.HostPath.Path
  61. dir, file := path.Split(*p)
  62. if file == o.OldDriverName {
  63. *p = path.Join(dir, o.NewDriverName)
  64. }
  65. }
  66. }
  67. }
  68. patchContainers := func(containers []v1.Container) {
  69. for i := range containers {
  70. container := &containers[i]
  71. if rename {
  72. for e := range container.Args {
  73. // Inject test-specific provider name into paths like this one:
  74. // --kubelet-registration-path=/var/lib/kubelet/plugins/csi-hostpath/csi.sock
  75. container.Args[e] = strings.Replace(container.Args[e], "/"+o.OldDriverName+"/", "/"+o.NewDriverName+"/", 1)
  76. }
  77. }
  78. // Overwrite driver name resp. provider name
  79. // by appending a parameter with the right
  80. // value.
  81. switch container.Name {
  82. case o.DriverContainerName:
  83. container.Args = append(container.Args, o.DriverContainerArguments...)
  84. case o.ProvisionerContainerName:
  85. // Driver name is expected to be the same
  86. // as the provisioner here.
  87. container.Args = append(container.Args, "--provisioner="+o.NewDriverName)
  88. case o.SnapshotterContainerName:
  89. // Driver name is expected to be the same
  90. // as the snapshotter here.
  91. container.Args = append(container.Args, "--snapshotter="+o.NewDriverName)
  92. case o.ClusterRegistrarContainerName:
  93. if o.PodInfo != nil {
  94. container.Args = append(container.Args, "--pod-info-mount="+strconv.FormatBool(*o.PodInfo))
  95. }
  96. }
  97. }
  98. }
  99. patchPodSpec := func(spec *v1.PodSpec) {
  100. patchContainers(spec.Containers)
  101. patchVolumes(spec.Volumes)
  102. if o.NodeName != "" {
  103. spec.NodeName = o.NodeName
  104. }
  105. }
  106. switch object := object.(type) {
  107. case *appsv1.ReplicaSet:
  108. patchPodSpec(&object.Spec.Template.Spec)
  109. case *appsv1.DaemonSet:
  110. patchPodSpec(&object.Spec.Template.Spec)
  111. case *appsv1.StatefulSet:
  112. patchPodSpec(&object.Spec.Template.Spec)
  113. case *appsv1.Deployment:
  114. patchPodSpec(&object.Spec.Template.Spec)
  115. case *storagev1.StorageClass:
  116. if o.NewDriverName != "" {
  117. // Driver name is expected to be the same
  118. // as the provisioner name here.
  119. object.Provisioner = o.NewDriverName
  120. }
  121. }
  122. return nil
  123. }
  124. // PatchCSIOptions controls how PatchCSIDeployment patches the objects.
  125. type PatchCSIOptions struct {
  126. // The original driver name.
  127. OldDriverName string
  128. // The driver name that replaces the original name.
  129. // Can be empty (not used at all) or equal to OldDriverName
  130. // (then it will be added were appropriate without renaming
  131. // in existing fields).
  132. NewDriverName string
  133. // The name of the container which has the CSI driver binary.
  134. // If non-empty, DriverContainerArguments are added to argument
  135. // list in container with that name.
  136. DriverContainerName string
  137. // List of arguments to add to container with
  138. // DriverContainerName.
  139. DriverContainerArguments []string
  140. // The name of the container which has the provisioner binary.
  141. // If non-empty, --provisioner with new name will be appended
  142. // to the argument list.
  143. ProvisionerContainerName string
  144. // The name of the container which has the snapshotter binary.
  145. // If non-empty, --snapshotter with new name will be appended
  146. // to the argument list.
  147. SnapshotterContainerName string
  148. // The name of the container which has the cluster-driver-registrar
  149. // binary.
  150. ClusterRegistrarContainerName string
  151. // If non-empty, all pods are forced to run on this node.
  152. NodeName string
  153. // If not nil, the argument to pass to the cluster-driver-registrar's
  154. // pod-info-mount argument.
  155. PodInfo *bool
  156. }