diff --git a/pkg/antctl/raw/check/cluster/command.go b/pkg/antctl/raw/check/cluster/command.go index 72f622b44e1..cfe20788ac6 100644 --- a/pkg/antctl/raw/check/cluster/command.go +++ b/pkg/antctl/raw/check/cluster/command.go @@ -16,6 +16,7 @@ package cluster import ( "context" + "errors" "fmt" "os" "time" @@ -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 } @@ -77,10 +90,14 @@ 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 { @@ -88,7 +105,7 @@ func Run() error { 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)) @@ -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 @@ -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), } } @@ -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...) diff --git a/pkg/antctl/raw/check/cluster/test_checkcniexistence.go b/pkg/antctl/raw/check/cluster/test_checkcniexistence.go index 5d1a56c7607..9de591887c2 100644 --- a/pkg/antctl/raw/check/cluster/test_checkcniexistence.go +++ b/pkg/antctl/raw/check/cluster/test_checkcniexistence.go @@ -33,7 +33,7 @@ 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 { @@ -41,14 +41,12 @@ func (t *checkCNIExistence) Run(ctx context.Context, testContext *testContext) e 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 } diff --git a/pkg/antctl/raw/check/cluster/test_checkcontrolplaneavailability.go b/pkg/antctl/raw/check/cluster/test_checkcontrolplaneavailability.go index 564344fd7f2..a4a7b1f8295 100644 --- a/pkg/antctl/raw/check/cluster/test_checkcontrolplaneavailability.go +++ b/pkg/antctl/raw/check/cluster/test_checkcontrolplaneavailability.go @@ -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.") } diff --git a/pkg/antctl/raw/check/cluster/test_checkovsloadable.go b/pkg/antctl/raw/check/cluster/test_checkovsloadable.go index 2ee302da62a..020016622e6 100644 --- a/pkg/antctl/raw/check/cluster/test_checkovsloadable.go +++ b/pkg/antctl/raw/check/cluster/test_checkovsloadable.go @@ -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 }