-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(internal): Introduce 'metrics' command for instances
Signed-off-by: Cezar Craciunoiu <cezar.craciunoiu@unikraft.io>
- Loading branch information
1 parent
bbc12f3
commit 069b4e7
Showing
3 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
// Copyright (c) 2024, Unikraft GmbH and The KraftKit Authors. | ||
// Licensed under the BSD-3-Clause License (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
|
||
package metrics | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/MakeNowJust/heredoc" | ||
"github.com/spf13/cobra" | ||
|
||
kraftcloud "sdk.kraft.cloud" | ||
|
||
"kraftkit.sh/cmdfactory" | ||
"kraftkit.sh/config" | ||
"kraftkit.sh/internal/cli/kraft/cloud/utils" | ||
) | ||
|
||
type MetricsOptions struct { | ||
Auth *config.AuthConfig `noattribute:"true"` | ||
Client kraftcloud.KraftCloud `noattribute:"true"` | ||
Metro string `noattribute:"true"` | ||
Token string `noattribute:"true"` | ||
Output string `long:"output" short:"o" usage:"Set output format. Options: table,yaml,json,list" default:"list"` | ||
} | ||
|
||
func NewCmd() *cobra.Command { | ||
cmd, err := cmdfactory.New(&MetricsOptions{}, cobra.Command{ | ||
Short: "Return metrics for instances", | ||
Use: "metrics [FLAGS] [UUID|NAME [UUID|NAME]...]", | ||
Aliases: []string{"metric", "m", "meter"}, | ||
Args: cobra.MinimumNArgs(1), | ||
Example: heredoc.Doc(` | ||
# Return metrics for an instance by UUID | ||
$ kraft cloud instance metrics fd1684ea-7970-4994-92d6-61dcc7905f2b | ||
# Return metrics for an instance by name | ||
$ kraft cloud instance metrics my-instance-431342 | ||
`), | ||
Annotations: map[string]string{ | ||
cmdfactory.AnnotationHelpGroup: "kraftcloud-instance", | ||
}, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func (opts *MetricsOptions) Pre(cmd *cobra.Command, _ []string) error { | ||
err := utils.PopulateMetroToken(cmd, &opts.Metro, &opts.Token) | ||
if err != nil { | ||
return fmt.Errorf("could not populate metro and token: %w", err) | ||
} | ||
|
||
if !utils.IsValidOutputFormat(opts.Output) { | ||
return fmt.Errorf("invalid output format: %s", opts.Output) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (opts *MetricsOptions) Run(ctx context.Context, args []string) error { | ||
auth, err := config.GetKraftCloudAuthConfig(ctx, opts.Token) | ||
if err != nil { | ||
return fmt.Errorf("could not retrieve credentials: %w", err) | ||
} | ||
|
||
client := kraftcloud.NewInstancesClient( | ||
kraftcloud.WithToken(config.GetKraftCloudTokenAuthConfig(*auth)), | ||
) | ||
|
||
resp, err := client.WithMetro(opts.Metro).Metrics(ctx, args...) | ||
if err != nil { | ||
return fmt.Errorf("could not get instance %s: %w", args, err) | ||
} | ||
|
||
return utils.PrintMetrics(ctx, opts.Output, *resp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters