Skip to content

Commit bc0cfd2

Browse files
committed
e2e/framework: remove direct imports to /pkg/api/v1/pod
1 parent 3c871ad commit bc0cfd2

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

test/e2e/framework/BUILD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ go_library(
2323
importpath = "k8s.io/kubernetes/test/e2e/framework",
2424
visibility = ["//visibility:public"],
2525
deps = [
26-
"//pkg/api/v1/pod:go_default_library",
2726
"//pkg/controller:go_default_library",
2827
"//pkg/kubelet/apis/config:go_default_library",
2928
"//pkg/kubelet/apis/stats/v1alpha1:go_default_library",
@@ -44,6 +43,7 @@ go_library(
4443
"//staging/src/k8s.io/apimachinery/pkg/util/yaml:go_default_library",
4544
"//staging/src/k8s.io/apimachinery/pkg/watch:go_default_library",
4645
"//staging/src/k8s.io/apiserver/pkg/authentication/serviceaccount:go_default_library",
46+
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
4747
"//staging/src/k8s.io/client-go/discovery:go_default_library",
4848
"//staging/src/k8s.io/client-go/discovery/cached/memory:go_default_library",
4949
"//staging/src/k8s.io/client-go/dynamic:go_default_library",
@@ -58,6 +58,7 @@ go_library(
5858
"//staging/src/k8s.io/client-go/tools/remotecommand:go_default_library",
5959
"//staging/src/k8s.io/client-go/tools/watch:go_default_library",
6060
"//staging/src/k8s.io/component-base/cli/flag:go_default_library",
61+
"//staging/src/k8s.io/component-base/featuregate:go_default_library",
6162
"//staging/src/k8s.io/kubectl/pkg/util/podutils:go_default_library",
6263
"//test/e2e/framework/auth:go_default_library",
6364
"//test/e2e/framework/ginkgowrapper:go_default_library",

test/e2e/framework/util.go

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ import (
5454
"k8s.io/apimachinery/pkg/util/wait"
5555
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
5656
"k8s.io/apimachinery/pkg/watch"
57+
utilfeature "k8s.io/apiserver/pkg/util/feature"
5758
clientset "k8s.io/client-go/kubernetes"
5859
"k8s.io/client-go/kubernetes/scheme"
5960
"k8s.io/client-go/rest"
6061
restclient "k8s.io/client-go/rest"
6162
"k8s.io/client-go/tools/clientcmd"
6263
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
6364
watchtools "k8s.io/client-go/tools/watch"
64-
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
65+
"k8s.io/component-base/featuregate"
6566
"k8s.io/kubernetes/pkg/controller"
6667
testutils "k8s.io/kubernetes/test/utils"
6768
imageutils "k8s.io/kubernetes/test/utils/image"
@@ -142,6 +143,10 @@ const (
142143

143144
// TODO(justinsb): Avoid hardcoding this.
144145
awsMasterIP = "172.20.0.9"
146+
147+
// AllContainers specifies that all containers be visited
148+
// Copied from pkg/api/v1/pod to avoid pulling extra dependencies
149+
AllContainers = InitContainers | Containers | EphemeralContainers
145150
)
146151

147152
var (
@@ -734,6 +739,66 @@ func (f *Framework) testContainerOutputMatcher(scenarioName string,
734739
ExpectNoError(f.MatchContainerOutput(pod, pod.Spec.Containers[containerIndex].Name, expectedOutput, matcher))
735740
}
736741

742+
// ContainerType signifies container type
743+
type ContainerType int
744+
745+
const (
746+
// FeatureEphemeralContainers allows running an ephemeral container in pod namespaces to troubleshoot a running pod
747+
FeatureEphemeralContainers featuregate.Feature = "EphemeralContainers"
748+
// Containers is for normal containers
749+
Containers ContainerType = 1 << iota
750+
// InitContainers is for init containers
751+
InitContainers
752+
// EphemeralContainers is for ephemeral containers
753+
EphemeralContainers
754+
)
755+
756+
// allFeatureEnabledContainers returns a ContainerType mask which includes all container
757+
// types except for the ones guarded by feature gate.
758+
// Copied from pkg/api/v1/pod to avoid pulling extra dependencies
759+
func allFeatureEnabledContainers() ContainerType {
760+
containerType := AllContainers
761+
if !utilfeature.DefaultFeatureGate.Enabled(FeatureEphemeralContainers) {
762+
containerType &= ^EphemeralContainers
763+
}
764+
return containerType
765+
}
766+
767+
// ContainerVisitor is called with each container spec, and returns true
768+
// if visiting should continue.
769+
// Copied from pkg/api/v1/pod to avoid pulling extra dependencies
770+
type ContainerVisitor func(container *v1.Container, containerType ContainerType) (shouldContinue bool)
771+
772+
// visitContainers invokes the visitor function with a pointer to every container
773+
// spec in the given pod spec with type set in mask. If visitor returns false,
774+
// visiting is short-circuited. visitContainers returns true if visiting completes,
775+
// false if visiting was short-circuited.
776+
// Copied from pkg/api/v1/pod to avoid pulling extra dependencies
777+
func visitContainers(podSpec *v1.PodSpec, mask ContainerType, visitor ContainerVisitor) bool {
778+
if mask&InitContainers != 0 {
779+
for i := range podSpec.InitContainers {
780+
if !visitor(&podSpec.InitContainers[i], InitContainers) {
781+
return false
782+
}
783+
}
784+
}
785+
if mask&Containers != 0 {
786+
for i := range podSpec.Containers {
787+
if !visitor(&podSpec.Containers[i], Containers) {
788+
return false
789+
}
790+
}
791+
}
792+
if mask&EphemeralContainers != 0 {
793+
for i := range podSpec.EphemeralContainers {
794+
if !visitor((*v1.Container)(&podSpec.EphemeralContainers[i].EphemeralContainerCommon), EphemeralContainers) {
795+
return false
796+
}
797+
}
798+
}
799+
return true
800+
}
801+
737802
// MatchContainerOutput creates a pod and waits for all it's containers to exit with success.
738803
// It then tests that the matcher with each expectedOutput matches the output of the specified container.
739804
func (f *Framework) MatchContainerOutput(
@@ -764,7 +829,7 @@ func (f *Framework) MatchContainerOutput(
764829

765830
if podErr != nil {
766831
// Pod failed. Dump all logs from all containers to see what's wrong
767-
_ = podutil.VisitContainers(&podStatus.Spec, podutil.AllFeatureEnabledContainers(), func(c *v1.Container, containerType podutil.ContainerType) bool {
832+
_ = visitContainers(&podStatus.Spec, allFeatureEnabledContainers(), func(c *v1.Container, containerType ContainerType) bool {
768833
logs, err := e2epod.GetPodLogs(f.ClientSet, ns, podStatus.Name, c.Name)
769834
if err != nil {
770835
Logf("Failed to get logs from node %q pod %q container %q: %v",

0 commit comments

Comments
 (0)