container.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright 2016 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 common
  14. import (
  15. "context"
  16. "fmt"
  17. "time"
  18. v1 "k8s.io/api/core/v1"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/util/uuid"
  22. podutil "k8s.io/kubernetes/pkg/api/v1/pod"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. )
  25. const (
  26. ContainerStatusRetryTimeout = time.Minute * 5
  27. ContainerStatusPollInterval = time.Second * 1
  28. )
  29. // One pod one container
  30. type ConformanceContainer struct {
  31. Container v1.Container
  32. RestartPolicy v1.RestartPolicy
  33. Volumes []v1.Volume
  34. ImagePullSecrets []string
  35. PodClient *framework.PodClient
  36. podName string
  37. PodSecurityContext *v1.PodSecurityContext
  38. }
  39. func (cc *ConformanceContainer) Create() {
  40. cc.podName = cc.Container.Name + string(uuid.NewUUID())
  41. imagePullSecrets := []v1.LocalObjectReference{}
  42. for _, s := range cc.ImagePullSecrets {
  43. imagePullSecrets = append(imagePullSecrets, v1.LocalObjectReference{Name: s})
  44. }
  45. pod := &v1.Pod{
  46. ObjectMeta: metav1.ObjectMeta{
  47. Name: cc.podName,
  48. },
  49. Spec: v1.PodSpec{
  50. RestartPolicy: cc.RestartPolicy,
  51. Containers: []v1.Container{
  52. cc.Container,
  53. },
  54. SecurityContext: cc.PodSecurityContext,
  55. Volumes: cc.Volumes,
  56. ImagePullSecrets: imagePullSecrets,
  57. },
  58. }
  59. cc.PodClient.Create(pod)
  60. }
  61. func (cc *ConformanceContainer) Delete() error {
  62. return cc.PodClient.Delete(context.TODO(), cc.podName, metav1.NewDeleteOptions(0))
  63. }
  64. func (cc *ConformanceContainer) IsReady() (bool, error) {
  65. pod, err := cc.PodClient.Get(context.TODO(), cc.podName, metav1.GetOptions{})
  66. if err != nil {
  67. return false, err
  68. }
  69. return podutil.IsPodReady(pod), nil
  70. }
  71. func (cc *ConformanceContainer) GetPhase() (v1.PodPhase, error) {
  72. pod, err := cc.PodClient.Get(context.TODO(), cc.podName, metav1.GetOptions{})
  73. if err != nil {
  74. return v1.PodUnknown, err
  75. }
  76. return pod.Status.Phase, nil
  77. }
  78. func (cc *ConformanceContainer) GetStatus() (v1.ContainerStatus, error) {
  79. pod, err := cc.PodClient.Get(context.TODO(), cc.podName, metav1.GetOptions{})
  80. if err != nil {
  81. return v1.ContainerStatus{}, err
  82. }
  83. statuses := pod.Status.ContainerStatuses
  84. if len(statuses) != 1 || statuses[0].Name != cc.Container.Name {
  85. return v1.ContainerStatus{}, fmt.Errorf("unexpected container statuses %v", statuses)
  86. }
  87. return statuses[0], nil
  88. }
  89. func (cc *ConformanceContainer) Present() (bool, error) {
  90. _, err := cc.PodClient.Get(context.TODO(), cc.podName, metav1.GetOptions{})
  91. if err == nil {
  92. return true, nil
  93. }
  94. if apierrors.IsNotFound(err) {
  95. return false, nil
  96. }
  97. return false, err
  98. }
  99. type ContainerState string
  100. const (
  101. ContainerStateWaiting ContainerState = "Waiting"
  102. ContainerStateRunning ContainerState = "Running"
  103. ContainerStateTerminated ContainerState = "Terminated"
  104. ContainerStateUnknown ContainerState = "Unknown"
  105. )
  106. func GetContainerState(state v1.ContainerState) ContainerState {
  107. if state.Waiting != nil {
  108. return ContainerStateWaiting
  109. }
  110. if state.Running != nil {
  111. return ContainerStateRunning
  112. }
  113. if state.Terminated != nil {
  114. return ContainerStateTerminated
  115. }
  116. return ContainerStateUnknown
  117. }