Skip to content

Instantly share code, notes, and snippets.

@nixpanic
Created July 22, 2024 13:06
Show Gist options
  • Save nixpanic/3aab1eb6ad1937a52fb289e91ace3272 to your computer and use it in GitHub Desktop.
Save nixpanic/3aab1eb6ad1937a52fb289e91ace3272 to your computer and use it in GitHub Desktop.
testing tool for CSI-Addons VolumeGroup
/*
Copyright 2024 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"
"github.com/csi-addons/spec/lib/go/volumegroup"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var (
endpoint = "unix:///csi/csi-addons.sock"
parameters = map[string]string{
"clusterID": "openshift-storage",
"pool": "ocs-storagecluster-cephblockpool",
}
secrets = map[string]string{
"userID": "csi-rbd-provisioner",
"userKey": "AQApGp5m/sr+ERAAHqEBpaSe7ezCFQUZN5I87g==",
}
volumes = []string{
"0001-0011-openshift-storage-0000000000000001-eafd9e22-70de-459b-97fc-2d46e03d5f88",
"0001-0011-openshift-storage-0000000000000001-2f1acca8-95ca-4594-be0c-e5e70b6cbc43",
}
)
func main() {
conn, err := grpc.NewClient(
endpoint,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
fmt.Errorf("failed to connect to %q: %v", endpoint, err)
return
}
defer conn.Close()
client := volumegroup.NewControllerClient(conn)
fmt.Println("connected to socket " + endpoint)
vgID, err := createVG(client)
if err != nil {
fmt.Println(err)
return
}
err = deleteVG(client, vgID)
if err != nil {
fmt.Println(err)
return
}
}
func createVG(client volumegroup.ControllerClient) (string, error) {
req := &volumegroup.CreateVolumeGroupRequest{
Parameters: parameters,
Secrets: secrets,
Name: "vg-test",
// optional
VolumeIds: volumes,
}
fmt.Println("sending request to controller")
ctx := context.Background()
res, err := client.CreateVolumeGroup(ctx, req)
if err != nil {
fmt.Printf("failed to create volume group %q: %v\n", req.GetName(), err)
return "", err
}
fmt.Printf("created volume group: %v\n", res)
return res.GetVolumeGroup().GetVolumeGroupId(), nil
}
func deleteVG(client volumegroup.ControllerClient, id string) error {
req := &volumegroup.DeleteVolumeGroupRequest{
Secrets: secrets,
VolumeGroupId: id,
}
fmt.Println("sending request to controller")
ctx := context.Background()
res, err := client.DeleteVolumeGroup(ctx, req)
if err != nil {
fmt.Printf("failed to delete volume group %q: %v\n", req.GetVolumeGroupId(), err)
return err
}
fmt.Printf("deleted volume group: %v\n", res)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment