runtime.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. "path"
  18. "time"
  19. v1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/util/uuid"
  22. "k8s.io/kubernetes/pkg/kubelet/images"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. imageutils "k8s.io/kubernetes/test/utils/image"
  25. "github.com/onsi/ginkgo"
  26. "github.com/onsi/gomega"
  27. gomegatypes "github.com/onsi/gomega/types"
  28. )
  29. var _ = framework.KubeDescribe("Container Runtime", func() {
  30. f := framework.NewDefaultFramework("container-runtime")
  31. ginkgo.Describe("blackbox test", func() {
  32. ginkgo.Context("when starting a container that exits", func() {
  33. /*
  34. Release : v1.13
  35. Testname: Container Runtime, Restart Policy, Pod Phases
  36. Description: If the restart policy is set to 'Always', Pod MUST be restarted when terminated, If restart policy is 'OnFailure', Pod MUST be started only if it is terminated with non-zero exit code. If the restart policy is 'Never', Pod MUST never be restarted. All these three test cases MUST verify the restart counts accordingly.
  37. */
  38. framework.ConformanceIt("should run with the expected status [NodeConformance]", func() {
  39. restartCountVolumeName := "restart-count"
  40. restartCountVolumePath := "/restart-count"
  41. testContainer := v1.Container{
  42. Image: framework.BusyBoxImage,
  43. VolumeMounts: []v1.VolumeMount{
  44. {
  45. MountPath: restartCountVolumePath,
  46. Name: restartCountVolumeName,
  47. },
  48. },
  49. }
  50. testVolumes := []v1.Volume{
  51. {
  52. Name: restartCountVolumeName,
  53. VolumeSource: v1.VolumeSource{
  54. EmptyDir: &v1.EmptyDirVolumeSource{Medium: v1.StorageMediumMemory},
  55. },
  56. },
  57. }
  58. testCases := []struct {
  59. Name string
  60. RestartPolicy v1.RestartPolicy
  61. Phase v1.PodPhase
  62. State ContainerState
  63. RestartCount int32
  64. Ready bool
  65. }{
  66. {"terminate-cmd-rpa", v1.RestartPolicyAlways, v1.PodRunning, ContainerStateRunning, 2, true},
  67. {"terminate-cmd-rpof", v1.RestartPolicyOnFailure, v1.PodSucceeded, ContainerStateTerminated, 1, false},
  68. {"terminate-cmd-rpn", v1.RestartPolicyNever, v1.PodFailed, ContainerStateTerminated, 0, false},
  69. }
  70. for _, testCase := range testCases {
  71. // It failed at the 1st run, then succeeded at 2nd run, then run forever
  72. cmdScripts := `
  73. f=%s
  74. count=$(echo 'hello' >> $f ; wc -l $f | awk {'print $1'})
  75. if [ $count -eq 1 ]; then
  76. exit 1
  77. fi
  78. if [ $count -eq 2 ]; then
  79. exit 0
  80. fi
  81. while true; do sleep 1; done
  82. `
  83. tmpCmd := fmt.Sprintf(cmdScripts, path.Join(restartCountVolumePath, "restartCount"))
  84. testContainer.Name = testCase.Name
  85. testContainer.Command = []string{"sh", "-c", tmpCmd}
  86. terminateContainer := ConformanceContainer{
  87. PodClient: f.PodClient(),
  88. Container: testContainer,
  89. RestartPolicy: testCase.RestartPolicy,
  90. Volumes: testVolumes,
  91. }
  92. terminateContainer.Create()
  93. defer terminateContainer.Delete()
  94. ginkgo.By(fmt.Sprintf("Container '%s': should get the expected 'RestartCount'", testContainer.Name))
  95. gomega.Eventually(func() (int32, error) {
  96. status, err := terminateContainer.GetStatus()
  97. return status.RestartCount, err
  98. }, ContainerStatusRetryTimeout, ContainerStatusPollInterval).Should(gomega.Equal(testCase.RestartCount))
  99. ginkgo.By(fmt.Sprintf("Container '%s': should get the expected 'Phase'", testContainer.Name))
  100. gomega.Eventually(terminateContainer.GetPhase, ContainerStatusRetryTimeout, ContainerStatusPollInterval).Should(gomega.Equal(testCase.Phase))
  101. ginkgo.By(fmt.Sprintf("Container '%s': should get the expected 'Ready' condition", testContainer.Name))
  102. isReady, err := terminateContainer.IsReady()
  103. framework.ExpectEqual(isReady, testCase.Ready)
  104. framework.ExpectNoError(err)
  105. status, err := terminateContainer.GetStatus()
  106. framework.ExpectNoError(err)
  107. ginkgo.By(fmt.Sprintf("Container '%s': should get the expected 'State'", testContainer.Name))
  108. framework.ExpectEqual(GetContainerState(status.State), testCase.State)
  109. ginkgo.By(fmt.Sprintf("Container '%s': should be possible to delete [NodeConformance]", testContainer.Name))
  110. gomega.Expect(terminateContainer.Delete()).To(gomega.Succeed())
  111. gomega.Eventually(terminateContainer.Present, ContainerStatusRetryTimeout, ContainerStatusPollInterval).Should(gomega.BeFalse())
  112. }
  113. })
  114. })
  115. ginkgo.Context("on terminated container", func() {
  116. rootUser := int64(0)
  117. nonRootUser := int64(10000)
  118. adminUserName := "ContainerAdministrator"
  119. nonAdminUserName := "ContainerUser"
  120. // Create and then terminate the container under defined PodPhase to verify if termination message matches the expected output. Lastly delete the created container.
  121. matchTerminationMessage := func(container v1.Container, expectedPhase v1.PodPhase, expectedMsg gomegatypes.GomegaMatcher) {
  122. container.Name = "termination-message-container"
  123. c := ConformanceContainer{
  124. PodClient: f.PodClient(),
  125. Container: container,
  126. RestartPolicy: v1.RestartPolicyNever,
  127. }
  128. ginkgo.By("create the container")
  129. c.Create()
  130. defer c.Delete()
  131. ginkgo.By(fmt.Sprintf("wait for the container to reach %s", expectedPhase))
  132. gomega.Eventually(c.GetPhase, ContainerStatusRetryTimeout, ContainerStatusPollInterval).Should(gomega.Equal(expectedPhase))
  133. ginkgo.By("get the container status")
  134. status, err := c.GetStatus()
  135. framework.ExpectNoError(err)
  136. ginkgo.By("the container should be terminated")
  137. framework.ExpectEqual(GetContainerState(status.State), ContainerStateTerminated)
  138. ginkgo.By("the termination message should be set")
  139. framework.Logf("Expected: %v to match Container's Termination Message: %v --", expectedMsg, status.State.Terminated.Message)
  140. gomega.Expect(status.State.Terminated.Message).Should(expectedMsg)
  141. ginkgo.By("delete the container")
  142. gomega.Expect(c.Delete()).To(gomega.Succeed())
  143. }
  144. ginkgo.It("should report termination message [LinuxOnly] if TerminationMessagePath is set [NodeConformance]", func() {
  145. // Cannot mount files in Windows Containers.
  146. // TODO(claudiub): Remove [LinuxOnly] tag once Containerd becomes the default
  147. // container runtime on Windows.
  148. container := v1.Container{
  149. Image: framework.BusyBoxImage,
  150. Command: []string{"/bin/sh", "-c"},
  151. Args: []string{"/bin/echo -n DONE > /dev/termination-log"},
  152. TerminationMessagePath: "/dev/termination-log",
  153. SecurityContext: &v1.SecurityContext{},
  154. }
  155. if framework.NodeOSDistroIs("windows") {
  156. container.SecurityContext.WindowsOptions = &v1.WindowsSecurityContextOptions{RunAsUserName: &adminUserName}
  157. } else {
  158. container.SecurityContext.RunAsUser = &rootUser
  159. }
  160. matchTerminationMessage(container, v1.PodSucceeded, gomega.Equal("DONE"))
  161. })
  162. /*
  163. Release: v1.15
  164. Name: Container Runtime, TerminationMessagePath, non-root user and non-default path
  165. Description: Create a pod with a container to run it as a non-root user with a custom TerminationMessagePath set. Pod redirects the output to the provided path successfully. When the container is terminated, the termination message MUST match the expected output logged in the provided custom path.
  166. [LinuxOnly]: Tagged LinuxOnly due to use of 'uid' and unable to mount files in Windows Containers.
  167. */
  168. framework.ConformanceIt("should report termination message [LinuxOnly] if TerminationMessagePath is set as non-root user and at a non-default path [NodeConformance]", func() {
  169. // TODO(claudiub): Remove [LinuxOnly] tag once Containerd becomes the default
  170. // container runtime on Windows
  171. container := v1.Container{
  172. Image: framework.BusyBoxImage,
  173. Command: []string{"/bin/sh", "-c"},
  174. Args: []string{"/bin/echo -n DONE > /dev/termination-custom-log"},
  175. TerminationMessagePath: "/dev/termination-custom-log",
  176. SecurityContext: &v1.SecurityContext{},
  177. }
  178. if framework.NodeOSDistroIs("windows") {
  179. container.SecurityContext.WindowsOptions = &v1.WindowsSecurityContextOptions{RunAsUserName: &nonAdminUserName}
  180. } else {
  181. container.SecurityContext.RunAsUser = &nonRootUser
  182. }
  183. matchTerminationMessage(container, v1.PodSucceeded, gomega.Equal("DONE"))
  184. })
  185. /*
  186. Release: v1.15
  187. Name: Container Runtime, TerminationMessage, from container's log output of failing container
  188. Description: Create a pod with an container. Container's output is recorded in log and container exits with an error. When container is terminated, termination message MUST match the expected output recorded from container's log.
  189. [LinuxOnly]: Cannot mount files in Windows Containers.
  190. */
  191. framework.ConformanceIt("should report termination message [LinuxOnly] from log output if TerminationMessagePolicy FallbackToLogsOnError is set [NodeConformance]", func() {
  192. container := v1.Container{
  193. Image: framework.BusyBoxImage,
  194. Command: []string{"/bin/sh", "-c"},
  195. Args: []string{"/bin/echo -n DONE; /bin/false"},
  196. TerminationMessagePath: "/dev/termination-log",
  197. TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
  198. }
  199. matchTerminationMessage(container, v1.PodFailed, gomega.Equal("DONE"))
  200. })
  201. /*
  202. Release: v1.15
  203. Name: Container Runtime, TerminationMessage, from log output of succeeding container
  204. Description: Create a pod with an container. Container's output is recorded in log and container exits successfully without an error. When container is terminated, terminationMessage MUST have no content as container succeed.
  205. [LinuxOnly]: Cannot mount files in Windows Containers.
  206. */
  207. framework.ConformanceIt("should report termination message [LinuxOnly] as empty when pod succeeds and TerminationMessagePolicy FallbackToLogsOnError is set [NodeConformance]", func() {
  208. container := v1.Container{
  209. Image: framework.BusyBoxImage,
  210. Command: []string{"/bin/sh", "-c"},
  211. Args: []string{"/bin/echo -n DONE; /bin/true"},
  212. TerminationMessagePath: "/dev/termination-log",
  213. TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
  214. }
  215. matchTerminationMessage(container, v1.PodSucceeded, gomega.Equal(""))
  216. })
  217. /*
  218. Release: v1.15
  219. Name: Container Runtime, TerminationMessage, from file of succeeding container
  220. Description: Create a pod with an container. Container's output is recorded in a file and the container exits successfully without an error. When container is terminated, terminationMessage MUST match with the content from file.
  221. [LinuxOnly]: Cannot mount files in Windows Containers.
  222. */
  223. framework.ConformanceIt("should report termination message [LinuxOnly] from file when pod succeeds and TerminationMessagePolicy FallbackToLogsOnError is set [NodeConformance]", func() {
  224. container := v1.Container{
  225. Image: framework.BusyBoxImage,
  226. Command: []string{"/bin/sh", "-c"},
  227. Args: []string{"/bin/echo -n OK > /dev/termination-log; /bin/echo DONE; /bin/true"},
  228. TerminationMessagePath: "/dev/termination-log",
  229. TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
  230. }
  231. matchTerminationMessage(container, v1.PodSucceeded, gomega.Equal("OK"))
  232. })
  233. })
  234. ginkgo.Context("when running a container with a new image", func() {
  235. // Images used for ConformanceContainer are not added into NodeImageWhiteList, because this test is
  236. // testing image pulling, these images don't need to be prepulled. The ImagePullPolicy
  237. // is v1.PullAlways, so it won't be blocked by framework image white list check.
  238. imagePullTest := func(image string, hasSecret bool, expectedPhase v1.PodPhase, expectedPullStatus bool, windowsImage bool) {
  239. command := []string{"/bin/sh", "-c", "while true; do sleep 1; done"}
  240. if windowsImage {
  241. // -t: Ping the specified host until stopped.
  242. command = []string{"ping", "-t", "localhost"}
  243. }
  244. container := ConformanceContainer{
  245. PodClient: f.PodClient(),
  246. Container: v1.Container{
  247. Name: "image-pull-test",
  248. Image: image,
  249. Command: command,
  250. ImagePullPolicy: v1.PullAlways,
  251. },
  252. RestartPolicy: v1.RestartPolicyNever,
  253. }
  254. if hasSecret {
  255. // The service account only has pull permission
  256. auth := `
  257. {
  258. "auths": {
  259. "https://gcr.io": {
  260. "auth": "X2pzb25fa2V5OnsKICAidHlwZSI6ICJzZXJ2aWNlX2FjY291bnQiLAogICJwcm9qZWN0X2lkIjogImF1dGhlbnRpY2F0ZWQtaW1hZ2UtcHVsbGluZyIsCiAgInByaXZhdGVfa2V5X2lkIjogImI5ZjJhNjY0YWE5YjIwNDg0Y2MxNTg2MDYzZmVmZGExOTIyNGFjM2IiLAogICJwcml2YXRlX2tleSI6ICItLS0tLUJFR0lOIFBSSVZBVEUgS0VZLS0tLS1cbk1JSUV2UUlCQURBTkJna3Foa2lHOXcwQkFRRUZBQVNDQktjd2dnU2pBZ0VBQW9JQkFRQzdTSG5LVEVFaVlMamZcbkpmQVBHbUozd3JCY2VJNTBKS0xxS21GWE5RL3REWGJRK2g5YVl4aldJTDhEeDBKZTc0bVovS01uV2dYRjVLWlNcbm9BNktuSU85Yi9SY1NlV2VpSXRSekkzL1lYVitPNkNjcmpKSXl4anFWam5mVzJpM3NhMzd0OUE5VEZkbGZycm5cbjR6UkpiOWl4eU1YNGJMdHFGR3ZCMDNOSWl0QTNzVlo1ODhrb1FBZmgzSmhhQmVnTWorWjRSYko0aGVpQlFUMDNcbnZVbzViRWFQZVQ5RE16bHdzZWFQV2dydDZOME9VRGNBRTl4bGNJek11MjUzUG4vSzgySFpydEx4akd2UkhNVXhcbng0ZjhwSnhmQ3h4QlN3Z1NORit3OWpkbXR2b0wwRmE3ZGducFJlODZWRDY2ejNZenJqNHlLRXRqc2hLZHl5VWRcbkl5cVhoN1JSQWdNQkFBRUNnZ0VBT3pzZHdaeENVVlFUeEFka2wvSTVTRFVidi9NazRwaWZxYjJEa2FnbmhFcG9cbjFJajJsNGlWMTByOS9uenJnY2p5VlBBd3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yb2JvdC92MS9tZXRhZGF0YS94NTA5L2ltYWdlLXB1bGxpbmclNDBhdXRoZW50aWNhdGVkLWltYWdlLXB1bGxpbmcuaWFtLmdzZXJ2aWNlYWNjb3VudC5jb20iCn0=",
  261. "email": "image-pulling@authenticated-image-pulling.iam.gserviceaccount.com"
  262. }
  263. }
  264. }`
  265. secret := &v1.Secret{
  266. Data: map[string][]byte{v1.DockerConfigJsonKey: []byte(auth)},
  267. Type: v1.SecretTypeDockerConfigJson,
  268. }
  269. secret.Name = "image-pull-secret-" + string(uuid.NewUUID())
  270. ginkgo.By("create image pull secret")
  271. _, err := f.ClientSet.CoreV1().Secrets(f.Namespace.Name).Create(context.TODO(), secret, metav1.CreateOptions{})
  272. framework.ExpectNoError(err)
  273. defer f.ClientSet.CoreV1().Secrets(f.Namespace.Name).Delete(context.TODO(), secret.Name, nil)
  274. container.ImagePullSecrets = []string{secret.Name}
  275. }
  276. // checkContainerStatus checks whether the container status matches expectation.
  277. checkContainerStatus := func() error {
  278. status, err := container.GetStatus()
  279. if err != nil {
  280. return fmt.Errorf("failed to get container status: %v", err)
  281. }
  282. // We need to check container state first. The default pod status is pending, If we check pod phase first,
  283. // and the expected pod phase is Pending, the container status may not even show up when we check it.
  284. // Check container state
  285. if !expectedPullStatus {
  286. if status.State.Running == nil {
  287. return fmt.Errorf("expected container state: Running, got: %q",
  288. GetContainerState(status.State))
  289. }
  290. }
  291. if expectedPullStatus {
  292. if status.State.Waiting == nil {
  293. return fmt.Errorf("expected container state: Waiting, got: %q",
  294. GetContainerState(status.State))
  295. }
  296. reason := status.State.Waiting.Reason
  297. if reason != images.ErrImagePull.Error() &&
  298. reason != images.ErrImagePullBackOff.Error() {
  299. return fmt.Errorf("unexpected waiting reason: %q", reason)
  300. }
  301. }
  302. // Check pod phase
  303. phase, err := container.GetPhase()
  304. if err != nil {
  305. return fmt.Errorf("failed to get pod phase: %v", err)
  306. }
  307. if phase != expectedPhase {
  308. return fmt.Errorf("expected pod phase: %q, got: %q", expectedPhase, phase)
  309. }
  310. return nil
  311. }
  312. // The image registry is not stable, which sometimes causes the test to fail. Add retry mechanism to make this less flaky.
  313. const flakeRetry = 3
  314. for i := 1; i <= flakeRetry; i++ {
  315. var err error
  316. ginkgo.By("create the container")
  317. container.Create()
  318. ginkgo.By("check the container status")
  319. for start := time.Now(); time.Since(start) < ContainerStatusRetryTimeout; time.Sleep(ContainerStatusPollInterval) {
  320. if err = checkContainerStatus(); err == nil {
  321. break
  322. }
  323. }
  324. ginkgo.By("delete the container")
  325. container.Delete()
  326. if err == nil {
  327. break
  328. }
  329. if i < flakeRetry {
  330. framework.Logf("No.%d attempt failed: %v, retrying...", i, err)
  331. } else {
  332. framework.Failf("All %d attempts failed: %v", flakeRetry, err)
  333. }
  334. }
  335. }
  336. ginkgo.It("should not be able to pull image from invalid registry [NodeConformance]", func() {
  337. image := imageutils.GetE2EImage(imageutils.InvalidRegistryImage)
  338. imagePullTest(image, false, v1.PodPending, true, false)
  339. })
  340. ginkgo.It("should be able to pull image [NodeConformance]", func() {
  341. // NOTE(claudiub): The agnhost image is supposed to work on both Linux and Windows.
  342. image := imageutils.GetE2EImage(imageutils.Agnhost)
  343. imagePullTest(image, false, v1.PodRunning, false, false)
  344. })
  345. ginkgo.It("should not be able to pull from private registry without secret [NodeConformance]", func() {
  346. image := imageutils.GetE2EImage(imageutils.AuthenticatedAlpine)
  347. imagePullTest(image, false, v1.PodPending, true, false)
  348. })
  349. ginkgo.It("should be able to pull from private registry with secret [NodeConformance]", func() {
  350. image := imageutils.GetE2EImage(imageutils.AuthenticatedAlpine)
  351. isWindows := false
  352. if framework.NodeOSDistroIs("windows") {
  353. image = imageutils.GetE2EImage(imageutils.AuthenticatedWindowsNanoServer)
  354. isWindows = true
  355. }
  356. imagePullTest(image, true, v1.PodRunning, false, isWindows)
  357. })
  358. })
  359. })
  360. })