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

test: Adding NetworkFence capability to test tool #410

Merged
merged 5 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions cmd/csi-addons/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,35 @@ similar to:

```console
$ kubectl exec -c csi-addons csi-backend-nodeplugin -- csi-addons -h
-cidrs string
comma separated list of cidrs
-clusterid string
clusterID
-drivername string
name of the CSI driver
name of the CSI driver
-endpoint string
CSI-Addons endpoint (default "unix:///tmp/csi-addons.sock")
CSI-Addons endpoint (default "unix:///tmp/csi-addons.sock")
-legacy
use legacy format for old Kubernetes versions
use legacy format for old Kubernetes versions
-operation string
csi-addons operation
csi-addons operation
-persistentvolume string
name of the PersistentVolume
name of the PersistentVolume
-secret namespace/name
kubernetes secret in the format namespace/name
-stagingpath string
staging path (default "/var/lib/kubelet/plugins/kubernetes.io/csi/")
staging path (default "/var/lib/kubelet/plugins/kubernetes.io/csi/")
-version
print Version details

The following operations are supported:
- ControllerReclaimSpace
- NodeReclaimSpace
- GetIdentity
- GetCapabilities
- Probe
- ControllerReclaimSpace
- NetworkFence
- NetworkUnFence
```

The above command assumes the running `csi-backend-nodeplugin` Pod has the
Expand Down
6 changes: 6 additions & 0 deletions cmd/csi-addons/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type command struct {
operation string
persistentVolume string
drivername string
secret string
cidrs string
clusterid string
legacy bool
}

Expand All @@ -56,6 +59,9 @@ func init() {
flag.StringVar(&cmd.operation, "operation", "", "csi-addons operation")
flag.StringVar(&cmd.persistentVolume, "persistentvolume", "", "name of the PersistentVolume")
flag.StringVar(&cmd.drivername, "drivername", "", "name of the CSI driver")
flag.StringVar(&cmd.secret, "secret", "", "kubernetes secret in the format `namespace/name`")
flag.StringVar(&cmd.cidrs, "cidrs", "", "comma separated list of cidrs")
flag.StringVar(&cmd.clusterid, "clusterid", "", "clusterID")
flag.BoolVar(&cmd.legacy, "legacy", false, "use legacy format for old Kubernetes versions")
flag.BoolVar(&showVersion, "version", false, "print Version details")

Expand Down
119 changes: 119 additions & 0 deletions cmd/csi-addons/networkfence.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
Copyright 2023 The Ceph-CSI 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 main

import (
"context"
"fmt"
"strings"

"github.com/csi-addons/kubernetes-csi-addons/internal/proto"
"github.com/csi-addons/kubernetes-csi-addons/internal/sidecar/service"
)

// NetworkFenceServer executes the NetworkFenceServer operation.
type networkFenceBase struct {
// inherit Connect() and Close() from type grpcClient
grpcClient

parameters map[string]string
secretName string
secretNamespace string
cidrs []string
}

func (ns *networkFenceBase) Init(c *command) error {
Copy link
Member

Choose a reason for hiding this comment

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

Can we add unit test for this function to make sure we don't break it in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. PTAL.

ns.parameters = make(map[string]string)
ns.parameters["clusterID"] = c.clusterid
if ns.parameters["clusterID"] == "" {
return fmt.Errorf("clusterID not set")
}

secrets := strings.Split(c.secret, "/")
if len(secrets) != 2 {
return fmt.Errorf("secret should be specified in the format `namespace/name`")
}
ns.secretNamespace = secrets[0]
if ns.secretNamespace == "" {
return fmt.Errorf("secret namespace is not set")
}

ns.secretName = secrets[1]
if ns.secretName == "" {
return fmt.Errorf("secret name is not set")
}

ns.cidrs = (strings.Split(c.cidrs, ","))
if len(ns.cidrs) == 0 || (len(ns.cidrs) == 1 && ns.cidrs[0] == "") {
return fmt.Errorf("cidrs not set")
}
return nil
}

type NetworkFenceServer struct {
networkFenceBase
}

var _ = registerOperation("NetworkFence", &NetworkFenceServer{})

func (ns *NetworkFenceServer) Execute() error {
k := getKubernetesClient()

nfs := service.NewNetworkFenceServer(ns.Client, k)

req := &proto.NetworkFenceRequest{
Parameters: ns.parameters,
SecretName: ns.secretName,
SecretNamespace: ns.secretNamespace,
Cidrs: ns.cidrs,
}

_, err := nfs.FenceClusterNetwork(context.TODO(), req)
if err != nil {
return err
}

fmt.Printf("Network fence successful")
return nil
}

type NetworkUnFenceServer struct {
networkFenceBase
}

var _ = registerOperation("NetworkUnFence", &NetworkUnFenceServer{})

func (ns *NetworkUnFenceServer) Execute() error {
k := getKubernetesClient()

nfs := service.NewNetworkFenceServer(ns.Client, k)

req := &proto.NetworkFenceRequest{
Parameters: ns.parameters,
SecretName: ns.secretName,
SecretNamespace: ns.secretNamespace,
Cidrs: ns.cidrs,
}

_, err := nfs.UnFenceClusterNetwork(context.TODO(), req)
if err != nil {
return err
}

fmt.Printf("Network unfence successful")
return nil
}