2015-06-13 03:02:29 +00:00
/ *
2016-07-22 07:02:27 +00:00
Copyright 2016 The Kubernetes Authors .
2015-06-13 03:02:29 +00:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2016-07-22 07:02:27 +00:00
package common
2015-06-13 03:02:29 +00:00
import (
2018-11-20 13:05:19 +00:00
"fmt"
2019-05-03 16:44:04 +00:00
"time"
v1 "k8s.io/api/core/v1"
2017-01-17 03:38:19 +00:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2017-01-24 14:35:22 +00:00
"k8s.io/apimachinery/pkg/util/uuid"
2018-11-20 13:05:19 +00:00
"k8s.io/apimachinery/pkg/util/wait"
2016-04-07 17:21:31 +00:00
"k8s.io/kubernetes/test/e2e/framework"
2019-05-03 16:44:04 +00:00
e2elog "k8s.io/kubernetes/test/e2e/framework/log"
2018-08-14 13:37:40 +00:00
imageutils "k8s.io/kubernetes/test/utils/image"
2017-07-21 14:42:03 +00:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
2015-06-13 03:02:29 +00:00
)
2016-02-09 07:30:32 +00:00
// These tests exercise the Kubernetes expansion syntax $(VAR).
2017-09-29 20:24:33 +00:00
// For more information, see:
// https://github.com/kubernetes/community/blob/master/contributors/design-proposals/node/expansion.md
2016-04-07 17:21:31 +00:00
var _ = framework . KubeDescribe ( "Variable Expansion" , func ( ) {
f := framework . NewDefaultFramework ( "var-expansion" )
2015-06-13 03:02:29 +00:00
2017-10-20 20:29:25 +00:00
/ *
2018-02-27 22:57:56 +00:00
Release : v1 .9
Testname : Environment variables , expansion
Description : Create a Pod with environment variables . Environment variables defined using previously defined environment variables MUST expand to proper values .
2017-10-20 20:29:25 +00:00
* /
2018-05-22 01:09:33 +00:00
framework . ConformanceIt ( "should allow composing env vars into new env vars [NodeConformance]" , func ( ) {
2016-07-26 15:13:18 +00:00
podName := "var-expansion-" + string ( uuid . NewUUID ( ) )
2016-11-18 20:55:17 +00:00
pod := & v1 . Pod {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2015-06-13 03:02:29 +00:00
Name : podName ,
Labels : map [ string ] string { "name" : podName } ,
} ,
2016-11-18 20:55:17 +00:00
Spec : v1 . PodSpec {
Containers : [ ] v1 . Container {
2015-06-13 03:02:29 +00:00
{
Name : "dapi-container" ,
2018-08-14 13:37:40 +00:00
Image : imageutils . GetE2EImage ( imageutils . BusyBox ) ,
2015-06-13 03:02:29 +00:00
Command : [ ] string { "sh" , "-c" , "env" } ,
2016-11-18 20:55:17 +00:00
Env : [ ] v1 . EnvVar {
2015-06-13 03:02:29 +00:00
{
Name : "FOO" ,
Value : "foo-value" ,
} ,
{
Name : "BAR" ,
Value : "bar-value" ,
} ,
{
Name : "FOOBAR" ,
Value : "$(FOO);;$(BAR)" ,
} ,
} ,
} ,
} ,
2016-11-18 20:55:17 +00:00
RestartPolicy : v1 . RestartPolicyNever ,
2015-06-13 03:02:29 +00:00
} ,
}
2016-04-07 17:21:31 +00:00
f . TestContainerOutput ( "env composition" , pod , 0 , [ ] string {
2015-06-13 03:02:29 +00:00
"FOO=foo-value" ,
"BAR=bar-value" ,
"FOOBAR=foo-value;;bar-value" ,
2015-07-09 11:56:19 +00:00
} )
2015-06-13 03:02:29 +00:00
} )
2017-10-20 20:29:25 +00:00
/ *
2018-02-27 22:57:56 +00:00
Release : v1 .9
Testname : Environment variables , command expansion
Description : Create a Pod with environment variables and container command using them . Container command using the defined environment variables MUST expand to proper values .
2017-10-20 20:29:25 +00:00
* /
2018-05-22 01:09:33 +00:00
framework . ConformanceIt ( "should allow substituting values in a container's command [NodeConformance]" , func ( ) {
2016-07-26 15:13:18 +00:00
podName := "var-expansion-" + string ( uuid . NewUUID ( ) )
2016-11-18 20:55:17 +00:00
pod := & v1 . Pod {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2015-06-13 03:02:29 +00:00
Name : podName ,
Labels : map [ string ] string { "name" : podName } ,
} ,
2016-11-18 20:55:17 +00:00
Spec : v1 . PodSpec {
Containers : [ ] v1 . Container {
2015-06-13 03:02:29 +00:00
{
Name : "dapi-container" ,
2018-08-14 13:37:40 +00:00
Image : imageutils . GetE2EImage ( imageutils . BusyBox ) ,
2015-06-13 03:02:29 +00:00
Command : [ ] string { "sh" , "-c" , "TEST_VAR=wrong echo \"$(TEST_VAR)\"" } ,
2016-11-18 20:55:17 +00:00
Env : [ ] v1 . EnvVar {
2015-06-13 03:02:29 +00:00
{
Name : "TEST_VAR" ,
Value : "test-value" ,
} ,
} ,
} ,
} ,
2016-11-18 20:55:17 +00:00
RestartPolicy : v1 . RestartPolicyNever ,
2015-06-13 03:02:29 +00:00
} ,
}
2016-04-07 17:21:31 +00:00
f . TestContainerOutput ( "substitution in container's command" , pod , 0 , [ ] string {
2015-06-13 03:02:29 +00:00
"test-value" ,
2015-07-09 11:56:19 +00:00
} )
2015-06-13 03:02:29 +00:00
} )
2017-10-20 20:29:25 +00:00
/ *
2018-02-27 22:57:56 +00:00
Release : v1 .9
Testname : Environment variables , command argument expansion
Description : Create a Pod with environment variables and container command arguments using them . Container command arguments using the defined environment variables MUST expand to proper values .
2017-10-20 20:29:25 +00:00
* /
2018-05-22 01:09:33 +00:00
framework . ConformanceIt ( "should allow substituting values in a container's args [NodeConformance]" , func ( ) {
2016-07-26 15:13:18 +00:00
podName := "var-expansion-" + string ( uuid . NewUUID ( ) )
2016-11-18 20:55:17 +00:00
pod := & v1 . Pod {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2015-06-13 03:02:29 +00:00
Name : podName ,
Labels : map [ string ] string { "name" : podName } ,
} ,
2016-11-18 20:55:17 +00:00
Spec : v1 . PodSpec {
Containers : [ ] v1 . Container {
2015-06-13 03:02:29 +00:00
{
Name : "dapi-container" ,
2018-08-14 13:37:40 +00:00
Image : imageutils . GetE2EImage ( imageutils . BusyBox ) ,
2015-06-13 03:02:29 +00:00
Command : [ ] string { "sh" , "-c" } ,
Args : [ ] string { "TEST_VAR=wrong echo \"$(TEST_VAR)\"" } ,
2016-11-18 20:55:17 +00:00
Env : [ ] v1 . EnvVar {
2015-06-13 03:02:29 +00:00
{
Name : "TEST_VAR" ,
Value : "test-value" ,
} ,
} ,
} ,
} ,
2016-11-18 20:55:17 +00:00
RestartPolicy : v1 . RestartPolicyNever ,
2015-06-13 03:02:29 +00:00
} ,
}
2016-04-07 17:21:31 +00:00
f . TestContainerOutput ( "substitution in container's args" , pod , 0 , [ ] string {
2015-06-13 03:02:29 +00:00
"test-value" ,
2015-07-09 11:56:19 +00:00
} )
2015-06-13 03:02:29 +00:00
} )
2017-07-21 14:42:03 +00:00
/ *
Testname : var - expansion - subpath
Description : Make sure a container ' s subpath can be set using an
expansion of environment variables .
* /
2019-04-13 16:02:52 +00:00
It ( "should allow substituting values in a volume subpath [sig-storage][NodeFeature:VolumeSubpathEnvExpansion]" , func ( ) {
2017-07-21 14:42:03 +00:00
podName := "var-expansion-" + string ( uuid . NewUUID ( ) )
pod := & v1 . Pod {
ObjectMeta : metav1 . ObjectMeta {
Name : podName ,
Labels : map [ string ] string { "name" : podName } ,
} ,
Spec : v1 . PodSpec {
Containers : [ ] v1 . Container {
{
Name : "dapi-container" ,
2018-08-14 13:37:40 +00:00
Image : imageutils . GetE2EImage ( imageutils . BusyBox ) ,
2017-07-21 14:42:03 +00:00
Command : [ ] string { "sh" , "-c" , "test -d /testcontainer/" + podName + ";echo $?" } ,
Env : [ ] v1 . EnvVar {
{
Name : "POD_NAME" ,
Value : podName ,
} ,
} ,
VolumeMounts : [ ] v1 . VolumeMount {
{
2018-11-20 13:05:19 +00:00
Name : "workdir1" ,
MountPath : "/logscontainer" ,
SubPathExpr : "$(POD_NAME)" ,
2017-07-21 14:42:03 +00:00
} ,
{
Name : "workdir2" ,
MountPath : "/testcontainer" ,
} ,
} ,
} ,
} ,
RestartPolicy : v1 . RestartPolicyNever ,
Volumes : [ ] v1 . Volume {
{
Name : "workdir1" ,
VolumeSource : v1 . VolumeSource {
HostPath : & v1 . HostPathVolumeSource { Path : "/tmp" } ,
} ,
} ,
{
Name : "workdir2" ,
VolumeSource : v1 . VolumeSource {
HostPath : & v1 . HostPathVolumeSource { Path : "/tmp" } ,
} ,
} ,
} ,
} ,
}
f . TestContainerOutput ( "substitution in volume subpath" , pod , 0 , [ ] string {
"0" ,
} )
} )
/ *
Testname : var - expansion - subpath - with - backticks
Description : Make sure a container ' s subpath can not be set using an
expansion of environment variables when backticks are supplied .
* /
2019-04-13 16:02:52 +00:00
It ( "should fail substituting values in a volume subpath with backticks [sig-storage][NodeFeature:VolumeSubpathEnvExpansion][Slow]" , func ( ) {
2017-07-21 14:42:03 +00:00
podName := "var-expansion-" + string ( uuid . NewUUID ( ) )
pod := & v1 . Pod {
ObjectMeta : metav1 . ObjectMeta {
Name : podName ,
Labels : map [ string ] string { "name" : podName } ,
} ,
Spec : v1 . PodSpec {
Containers : [ ] v1 . Container {
{
Name : "dapi-container" ,
2018-08-14 13:37:40 +00:00
Image : imageutils . GetE2EImage ( imageutils . BusyBox ) ,
2017-07-21 14:42:03 +00:00
Env : [ ] v1 . EnvVar {
{
Name : "POD_NAME" ,
Value : ".." ,
} ,
} ,
VolumeMounts : [ ] v1 . VolumeMount {
{
2018-11-20 13:05:19 +00:00
Name : "workdir1" ,
MountPath : "/logscontainer" ,
SubPathExpr : "$(POD_NAME)" ,
2017-07-21 14:42:03 +00:00
} ,
} ,
} ,
} ,
RestartPolicy : v1 . RestartPolicyNever ,
Volumes : [ ] v1 . Volume {
{
Name : "workdir1" ,
VolumeSource : v1 . VolumeSource {
EmptyDir : & v1 . EmptyDirVolumeSource { } ,
} ,
} ,
} ,
} ,
}
// Pod should fail
2018-06-04 15:11:28 +00:00
testPodFailSubpath ( f , pod )
2017-07-21 14:42:03 +00:00
} )
/ *
Testname : var - expansion - subpath - with - absolute - path
Description : Make sure a container ' s subpath can not be set using an
2018-06-04 15:11:28 +00:00
expansion of environment variables when absolute path is supplied .
2017-07-21 14:42:03 +00:00
* /
2019-04-13 16:02:52 +00:00
It ( "should fail substituting values in a volume subpath with absolute path [sig-storage][NodeFeature:VolumeSubpathEnvExpansion][Slow]" , func ( ) {
2017-07-21 14:42:03 +00:00
podName := "var-expansion-" + string ( uuid . NewUUID ( ) )
pod := & v1 . Pod {
ObjectMeta : metav1 . ObjectMeta {
Name : podName ,
Labels : map [ string ] string { "name" : podName } ,
} ,
Spec : v1 . PodSpec {
Containers : [ ] v1 . Container {
{
Name : "dapi-container" ,
2018-08-14 13:37:40 +00:00
Image : imageutils . GetE2EImage ( imageutils . BusyBox ) ,
2017-07-21 14:42:03 +00:00
Env : [ ] v1 . EnvVar {
{
Name : "POD_NAME" ,
Value : "/tmp" ,
} ,
} ,
VolumeMounts : [ ] v1 . VolumeMount {
{
2018-11-20 13:05:19 +00:00
Name : "workdir1" ,
MountPath : "/logscontainer" ,
SubPathExpr : "$(POD_NAME)" ,
2017-07-21 14:42:03 +00:00
} ,
} ,
} ,
} ,
RestartPolicy : v1 . RestartPolicyNever ,
Volumes : [ ] v1 . Volume {
{
Name : "workdir1" ,
VolumeSource : v1 . VolumeSource {
EmptyDir : & v1 . EmptyDirVolumeSource { } ,
} ,
} ,
} ,
} ,
}
// Pod should fail
2018-06-04 15:11:28 +00:00
testPodFailSubpath ( f , pod )
2017-07-21 14:42:03 +00:00
} )
2018-11-20 13:05:19 +00:00
/ *
Testname : var - expansion - subpath - ready - from - failed - state
Description : Verify that a failing subpath expansion can be modified during the lifecycle of a container .
* /
2019-04-13 16:02:52 +00:00
It ( "should verify that a failing subpath expansion can be modified during the lifecycle of a container [sig-storage][NodeFeature:VolumeSubpathEnvExpansion][Slow]" , func ( ) {
2018-11-20 13:05:19 +00:00
podName := "var-expansion-" + string ( uuid . NewUUID ( ) )
containerName := "dapi-container"
pod := & v1 . Pod {
ObjectMeta : metav1 . ObjectMeta {
Name : podName ,
Labels : map [ string ] string { "name" : podName } ,
Annotations : map [ string ] string { "notmysubpath" : "mypath" } ,
} ,
Spec : v1 . PodSpec {
Containers : [ ] v1 . Container {
{
Name : containerName ,
Image : imageutils . GetE2EImage ( imageutils . BusyBox ) ,
Command : [ ] string { "sh" , "-c" , "tail -f /dev/null" } ,
Env : [ ] v1 . EnvVar {
{
Name : "POD_NAME" ,
Value : "foo" ,
} ,
{
Name : "ANNOTATION" ,
ValueFrom : & v1 . EnvVarSource {
FieldRef : & v1 . ObjectFieldSelector {
APIVersion : "v1" ,
FieldPath : "metadata.annotations['mysubpath']" ,
} ,
} ,
} ,
} ,
VolumeMounts : [ ] v1 . VolumeMount {
{
Name : "workdir1" ,
MountPath : "/subpath_mount" ,
SubPathExpr : "$(ANNOTATION)/$(POD_NAME)" ,
} ,
{
Name : "workdir2" ,
MountPath : "/volume_mount" ,
} ,
} ,
} ,
} ,
Volumes : [ ] v1 . Volume {
{
Name : "workdir1" ,
VolumeSource : v1 . VolumeSource {
HostPath : & v1 . HostPathVolumeSource { Path : "/tmp" } ,
} ,
} ,
{
Name : "workdir2" ,
VolumeSource : v1 . VolumeSource {
HostPath : & v1 . HostPathVolumeSource { Path : "/tmp" } ,
} ,
} ,
} ,
} ,
}
By ( "creating the pod with failed condition" )
var podClient * framework . PodClient
podClient = f . PodClient ( )
2019-02-27 12:51:56 +00:00
pod = podClient . Create ( pod )
err := framework . WaitTimeoutForPodRunningInNamespace ( f . ClientSet , pod . Name , pod . Namespace , framework . PodStartShortTimeout )
Expect ( err ) . To ( HaveOccurred ( ) , "while waiting for pod to be running" )
2018-11-20 13:05:19 +00:00
By ( "updating the pod" )
podClient . Update ( podName , func ( pod * v1 . Pod ) {
pod . ObjectMeta . Annotations = map [ string ] string { "mysubpath" : "mypath" }
} )
By ( "waiting for pod running" )
err = framework . WaitTimeoutForPodRunningInNamespace ( f . ClientSet , pod . Name , pod . Namespace , framework . PodStartShortTimeout )
Expect ( err ) . NotTo ( HaveOccurred ( ) , "while waiting for pod to be running" )
By ( "deleting the pod gracefully" )
err = framework . DeletePodWithWait ( f , f . ClientSet , pod )
Expect ( err ) . NotTo ( HaveOccurred ( ) , "failed to delete pod" )
} )
/ *
Testname : var - expansion - subpath - test - writes
Description : Verify that a subpath expansion can be used to write files into subpaths .
1. valid subpathexpr starts a container running
2. test for valid subpath writes
3. successful expansion of the subpathexpr isn ' t required for volume cleanup
* /
2019-04-13 16:02:52 +00:00
It ( "should succeed in writing subpaths in container [sig-storage][NodeFeature:VolumeSubpathEnvExpansion][Slow]" , func ( ) {
2018-11-20 13:05:19 +00:00
podName := "var-expansion-" + string ( uuid . NewUUID ( ) )
containerName := "dapi-container"
pod := & v1 . Pod {
ObjectMeta : metav1 . ObjectMeta {
Name : podName ,
Labels : map [ string ] string { "name" : podName } ,
Annotations : map [ string ] string { "mysubpath" : "mypath" } ,
} ,
Spec : v1 . PodSpec {
Containers : [ ] v1 . Container {
{
Name : containerName ,
Image : imageutils . GetE2EImage ( imageutils . BusyBox ) ,
Command : [ ] string { "sh" , "-c" , "tail -f /dev/null" } ,
Env : [ ] v1 . EnvVar {
{
Name : "POD_NAME" ,
Value : "foo" ,
} ,
{
Name : "ANNOTATION" ,
ValueFrom : & v1 . EnvVarSource {
FieldRef : & v1 . ObjectFieldSelector {
APIVersion : "v1" ,
FieldPath : "metadata.annotations['mysubpath']" ,
} ,
} ,
} ,
} ,
VolumeMounts : [ ] v1 . VolumeMount {
{
Name : "workdir1" ,
MountPath : "/subpath_mount" ,
SubPathExpr : "$(ANNOTATION)/$(POD_NAME)" ,
} ,
{
Name : "workdir2" ,
MountPath : "/volume_mount" ,
} ,
} ,
} ,
} ,
RestartPolicy : v1 . RestartPolicyNever ,
Volumes : [ ] v1 . Volume {
{
Name : "workdir1" ,
VolumeSource : v1 . VolumeSource {
HostPath : & v1 . HostPathVolumeSource { Path : "/tmp" } ,
} ,
} ,
{
Name : "workdir2" ,
VolumeSource : v1 . VolumeSource {
HostPath : & v1 . HostPathVolumeSource { Path : "/tmp" } ,
} ,
} ,
} ,
} ,
}
By ( "creating the pod" )
2019-02-27 12:51:56 +00:00
var podClient * framework . PodClient
podClient = f . PodClient ( )
pod = podClient . Create ( pod )
2018-11-20 13:05:19 +00:00
By ( "waiting for pod running" )
2019-02-27 12:51:56 +00:00
err := framework . WaitTimeoutForPodRunningInNamespace ( f . ClientSet , pod . Name , pod . Namespace , framework . PodStartShortTimeout )
2018-11-20 13:05:19 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) , "while waiting for pod to be running" )
By ( "creating a file in subpath" )
cmd := "touch /volume_mount/mypath/foo/test.log"
2019-03-01 11:33:03 +00:00
_ , _ , err = f . ExecShellInPodWithFullOutput ( pod . Name , cmd )
2018-11-20 13:05:19 +00:00
if err != nil {
framework . Failf ( "expected to be able to write to subpath" )
}
By ( "test for file in mounted path" )
cmd = "test -f /subpath_mount/test.log"
2019-03-01 11:33:03 +00:00
_ , _ , err = f . ExecShellInPodWithFullOutput ( pod . Name , cmd )
2018-11-20 13:05:19 +00:00
if err != nil {
framework . Failf ( "expected to be able to verify file" )
}
By ( "updating the annotation value" )
podClient . Update ( podName , func ( pod * v1 . Pod ) {
pod . ObjectMeta . Annotations [ "mysubpath" ] = "mynewpath"
} )
By ( "waiting for annotated pod running" )
err = framework . WaitTimeoutForPodRunningInNamespace ( f . ClientSet , pod . Name , pod . Namespace , framework . PodStartShortTimeout )
Expect ( err ) . NotTo ( HaveOccurred ( ) , "while waiting for annotated pod to be running" )
By ( "deleting the pod gracefully" )
err = framework . DeletePodWithWait ( f , f . ClientSet , pod )
Expect ( err ) . NotTo ( HaveOccurred ( ) , "failed to delete pod" )
} )
/ *
Testname : var - expansion - subpath - lifecycle
Description : Verify should not change the subpath mount on a container restart if the environment variable changes
1. valid subpathexpr starts a container running
2. test for valid subpath writes
3. container restarts
4. delete cleanly
* /
2019-04-13 16:02:52 +00:00
It ( "should not change the subpath mount on a container restart if the environment variable changes [sig-storage][NodeFeature:VolumeSubpathEnvExpansion][Slow]" , func ( ) {
2018-11-20 13:05:19 +00:00
suffix := string ( uuid . NewUUID ( ) )
podName := fmt . Sprintf ( "var-expansion-%s" , suffix )
containerName := "dapi-container"
pod := & v1 . Pod {
ObjectMeta : metav1 . ObjectMeta {
Name : podName ,
Labels : map [ string ] string { "name" : podName } ,
Annotations : map [ string ] string { "mysubpath" : "foo" } ,
} ,
Spec : v1 . PodSpec {
InitContainers : [ ] v1 . Container {
{
Name : fmt . Sprintf ( "init-volume-%s" , suffix ) ,
Image : imageutils . GetE2EImage ( imageutils . BusyBox ) ,
Command : [ ] string { "sh" , "-c" , "mkdir -p /volume_mount/foo; touch /volume_mount/foo/test.log" } ,
VolumeMounts : [ ] v1 . VolumeMount {
{
Name : "workdir1" ,
MountPath : "/subpath_mount" ,
} ,
{
Name : "workdir2" ,
MountPath : "/volume_mount" ,
} ,
} ,
} ,
} ,
Containers : [ ] v1 . Container {
{
Name : containerName ,
Image : imageutils . GetE2EImage ( imageutils . BusyBox ) ,
Command : [ ] string { "/bin/sh" , "-ec" , "sleep 100000" } ,
Env : [ ] v1 . EnvVar {
{
Name : "POD_NAME" ,
ValueFrom : & v1 . EnvVarSource {
FieldRef : & v1 . ObjectFieldSelector {
APIVersion : "v1" ,
FieldPath : "metadata.annotations['mysubpath']" ,
} ,
} ,
} ,
} ,
VolumeMounts : [ ] v1 . VolumeMount {
{
Name : "workdir1" ,
MountPath : "/subpath_mount" ,
SubPathExpr : "$(POD_NAME)" ,
} ,
{
Name : "workdir2" ,
MountPath : "/volume_mount" ,
} ,
} ,
} ,
} ,
RestartPolicy : v1 . RestartPolicyOnFailure ,
Volumes : [ ] v1 . Volume {
{
Name : "workdir1" ,
VolumeSource : v1 . VolumeSource {
HostPath : & v1 . HostPathVolumeSource { Path : "/tmp" } ,
} ,
} ,
{
Name : "workdir2" ,
VolumeSource : v1 . VolumeSource {
HostPath : & v1 . HostPathVolumeSource { Path : "/tmp" } ,
} ,
} ,
} ,
} ,
}
// Add liveness probe to subpath container
pod . Spec . Containers [ 0 ] . LivenessProbe = & v1 . Probe {
Handler : v1 . Handler {
Exec : & v1 . ExecAction {
Command : [ ] string { "cat" , "/subpath_mount/test.log" } ,
} ,
} ,
InitialDelaySeconds : 1 ,
FailureThreshold : 1 ,
PeriodSeconds : 2 ,
}
// Start pod
By ( fmt . Sprintf ( "Creating pod %s" , pod . Name ) )
2019-02-27 12:51:56 +00:00
var podClient * framework . PodClient
podClient = f . PodClient ( )
pod = podClient . Create ( pod )
2018-11-20 13:05:19 +00:00
defer func ( ) {
framework . DeletePodWithWait ( f , f . ClientSet , pod )
} ( )
2019-02-27 12:51:56 +00:00
err := framework . WaitForPodRunningInNamespace ( f . ClientSet , pod )
2018-11-20 13:05:19 +00:00
Expect ( err ) . ToNot ( HaveOccurred ( ) , "while waiting for pod to be running" )
By ( "updating the pod" )
podClient . Update ( podName , func ( pod * v1 . Pod ) {
pod . ObjectMeta . Annotations = map [ string ] string { "mysubpath" : "newsubpath" }
} )
By ( "waiting for pod and container restart" )
waitForPodContainerRestart ( f , pod , "/volume_mount/foo/test.log" )
By ( "test for subpath mounted with old value" )
cmd := "test -f /volume_mount/foo/test.log"
2019-03-01 11:33:03 +00:00
_ , _ , err = f . ExecShellInPodWithFullOutput ( pod . Name , cmd )
2018-11-20 13:05:19 +00:00
if err != nil {
framework . Failf ( "expected to be able to verify old file exists" )
}
cmd = "test ! -f /volume_mount/newsubpath/test.log"
2019-03-01 11:33:03 +00:00
_ , _ , err = f . ExecShellInPodWithFullOutput ( pod . Name , cmd )
2018-11-20 13:05:19 +00:00
if err != nil {
framework . Failf ( "expected to be able to verify new file does not exist" )
}
} )
2015-06-13 03:02:29 +00:00
} )
2017-07-21 14:42:03 +00:00
2018-06-04 15:11:28 +00:00
func testPodFailSubpath ( f * framework . Framework , pod * v1 . Pod ) {
2017-07-21 14:42:03 +00:00
2019-02-27 12:51:56 +00:00
var podClient * framework . PodClient
podClient = f . PodClient ( )
pod = podClient . Create ( pod )
2017-07-21 14:42:03 +00:00
defer func ( ) {
framework . DeletePodWithWait ( f , f . ClientSet , pod )
} ( )
2019-02-27 12:51:56 +00:00
err := framework . WaitTimeoutForPodRunningInNamespace ( f . ClientSet , pod . Name , pod . Namespace , framework . PodStartShortTimeout )
2017-07-21 14:42:03 +00:00
Expect ( err ) . To ( HaveOccurred ( ) , "while waiting for pod to be running" )
}
2018-11-20 13:05:19 +00:00
// Tests that the existing subpath mount is detected when a container restarts
func waitForPodContainerRestart ( f * framework . Framework , pod * v1 . Pod , volumeMount string ) {
By ( "Failing liveness probe" )
2019-03-01 11:33:03 +00:00
stdout , stderr , err := f . ExecShellInPodWithFullOutput ( pod . Name , fmt . Sprintf ( "rm %v" , volumeMount ) )
2018-11-20 13:05:19 +00:00
2019-05-03 16:44:04 +00:00
e2elog . Logf ( "Pod exec output: %v / %v" , stdout , stderr )
2018-11-20 13:05:19 +00:00
Expect ( err ) . ToNot ( HaveOccurred ( ) , "while failing liveness probe" )
// Check that container has restarted
By ( "Waiting for container to restart" )
restarts := int32 ( 0 )
err = wait . PollImmediate ( 10 * time . Second , 2 * time . Minute , func ( ) ( bool , error ) {
pod , err := f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Get ( pod . Name , metav1 . GetOptions { } )
if err != nil {
return false , err
}
for _ , status := range pod . Status . ContainerStatuses {
if status . Name == pod . Spec . Containers [ 0 ] . Name {
2019-05-03 16:44:04 +00:00
e2elog . Logf ( "Container %v, restarts: %v" , status . Name , status . RestartCount )
2018-11-20 13:05:19 +00:00
restarts = status . RestartCount
if restarts > 0 {
2019-05-03 16:44:04 +00:00
e2elog . Logf ( "Container has restart count: %v" , restarts )
2018-11-20 13:05:19 +00:00
return true , nil
}
}
}
return false , nil
} )
Expect ( err ) . ToNot ( HaveOccurred ( ) , "while waiting for container to restart" )
// Fix liveness probe
By ( "Rewriting the file" )
2019-03-01 11:33:03 +00:00
stdout , _ , err = f . ExecShellInPodWithFullOutput ( pod . Name , fmt . Sprintf ( "echo test-after > %v" , volumeMount ) )
2019-05-03 16:44:04 +00:00
e2elog . Logf ( "Pod exec output: %v" , stdout )
2018-11-20 13:05:19 +00:00
Expect ( err ) . ToNot ( HaveOccurred ( ) , "while rewriting the probe file" )
// Wait for container restarts to stabilize
By ( "Waiting for container to stop restarting" )
stableCount := int ( 0 )
stableThreshold := int ( time . Minute / framework . Poll )
err = wait . PollImmediate ( framework . Poll , 2 * time . Minute , func ( ) ( bool , error ) {
pod , err := f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Get ( pod . Name , metav1 . GetOptions { } )
if err != nil {
return false , err
}
for _ , status := range pod . Status . ContainerStatuses {
if status . Name == pod . Spec . Containers [ 0 ] . Name {
if status . RestartCount == restarts {
stableCount ++
if stableCount > stableThreshold {
2019-05-03 16:44:04 +00:00
e2elog . Logf ( "Container restart has stabilized" )
2018-11-20 13:05:19 +00:00
return true , nil
}
} else {
restarts = status . RestartCount
stableCount = 0
2019-05-03 16:44:04 +00:00
e2elog . Logf ( "Container has restart count: %v" , restarts )
2018-11-20 13:05:19 +00:00
}
break
}
}
return false , nil
} )
Expect ( err ) . ToNot ( HaveOccurred ( ) , "while waiting for container to stabilize" )
}