Skip to content

Commit

Permalink
om pool command and api tidy up
Browse files Browse the repository at this point in the history
* Use the tab renderer for "pool ls" and "pool volume ls"
* Fix the PoolList not reporting the "shm" pool
* Use a int64 to report pool usage and vol size in bytes
* Add Pool and Size to instance.Config so "volume ls" doesn't
have to go to disk to read this on every request
* Add a path.Relations type to ease conversion to/from string
slice
  • Loading branch information
cvaroqui committed Sep 18, 2023
1 parent 3993f49 commit 722edc5
Show file tree
Hide file tree
Showing 31 changed files with 844 additions and 571 deletions.
13 changes: 6 additions & 7 deletions cmd/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2852,23 +2852,22 @@ func newCmdPoolLs() *cobra.Command {
}
flags := cmd.Flags()
addFlagsGlobal(flags, &options.OptsGlobal)
addFlagPoolName(flags, &options.Name)
return cmd
}

func newCmdPoolStatus() *cobra.Command {
var options commands.CmdPoolStatus
func newCmdPoolVolumeLs() *cobra.Command {
var options commands.CmdPoolVolumeLs
cmd := &cobra.Command{
Use: "status",
Short: "show the cluster pools usage",
Aliases: []string{"statu", "stat", "sta", "st"},
Use: "ls",
Short: "list the pool volumes",
RunE: func(cmd *cobra.Command, args []string) error {
return options.Run()
},
}
flags := cmd.Flags()
addFlagsGlobal(flags, &options.OptsGlobal)
addFlagPoolStatusName(flags, &options.Name)
addFlagPoolStatusExtended(flags, &options.Verbose)
addFlagPoolName(flags, &options.Name)
return cmd
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func addFlagObjectSelector(flagSet *pflag.FlagSet, p *string) {
flagSet.StringVarP(p, "service", "s", "", "An object selector expression. `**/s[12]+!*/vol/*`.")
}

func addFlagPoolStatusName(flagSet *pflag.FlagSet, p *string) {
func addFlagPoolName(flagSet *pflag.FlagSet, p *string) {
flagSet.StringVar(p, "name", "", "Filter on a pool name.")
}

Expand Down
13 changes: 7 additions & 6 deletions cmd/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ var (
Short: "Manage storage pools",
Long: ` A pool is a vol provider. Pools abstract the hardware and software specificities of the cluster infrastructure.`,
}
cmdPoolCreate = &cobra.Command{
Use: "create",
Short: "create a pool object",
Aliases: []string{"creat", "crea", "cre", "cr"},
cmdPoolVolume = &cobra.Command{
Use: "volume",
Short: "Manage storage pool volumes",
}
)

Expand All @@ -22,8 +21,10 @@ func init() {
cmdPool,
)
cmdPool.AddCommand(
cmdPoolCreate,
cmdPoolVolume,
newCmdPoolLs(),
newCmdPoolStatus(),
)
cmdPoolVolume.AddCommand(
newCmdPoolVolumeLs(),
)
}
71 changes: 31 additions & 40 deletions core/commands/pool_ls.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,53 @@
package commands

import (
"context"
"fmt"

"github.com/opensvc/om3/core/client"
"github.com/opensvc/om3/core/clientcontext"
"github.com/opensvc/om3/core/object"
"github.com/opensvc/om3/core/output"
"github.com/opensvc/om3/core/rawconfig"
"github.com/opensvc/om3/daemon/api"
)

type (
CmdPoolLs struct {
OptsGlobal
Name string
}
)

func (t *CmdPoolLs) Run() error {
var (
data []string
err error
)
if t.Local || !clientcontext.IsSet() {
data, err = t.extractLocal()
} else {
data, err = t.extractDaemon()
c, err := client.New(client.WithURL(t.Server))
if err != nil {
return err
}
output.Renderer{
Output: t.Output,
Color: t.Color,
Data: data,
HumanRenderer: func() string {
s := ""
for _, e := range data {
s += e + "\n"
}
return s
},
Colorize: rawconfig.Colorize,
}.Print()
return err
}

func (t *CmdPoolLs) extractLocal() ([]string, error) {
n, err := object.NewNode()
params := api.GetPoolParams{}
if t.Name != "" {
params.Name = &t.Name
}
resp, err := c.GetPoolWithResponse(context.Background(), &params)
if err != nil {
return []string{}, err
return err
}
return n.ListPools(), nil
}

func (t *CmdPoolLs) extractDaemon() ([]string, error) {
var (
c *client.T
err error
)
if c, err = client.New(client.WithURL(t.Server)); err != nil {
return []string{}, err
switch resp.StatusCode() {
case 200:
output.Renderer{
DefaultOutput: "tab=NAME:name,TYPE:type,CAPABILITIES:capabilities[*],HEAD:head,SIZE:size,USED:used,FREE:free",
Output: t.Output,
Color: t.Color,
Data: *resp.JSON200,
Colorize: rawconfig.Colorize,
}.Print()
return nil
case 401:
return fmt.Errorf("%s", resp.JSON401)
case 403:
return fmt.Errorf("%s", resp.JSON403)
case 500:
return fmt.Errorf("%s", resp.JSON500)
default:
return fmt.Errorf("unexpected status code: %d", resp.StatusCode())
}
return []string{}, fmt.Errorf("todo %v", c)
return nil
}
93 changes: 0 additions & 93 deletions core/commands/pool_status.go

This file was deleted.

53 changes: 53 additions & 0 deletions core/commands/pool_volume_ls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package commands

import (
"context"
"fmt"

"github.com/opensvc/om3/core/client"
"github.com/opensvc/om3/core/output"
"github.com/opensvc/om3/core/rawconfig"
"github.com/opensvc/om3/daemon/api"
)

type (
CmdPoolVolumeLs struct {
OptsGlobal
Name string
}
)

func (t *CmdPoolVolumeLs) Run() error {
c, err := client.New(client.WithURL(t.Server))
if err != nil {
return err
}
params := api.GetPoolVolumeParams{}
if t.Name != "" {
params.Name = &t.Name
}
resp, err := c.GetPoolVolumeWithResponse(context.Background(), &params)
if err != nil {
return err
}
switch resp.StatusCode() {
case 200:
output.Renderer{
DefaultOutput: "tab=POOL:pool,PATH:path,SIZE:size,CHILDREN:children[*],IS_ORPHAN:is_orphan",
Output: t.Output,
Color: t.Color,
Data: *resp.JSON200,
Colorize: rawconfig.Colorize,
}.Print()
return nil
case 401:
return fmt.Errorf("%s", resp.JSON401)
case 403:
return fmt.Errorf("%s", resp.JSON403)
case 500:
return fmt.Errorf("%s", resp.JSON500)
default:
return fmt.Errorf("unexpected status code: %d", resp.StatusCode())
}
return nil
}
8 changes: 6 additions & 2 deletions core/instance/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type (
Config struct {
App string `json:"app,omitempty"`
Checksum string `json:"csum"`
Children []path.Relation `json:"children,omitempty"`
Children path.Relations `json:"children,omitempty"`
DRP bool `json:"drp,omitempty"`
Env string `json:"env,omitempty"`
FlexMax int `json:"flex_max,omitempty"`
Expand All @@ -28,14 +28,18 @@ type (
Nodename string `json:"-"`
Orchestrate string `json:"orchestrate"`
Path path.T `json:"-"`
Parents []path.Relation `json:"parents,omitempty"`
Parents path.Relations `json:"parents,omitempty"`
PlacementPolicy placement.Policy `json:"placement_policy"`
Priority priority.T `json:"priority,omitempty"`
Resources ResourceConfigs `json:"resources"`
Scope []string `json:"scope"`
Subsets SubsetConfigs `json:"subsets"`
Topology topology.T `json:"topology"`
UpdatedAt time.Time `json:"updated_at"`

// Volume specific
Pool *string `json:"pool,omitempty"`
Size *int64 `json:"size,omitempty"`
}
ResourceConfigs map[string]ResourceConfig
ResourceConfig struct {
Expand Down
13 changes: 13 additions & 0 deletions core/object/node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,24 @@ func (t *Node) Pools() []pool.Pooler {

func (t *Node) ListPools() []string {
l := make([]string, 0)
var hasSHM, hasDefault bool
for _, s := range t.MergedConfig().SectionStrings() {
if !strings.HasPrefix(s, "pool#") {
continue
}
name := s[5:]
if name == "shm" {
hasSHM = true
} else if name == "default" {
hasDefault = true
}
l = append(l, s[5:])
}
if !hasSHM {
l = append(l, "shm")
}
if !hasDefault {
l = append(l, "default")
}
return l
}
6 changes: 6 additions & 0 deletions core/object/type_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ type (
Topology topology.T `json:"topology"`
UpInstancesCount int `json:"up_instances_count"`

// Volume specific
Pool *string `json:"pool,omitempty"`
Size *int64 `json:"size,omitempty"`

UpdatedAt time.Time `json:"updated_at"`
}
)
Expand Down Expand Up @@ -140,13 +144,15 @@ func (s *Status) DeepCopy() *Status {
Orchestrate: s.Orchestrate,
PlacementState: s.PlacementState,
PlacementPolicy: s.PlacementPolicy,
Pool: s.Pool,
Provisioned: s.Provisioned,
Priority: s.Priority,
Topology: s.Topology,
FlexTarget: s.FlexTarget,
FlexMin: s.FlexMin,
FlexMax: s.FlexMax,
UpInstancesCount: s.UpInstancesCount,
Size: s.Size,
Scope: append([]string{}, s.Scope...),
UpdatedAt: s.UpdatedAt,
}
Expand Down
Loading

0 comments on commit 722edc5

Please sign in to comment.