Skip to content

Commit f86bf71

Browse files
authored
Merge pull request kubernetes#89560 from gavinfish/direct-k8s-pod
e2e/framework: remove direct imports to /pkg/api/v1/pod
2 parents 54e1e5b + bc0cfd2 commit f86bf71

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",
@@ -42,6 +41,7 @@ go_library(
4241
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
4342
"//staging/src/k8s.io/apimachinery/pkg/watch:go_default_library",
4443
"//staging/src/k8s.io/apiserver/pkg/authentication/serviceaccount:go_default_library",
44+
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
4545
"//staging/src/k8s.io/client-go/discovery:go_default_library",
4646
"//staging/src/k8s.io/client-go/discovery/cached/memory:go_default_library",
4747
"//staging/src/k8s.io/client-go/dynamic:go_default_library",
@@ -57,6 +57,7 @@ go_library(
5757
"//staging/src/k8s.io/client-go/tools/remotecommand:go_default_library",
5858
"//staging/src/k8s.io/client-go/tools/watch:go_default_library",
5959
"//staging/src/k8s.io/component-base/cli/flag:go_default_library",
60+
"//staging/src/k8s.io/component-base/featuregate:go_default_library",
6061
"//staging/src/k8s.io/kubectl/pkg/util/podutils:go_default_library",
6162
"//test/e2e/framework/auth:go_default_library",
6263
"//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
@@ -50,14 +50,15 @@ import (
5050
"k8s.io/apimachinery/pkg/util/uuid"
5151
"k8s.io/apimachinery/pkg/util/wait"
5252
"k8s.io/apimachinery/pkg/watch"
53+
utilfeature "k8s.io/apiserver/pkg/util/feature"
5354
clientset "k8s.io/client-go/kubernetes"
5455
"k8s.io/client-go/rest"
5556
restclient "k8s.io/client-go/rest"
5657
"k8s.io/client-go/tools/cache"
5758
"k8s.io/client-go/tools/clientcmd"
5859
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
5960
watchtools "k8s.io/client-go/tools/watch"
60-
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
61+
"k8s.io/component-base/featuregate"
6162
"k8s.io/kubernetes/pkg/controller"
6263
testutils "k8s.io/kubernetes/test/utils"
6364
imageutils "k8s.io/kubernetes/test/utils/image"
@@ -138,6 +139,10 @@ const (
138139

139140
// TODO(justinsb): Avoid hardcoding this.
140141
awsMasterIP = "172.20.0.9"
142+
143+
// AllContainers specifies that all containers be visited
144+
// Copied from pkg/api/v1/pod to avoid pulling extra dependencies
145+
AllContainers = InitContainers | Containers | EphemeralContainers
141146
)
142147

143148
var (
@@ -711,6 +716,66 @@ func (f *Framework) testContainerOutputMatcher(scenarioName string,
711716
ExpectNoError(f.MatchContainerOutput(pod, pod.Spec.Containers[containerIndex].Name, expectedOutput, matcher))
712717
}
713718

719+
// ContainerType signifies container type
720+
type ContainerType int
721+
722+
const (
723+
// FeatureEphemeralContainers allows running an ephemeral container in pod namespaces to troubleshoot a running pod
724+
FeatureEphemeralContainers featuregate.Feature = "EphemeralContainers"
725+
// Containers is for normal containers
726+
Containers ContainerType = 1 << iota
727+
// InitContainers is for init containers
728+
InitContainers
729+
// EphemeralContainers is for ephemeral containers
730+
EphemeralContainers
731+
)
732+
733+
// allFeatureEnabledContainers returns a ContainerType mask which includes all container
734+
// types except for the ones guarded by feature gate.
735+
// Copied from pkg/api/v1/pod to avoid pulling extra dependencies
736+
func allFeatureEnabledContainers() ContainerType {
737+
containerType := AllContainers
738+
if !utilfeature.DefaultFeatureGate.Enabled(FeatureEphemeralContainers) {
739+
containerType &= ^EphemeralContainers
740+
}
741+
return containerType
742+
}
743+
744+
// ContainerVisitor is called with each container spec, and returns true
745+
// if visiting should continue.
746+
// Copied from pkg/api/v1/pod to avoid pulling extra dependencies
747+
type ContainerVisitor func(container *v1.Container, containerType ContainerType) (shouldContinue bool)
748+
749+
// visitContainers invokes the visitor function with a pointer to every container
750+
// spec in the given pod spec with type set in mask. If visitor returns false,
751+
// visiting is short-circuited. visitContainers returns true if visiting completes,
752+
// false if visiting was short-circuited.
753+
// Copied from pkg/api/v1/pod to avoid pulling extra dependencies
754+
func visitContainers(podSpec *v1.PodSpec, mask ContainerType, visitor ContainerVisitor) bool {
755+
if mask&InitContainers != 0 {
756+
for i := range podSpec.InitContainers {
757+
if !visitor(&podSpec.InitContainers[i], InitContainers) {
758+
return false
759+
}
760+
}
761+
}
762+
if mask&Containers != 0 {
763+
for i := range podSpec.Containers {
764+
if !visitor(&podSpec.Containers[i], Containers) {
765+
return false
766+
}
767+
}
768+
}
769+
if mask&EphemeralContainers != 0 {
770+
for i := range podSpec.EphemeralContainers {
771+
if !visitor((*v1.Container)(&podSpec.EphemeralContainers[i].EphemeralContainerCommon), EphemeralContainers) {
772+
return false
773+
}
774+
}
775+
}
776+
return true
777+
}
778+
714779
// MatchContainerOutput creates a pod and waits for all it's containers to exit with success.
715780
// It then tests that the matcher with each expectedOutput matches the output of the specified container.
716781
func (f *Framework) MatchContainerOutput(
@@ -741,7 +806,7 @@ func (f *Framework) MatchContainerOutput(
741806

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

0 commit comments

Comments
 (0)