diff --git a/README.md b/README.md index 9753f1c..7393ff9 100644 --- a/README.md +++ b/README.md @@ -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) ``` diff --git a/pkg/query/options.go b/pkg/query/options.go index 0d9358b..0c4f0f7 100644 --- a/pkg/query/options.go +++ b/pkg/query/options.go @@ -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 { @@ -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) @@ -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()