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

refactor(storage): adjust storage structure to add backend api #430

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
22 changes: 22 additions & 0 deletions cmd/storage/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2023-2024 Intel Corporation

// Package common has common constants, functions for all storage commands
package common

import (
"fmt"
"os"
)

// AddrCmdLineArg cmdline arg name for address
const AddrCmdLineArg = "addr"

// TimeoutCmdLineArg cmdline arg name for timeout
const TimeoutCmdLineArg = "timeout"

// PrintResponse prints only response string into stdout without any
// additional information
func PrintResponse(response string) {
fmt.Fprintln(os.Stdout, response)
}
61 changes: 13 additions & 48 deletions cmd/storage.go → cmd/storage/frontend/frontend.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,17 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2023-2024 Intel Corporation

// Package cmd implements the CLI commands
package cmd
// Package frontend implements the CLI commands for storage frontend
package frontend

import (
"fmt"
"os"
"time"
import "github.com/spf13/cobra"

"github.com/spf13/cobra"
)

const addrCmdLineArg = "addr"
const timeoutCmdLineArg = "timeout"

// NewStorageCommand tests the storage functionality
func NewStorageCommand() *cobra.Command {
// NewCreateCommand creates a new command to create frontend resources
func NewCreateCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "storage",
Aliases: []string{"g"},
Short: "Tests storage functionality",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
err := c.Help()
cobra.CheckErr(err)
},
}

flags := cmd.PersistentFlags()
flags.String(addrCmdLineArg, "localhost:50151", "address of OPI gRPC server")
flags.Duration(timeoutCmdLineArg, 10*time.Second, "timeout for a cmd")

cmd.AddCommand(newStorageCreateCommand())
cmd.AddCommand(newStorageDeleteCommand())
cmd.AddCommand(newStorageTestCommand())

return cmd
}

func newStorageCreateCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Aliases: []string{"c"},
Short: "Creates resource",
Use: "frontend",
Aliases: []string{"f"},
Short: "Creates frontend resource",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
err := c.Help()
Expand Down Expand Up @@ -93,11 +61,12 @@ func newCreateVirtioCommand() *cobra.Command {
return cmd
}

func newStorageDeleteCommand() *cobra.Command {
// NewDeleteCommand creates a new command to delete frontend resources
func NewDeleteCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"d"},
Short: "Deletes resource",
Use: "frontend",
Aliases: []string{"f"},
Short: "Deletes frontend resource",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
err := c.Help()
Expand Down Expand Up @@ -146,7 +115,3 @@ func newDeleteVirtioCommand() *cobra.Command {

return cmd
}

