Skip to content

Commit

Permalink
Review 5 changes
Browse files Browse the repository at this point in the history
Signed-off-by: Kanha gupta <kanhag4163@gmail.com>
  • Loading branch information
kanha-gupta committed May 11, 2024
1 parent 0a0801f commit 66f0fe0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
38 changes: 28 additions & 10 deletions pkg/antctl/raw/check/cluster/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cluster

import (
"context"
"errors"
"fmt"
"os"
"time"
Expand Down Expand Up @@ -44,11 +45,23 @@ func Command() *cobra.Command {
}

const (
testNamespace = "antrea-test"
deploymentName = "cluster-checker"
podReadyTimeout = 1 * time.Minute
testNamespacePrefix = "antrea-test"
deploymentName = "cluster-checker"
podReadyTimeout = 1 * time.Minute
)

type uncertainError struct {
reason string
}

func (e uncertainError) Error() string {
return fmt.Sprintf("test results are uncertain: %s", e.reason)
}

func newUncertainError(reason string, a ...interface{}) uncertainError {
return uncertainError{reason: fmt.Sprintf(reason, a...)}
}

type Test interface {
Run(ctx context.Context, testContext *testContext) error
}
Expand Down Expand Up @@ -77,18 +90,22 @@ func Run() error {
if err := testContext.setup(ctx); err != nil {
return err
}
var numSuccess, numFailure int
var numSuccess, numFailure, numSkipped int
for name, test := range testsRegistry {
testContext.Header("Running test: %s", name)
if err := test.Run(ctx, testContext); err != nil {
if errors.As(err, new(uncertainError)) {
testContext.Warning("Test %s was skipped: %v", name, err)
numSkipped++
}
testContext.Fail("Test %s failed: %v", name, err)
numFailure++
} else {
testContext.Success("Test %s passed", name)
numSuccess++
}
}
testContext.Log("Test finished: %v tests succeeded, %v tests failed ", numSuccess, numFailure)
testContext.Log("Test finished: %v tests succeeded, %v tests failed, %v tests were skipped", numSuccess, numFailure, numSkipped)
check.Teardown(ctx, testContext.client, testContext.clusterName, testContext.namespace)
if numFailure > 0 {
return fmt.Errorf("%v/%v tests failed", numFailure, len(testsRegistry))
Expand Down Expand Up @@ -168,10 +185,7 @@ func (t *testContext) setup(ctx context.Context) error {
}
testPods, err := t.client.CoreV1().Pods(t.namespace).List(ctx, metav1.ListOptions{LabelSelector: "component=cluster-checker"})
if err != nil {
return fmt.Errorf("unable to list test Pod: %s", err)
}
if len(testPods.Items) == 0 {
return fmt.Errorf("unable to list pods")
return fmt.Errorf("no pod found for test Deployment")
}
t.testPod = &testPods.Items[0]
return nil
Expand All @@ -189,7 +203,7 @@ func NewTestContext(client kubernetes.Interface, config *rest.Config, clusterNam
client: client,
config: config,
clusterName: clusterName,
namespace: check.GenerateRandomNamespace(testNamespace),
namespace: check.GenerateRandomNamespace(testNamespacePrefix),
}
}

Expand All @@ -205,6 +219,10 @@ func (t *testContext) Fail(format string, a ...interface{}) {
fmt.Fprintf(os.Stdout, fmt.Sprintf("[%s] ", t.clusterName)+color.RedString(format, a...)+"\n")
}

func (t *testContext) Warning(format string, a ...interface{}) {
fmt.Fprintf(os.Stdout, fmt.Sprintf("[%s] ", t.clusterName)+color.YellowString(format, a...)+"\n")
}

func (t *testContext) Header(format string, a ...interface{}) {
t.Log("-------------------------------------------------------------------------------------------")
t.Log(format, a...)
Expand Down
16 changes: 7 additions & 9 deletions pkg/antctl/raw/check/cluster/test_checkcniexistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,20 @@ func (t *checkCNIExistence) Run(ctx context.Context, testContext *testContext) e
command := []string{"ls", "-1", "/etc/cni/net.d"}
output, _, err := check.ExecInPod(ctx, testContext.client, testContext.config, testContext.namespace, testContext.testPod.Name, "", command)
if err != nil {
return fmt.Errorf("Failed to execute command in Pod %s, error: %v", testContext.testPod.Name, err)
return fmt.Errorf("failed to execute command in Pod %s, error: %v", testContext.testPod.Name, err)
}
files := strings.Fields(output)
if len(files) == 0 {
testContext.Log("No files present in /etc/cni/net.d in Node %s", testContext.testPod.Spec.NodeName)
return nil
}
sort.Strings(files)
if len(files) > 0 {
if files[0] < "10-antrea.conflist" {
return fmt.Errorf("Another CNI configuration file with higher priority than Antrea's CNI configuration file found: %s; this may be expected if networkPolicyOnly mode is enabled", files[0])
} else if files[0] != "10-antrea.conflist" {
testContext.Log("Another CNI configuration file found: %s with Antrea having higher precedence", files[0])
} else {
testContext.Log("Antrea's CNI configuration file already present: %s", files[0])
}
if files[0] < "10-antrea.conflist" {
return newUncertainError("another CNI configuration file with higher priority than Antrea's CNI configuration file found: %s; this may be expected if networkPolicyOnly mode is enabled", files[0])
} else if files[0] != "10-antrea.conflist" {
testContext.Log("Another CNI configuration file found: %s with Antrea having higher precedence", files[0])
} else {
testContext.Log("Antrea's CNI configuration file already present: %s", files[0])
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (t *checkControlPlaneAvailability) Run(ctx context.Context, testContext *te
}
}
if controlPlaneNodes.Len() == 0 {
testContext.Log("No control-plane Nodes were found; if installing Antrea in encap mode, some K8s functionalities (API aggregation, apiserver proxy, admission controllers) may be impacted.")
return newUncertainError("No control-plane Nodes were found; if installing Antrea in encap mode, some K8s functionalities (API aggregation, apiserver proxy, admission controllers) may be impacted.")
} else {
testContext.Log("control-plane Nodes were found in the cluster.")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/antctl/raw/check/cluster/test_checkovsloadable.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ func (c *checkOVSLoadable) Run(ctx context.Context, testContext *testContext) er
if err != nil {
return fmt.Errorf("error executing modprobe command in Pod %s: %v", testContext.testPod.Name, err)
} else if stderr != "" {
return fmt.Errorf("failed to load the OVS kernel module from the container %s, try running 'modprobe openvswitch' on your Nodes", stderr)
return fmt.Errorf("failed to load the OVS kernel module: %s, try running 'modprobe openvswitch' on your Nodes", stderr)
} else {
testContext.Log("openvswitch kernel module loaded successfully")
}
} else {
return fmt.Errorf("error encountered while executing modprobe command %s", stderr)
return fmt.Errorf("error encountered while check if cni is existent - stderr: %s", stderr)
}
return nil
}

0 comments on commit 66f0fe0

Please sign in to comment.