Skip to content

Commit

Permalink
test: Add testing tool for VolumeGroup
Browse files Browse the repository at this point in the history
This adds testing tool for VolumeGroup operations.

Signed-off-by: ShravaniVangur <[email protected]>
  • Loading branch information
ShravaniVangur committed Sep 23, 2024
1 parent 24832d2 commit 2c7bdb6
Show file tree
Hide file tree
Showing 3 changed files with 295 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cmd/csi-addons/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ $ kubectl exec -c csi-addons csi-backend-nodeplugin -- csi-addons -h
use legacy format for old Kubernetes versions
-operation string
csi-addons operation
-parameters string
parameters in key=value format separated by commas(Eg:- k1=v1,k2=v2...)
-persistentvolume string
name of the PersistentVolume
-secret namespace/name
Expand All @@ -29,6 +31,12 @@ $ kubectl exec -c csi-addons csi-backend-nodeplugin -- csi-addons -h
staging path (default "/var/lib/kubelet/plugins/kubernetes.io/csi/")
-version
print Version details
-volumeids string
comma separated list of VolumeIDs
-volumegroupid string
VolumeGroupID
-volumegroupname string
name of the Volume Group to be created

The following operations are supported:
- ControllerReclaimSpace
Expand All @@ -44,6 +52,10 @@ The following operations are supported:
- DemoteVolume
- ResyncVolume
- GetVolumeReplicationInfo
- CreateVolumeGroup
- ModifyVolumeGroupMembership
- DeleteVolumeGroup
- ControllerGetVolumeGroup
```

The above command assumes the running `csi-backend-nodeplugin` Pod has the
Expand Down
8 changes: 8 additions & 0 deletions cmd/csi-addons/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ type command struct {
cidrs string
clusterid string
legacy bool
volumeGroupName string
volumeGroupID string
parameters string
volumeIDs string
}

// cmd is the single instance of the command struct, used inside main().
Expand All @@ -66,6 +70,10 @@ func init() {
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")
flag.StringVar(&cmd.volumeGroupName, "volumegroupname", "", "name of the Volume Group to be created")
flag.StringVar(&cmd.volumeGroupID, "volumegroupid", "", "VolumeGroupID")
flag.StringVar(&cmd.parameters, "parameters", "", "parameters in key=value format separated by commas(Eg:- k1=v1,k2=v2...)")
flag.StringVar(&cmd.volumeIDs, "volumeids", "", "comma separated list of VolumeIds")

// output to show when --help is passed
flag.Usage = func() {
Expand Down
275 changes: 275 additions & 0 deletions cmd/csi-addons/volumegroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
/*
Copyright 2024 The Kubernetes-CSI-Addons 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"
"errors"
"fmt"
"strings"

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

// CreateVolumeGroup executes the CreateVolumeGroup operation.
type CreateVolumeGroup struct {
grpcClient
volumeGroupName string
parameters map[string]string
secretName string
secretNamespace string
volumeIDs []string
}

var _ = registerOperation("CreateVolumeGroup", &CreateVolumeGroup{})

func (cvg *CreateVolumeGroup) Init(c *command) error {
cvg.volumeGroupName = c.volumeGroupName
if cvg.volumeGroupName == "" {
return fmt.Errorf("volumegroup name is not set")
}

params := make(map[string]string)
if c.parameters != "" {
pairs := strings.Split(c.parameters, ",")
for _, pair := range pairs {
kv := strings.SplitN(pair, "=", 2)
if len(kv) == 2 {
key := kv[0]
value := kv[1]
params[key] = value
} else {
fmt.Printf("Invalid parameter : %s\n", pair)
}
}

cvg.parameters = params
}

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

if c.volumeIDs != "" {
if strings.Contains(c.volumeIDs, ",") {
cvg.volumeIDs = (strings.Split(c.volumeIDs, ","))
} else {
cvg.volumeIDs = []string{c.volumeIDs}
}
}
return nil
}

func (cvg *CreateVolumeGroup) Execute() error {
vgs := service.NewVolumeGroupServer(cvg.Client, getKubernetesClient())

req := &proto.CreateVolumeGroupRequest{
Name: cvg.volumeGroupName,
Parameters: cvg.parameters,
SecretName: cvg.secretName,
SecretNamespace: cvg.secretNamespace,
VolumeIds: cvg.volumeIDs,
}
res, err := vgs.CreateVolumeGroup(context.TODO(), req)
if err != nil {
return err
}
fmt.Printf("Volume Group created: %+v", res)
return nil

}

// ModifyVolumeGroupMembership executes the ModifyVolumeGroupMembership operation.
type ModifyVolumeGroupMembership struct {
grpcClient
volumeGroupID string
volumeIDs []string
secretName string
secretNamespace string
parameters map[string]string
}

var _ = registerOperation("ModifyVolumeGroupMembership", &ModifyVolumeGroupMembership{})

func (mvgm *ModifyVolumeGroupMembership) Init(c *command) error {
mvgm.volumeGroupID = c.volumeGroupID
if mvgm.volumeGroupID == "" {
return fmt.Errorf("volumeGroupID is not set")
}
if c.volumeIDs != "" {
if strings.Contains(c.volumeIDs, ",") {
mvgm.volumeIDs = (strings.Split(c.volumeIDs, ","))
} else {
mvgm.volumeIDs = []string{c.volumeIDs}
}

}
secrets := strings.Split(c.secret, "/")
if len(secrets) != 2 {
return errors.New("secret should be specified in the format `namespace/name`")
}
mvgm.secretNamespace = secrets[0]
if mvgm.secretNamespace == "" {
return errors.New("secret namespace is not set")
}
mvgm.secretName = secrets[1]
if mvgm.secretName == "" {
return errors.New("secret name is not set")
}
params := make(map[string]string)
if c.parameters != "" {
pairs := strings.Split(c.parameters, ",")
for _, pair := range pairs {
kv := strings.SplitN(pair, "=", 2)
if len(kv) == 2 {
key := kv[0]
value := kv[1]
params[key] = value
} else {
fmt.Printf("Invalid parameter : %s\n", pair)
}
}

mvgm.parameters = params
}
return nil
}

func (mvgm *ModifyVolumeGroupMembership) Execute() error {
vgs := service.NewVolumeGroupServer(mvgm.Client, getKubernetesClient())

req := &proto.ModifyVolumeGroupMembershipRequest{
VolumeGroupId: mvgm.volumeGroupID,
VolumeIds: mvgm.volumeIDs,
SecretName: mvgm.secretName,
SecretNamespace: mvgm.secretNamespace,
Parameters: mvgm.parameters,
}

res, err := vgs.ModifyVolumeGroupMembership(context.TODO(), req)
if err != nil {
return err
}
fmt.Printf("Volume Group Membership modified for- %+v", res)
return nil
}

// DeleteVolumeGroup executes the DeleteVolumeGroup operation.
type DeleteVolumeGroup struct {
grpcClient
volumeGroupID string
secretName string
secretNamespace string
}

var _ = registerOperation("DeleteVolumeGroup", &DeleteVolumeGroup{})

func (dvg *DeleteVolumeGroup) Init(c *command) error {
dvg.volumeGroupID = c.volumeGroupID
if dvg.volumeGroupID == "" {
return fmt.Errorf("volumeGroupID is not set")
}
secrets := strings.Split(c.secret, "/")
if len(secrets) != 2 {
return errors.New("secret should be specified in the format `namespace/name`")
}
dvg.secretNamespace = secrets[0]
if dvg.secretNamespace == "" {
return errors.New("secret namespace is not set")
}
dvg.secretName = secrets[1]
if dvg.secretName == "" {
return errors.New("secret name is not set")
}
return nil

}

func (dvg *DeleteVolumeGroup) Execute() error {
vgs := service.NewVolumeGroupServer(dvg.Client, getKubernetesClient())

req := &proto.DeleteVolumeGroupRequest{
VolumeGroupId: dvg.volumeGroupID,
SecretName: dvg.secretName,
SecretNamespace: dvg.secretNamespace,
}

_, err := vgs.DeleteVolumeGroup(context.TODO(), req)
if err != nil {
return err
}
fmt.Printf("Volume Group Deleted.")
return nil

}

// ControllerGetVolumeGroup executes the ControllerGetVolumeGroup operation.
type ControllerGetVolumeGroup struct {
grpcClient
volumeGroupID string
secretName string
secretNamespace string
}

var _ = registerOperation("ControllerGetVolumeGroup", &ControllerGetVolumeGroup{})

func (gvg *ControllerGetVolumeGroup) Init(c *command) error {
gvg.volumeGroupID = c.volumeGroupID
if gvg.volumeGroupID == "" {
return fmt.Errorf("volumeGroupId is not set")
}
secrets := strings.Split(c.secret, "/")
if len(secrets) != 2 {
return errors.New("secret should be specified in the format `namespace/name`")
}
gvg.secretNamespace = secrets[0]
if gvg.secretNamespace == "" {
return errors.New("secret namespace is not set")
}
gvg.secretName = secrets[1]
if gvg.secretName == "" {
return errors.New("secret name is not set")
}
return nil
}

func (gvg *ControllerGetVolumeGroup) Execute() error {
vgs := service.NewVolumeGroupServer(gvg.Client, getKubernetesClient())

req := &proto.ControllerGetVolumeGroupRequest{
VolumeGroupId: gvg.volumeGroupID,
SecretName: gvg.secretName,
SecretNamespace: gvg.secretNamespace,
}

res, err := vgs.ControllerGetVolumeGroup(context.TODO(), req)
if err != nil {
return err
}
fmt.Printf("Controller Volume Group Info: %+v", res)
return nil
}

0 comments on commit 2c7bdb6

Please sign in to comment.