Skip to content

Commit

Permalink
review 7 changes
Browse files Browse the repository at this point in the history
Signed-off-by: Kanha gupta <[email protected]>
  • Loading branch information
kanha-gupta committed Apr 23, 2024
1 parent d8ece9f commit 130283a
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 58 deletions.
105 changes: 57 additions & 48 deletions pkg/antctl/raw/check/installation/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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,
},
},
},
Expand All @@ -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),
}
}

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

Expand All @@ -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"},
Expand Down Expand Up @@ -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,
Expand All @@ -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"},
Expand All @@ -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) {
Expand Down
8 changes: 5 additions & 3 deletions pkg/antctl/raw/check/installation/test_podtointernet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
48 changes: 48 additions & 0 deletions pkg/antctl/raw/check/installation/test_podtopodinternode.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 130283a

Please sign in to comment.