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

feat(storage/nvme): add cli for nvme controller #432

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
77 changes: 77 additions & 0 deletions cmd/storage/backend/backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2024 Intel Corporation

// Package backend implements the CLI commands for storage backend
package backend

import "github.com/spf13/cobra"

// NewCreateCommand creates a new command to create backend resources
func NewCreateCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "backend",
Aliases: []string{"b"},
Short: "Creates backend resource",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
err := c.Help()
cobra.CheckErr(err)
},
}

cmd.AddCommand(newCreateNvmeCommand())

return cmd
}

func newCreateNvmeCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "nvme",
Aliases: []string{"n"},
Short: "Creates nvme resource",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
err := c.Help()
cobra.CheckErr(err)
},
}

cmd.AddCommand(newCreateNvmeControllerCommand())

return cmd
}

// NewDeleteCommand creates a new command to delete backend resources
func NewDeleteCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "backend",
Aliases: []string{"b"},
Short: "Deletes backend resource",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
err := c.Help()
cobra.CheckErr(err)
},
}

cmd.AddCommand(newDeleteNvmeCommand())

return cmd
}

func newDeleteNvmeCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "nvme",
Aliases: []string{"n"},
Short: "Deletes nvme resource",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
err := c.Help()
cobra.CheckErr(err)
},
}

cmd.AddCommand(newDeleteNvmeControllerCommand())

return cmd
}
95 changes: 95 additions & 0 deletions cmd/storage/backend/nvme_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2024 Intel Corporation

// Package backend implements the CLI commands for storage backend
package backend

import (
"context"
"fmt"
"strings"

"github.com/opiproject/godpu/cmd/storage/common"
backendclient "github.com/opiproject/godpu/storage/backend"
pb "github.com/opiproject/opi-api/storage/v1alpha1/gen/go"
"github.com/spf13/cobra"
)

func newCreateNvmeControllerCommand() *cobra.Command {
id := ""
multipath := ""
cmd := &cobra.Command{
Use: "controller",
Aliases: []string{"c"},
Short: "Creates nvme controller representing an external nvme device",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
addr, err := c.Flags().GetString(common.AddrCmdLineArg)
cobra.CheckErr(err)

timeout, err := c.Flags().GetDuration(common.TimeoutCmdLineArg)
cobra.CheckErr(err)

client, err := backendclient.New(addr)
cobra.CheckErr(err)

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

allowedModes := map[string]pb.NvmeMultipath{
"disable": pb.NvmeMultipath_NVME_MULTIPATH_DISABLE,
"failover": pb.NvmeMultipath_NVME_MULTIPATH_FAILOVER,
"multipath": pb.NvmeMultipath_NVME_MULTIPATH_MULTIPATH,
}

mode, ok := allowedModes[strings.ToLower(multipath)]
if !ok {
cobra.CheckErr(fmt.Errorf("not allowed multipath mode: '%s'", multipath))
}

response, err := client.CreateNvmeController(ctx, id, mode)
cobra.CheckErr(err)

common.PrintResponse(response.Name)
},
}

cmd.Flags().StringVar(&id, "id", "", "id for created resource. Assigned by server if omitted.")
cmd.Flags().StringVar(&multipath, "multipath", "disable", "multipath mode (disable, failover, enable)")

return cmd
}

func newDeleteNvmeControllerCommand() *cobra.Command {
name := ""
allowMissing := false
cmd := &cobra.Command{
Use: "controller",
Aliases: []string{"c"},
Short: "Deletes nvme controller representing an external nvme device",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
addr, err := c.Flags().GetString(common.AddrCmdLineArg)
cobra.CheckErr(err)

timeout, err := c.Flags().GetDuration(common.TimeoutCmdLineArg)
cobra.CheckErr(err)

client, err := backendclient.New(addr)
cobra.CheckErr(err)

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

err = client.DeleteNvmeController(ctx, name, allowMissing)
cobra.CheckErr(err)
},
}

cmd.Flags().StringVar(&name, "name", "", "name of deleted remote controller")
cmd.Flags().BoolVar(&allowMissing, "allowMissing", false, "cmd succeeds if attempts to delete a resource that is not present")

cobra.CheckErr(cmd.MarkFlagRequired("name"))

