
This is the result of automatically editing source files like this: go install golang.org/x/tools/cmd/goimports@latest find ./test/e2e* -name "*.go" | xargs env PATH=$GOPATH/bin:$PATH ./e2e-framework-sed.sh with e2e-framework-sed.sh containing this: sed -i \ -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.ExecCommandInContainer(/e2epod.ExecCommandInContainer(\1, /" \ -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.ExecCommandInContainerWithFullOutput(/e2epod.ExecCommandInContainerWithFullOutput(\1, /" \ -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.ExecShellInContainer(/e2epod.ExecShellInContainer(\1, /" \ -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.ExecShellInPod(/e2epod.ExecShellInPod(\1, /" \ -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.ExecShellInPodWithFullOutput(/e2epod.ExecShellInPodWithFullOutput(\1, /" \ -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.ExecWithOptions(/e2epod.ExecWithOptions(\1, /" \ -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.MatchContainerOutput(/e2eoutput.MatchContainerOutput(\1, /" \ -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.PodClient(/e2epod.NewPodClient(\1, /" \ -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.PodClientNS(/e2epod.PodClientNS(\1, /" \ -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.TestContainerOutput(/e2eoutput.TestContainerOutput(\1, /" \ -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.TestContainerOutputRegexp(/e2eoutput.TestContainerOutputRegexp(\1, /" \ -e "s/framework.AddOrUpdateLabelOnNode\b/e2enode.AddOrUpdateLabelOnNode/" \ -e "s/framework.AllNodes\b/e2edebug.AllNodes/" \ -e "s/framework.AllNodesReady\b/e2enode.AllNodesReady/" \ -e "s/framework.ContainerResourceGatherer\b/e2edebug.ContainerResourceGatherer/" \ -e "s/framework.ContainerResourceUsage\b/e2edebug.ContainerResourceUsage/" \ -e "s/framework.CreateEmptyFileOnPod\b/e2eoutput.CreateEmptyFileOnPod/" \ -e "s/framework.DefaultPodDeletionTimeout\b/e2epod.DefaultPodDeletionTimeout/" \ -e "s/framework.DumpAllNamespaceInfo\b/e2edebug.DumpAllNamespaceInfo/" \ -e "s/framework.DumpDebugInfo\b/e2eoutput.DumpDebugInfo/" \ -e "s/framework.DumpNodeDebugInfo\b/e2edebug.DumpNodeDebugInfo/" \ -e "s/framework.EtcdUpgrade\b/e2eproviders.EtcdUpgrade/" \ -e "s/framework.EventsLister\b/e2edebug.EventsLister/" \ -e "s/framework.ExecOptions\b/e2epod.ExecOptions/" \ -e "s/framework.ExpectNodeHasLabel\b/e2enode.ExpectNodeHasLabel/" \ -e "s/framework.ExpectNodeHasTaint\b/e2enode.ExpectNodeHasTaint/" \ -e "s/framework.GCEUpgradeScript\b/e2eproviders.GCEUpgradeScript/" \ -e "s/framework.ImagePrePullList\b/e2epod.ImagePrePullList/" \ -e "s/framework.KubectlBuilder\b/e2ekubectl.KubectlBuilder/" \ -e "s/framework.LocationParamGKE\b/e2eproviders.LocationParamGKE/" \ -e "s/framework.LogSizeDataTimeseries\b/e2edebug.LogSizeDataTimeseries/" \ -e "s/framework.LogSizeGatherer\b/e2edebug.LogSizeGatherer/" \ -e "s/framework.LogsSizeData\b/e2edebug.LogsSizeData/" \ -e "s/framework.LogsSizeDataSummary\b/e2edebug.LogsSizeDataSummary/" \ -e "s/framework.LogsSizeVerifier\b/e2edebug.LogsSizeVerifier/" \ -e "s/framework.LookForStringInLog\b/e2eoutput.LookForStringInLog/" \ -e "s/framework.LookForStringInPodExec\b/e2eoutput.LookForStringInPodExec/" \ -e "s/framework.LookForStringInPodExecToContainer\b/e2eoutput.LookForStringInPodExecToContainer/" \ -e "s/framework.MasterAndDNSNodes\b/e2edebug.MasterAndDNSNodes/" \ -e "s/framework.MasterNodes\b/e2edebug.MasterNodes/" \ -e "s/framework.MasterUpgradeGKE\b/e2eproviders.MasterUpgradeGKE/" \ -e "s/framework.NewKubectlCommand\b/e2ekubectl.NewKubectlCommand/" \ -e "s/framework.NewLogsVerifier\b/e2edebug.NewLogsVerifier/" \ -e "s/framework.NewNodeKiller\b/e2enode.NewNodeKiller/" \ -e "s/framework.NewResourceUsageGatherer\b/e2edebug.NewResourceUsageGatherer/" \ -e "s/framework.NodeHasTaint\b/e2enode.NodeHasTaint/" \ -e "s/framework.NodeKiller\b/e2enode.NodeKiller/" \ -e "s/framework.NodesSet\b/e2edebug.NodesSet/" \ -e "s/framework.PodClient\b/e2epod.PodClient/" \ -e "s/framework.RemoveLabelOffNode\b/e2enode.RemoveLabelOffNode/" \ -e "s/framework.ResourceConstraint\b/e2edebug.ResourceConstraint/" \ -e "s/framework.ResourceGathererOptions\b/e2edebug.ResourceGathererOptions/" \ -e "s/framework.ResourceUsagePerContainer\b/e2edebug.ResourceUsagePerContainer/" \ -e "s/framework.ResourceUsageSummary\b/e2edebug.ResourceUsageSummary/" \ -e "s/framework.RunHostCmd\b/e2eoutput.RunHostCmd/" \ -e "s/framework.RunHostCmdOrDie\b/e2eoutput.RunHostCmdOrDie/" \ -e "s/framework.RunHostCmdWithFullOutput\b/e2eoutput.RunHostCmdWithFullOutput/" \ -e "s/framework.RunHostCmdWithRetries\b/e2eoutput.RunHostCmdWithRetries/" \ -e "s/framework.RunKubectl\b/e2ekubectl.RunKubectl/" \ -e "s/framework.RunKubectlInput\b/e2ekubectl.RunKubectlInput/" \ -e "s/framework.RunKubectlOrDie\b/e2ekubectl.RunKubectlOrDie/" \ -e "s/framework.RunKubectlOrDieInput\b/e2ekubectl.RunKubectlOrDieInput/" \ -e "s/framework.RunKubectlWithFullOutput\b/e2ekubectl.RunKubectlWithFullOutput/" \ -e "s/framework.RunKubemciCmd\b/e2ekubectl.RunKubemciCmd/" \ -e "s/framework.RunKubemciWithKubeconfig\b/e2ekubectl.RunKubemciWithKubeconfig/" \ -e "s/framework.SingleContainerSummary\b/e2edebug.SingleContainerSummary/" \ -e "s/framework.SingleLogSummary\b/e2edebug.SingleLogSummary/" \ -e "s/framework.TimestampedSize\b/e2edebug.TimestampedSize/" \ -e "s/framework.WaitForAllNodesSchedulable\b/e2enode.WaitForAllNodesSchedulable/" \ -e "s/framework.WaitForSSHTunnels\b/e2enode.WaitForSSHTunnels/" \ -e "s/framework.WorkItem\b/e2edebug.WorkItem/" \ "$@" for i in "$@"; do # Import all sub packages and let goimports figure out which of those # are redundant (= already imported) or not needed. sed -i -e '/"k8s.io.kubernetes.test.e2e.framework"/a e2edebug "k8s.io/kubernetes/test/e2e/framework/debug"' "$i" sed -i -e '/"k8s.io.kubernetes.test.e2e.framework"/a e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl"' "$i" sed -i -e '/"k8s.io.kubernetes.test.e2e.framework"/a e2enode "k8s.io/kubernetes/test/e2e/framework/node"' "$i" sed -i -e '/"k8s.io.kubernetes.test.e2e.framework"/a e2eoutput "k8s.io/kubernetes/test/e2e/framework/pod/output"' "$i" sed -i -e '/"k8s.io.kubernetes.test.e2e.framework"/a e2epod "k8s.io/kubernetes/test/e2e/framework/pod"' "$i" sed -i -e '/"k8s.io.kubernetes.test.e2e.framework"/a e2eproviders "k8s.io/kubernetes/test/e2e/framework/providers"' "$i" goimports -w "$i" done
205 lines
7.6 KiB
Go
205 lines
7.6 KiB
Go
/*
|
|
Copyright 2015 The Kubernetes Authors.
|
|
|
|
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.
|
|
*/
|
|
|
|
package network
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/onsi/ginkgo/v2"
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
clientset "k8s.io/client-go/kubernetes"
|
|
api "k8s.io/kubernetes/pkg/apis/core"
|
|
"k8s.io/kubernetes/test/e2e/framework"
|
|
e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl"
|
|
e2enetwork "k8s.io/kubernetes/test/e2e/framework/network"
|
|
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
|
|
e2eoutput "k8s.io/kubernetes/test/e2e/framework/pod/output"
|
|
e2eresource "k8s.io/kubernetes/test/e2e/framework/resource"
|
|
e2eservice "k8s.io/kubernetes/test/e2e/framework/service"
|
|
e2etestfiles "k8s.io/kubernetes/test/e2e/framework/testfiles"
|
|
"k8s.io/kubernetes/test/e2e/network/common"
|
|
admissionapi "k8s.io/pod-security-admission/api"
|
|
)
|
|
|
|
const (
|
|
dnsReadyTimeout = time.Minute
|
|
|
|
// RespondingTimeout is how long to wait for a service to be responding.
|
|
RespondingTimeout = 2 * time.Minute
|
|
)
|
|
|
|
const queryDNSPythonTemplate string = `
|
|
import socket
|
|
try:
|
|
socket.gethostbyname('%s')
|
|
print('ok')
|
|
except:
|
|
print('err')`
|
|
|
|
var _ = common.SIGDescribe("ClusterDns [Feature:Example]", func() {
|
|
f := framework.NewDefaultFramework("cluster-dns")
|
|
f.NamespacePodSecurityEnforceLevel = admissionapi.LevelPrivileged
|
|
|
|
var c clientset.Interface
|
|
ginkgo.BeforeEach(func() {
|
|
c = f.ClientSet
|
|
})
|
|
|
|
read := func(file string) string {
|
|
data, err := e2etestfiles.Read(file)
|
|
if err != nil {
|
|
framework.Fail(err.Error())
|
|
}
|
|
return string(data)
|
|
}
|
|
|
|
ginkgo.It("should create pod that uses dns", func() {
|
|
// contrary to the example, this test does not use contexts, for simplicity
|
|
// namespaces are passed directly.
|
|
// Also, for simplicity, we don't use yamls with namespaces, but we
|
|
// create testing namespaces instead.
|
|
|
|
backendName := "dns-backend"
|
|
frontendName := "dns-frontend"
|
|
clusterDnsPath := "test/e2e/testing-manifests/cluster-dns"
|
|
podOutput := "Hello World!"
|
|
|
|
// we need two namespaces anyway, so let's forget about
|
|
// the one created in BeforeEach and create two new ones.
|
|
namespaces := []*v1.Namespace{nil, nil}
|
|
for i := range namespaces {
|
|
var err error
|
|
namespaceName := fmt.Sprintf("dnsexample%d", i)
|
|
namespaces[i], err = f.CreateNamespace(namespaceName, nil)
|
|
framework.ExpectNoError(err, "failed to create namespace: %s", namespaceName)
|
|
}
|
|
|
|
for _, ns := range namespaces {
|
|
e2ekubectl.RunKubectlOrDieInput(ns.Name, read(filepath.Join(clusterDnsPath, "dns-backend-rc.yaml")), "create", "-f", "-")
|
|
}
|
|
|
|
for _, ns := range namespaces {
|
|
e2ekubectl.RunKubectlOrDieInput(ns.Name, read(filepath.Join(clusterDnsPath, "dns-backend-service.yaml")), "create", "-f", "-")
|
|
}
|
|
|
|
// wait for objects
|
|
for _, ns := range namespaces {
|
|
e2eresource.WaitForControlledPodsRunning(c, ns.Name, backendName, api.Kind("ReplicationController"))
|
|
e2enetwork.WaitForService(c, ns.Name, backendName, true, framework.Poll, framework.ServiceStartTimeout)
|
|
}
|
|
// it is not enough that pods are running because they may be set to running, but
|
|
// the application itself may have not been initialized. Just query the application.
|
|
for _, ns := range namespaces {
|
|
label := labels.SelectorFromSet(labels.Set(map[string]string{"name": backendName}))
|
|
options := metav1.ListOptions{LabelSelector: label.String()}
|
|
pods, err := c.CoreV1().Pods(ns.Name).List(context.TODO(), options)
|
|
framework.ExpectNoError(err, "failed to list pods in namespace: %s", ns.Name)
|
|
err = e2epod.PodsResponding(c, ns.Name, backendName, false, pods)
|
|
framework.ExpectNoError(err, "waiting for all pods to respond")
|
|
framework.Logf("found %d backend pods responding in namespace %s", len(pods.Items), ns.Name)
|
|
|
|
err = waitForServiceResponding(c, ns.Name, backendName)
|
|
framework.ExpectNoError(err, "waiting for the service to respond")
|
|
}
|
|
|
|
// Now another tricky part:
|
|
// It may happen that the service name is not yet in DNS.
|
|
// So if we start our pod, it will fail. We must make sure
|
|
// the name is already resolvable. So let's try to query DNS from
|
|
// the pod we have, until we find our service name.
|
|
// This complicated code may be removed if the pod itself retried after
|
|
// dns error or timeout.
|
|
// This code is probably unnecessary, but let's stay on the safe side.
|
|
label := labels.SelectorFromSet(labels.Set(map[string]string{"name": backendName}))
|
|
options := metav1.ListOptions{LabelSelector: label.String()}
|
|
pods, err := c.CoreV1().Pods(namespaces[0].Name).List(context.TODO(), options)
|
|
|
|
if err != nil || pods == nil || len(pods.Items) == 0 {
|
|
framework.Failf("no running pods found")
|
|
}
|
|
podName := pods.Items[0].Name
|
|
|
|
queryDNS := fmt.Sprintf(queryDNSPythonTemplate, backendName+"."+namespaces[0].Name)
|
|
_, err = e2eoutput.LookForStringInPodExec(namespaces[0].Name, podName, []string{"python", "-c", queryDNS}, "ok", dnsReadyTimeout)
|
|
framework.ExpectNoError(err, "waiting for output from pod exec")
|
|
|
|
updatedPodYaml := strings.Replace(read(filepath.Join(clusterDnsPath, "dns-frontend-pod.yaml")), fmt.Sprintf("dns-backend.development.svc.%s", framework.TestContext.ClusterDNSDomain), fmt.Sprintf("dns-backend.%s.svc.%s", namespaces[0].Name, framework.TestContext.ClusterDNSDomain), 1)
|
|
|
|
// create a pod in each namespace
|
|
for _, ns := range namespaces {
|
|
e2ekubectl.RunKubectlOrDieInput(ns.Name, updatedPodYaml, "create", "-f", "-")
|
|
}
|
|
|
|
// wait until the pods have been scheduler, i.e. are not Pending anymore. Remember
|
|
// that we cannot wait for the pods to be running because our pods terminate by themselves.
|
|
for _, ns := range namespaces {
|
|
err := e2epod.WaitForPodNotPending(c, ns.Name, frontendName)
|
|
framework.ExpectNoError(err)
|
|
}
|
|
|
|
// wait for pods to print their result
|
|
for _, ns := range namespaces {
|
|
_, err := e2eoutput.LookForStringInLog(ns.Name, frontendName, frontendName, podOutput, framework.PodStartTimeout)
|
|
framework.ExpectNoError(err, "pod %s failed to print result in logs", frontendName)
|
|
}
|
|
})
|
|
})
|
|
|
|
// waitForServiceResponding waits for the service to be responding.
|
|
func waitForServiceResponding(c clientset.Interface, ns, name string) error {
|
|
ginkgo.By(fmt.Sprintf("trying to dial the service %s.%s via the proxy", ns, name))
|
|
|
|
return wait.PollImmediate(framework.Poll, RespondingTimeout, func() (done bool, err error) {
|
|
proxyRequest, errProxy := e2eservice.GetServicesProxyRequest(c, c.CoreV1().RESTClient().Get())
|
|
if errProxy != nil {
|
|
framework.Logf("Failed to get services proxy request: %v:", errProxy)
|
|
return false, nil
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), framework.SingleCallTimeout)
|
|
defer cancel()
|
|
|
|
body, err := proxyRequest.Namespace(ns).
|
|
Name(name).
|
|
Do(ctx).
|
|
Raw()
|
|
if err != nil {
|
|
if ctx.Err() != nil {
|
|
framework.Failf("Failed to GET from service %s: %v", name, err)
|
|
return true, err
|
|
}
|
|
framework.Logf("Failed to GET from service %s: %v:", name, err)
|
|
return false, nil
|
|
}
|
|
got := string(body)
|
|
if len(got) == 0 {
|
|
framework.Logf("Service %s: expected non-empty response", name)
|
|
return false, err // stop polling
|
|
}
|
|
framework.Logf("Service %s: found nonempty answer: %s", name, got)
|
|
return true, nil
|
|
})
|
|
}
|