Selaa lähdekoodia

feat(cmd): Implement command-line flag support for cost-model and age… (#3304)

Signed-off-by: sneax <paladesh600@gmail.com>
Co-authored-by: Alex Meijer <ameijer@users.noreply.github.com>
segfault_bits 4 kuukautta sitten
vanhempi
sitoutus
75253e9ce3
2 muutettua tiedostoa jossa 48 lisäystä ja 7 poistoa
  1. 9 1
      pkg/cmd/agent/agent.go
  2. 39 6
      pkg/cmd/commands.go

+ 9 - 1
pkg/cmd/agent/agent.go

@@ -31,6 +31,8 @@ import (
 
 // AgentOpts contain configuration options that can be passed to the Execute() method
 type AgentOpts struct {
+	// Port is the port the agent will bind to
+	Port int
 	// Stubbed for future configuration
 }
 
@@ -163,5 +165,11 @@ func Execute(opts *AgentOpts) error {
 	telemetryHandler := metrics.ResponseMetricMiddleware(rootMux)
 	handler := cors.AllowAll().Handler(telemetryHandler)
 
-	return http.ListenAndServe(fmt.Sprintf(":%d", env.GetKubecostMetricsPort()), handler)
+	// Use the port from AgentOpts, or default to the environment variable value
+	port := opts.Port
+	if port == 0 {
+		port = env.GetKubecostMetricsPort()
+	}
+	
+	return http.ListenAndServe(fmt.Sprintf(":%d", port), handler)
 }

+ 39 - 6
pkg/cmd/commands.go

@@ -8,6 +8,7 @@ import (
 	"github.com/opencost/opencost/core/pkg/log"
 	"github.com/opencost/opencost/pkg/cmd/agent"
 	"github.com/opencost/opencost/pkg/cmd/costmodel"
+	"github.com/opencost/opencost/pkg/env"
 	"github.com/spf13/cobra"
 	"github.com/spf13/viper"
 )
@@ -105,13 +106,39 @@ func newCostModelCommand() *cobra.Command {
 			// Init logging here so cobra/viper has processed the command line args and flags
 			// otherwise only envvars are available during init
 			log.InitLogging(true)
+			
+			// Update config with command-line flag values if they were explicitly set
+			if cmd.Flags().Changed("port") {
+				port, _ := cmd.Flags().GetInt("port")
+				config.Port = port
+			}
+			if cmd.Flags().Changed("kubernetes-enabled") {
+				kubernetesEnabled, _ := cmd.Flags().GetBool("kubernetes-enabled")
+				config.KubernetesEnabled = kubernetesEnabled
+			}
+			if cmd.Flags().Changed("carbon-estimates-enabled") {
+				carbonEstimatesEnabled, _ := cmd.Flags().GetBool("carbon-estimates-enabled")
+				config.CarbonEstimatesEnabled = carbonEstimatesEnabled
+			}
+			if cmd.Flags().Changed("cloud-cost-enabled") {
+				cloudCostEnabled, _ := cmd.Flags().GetBool("cloud-cost-enabled")
+				config.CloudCostEnabled = cloudCostEnabled
+			}
+			if cmd.Flags().Changed("custom-cost-enabled") {
+				customCostEnabled, _ := cmd.Flags().GetBool("custom-cost-enabled")
+				config.CustomCostEnabled = customCostEnabled
+			}
+			
 			return costmodel.Execute(config)
 		},
 	}
 
-	// TODO: We could introduce a way of mapping input command-line parameters to a configuration
-	// TODO: object, and pass that object to the agent Execute()
-	// cmCmd.Flags().<Type>VarP(&opts.<Property>, "<flag>", "<short>", <default>, "<usage>")
+	// Add command-line flags for cost-model configuration
+	cmCmd.Flags().Int("port", config.Port, "Port to bind to")
+	cmCmd.Flags().Bool("kubernetes-enabled", config.KubernetesEnabled, "Enable Kubernetes metrics")
+	cmCmd.Flags().Bool("carbon-estimates-enabled", config.CarbonEstimatesEnabled, "Enable Carbon Estimates")
+	cmCmd.Flags().Bool("cloud-cost-enabled", config.CloudCostEnabled, "Enable Cloud Costs")
+	cmCmd.Flags().Bool("custom-cost-enabled", config.CustomCostEnabled, "Enable Custom Costs")
 
 	return cmCmd
 }
@@ -126,13 +153,19 @@ func newAgentCommand() *cobra.Command {
 			// Init logging here so cobra/viper has processed the command line args and flags
 			// otherwise only envvars are available during init
 			log.InitLogging(true)
+			
+			// Update opts with command-line flag values if they were explicitly set
+			if cmd.Flags().Changed("port") {
+				port, _ := cmd.Flags().GetInt("port")
+				opts.Port = port
+			}
+			
 			return agent.Execute(opts)
 		},
 	}
 
-	// TODO: We could introduce a way of mapping input command-line parameters to a configuration
-	// TODO: object, and pass that object to the agent Execute()
-	// agentCmd.Flags().<Type>VarP(&opts.<Property>, "<flag>", "<short>", <default>, "<usage>")
+	// Add command-line flags for agent configuration
+	agentCmd.Flags().Int("port", env.GetKubecostMetricsPort(), "Port to bind to")
 
 	return agentCmd
 }