Skip to content

Commit

Permalink
Check for subpath error in container status instead of pod events
Browse files Browse the repository at this point in the history
  • Loading branch information
msau42 committed Nov 29, 2018
1 parent 409bfc4 commit 7571b7e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
1 change: 0 additions & 1 deletion test/e2e/storage/testsuites/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ go_library(
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/fields:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/errors:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/rand:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
Expand Down
57 changes: 47 additions & 10 deletions test/e2e/storage/testsuites/subpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/kubernetes/test/e2e/framework"
Expand Down Expand Up @@ -587,16 +586,54 @@ func testPodFailSubpathError(f *framework.Framework, pod *v1.Pod, errorMsg strin
defer func() {
framework.DeletePodWithWait(f, f.ClientSet, pod)
}()
By("Checking for subpath error in container status")
err = waitForPodSubpathError(f, pod)
Expect(err).NotTo(HaveOccurred(), "while waiting for subpath failure")
}

func findSubpathContainerName(pod *v1.Pod) string {
for _, container := range pod.Spec.Containers {
for _, mount := range container.VolumeMounts {
if mount.SubPath != "" {
return container.Name
}
}
}
return ""
}

func waitForPodSubpathError(f *framework.Framework, pod *v1.Pod) error {
subpathContainerName := findSubpathContainerName(pod)
if subpathContainerName == "" {
return fmt.Errorf("failed to find container that uses subpath")
}

By("Checking for subpath error event")
selector := fields.Set{
"involvedObject.kind": "Pod",
"involvedObject.name": pod.Name,
"involvedObject.namespace": f.Namespace.Name,
"reason": "Failed",
}.AsSelector().String()
err = framework.WaitTimeoutForPodEvent(f.ClientSet, pod.Name, f.Namespace.Name, selector, errorMsg, framework.PodStartTimeout)
Expect(err).NotTo(HaveOccurred(), "while waiting for failed event to occur")
return wait.PollImmediate(framework.Poll, framework.PodStartTimeout, func() (bool, error) {
pod, err := f.ClientSet.CoreV1().Pods(pod.Namespace).Get(pod.Name, metav1.GetOptions{})
if err != nil {
return false, err
}
for _, status := range pod.Status.ContainerStatuses {
// 0 is the container that uses subpath
if status.Name == subpathContainerName {
switch {
case status.State.Running != nil:
return false, fmt.Errorf("subpath container unexpectedly became running")
case status.State.Terminated != nil:
return false, fmt.Errorf("subpath container unexpectedly terminated")
case status.State.Waiting != nil:
if status.State.Waiting.Reason == "CreateContainerConfigError" &&
strings.Contains(status.State.Waiting.Message, "subPath") {
return true, nil
}
return false, nil
default:
return false, nil
}
}
}
return false, nil
})
}

// Tests that the existing subpath mount is detected when a container restarts
Expand Down

0 comments on commit 7571b7e

Please sign in to comment.