rc_utils.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Copyright 2019 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 rc
  14. import (
  15. "fmt"
  16. "github.com/onsi/ginkgo"
  17. v1 "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. clientset "k8s.io/client-go/kubernetes"
  20. scaleclient "k8s.io/client-go/scale"
  21. api "k8s.io/kubernetes/pkg/apis/core"
  22. "k8s.io/kubernetes/test/e2e/framework"
  23. e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl"
  24. testutils "k8s.io/kubernetes/test/utils"
  25. )
  26. // ByNameContainer returns a ReplicationController with specified name and container
  27. func ByNameContainer(name string, replicas int32, labels map[string]string, c v1.Container,
  28. gracePeriod *int64) *v1.ReplicationController {
  29. zeroGracePeriod := int64(0)
  30. // Add "name": name to the labels, overwriting if it exists.
  31. labels["name"] = name
  32. if gracePeriod == nil {
  33. gracePeriod = &zeroGracePeriod
  34. }
  35. return &v1.ReplicationController{
  36. TypeMeta: metav1.TypeMeta{
  37. Kind: "ReplicationController",
  38. APIVersion: "v1",
  39. },
  40. ObjectMeta: metav1.ObjectMeta{
  41. Name: name,
  42. },
  43. Spec: v1.ReplicationControllerSpec{
  44. Replicas: func(i int32) *int32 { return &i }(replicas),
  45. Selector: map[string]string{
  46. "name": name,
  47. },
  48. Template: &v1.PodTemplateSpec{
  49. ObjectMeta: metav1.ObjectMeta{
  50. Labels: labels,
  51. },
  52. Spec: v1.PodSpec{
  53. Containers: []v1.Container{c},
  54. TerminationGracePeriodSeconds: gracePeriod,
  55. },
  56. },
  57. },
  58. }
  59. }
  60. // DeleteRCAndWaitForGC deletes only the Replication Controller and waits for GC to delete the pods.
  61. func DeleteRCAndWaitForGC(c clientset.Interface, ns, name string) error {
  62. return framework.DeleteResourceAndWaitForGC(c, api.Kind("ReplicationController"), ns, name)
  63. }
  64. // ScaleRC scales Replication Controller to be desired size.
  65. func ScaleRC(clientset clientset.Interface, scalesGetter scaleclient.ScalesGetter, ns, name string, size uint, wait bool) error {
  66. return framework.ScaleResource(clientset, scalesGetter, ns, name, size, wait, api.Kind("ReplicationController"), api.SchemeGroupVersion.WithResource("replicationcontrollers"))
  67. }
  68. // RunRC Launches (and verifies correctness) of a Replication Controller
  69. // and will wait for all pods it spawns to become "Running".
  70. func RunRC(config testutils.RCConfig) error {
  71. ginkgo.By(fmt.Sprintf("creating replication controller %s in namespace %s", config.Name, config.Namespace))
  72. config.NodeDumpFunc = framework.DumpNodeDebugInfo
  73. config.ContainerDumpFunc = e2ekubectl.LogFailedContainers
  74. return testutils.RunRC(config)
  75. }