Skip to content
Closed
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
1 change: 1 addition & 0 deletions cmd/admin/v2/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func AddCmds(cmd *cobra.Command, c *config.Config) {
adminCmd.AddCommand(newComponentCmd(c))
adminCmd.AddCommand(newImageCmd(c))
adminCmd.AddCommand(newIPCmd(c))
adminCmd.AddCommand(newMachineCmd(c))
adminCmd.AddCommand(newNetworkCmd(c))
adminCmd.AddCommand(newPartitionCmd(c))
adminCmd.AddCommand(newProjectCmd(c))
Expand Down
61 changes: 55 additions & 6 deletions cmd/admin/v2/component.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package v2

import (
"context"
"fmt"
"time"

"github.com/metal-stack/api/go/enum"
adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2"
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
"github.com/metal-stack/cli/cmd/config"
Expand Down Expand Up @@ -36,10 +37,24 @@ func newComponentCmd(c *config.Config) *cobra.Command {
cmd.Flags().String("uuid", "", "lists only component with this uuid")
cmd.Flags().String("type", "", "lists only component of this type")
cmd.Flags().String("identifier", "", "lists only component with this identifier")
genericcli.Must(cmd.RegisterFlagCompletionFunc("type", c.Completion.ComponentTypes))
},
}

return genericcli.NewCmds(cmdsConfig)
pruneCmd := &cobra.Command{
Use: "prune",
Short: "prune outdated component entries",
Long: "prune outdated component entries to shorten the component list",
RunE: func(cmd *cobra.Command, args []string) error {
return w.prune(cmd.Context())
},
ValidArgsFunction: c.Completion.Firewall,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ValidArgsFunction: c.Completion.Firewall,
ValidArgsFunction: c.Completion.Component,

}
pruneCmd.Flags().Duration("max-age", 12*time.Hour, "prune all components which are older than max-age and not active anymore")
pruneCmd.Flags().String("type", "", "prune only component of this type")
genericcli.Must(pruneCmd.RegisterFlagCompletionFunc("type", c.Completion.ComponentTypes))

return genericcli.NewCmds(cmdsConfig, pruneCmd)
}

func (c *component) Get(id string) (*apiv2.Component, error) {
Expand Down Expand Up @@ -73,6 +88,7 @@ func (c *component) Delete(id string) (*apiv2.Component, error) {

return resp.Component, nil
}

func (c *component) List() ([]*apiv2.Component, error) {
ctx, cancel := c.c.NewRequestContext()
defer cancel()
Expand All @@ -83,11 +99,12 @@ func (c *component) List() ([]*apiv2.Component, error) {
}

if viper.IsSet("type") {
t, err := enum.GetEnum[apiv2.ComponentType](viper.GetString("type"))
if err != nil {
return nil, fmt.Errorf("unable to get component type of string %q %w", viper.GetString("type"), err)
componentString := viper.GetString("type")
ct, ok := apiv2.ComponentType_value[componentString]
if !ok {
return nil, fmt.Errorf("unknown component type: %s", componentString)
}
query.Type = &t
query.Type = new(apiv2.ComponentType(ct))
}

req := &adminv2.ComponentServiceListRequest{Query: query}
Expand All @@ -99,6 +116,38 @@ func (c *component) List() ([]*apiv2.Component, error) {

return resp.Components, nil
}

func (c *component) prune(ctx context.Context) error {
ctx, cancel := c.c.NewRequestContext()
defer cancel()

components, err := c.List()
if err != nil {
return err
}

maxAge := viper.GetDuration("max-age")

for _, ct := range components {
if time.Since(ct.ReportedAt.AsTime()) < maxAge {
if time.Until(ct.Token.Expires.AsTime()) > 0 {
continue
}
}

req := &adminv2.ComponentServiceDeleteRequest{
Uuid: ct.Uuid,
}
resp, err := c.c.Client.Adminv2().Component().Delete(ctx, req)
if err != nil {
return fmt.Errorf("failed to prune components: %w", err)
}
fmt.Fprintf(c.c.Out, "deleted component %s:%s\n", resp.Component.Uuid, resp.Component.Identifier)
}

return nil
}

func (c *component) Convert(r *apiv2.Component) (string, any, any, error) {
panic("unimplemented")
}
Expand Down
Loading