Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add antctl check installation command to conduct connectivity checks (#6133) #6133

Merged
merged 12 commits into from
Apr 29, 2024
42 changes: 42 additions & 0 deletions .github/workflows/kind.yml
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,48 @@ jobs:
path: log.tar.gz
retention-days: 30

conduct-connectivity-test:
kanha-gupta marked this conversation as resolved.
Show resolved Hide resolved
name: Test connectivity using antctl test command
kanha-gupta marked this conversation as resolved.
Show resolved Hide resolved
needs: [ build-antrea-coverage-image ]
runs-on: [ ubuntu-latest ]
steps:
- name: Free disk space
run: |
sudo apt-get clean
df -h
- uses: actions/checkout@v4
with:
show-progress: false
- uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
- name: Download Antrea image from previous job
uses: actions/download-artifact@v4
with:
name: antrea-ubuntu-cov
- name: Load Antrea image
run: |
docker load -i antrea-ubuntu.tar
- name: Install Kind
run: |
KIND_VERSION=$(head -n1 ./ci/kind/version)
curl -Lo ./kind https://github.com/kubernetes-sigs/kind/releases/download/${KIND_VERSION}/kind-$(uname)-amd64
chmod +x ./kind
sudo mv kind /usr/local/bin
- name: Create Kind Cluster
run: |
kind create cluster --config ci/kind/config-3nodes.yml
kanha-gupta marked this conversation as resolved.
Show resolved Hide resolved
- name: load docker image into kind
kanha-gupta marked this conversation as resolved.
Show resolved Hide resolved
run: |
kind load docker-image antrea/antrea-controller-ubuntu-coverage:latest antrea/antrea-agent-ubuntu-coverage:latest
kubectl apply -f build/yamls/antrea.yml
- name: build antctl binaries
run: |
make antctl-linux
- name: run antctl command
kanha-gupta marked this conversation as resolved.
Show resolved Hide resolved
run: |
./bin/antctl-linux check installation

validate-prometheus-metrics-doc:
name: Validate metrics in Prometheus document match running deployment's
needs: build-antrea-coverage-image
Expand Down
7 changes: 7 additions & 0 deletions pkg/antctl/antctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"antrea.io/antrea/pkg/antctl/raw/proxy"
"antrea.io/antrea/pkg/antctl/raw/set"
"antrea.io/antrea/pkg/antctl/raw/supportbundle"
"antrea.io/antrea/pkg/antctl/raw/test"
"antrea.io/antrea/pkg/antctl/raw/traceflow"
"antrea.io/antrea/pkg/antctl/raw/upgrade/apistorage"
"antrea.io/antrea/pkg/antctl/transform/addressgroup"
Expand Down Expand Up @@ -639,6 +640,12 @@ $ antctl get podmulticaststats pod -n namespace`,
},
},
rawCommands: []rawCommand{
{
cobraCommand: test.Command(),
kanha-gupta marked this conversation as resolved.
Show resolved Hide resolved
supportAgent: false,
supportController: false,
commandGroup: check,
},
{
cobraCommand: supportbundle.Command,
supportAgent: true,
Expand Down
5 changes: 5 additions & 0 deletions pkg/antctl/command_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const (
query
mc
upgrade
check
)

var groupCommands = map[commandGroup]*cobra.Command{
Expand All @@ -93,6 +94,10 @@ var groupCommands = map[commandGroup]*cobra.Command{
Short: "Sub-commands for upgrade operations",
Long: "Sub-commands for upgrade operations",
},
check: {
Use: "check",
Short: "Performs pre and post installation checks",
},
}

type endpointResponder interface {
Expand Down
3 changes: 2 additions & 1 deletion pkg/antctl/command_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func (cl *commandList) applyToRootCommand(root *cobra.Command, client AntctlClie
(runtime.Mode == runtime.ModeController && cmd.supportController) ||
(runtime.Mode == runtime.ModeFlowAggregator && cmd.supportFlowAggregator) ||
(!runtime.InPod && cmd.commandGroup == mc) ||
(!runtime.InPod && cmd.commandGroup == upgrade) {
(!runtime.InPod && cmd.commandGroup == upgrade) ||
(!runtime.InPod && cmd.commandGroup == check) {
if groupCommand, ok := groupCommands[cmd.commandGroup]; ok {
groupCommand.AddCommand(cmd.cobraCommand)
} else {
Expand Down
182 changes: 182 additions & 0 deletions pkg/antctl/raw/test/client.go
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add test cases for these new antctl command. The unit test coverage of this new patch should be higher than 70%. You can take other command's test as a reference.

Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// 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 test

import (
"bytes"
"context"
"fmt"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/remotecommand"

kanha-gupta marked this conversation as resolved.
Show resolved Hide resolved
appsv1 "k8s.io/api/apps/v1"
kanha-gupta marked this conversation as resolved.
Show resolved Hide resolved
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

type Client struct {
clientSet kubernetes.Interface
config *rest.Config
clusterName string
}

type ExecResult struct {
Stdout bytes.Buffer
Stderr bytes.Buffer
}

func NewClient() (*Client, error) {
rules := clientcmd.NewDefaultClientConfigLoadingRules()

nonInteractiveClient := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, &clientcmd.ConfigOverrides{})

config, err := nonInteractiveClient.ClientConfig()
if err != nil {
return nil, err
}

rawConfig, err := nonInteractiveClient.RawConfig()
if err != nil {
return nil, err
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}

contextName := rawConfig.CurrentContext

clusterName := ""
if context, ok := rawConfig.Contexts[contextName]; ok {
clusterName = context.Cluster
}

return &Client{
clientSet: clientset,
config: config,
clusterName: clusterName,
}, nil
}

func (c *Client) ClusterName() (name string) {
return c.clusterName
}

func (c *Client) CreateService(ctx context.Context, namespace string, service *corev1.Service, opts metav1.CreateOptions) (*corev1.Service, error) {
return c.clientSet.CoreV1().Services(namespace).Create(ctx, service, opts)
}

func (c *Client) CreateDeployment(ctx context.Context, namespace string, deployment *appsv1.Deployment, opts metav1.CreateOptions) (*appsv1.Deployment, error) {
return c.clientSet.AppsV1().Deployments(namespace).Create(ctx, deployment, opts)
}

func (c *Client) GetDeployment(ctx context.Context, namespace, name string, opts metav1.GetOptions) (*appsv1.Deployment, error) {
return c.clientSet.AppsV1().Deployments(namespace).Get(ctx, name, opts)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get here, what's the benefit to wrap these calls? You may check other raw commands about how to set up a K8s client and how to do the test.


func (c *Client) DeploymentIsReady(ctx context.Context, namespace, deploymentName string) (bool, error) {
deployment, err := c.GetDeployment(ctx, namespace, deploymentName, metav1.GetOptions{})
if err != nil {
return false, err
}

if deployment.Generation <= deployment.Status.ObservedGeneration {
for _, cond := range deployment.Status.Conditions {
if cond.Type == appsv1.DeploymentProgressing && cond.Reason == "ProgressDeadlineExceeded" {
return false, fmt.Errorf("deployment %q exceeded its progress deadline", deployment.Name)
}
}
if deployment.Spec.Replicas != nil && deployment.Status.UpdatedReplicas < *deployment.Spec.Replicas {
return false, nil
}
if deployment.Status.Replicas > deployment.Status.UpdatedReplicas {
return false, nil
}
if deployment.Status.AvailableReplicas < deployment.Status.UpdatedReplicas {
return false, nil
}
return true, nil
}
return false, nil
}

func (c *Client) CreateNamespace(ctx context.Context, namespace string, opts metav1.CreateOptions) (*corev1.Namespace, error) {
return c.clientSet.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}, opts)
}

func (c *Client) GetNamespace(ctx context.Context, namespace string, options metav1.GetOptions) (*corev1.Namespace, error) {
return c.clientSet.CoreV1().Namespaces().Get(ctx, namespace, options)
}

func (c *Client) DeleteNamespace(ctx context.Context, namespace string, opts metav1.DeleteOptions) error {
return c.clientSet.CoreV1().Namespaces().Delete(ctx, namespace, opts)
}

func (c *Client) ListPods(ctx context.Context, namespace string, options metav1.ListOptions) (*corev1.PodList, error) {
return c.clientSet.CoreV1().Pods(namespace).List(ctx, options)
}

func (c *Client) ExecInPod(ctx context.Context, namespace, pod, container string, command []string) (bytes.Buffer, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check and reuse our e2e test framework codes instead of building a new function to run exec.

func (data *TestData) RunCommandFromPod(podNamespace string, podName string, containerName string, cmd []string) (stdout string, stderr string, err error) {

It may need to import a big e2e package, not sure if there is a better way to reduce duplicate codes. @antoninbas @tnqn

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the duplication is fine here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is resolved ?

req := c.clientSet.CoreV1().RESTClient().Post().Resource("pods").Name(pod).Namespace(namespace).SubResource("exec")

scheme := runtime.NewScheme()
if err := corev1.AddToScheme(scheme); err != nil {
return bytes.Buffer{}, fmt.Errorf("error adding to scheme: %w", err)
}

parameterCodec := runtime.NewParameterCodec(scheme)
tnqn marked this conversation as resolved.
Show resolved Hide resolved

req.VersionedParams(&corev1.PodExecOptions{
Command: command,
Container: container,
Stdin: false,
Stdout: true,
Stderr: true,
TTY: false,
}, parameterCodec)

exec, err := remotecommand.NewSPDYExecutor(c.config, "POST", req.URL())
if err != nil {
return bytes.Buffer{}, fmt.Errorf("error while creating executor: %w", err)
}

result := &ExecResult{}
tnqn marked this conversation as resolved.
Show resolved Hide resolved

err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
Stdin: nil,
Stdout: &result.Stdout,
Stderr: &result.Stderr,
Tty: false,
})
if err != nil {
return bytes.Buffer{}, fmt.Errorf("error in stream: %w", err)
}

if errString := result.Stderr.String(); errString != "" {
return bytes.Buffer{}, fmt.Errorf("command failed: %s", errString)
}
kanha-gupta marked this conversation as resolved.
Show resolved Hide resolved

return result.Stdout, nil
}

func (c *Client) GetDaemonSet(ctx context.Context, namespace, name string, opts metav1.GetOptions) (*appsv1.DaemonSet, error) {
return c.clientSet.AppsV1().DaemonSets(namespace).Get(ctx, name, opts)
}
Loading