-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor some commands to reduce code duplication
Uses a command "generator" for common aggregation situations (not label) and updates table writing to more generically accept arbitrary aggregation levels. This will make future improvements to kubectl-cost easier by reducing the amount of code that needs to be updated.
- Loading branch information
1 parent
cad6381
commit fb9e148
Showing
12 changed files
with
172 additions
and
475 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/kubecost/kubectl-cost/pkg/query" | ||
) | ||
|
||
func buildStandardAggregatedAllocationCommand(streams genericclioptions.IOStreams, commandName string, commandAliases []string, aggregation []string, enableNamespaceFilter bool) *cobra.Command { | ||
kubeO := NewKubeOptions(streams) | ||
costO := CostOptions{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: commandName, | ||
Short: fmt.Sprintf("view cost information aggregated by %s", aggregation), | ||
Aliases: commandAliases, | ||
RunE: func(c *cobra.Command, args []string) error { | ||
if err := kubeO.Complete(c, args); err != nil { | ||
return err | ||
} | ||
if err := kubeO.Validate(); err != nil { | ||
return err | ||
} | ||
|
||
costO.Complete() | ||
|
||
if err := costO.Validate(); err != nil { | ||
return err | ||
} | ||
|
||
return runAggregatedAllocationCommand(kubeO, costO, aggregation) | ||
}, | ||
} | ||
|
||
// TODO: Replace entirely when we have generic filter language (v2) | ||
if enableNamespaceFilter { | ||
cmd.Flags().StringVarP(&costO.filterNamespace, "namespace", "n", "", "Limit results to only one namespace. Defaults to all namespaces.") | ||
} | ||
|
||
addCostOptionsFlags(cmd, &costO) | ||
addKubeOptionsFlags(cmd, kubeO) | ||
|
||
return cmd | ||
} | ||
|
||
func runAggregatedAllocationCommand(ko *KubeOptions, co CostOptions, aggregation []string) error { | ||
|
||
currencyCode, err := query.QueryCurrencyCode(query.CurrencyCodeParameters{ | ||
RestConfig: ko.restConfig, | ||
Ctx: context.Background(), | ||
QueryBackendOptions: co.QueryBackendOptions, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to get currency code: %s", err) | ||
} | ||
|
||
allocations, err := query.QueryAllocation(query.AllocationParameters{ | ||
RestConfig: ko.restConfig, | ||
Ctx: context.Background(), | ||
QueryParams: map[string]string{ | ||
"window": co.window, | ||
"aggregate": strings.Join(aggregation, ","), | ||
"accumulate": "true", | ||
"filterNamespaces": co.filterNamespace, | ||
}, | ||
QueryBackendOptions: co.QueryBackendOptions, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to query allocation API: %s", err) | ||
} | ||
|
||
writeAllocationTable(ko.Out, aggregation, allocations[0], co.displayOptions, currencyCode, !co.isHistorical) | ||
|
||
return nil | ||
} |
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 was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.