container.go 3.5 KB

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