fixtures.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Copyright 2017 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 deployment
  14. import (
  15. "context"
  16. "fmt"
  17. appsv1 "k8s.io/api/apps/v1"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/util/uuid"
  21. clientset "k8s.io/client-go/kubernetes"
  22. deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. testutils "k8s.io/kubernetes/test/utils"
  25. imageutils "k8s.io/kubernetes/test/utils/image"
  26. )
  27. // UpdateDeploymentWithRetries updates the specified deployment with retries.
  28. func UpdateDeploymentWithRetries(c clientset.Interface, namespace, name string, applyUpdate testutils.UpdateDeploymentFunc) (*appsv1.Deployment, error) {
  29. return testutils.UpdateDeploymentWithRetries(c, namespace, name, applyUpdate, framework.Logf, poll, pollShortTimeout)
  30. }
  31. // NewDeployment returns a deployment spec with the specified argument.
  32. func NewDeployment(deploymentName string, replicas int32, podLabels map[string]string, imageName, image string, strategyType appsv1.DeploymentStrategyType) *appsv1.Deployment {
  33. zero := int64(0)
  34. return &appsv1.Deployment{
  35. ObjectMeta: metav1.ObjectMeta{
  36. Name: deploymentName,
  37. Labels: podLabels,
  38. },
  39. Spec: appsv1.DeploymentSpec{
  40. Replicas: &replicas,
  41. Selector: &metav1.LabelSelector{MatchLabels: podLabels},
  42. Strategy: appsv1.DeploymentStrategy{
  43. Type: strategyType,
  44. },
  45. Template: v1.PodTemplateSpec{
  46. ObjectMeta: metav1.ObjectMeta{
  47. Labels: podLabels,
  48. },
  49. Spec: v1.PodSpec{
  50. TerminationGracePeriodSeconds: &zero,
  51. Containers: []v1.Container{
  52. {
  53. Name: imageName,
  54. Image: image,
  55. SecurityContext: &v1.SecurityContext{},
  56. },
  57. },
  58. },
  59. },
  60. },
  61. }
  62. }
  63. // CreateDeployment creates a deployment.
  64. func CreateDeployment(client clientset.Interface, replicas int32, podLabels map[string]string, nodeSelector map[string]string, namespace string, pvclaims []*v1.PersistentVolumeClaim, command string) (*appsv1.Deployment, error) {
  65. deploymentSpec := testDeployment(replicas, podLabels, nodeSelector, namespace, pvclaims, false, command)
  66. deployment, err := client.AppsV1().Deployments(namespace).Create(context.TODO(), deploymentSpec, metav1.CreateOptions{})
  67. if err != nil {
  68. return nil, fmt.Errorf("deployment %q Create API error: %v", deploymentSpec.Name, err)
  69. }
  70. framework.Logf("Waiting deployment %q to complete", deploymentSpec.Name)
  71. err = WaitForDeploymentComplete(client, deployment)
  72. if err != nil {
  73. return nil, fmt.Errorf("deployment %q failed to complete: %v", deploymentSpec.Name, err)
  74. }
  75. return deployment, nil
  76. }
  77. // GetPodsForDeployment gets pods for the given deployment
  78. func GetPodsForDeployment(client clientset.Interface, deployment *appsv1.Deployment) (*v1.PodList, error) {
  79. replicaSet, err := deploymentutil.GetNewReplicaSet(deployment, client.AppsV1())
  80. if err != nil {
  81. return nil, fmt.Errorf("Failed to get new replica set for deployment %q: %v", deployment.Name, err)
  82. }
  83. if replicaSet == nil {
  84. return nil, fmt.Errorf("expected a new replica set for deployment %q, found none", deployment.Name)
  85. }
  86. podListFunc := func(namespace string, options metav1.ListOptions) (*v1.PodList, error) {
  87. return client.CoreV1().Pods(namespace).List(context.TODO(), options)
  88. }
  89. rsList := []*appsv1.ReplicaSet{replicaSet}
  90. podList, err := deploymentutil.ListPods(deployment, rsList, podListFunc)
  91. if err != nil {
  92. return nil, fmt.Errorf("Failed to list Pods of Deployment %q: %v", deployment.Name, err)
  93. }
  94. return podList, nil
  95. }
  96. // testDeployment creates a deployment definition based on the namespace. The deployment references the PVC's
  97. // name. A slice of BASH commands can be supplied as args to be run by the pod
  98. func testDeployment(replicas int32, podLabels map[string]string, nodeSelector map[string]string, namespace string, pvclaims []*v1.PersistentVolumeClaim, isPrivileged bool, command string) *appsv1.Deployment {
  99. if len(command) == 0 {
  100. command = "trap exit TERM; while true; do sleep 1; done"
  101. }
  102. zero := int64(0)
  103. deploymentName := "deployment-" + string(uuid.NewUUID())
  104. deploymentSpec := &appsv1.Deployment{
  105. ObjectMeta: metav1.ObjectMeta{
  106. Name: deploymentName,
  107. Namespace: namespace,
  108. },
  109. Spec: appsv1.DeploymentSpec{
  110. Replicas: &replicas,
  111. Selector: &metav1.LabelSelector{
  112. MatchLabels: podLabels,
  113. },
  114. Template: v1.PodTemplateSpec{
  115. ObjectMeta: metav1.ObjectMeta{
  116. Labels: podLabels,
  117. },
  118. Spec: v1.PodSpec{
  119. TerminationGracePeriodSeconds: &zero,
  120. Containers: []v1.Container{
  121. {
  122. Name: "write-pod",
  123. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  124. Command: []string{"/bin/sh"},
  125. Args: []string{"-c", command},
  126. SecurityContext: &v1.SecurityContext{
  127. Privileged: &isPrivileged,
  128. },
  129. },
  130. },
  131. RestartPolicy: v1.RestartPolicyAlways,
  132. },
  133. },
  134. },
  135. }
  136. var volumeMounts = make([]v1.VolumeMount, len(pvclaims))
  137. var volumes = make([]v1.Volume, len(pvclaims))
  138. for index, pvclaim := range pvclaims {
  139. volumename := fmt.Sprintf("volume%v", index+1)
  140. volumeMounts[index] = v1.VolumeMount{Name: volumename, MountPath: "/mnt/" + volumename}
  141. volumes[index] = v1.Volume{Name: volumename, VolumeSource: v1.VolumeSource{PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ClaimName: pvclaim.Name, ReadOnly: false}}}
  142. }
  143. deploymentSpec.Spec.Template.Spec.Containers[0].VolumeMounts = volumeMounts
  144. deploymentSpec.Spec.Template.Spec.Volumes = volumes
  145. if nodeSelector != nil {
  146. deploymentSpec.Spec.Template.Spec.NodeSelector = nodeSelector
  147. }
  148. return deploymentSpec
  149. }