Skip to content

Commit

Permalink
Add --opencost flag to setup standard OpenCost configuration (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladComan0 authored Jul 17, 2024
1 parent 82a6d9d commit c8cda80
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,13 @@ Kubecost/OpenCost APIs:
--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.
--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)
-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.
--use-proxy Instead of temporarily port-forwarding, proxy a request to Kubecost through the Kubernetes API server.
--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")
--predict-speccost-path string URL path at which Prediction queries can be served from the configured service. (default "/model/prediction/speccost")
--no-usage Set true ignore historical usage data (if any exists) when performing cost prediction.
--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'
--only-after Set true to only show the overall predicted cost of the workload.
--only-diff Set true to only show the cost difference (cost "impact") instead of the overall cost plus diff. (default true)
```
Expand Down
18 changes: 18 additions & 0 deletions pkg/query/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ import (
"github.com/spf13/viper"
)

// OpenCost specification parameter values
const (
OpenCostServiceName = "opencost"
OpenCostServiceNamespace = "opencost"
OpenCostServicePort = 9003
OpenCostAllocationPath = "/allocation/compute"
)

// QueryBackendOptions holds common options for managing the query backend used
// by kubectl-cost, like service name, namespace, etc.
type QueryBackendOptions struct {
Expand Down Expand Up @@ -45,11 +53,20 @@ type QueryBackendOptions struct {
// e.g. "/prediction/speccost"
PredictSpecCostPath string

// A boolean value to automatically set parameters according to OpenCost specification.
OpenCost bool

restConfig *rest.Config
pfQuerier *PortForwardQuerier
}

func (o *QueryBackendOptions) Complete(restConfig *rest.Config) error {
if o.OpenCost {
o.ServiceName = OpenCostServiceName
o.KubecostNamespace = OpenCostServiceNamespace
o.ServicePort = OpenCostServicePort
o.AllocationPath = OpenCostAllocationPath
}
if o.ServiceName == "" {
o.ServiceName = fmt.Sprintf("%s-cost-analyzer", o.HelmReleaseName)
log.Debugf("ServiceName set to: %s", o.ServiceName)
Expand Down Expand Up @@ -92,6 +109,7 @@ func AddQueryBackendOptionsFlags(cmd *cobra.Command, options *QueryBackendOption
cmd.Flags().BoolVar(&options.UseProxy, "use-proxy", false, "Instead of temporarily port-forwarding, proxy a request to Kubecost through the Kubernetes API server.")
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'")
cmd.Flags().StringVar(&options.PredictSpecCostPath, "predict-speccost-path", "/model/prediction/speccost", "URL path at which Prediction queries can be served from the configured service.")
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'.")

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

0 comments on commit c8cda80

Please sign in to comment.