Check for subpath error in container status instead of pod events

This commit is contained in:
Michelle Au 2018-11-29 13:25:42 -08:00
parent 409bfc4337
commit 7571b7e100
2 changed files with 47 additions and 11 deletions

View File

@ -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",

View File

@ -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"
@ -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")
}
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")
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")
}
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