return cmd
}
3 changes: 3 additions & 0 deletions cmd/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package storage
import (
"time"

"github.com/opiproject/godpu/cmd/storage/backend"
"github.com/opiproject/godpu/cmd/storage/common"
"github.com/opiproject/godpu/cmd/storage/frontend"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,6 +50,7 @@ func newStorageCreateCommand() *cobra.Command {
}

cmd.AddCommand(frontend.NewCreateCommand())
cmd.AddCommand(backend.NewCreateCommand())

return cmd
}
Expand All @@ -66,6 +68,7 @@ func newStorageDeleteCommand() *cobra.Command {
}

cmd.AddCommand(frontend.NewDeleteCommand())
cmd.AddCommand(backend.NewDeleteCommand())

return cmd
}
44 changes: 44 additions & 0 deletions storage/backend/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2024 Intel Corporation

// Package backend implements the go library for OPI backend storage
package backend

import (
grpcOpi "github.com/opiproject/godpu/grpc"
pb "github.com/opiproject/opi-api/storage/v1alpha1/gen/go"
"google.golang.org/grpc"
)

// CreateNvmeClient defines the function type used to retrieve NvmeRemoteControllerServiceClient
type CreateNvmeClient func(cc grpc.ClientConnInterface) pb.NvmeRemoteControllerServiceClient

// Client is used for managing storage devices on OPI server
type Client struct {
connector grpcOpi.Connector
createNvmeClient CreateNvmeClient
}

// New creates a new instance of Client
func New(addr string) (*Client, error) {
connector, err := grpcOpi.New(addr)
if err != nil {
return nil, err
}

return NewWithArgs(
connector,
pb.NewNvmeRemoteControllerServiceClient,
)
}

// NewWithArgs creates a new instance of Client with non-default members
func NewWithArgs(
connector grpcOpi.Connector,
createNvmeClient CreateNvmeClient,
) (*Client, error) {
return &Client{
connector: connector,
createNvmeClient: createNvmeClient,
}, nil
}
40 changes: 40 additions & 0 deletions storage/backend/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2024 Intel Corporation

// Package backend implements the go library for OPI backend storage
package backend

import (
"testing"
)

func TestNewClient(t *testing.T) {
tests := map[string]struct {
address string
wantErr bool
wantClient bool
}{
"empty address": {
address: "",
wantErr: true,
wantClient: false,
},
"non-empty address": {
address: "localhost:50051",
wantErr: false,
wantClient: true,
},
}

for testName, tt := range tests {
t.Run(testName, func(t *testing.T) {
client, err := New(tt.address)
if (err != nil) == !tt.wantErr {
t.Errorf("expected err: %v, received: %v", tt.wantErr, err)
}
if (client != nil) == !tt.wantClient {
t.Errorf("expected client: %v, received: %v", tt.wantClient, client)
}
})
}
}
61 changes: 61 additions & 0 deletions storage/backend/nvme_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2024 Intel Corporation

// Package backend implements the go library for OPI backend storage
package backend

import (
"context"

pb "github.com/opiproject/opi-api/storage/v1alpha1/gen/go"
)

// CreateNvmeController creates an nvme controller representing
// an external nvme device
func (c *Client) CreateNvmeController(
ctx context.Context,
id string,
multipath pb.NvmeMultipath,
) (*pb.NvmeRemoteController, error) {
conn, connClose, err := c.connector.NewConn()
if err != nil {
return nil, err
}
defer connClose()

client := c.createNvmeClient(conn)
response, err := client.CreateNvmeRemoteController(
ctx,
&pb.CreateNvmeRemoteControllerRequest{
NvmeRemoteControllerId: id,
NvmeRemoteController: &pb.NvmeRemoteController{
Multipath: multipath,
},
})

return response, err
}

// DeleteNvmeController deletes an nvme controller representing
// an external nvme device
func (c *Client) DeleteNvmeController(
ctx context.Context,
name string,
allowMissing bool,
) error {
conn, connClose, err := c.connector.NewConn()
if err != nil {
return err
}
defer connClose()

client := c.createNvmeClient(conn)
_, err = client.DeleteNvmeRemoteController(
ctx,
&pb.DeleteNvmeRemoteControllerRequest{
Name: name,
AllowMissing: allowMissing,
})

return err
}
Loading
Loading