container.go 3.2 KB

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