Skip to content

Commit c8cda80

Browse files
authored
Add --opencost flag to setup standard OpenCost configuration (#173)
1 parent 82a6d9d commit c8cda80

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,13 @@ Kubecost/OpenCost APIs:
261261
--service-name string The name of the Kubecost cost analyzer service. By default, it is derived from the Helm release name and should not need to be overridden.
262262
--service-port int The port of the service at which the APIs are running. If using OpenCost, you may want to set this to 9003. (default 9090)
263263
-N, --kubecost-namespace string The namespace that Kubecost is deployed in. Requests to the API will be directed to this namespace. Defaults to the Helm release name.
264-
265264
--use-proxy Instead of temporarily port-forwarding, proxy a request to Kubecost through the Kubernetes API server.
266265
267266
--allocation-path string URL path at which Allocation queries can be served from the configured service. If using OpenCost, you may want to set this to '/allocation/compute' (default "/model/allocation")
268267
269268
--predict-speccost-path string URL path at which Prediction queries can be served from the configured service. (default "/model/prediction/speccost")
270269
--no-usage Set true ignore historical usage data (if any exists) when performing cost prediction.
270+
--opencost Set true to configure Kubecost parameters according to the OpenCost default specification. It is equivalent to providing the options '--service-port 9003 --service-name opencost --kubecost-namespace opencost --allocation-path /allocation/compute'
271271
--only-after Set true to only show the overall predicted cost of the workload.
272272
--only-diff Set true to only show the cost difference (cost "impact") instead of the overall cost plus diff. (default true)
273273
```

pkg/query/options.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ import (
1414
"github.com/spf13/viper"
1515
)
1616

17+
// OpenCost specification parameter values
18+
const (
19+
OpenCostServiceName = "opencost"
20+
OpenCostServiceNamespace = "opencost"
21+
OpenCostServicePort = 9003
22+
OpenCostAllocationPath = "/allocation/compute"
23+
)
24+
1725
// QueryBackendOptions holds common options for managing the query backend used
1826
// by kubectl-cost, like service name, namespace, etc.
1927
type QueryBackendOptions struct {
@@ -45,11 +53,20 @@ type QueryBackendOptions struct {
4553
// e.g. "/prediction/speccost"
4654
PredictSpecCostPath string
4755

56+
// A boolean value to automatically set parameters according to OpenCost specification.
57+
OpenCost bool
58+
4859
restConfig *rest.Config
4960
pfQuerier *PortForwardQuerier
5061
}
5162

5263
func (o *QueryBackendOptions) Complete(restConfig *rest.Config) error {
64+
if o.OpenCost {
65+
o.ServiceName = OpenCostServiceName
66+
o.KubecostNamespace = OpenCostServiceNamespace
67+
o.ServicePort = OpenCostServicePort
68+
o.AllocationPath = OpenCostAllocationPath
69+
}
5370
if o.ServiceName == "" {
5471
o.ServiceName = fmt.Sprintf("%s-cost-analyzer", o.HelmReleaseName)
5572
log.Debugf("ServiceName set to: %s", o.ServiceName)
@@ -92,6 +109,7 @@ func AddQueryBackendOptionsFlags(cmd *cobra.Command, options *QueryBackendOption
92109
cmd.Flags().BoolVar(&options.UseProxy, "use-proxy", false, "Instead of temporarily port-forwarding, proxy a request to Kubecost through the Kubernetes API server.")
93110
cmd.Flags().StringVar(&options.AllocationPath, "allocation-path", "/model/allocation", "URL path at which Allocation queries can be served from the configured service. If using OpenCost, you may want to set this to '/allocation/compute'")
94111
cmd.Flags().StringVar(&options.PredictSpecCostPath, "predict-speccost-path", "/model/prediction/speccost", "URL path at which Prediction queries can be served from the configured service.")
112+
cmd.Flags().BoolVar(&options.OpenCost, "opencost", false, " Set true to configure Kubecost parameters according to the OpenCost default specification. It is equivalent to providing the options '--service-port 9003 --service-name opencost --kubecost-namespace opencost --allocation-path /allocation/compute'.")
95113

96114
//Check if environment variable KUBECTL_COST_USE_PROXY is set, it defaults to false
97115
v := viper.New()

0 commit comments

Comments
 (0)