func printResponse(response string) {
fmt.Fprintln(os.Stdout, response)
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2023 Intel Corporation
// Copyright (C) 2023-2024 Intel Corporation

// Package cmd implements the CLI commands
package cmd
// Package frontend implements the CLI commands for storage frontend
package frontend

import (
"context"
"net"

"github.com/opiproject/godpu/storage"
"github.com/opiproject/godpu/cmd/storage/common"
frontendclient "github.com/opiproject/godpu/storage/frontend"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -41,13 +42,13 @@ func newCreateNvmeControllerTCPCommand() *cobra.Command {
Short: "Creates nvme TCP controller",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
addr, err := c.Flags().GetString(addrCmdLineArg)
addr, err := c.Flags().GetString(common.AddrCmdLineArg)
cobra.CheckErr(err)

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

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

ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand All @@ -56,7 +57,7 @@ func newCreateNvmeControllerTCPCommand() *cobra.Command {
response, err := client.CreateNvmeTCPController(ctx, id, subsystem, ip, port)
cobra.CheckErr(err)

printResponse(response.Name)
common.PrintResponse(response.Name)
},
}

Expand Down Expand Up @@ -84,13 +85,13 @@ func newCreateNvmeControllerPcieCommand() *cobra.Command {
Short: "Creates nvme PCIe controller",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
addr, err := c.Flags().GetString(addrCmdLineArg)
addr, err := c.Flags().GetString(common.AddrCmdLineArg)
cobra.CheckErr(err)

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

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

ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand All @@ -99,7 +100,7 @@ func newCreateNvmeControllerPcieCommand() *cobra.Command {
response, err := client.CreateNvmePcieController(ctx, id, subsystem, port, pf, vf)
cobra.CheckErr(err)

printResponse(response.Name)
common.PrintResponse(response.Name)
},
}

Expand All @@ -125,13 +126,13 @@ func newDeleteNvmeControllerCommand() *cobra.Command {
Short: "Deletes nvme controller",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
addr, err := c.Flags().GetString(addrCmdLineArg)
addr, err := c.Flags().GetString(common.AddrCmdLineArg)
cobra.CheckErr(err)

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

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

ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2023 Intel Corporation
// Copyright (C) 2023-2024 Intel Corporation

// Package cmd implements the CLI commands
package cmd
// Package frontend implements the CLI commands for storage frontend
package frontend

import (
"context"

"github.com/opiproject/godpu/storage"
"github.com/opiproject/godpu/cmd/storage/common"
frontendclient "github.com/opiproject/godpu/storage/frontend"
"github.com/spf13/cobra"
)

Expand All @@ -21,13 +22,13 @@ func newCreateNvmeNamespaceCommand() *cobra.Command {
Short: "Creates nvme namespace",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
addr, err := c.Flags().GetString(addrCmdLineArg)
addr, err := c.Flags().GetString(common.AddrCmdLineArg)
cobra.CheckErr(err)

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

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

ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand All @@ -36,7 +37,7 @@ func newCreateNvmeNamespaceCommand() *cobra.Command {
response, err := client.CreateNvmeNamespace(ctx, id, subsystem, volume)
cobra.CheckErr(err)

printResponse(response.Name)
common.PrintResponse(response.Name)
},
}

Expand All @@ -59,13 +60,13 @@ func newDeleteNvmeNamespaceCommand() *cobra.Command {
Short: "Deletes nvme namespace",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
addr, err := c.Flags().GetString(addrCmdLineArg)
addr, err := c.Flags().GetString(common.AddrCmdLineArg)
cobra.CheckErr(err)

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

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

ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2023 Intel Corporation
// Copyright (C) 2023-2024 Intel Corporation

// Package cmd implements the CLI commands
package cmd
// Package frontend implements the CLI commands for storage frontend
package frontend

import (
"context"

"github.com/opiproject/godpu/storage"
"github.com/opiproject/godpu/cmd/storage/common"
frontendclient "github.com/opiproject/godpu/storage/frontend"
"github.com/spf13/cobra"
)

Expand All @@ -21,13 +22,13 @@ func newCreateNvmeSubsystemCommand() *cobra.Command {
Short: "Creates nvme subsystem",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
addr, err := c.Flags().GetString(addrCmdLineArg)
addr, err := c.Flags().GetString(common.AddrCmdLineArg)
cobra.CheckErr(err)

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

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

ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand All @@ -36,7 +37,7 @@ func newCreateNvmeSubsystemCommand() *cobra.Command {
response, err := client.CreateNvmeSubsystem(ctx, id, nqn, hostnqn)
cobra.CheckErr(err)

printResponse(response.Name)
common.PrintResponse(response.Name)
},
}

Expand All @@ -58,13 +59,13 @@ func newDeleteNvmeSubsystemCommand() *cobra.Command {
Short: "Deletes nvme subsystem",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
addr, err := c.Flags().GetString(addrCmdLineArg)
addr, err := c.Flags().GetString(common.AddrCmdLineArg)
cobra.CheckErr(err)

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

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

ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand Down
21 changes: 11 additions & 10 deletions cmd/storage-virtio-blk.go → cmd/storage/frontend/virtio_blk.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2024 Intel Corporation

// Package cmd implements the CLI commands
package cmd
// Package frontend implements the CLI commands for storage frontend
package frontend

import (
"context"

"github.com/opiproject/godpu/storage"
"github.com/opiproject/godpu/cmd/storage/common"
frontendclient "github.com/opiproject/godpu/storage/frontend"
"github.com/spf13/cobra"
)

Expand All @@ -24,13 +25,13 @@ func newCreateVirtioBlkCommand() *cobra.Command {
Short: "Creates virtio-blk controller",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
addr, err := c.Flags().GetString(addrCmdLineArg)
addr, err := c.Flags().GetString(common.AddrCmdLineArg)
cobra.CheckErr(err)

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

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

ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand All @@ -39,7 +40,7 @@ func newCreateVirtioBlkCommand() *cobra.Command {
response, err := client.CreateVirtioBlk(ctx, id, volume, port, pf, vf, maxIoQPS)
cobra.CheckErr(err)

printResponse(response.Name)
common.PrintResponse(response.Name)
},
}

Expand All @@ -66,13 +67,13 @@ func newDeleteVirtioBlkCommand() *cobra.Command {
Short: "Deletes virtio-blk controller",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
addr, err := c.Flags().GetString(addrCmdLineArg)
addr, err := c.Flags().GetString(common.AddrCmdLineArg)
cobra.CheckErr(err)

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

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

ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand Down
Loading
Loading