From 130283a6364fac7f06b91ccfa235545bed72ee24 Mon Sep 17 00:00:00 2001 From: Kanha gupta Date: Tue, 23 Apr 2024 15:25:56 +0530 Subject: [PATCH] review 7 changes Signed-off-by: Kanha gupta --- pkg/antctl/raw/check/installation/command.go | 105 ++++++++++-------- .../check/installation/test_podtointernet.go | 8 +- .../installation/test_podtopodinternode.go | 48 ++++++++ ..._podtopod.go => test_podtopodintranode.go} | 16 +-- 4 files changed, 119 insertions(+), 58 deletions(-) create mode 100644 pkg/antctl/raw/check/installation/test_podtopodinternode.go rename pkg/antctl/raw/check/installation/{test_podtopod.go => test_podtopodintranode.go} (74%) diff --git a/pkg/antctl/raw/check/installation/command.go b/pkg/antctl/raw/check/installation/command.go index 5e55be97947..22dc6a78357 100644 --- a/pkg/antctl/raw/check/installation/command.go +++ b/pkg/antctl/raw/check/installation/command.go @@ -45,11 +45,6 @@ func Command() *cobra.Command { return command } -func init() { - RegisterTest("Pod-to-Pod Connectivity", &PodToPodConnectivityTest{}) - RegisterTest("Pod-to-Internet Connectivity", &PodToInternetConnectivityTest{}) -} - type options struct { antreaNamespace string } @@ -61,15 +56,15 @@ func newOptions() *options { } const ( - postInstallationTestsNamespace = "antrea-test" - clientDeploymentName = "test-client" - echoSameNodeDeploymentName = "echo-same-node" - echoOtherNodeDeploymentName = "echo-other-node" - kindEchoName = "echo" - kindClientName = "client" - agentDaemonSetName = "antrea-agent" - deploymentImage = "registry.k8s.io/e2e-test-images/agnhost:2.29" - podReadyTimeout = 5 * time.Minute + testNamespacePrefix = "antrea-test" + clientDeploymentName = "test-client" + echoSameNodeDeploymentName = "echo-same-node" + echoOtherNodeDeploymentName = "echo-other-node" + kindEchoName = "echo" + kindClientName = "client" + agentDaemonSetName = "antrea-agent" + deploymentImage = "registry.k8s.io/e2e-test-images/agnhost:2.29" + podReadyTimeout = 1 * time.Minute ) type Test interface { @@ -83,19 +78,20 @@ func RegisterTest(name string, test Test) { } type testContext struct { - client kubernetes.Interface - config *rest.Config - clusterName string - antreaNamespace string - clientPods *corev1.PodList - echoPods map[string]string - namespace string + client kubernetes.Interface + config *rest.Config + clusterName string + antreaNamespace string + clientPods *corev1.PodList + echoSameNodePod map[string]string + echoOtherNodePod map[string]string + namespace string } func Run(o *options) error { client, config, clusterName, err := check.NewClient() if err != nil { - fmt.Fprintf(os.Stderr, "Unable to create Kubernetes client: %s", err) + return fmt.Errorf("unable to create Kubernetes client: %s", err) } ctx := context.Background() testContext := NewTestContext(client, config, clusterName, o) @@ -136,7 +132,7 @@ func newService(name string, selector map[string]string, port int) *corev1.Servi type deploymentParameters struct { Name string - Kind string + Role string Image string Replicas int Port int @@ -156,7 +152,7 @@ func newDeployment(p deploymentParameters) *appsv1.Deployment { Name: p.Name, Labels: map[string]string{ "name": p.Name, - "kind": p.Kind, + "kind": p.Role, }, }, Spec: appsv1.DeploymentSpec{ @@ -165,7 +161,7 @@ func newDeployment(p deploymentParameters) *appsv1.Deployment { Name: p.Name, Labels: map[string]string{ "name": p.Name, - "kind": p.Kind, + "kind": p.Role, }, }, Spec: corev1.PodSpec{ @@ -190,7 +186,7 @@ func newDeployment(p deploymentParameters) *appsv1.Deployment { Selector: &metav1.LabelSelector{ MatchLabels: map[string]string{ "name": p.Name, - "kind": p.Kind, + "kind": p.Role, }, }, }, @@ -203,7 +199,7 @@ func NewTestContext(client kubernetes.Interface, config *rest.Config, clusterNam config: config, clusterName: clusterName, antreaNamespace: o.antreaNamespace, - namespace: generateRandomNamespace(postInstallationTestsNamespace), + namespace: generateRandomNamespace(testNamespacePrefix), } } @@ -232,9 +228,9 @@ func (t *testContext) teardown(ctx context.Context) { return false, nil }) if err != nil { - t.Log("setup deletion failed") + t.Log("Setup deletion failed") } else { - t.Log("setup deletion successful") + t.Log("Setup deletion successful") } } @@ -257,7 +253,7 @@ func (t *testContext) setup(ctx context.Context) error { } echoDeployment := newDeployment(deploymentParameters{ Name: echoSameNodeDeploymentName, - Kind: kindEchoName, + Role: kindEchoName, Port: 80, Image: deploymentImage, Command: []string{"/agnhost", "netexec", "--http-port=80"}, @@ -288,7 +284,7 @@ func (t *testContext) setup(ctx context.Context) error { t.Log("Deploying client Deployment...") clientDeployment := newDeployment(deploymentParameters{ Name: clientDeploymentName, - Kind: kindClientName, + Role: kindClientName, Image: deploymentImage, Command: []string{"/agnhost", "pause"}, Port: 80, @@ -307,7 +303,7 @@ func (t *testContext) setup(ctx context.Context) error { } echoOtherNodeDeployment := newDeployment(deploymentParameters{ Name: echoOtherNodeDeploymentName, - Kind: kindEchoName, + Role: kindEchoName, Port: 80, Image: deploymentImage, Command: []string{"/agnhost", "netexec", "--http-port=80"}, @@ -327,36 +323,49 @@ func (t *testContext) setup(ctx context.Context) error { }, Labels: map[string]string{"app": echoOtherNodeDeploymentName}, }) - _, err = t.client.AppsV1().Deployments(t.namespace).Create(ctx, echoOtherNodeDeployment, metav1.CreateOptions{}) + nodes, err := t.client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{}) if err != nil { - return fmt.Errorf("unable to create Deployment %s: %s", echoOtherNodeDeploymentName, err) - } - - srcDeployments := []string{clientDeploymentName, echoSameNodeDeploymentName} - dstDeployments := []string{echoOtherNodeDeploymentName} - if err := t.waitForDeploymentsReady(ctx, srcDeployments, time.Second, podReadyTimeout); err != nil { - return err + return fmt.Errorf("unable to list Nodes: %s", err) } - if err := t.waitForDeploymentsReady(ctx, dstDeployments, time.Second, podReadyTimeout); err != nil { - return err + if len(nodes.Items) > 2 { + _, err = t.client.AppsV1().Deployments(t.namespace).Create(ctx, echoOtherNodeDeployment, metav1.CreateOptions{}) + if err != nil { + return fmt.Errorf("unable to create Deployment %s: %s", echoOtherNodeDeploymentName, err) + } + if err := t.waitForDeploymentsReady(ctx, time.Second, podReadyTimeout, clientDeploymentName, echoSameNodeDeploymentName, echoOtherNodeDeploymentName); err != nil { + return err + } + t.echoOtherNodePod = map[string]string{} + echoOtherNodePod, err := t.client.CoreV1().Pods(t.namespace).List(ctx, metav1.ListOptions{LabelSelector: "name=" + echoOtherNodeDeploymentName}) + if err != nil { + return fmt.Errorf("unable to list Echo Other Node Pod: %s", err) + } + for _, echoOtherNodePod := range echoOtherNodePod.Items { + t.echoOtherNodePod[echoOtherNodePod.Name] = echoOtherNodePod.Status.PodIP + } + } else { + t.Log("Skipping other node Deployments as multiple nodes are not available") + if err := t.waitForDeploymentsReady(ctx, time.Second, podReadyTimeout, clientDeploymentName, echoSameNodeDeploymentName); err != nil { + return err + } } t.clientPods, err = t.client.CoreV1().Pods(t.namespace).List(ctx, metav1.ListOptions{LabelSelector: "kind=" + kindClientName}) if err != nil { return fmt.Errorf("unable to list Client Pods: %s", err) } - t.echoPods = map[string]string{} - echoPods, err := t.client.CoreV1().Pods(t.namespace).List(ctx, metav1.ListOptions{LabelSelector: "kind=" + kindEchoName}) + t.echoSameNodePod = map[string]string{} + echoSameNodePod, err := t.client.CoreV1().Pods(t.namespace).List(ctx, metav1.ListOptions{LabelSelector: "name=" + echoSameNodeDeploymentName}) if err != nil { - return fmt.Errorf("unable to list Echo Pods: %s", err) + return fmt.Errorf("unable to list Echo same node Pod: %s", err) } - for _, echoPod := range echoPods.Items { - t.echoPods[echoPod.Name] = echoPod.Status.PodIP + for _, echoSameNodePod := range echoSameNodePod.Items { + t.echoSameNodePod[echoSameNodePod.Name] = echoSameNodePod.Status.PodIP } t.Log("Deployment is validated successfully") return nil } -func (t *testContext) waitForDeploymentsReady(ctx context.Context, deployments []string, interval, timeout time.Duration) error { +func (t *testContext) waitForDeploymentsReady(ctx context.Context, interval, timeout time.Duration, deployments ...string) error { for _, deployment := range deployments { t.Log("Waiting for Deployment %s to become ready...", deployment) err := wait.PollUntilContextTimeout(ctx, interval, timeout, false, func(ctx context.Context) (bool, error) { diff --git a/pkg/antctl/raw/check/installation/test_podtointernet.go b/pkg/antctl/raw/check/installation/test_podtointernet.go index 1f6150b6038..65137aa1b79 100644 --- a/pkg/antctl/raw/check/installation/test_podtointernet.go +++ b/pkg/antctl/raw/check/installation/test_podtointernet.go @@ -23,11 +23,13 @@ import ( type PodToInternetConnectivityTest struct{} +func init() { + RegisterTest("pod-to-internet-connectivity", &PodToInternetConnectivityTest{}) +} + func (t *PodToInternetConnectivityTest) Run(ctx context.Context, testContext *testContext) error { for _, clientPod := range testContext.clientPods.Items { - var ( - srcPod = testContext.namespace + "/" + clientPod.Name - ) + srcPod := testContext.namespace + "/" + clientPod.Name testContext.Log("Validating connectivity from pod %s to the world (google.com)...", srcPod) _, _, err := check.ExecInPod(ctx, testContext.client, testContext.config, testContext.namespace, clientPod.Name, clientDeploymentName, agnhostConnectCommand("google.com:80")) if err != nil { diff --git a/pkg/antctl/raw/check/installation/test_podtopodinternode.go b/pkg/antctl/raw/check/installation/test_podtopodinternode.go new file mode 100644 index 00000000000..f123dec908b --- /dev/null +++ b/pkg/antctl/raw/check/installation/test_podtopodinternode.go @@ -0,0 +1,48 @@ +// Copyright 2024 Antrea 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 installation + +import ( + "context" + "fmt" + + "antrea.io/antrea/pkg/antctl/raw/check" +) + +type PodToPodInterNodeConnectivityTest struct{} + +func init() { + RegisterTest("pod-to-pod-internode-connectivity", &PodToPodInterNodeConnectivityTest{}) +} + +func (t *PodToPodInterNodeConnectivityTest) Run(ctx context.Context, testContext *testContext) error { + if len(testContext.echoOtherNodePod) == 0 { + return fmt.Errorf("Skipping Inter-Node test because multiple nodes are not available") + } + for _, clientPod := range testContext.clientPods.Items { + for echoName, echoIP := range testContext.echoOtherNodePod { + srcPod := testContext.namespace + "/" + clientPod.Name + dstPod := testContext.namespace + "/" + echoName + testContext.Log("Validating from pod %s to pod %s...", srcPod, dstPod) + _, _, err := check.ExecInPod(ctx, testContext.client, testContext.config, testContext.namespace, clientPod.Name, "", agnhostConnectCommand(echoIP+":80")) + if err != nil { + return fmt.Errorf("client pod %s was not able to communicate with echo pod %s (%s): %w", clientPod.Name, echoName, echoIP, err) + } + testContext.Log("client pod %s was able to communicate with echo pod %s (%s)", clientPod.Name, echoName, echoIP) + + } + } + return nil +} diff --git a/pkg/antctl/raw/check/installation/test_podtopod.go b/pkg/antctl/raw/check/installation/test_podtopodintranode.go similarity index 74% rename from pkg/antctl/raw/check/installation/test_podtopod.go rename to pkg/antctl/raw/check/installation/test_podtopodintranode.go index d3e262d922a..61561d0cf5f 100644 --- a/pkg/antctl/raw/check/installation/test_podtopod.go +++ b/pkg/antctl/raw/check/installation/test_podtopodintranode.go @@ -21,15 +21,17 @@ import ( "antrea.io/antrea/pkg/antctl/raw/check" ) -type PodToPodConnectivityTest struct{} +type PodToPodIntraNodeConnectivityTest struct{} -func (t *PodToPodConnectivityTest) Run(ctx context.Context, testContext *testContext) error { +func init() { + RegisterTest("pod-to-pod-intranode-connectivity", &PodToPodIntraNodeConnectivityTest{}) +} + +func (t *PodToPodIntraNodeConnectivityTest) Run(ctx context.Context, testContext *testContext) error { for _, clientPod := range testContext.clientPods.Items { - for echoName, echoIP := range testContext.echoPods { - var ( - srcPod = testContext.namespace + "/" + clientPod.Name - dstPod = testContext.namespace + "/" + echoName - ) + for echoName, echoIP := range testContext.echoSameNodePod { + srcPod := testContext.namespace + "/" + clientPod.Name + dstPod := testContext.namespace + "/" + echoName testContext.Log("Validating from pod %s to pod %s...", srcPod, dstPod) _, _, err := check.ExecInPod(ctx, testContext.client, testContext.config, testContext.namespace, clientPod.Name, "", agnhostConnectCommand(echoIP+":80")) if err != nil {