commands.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package cmd
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "github.com/kubecost/cost-model/pkg/cmd/agent"
  7. "github.com/kubecost/cost-model/pkg/cmd/costmodel"
  8. "github.com/spf13/cobra"
  9. "github.com/spf13/pflag"
  10. "k8s.io/klog"
  11. )
  12. const (
  13. // commandRoot is the root command used to route to sub-commands
  14. commandRoot string = "root"
  15. // CommandCostModel is the command used to execute the metrics emission and ETL pipeline
  16. CommandCostModel string = "cost-model"
  17. // CommandAgent executes the application in agent mode, which provides only metrics exporting.
  18. CommandAgent string = "agent"
  19. )
  20. // Execute runs the root command for the application. By default, if no command argument is provided,
  21. // on the command line, the cost-model is executed by default.
  22. //
  23. // This function accepts a costModelCmd parameter to provide support for various cost-model implementations
  24. // (ie: open source, enterprise).
  25. func Execute(costModelCmd *cobra.Command) error {
  26. // use the open-source cost-model if a command is not provided
  27. if costModelCmd == nil {
  28. costModelCmd = newCostModelCommand()
  29. }
  30. // validate the command being passed
  31. if err := validate(costModelCmd); err != nil {
  32. return err
  33. }
  34. rootCmd := newRootCommand(costModelCmd)
  35. // initialize klog and make cobra aware of all the go flags
  36. klog.InitFlags(nil)
  37. flag.CommandLine.VisitAll(func(f *flag.Flag) {
  38. pflag.CommandLine.AddGoFlag(f)
  39. })
  40. pflag.CommandLine.Set("v", "3")
  41. // in the event that no directive/command is passed, we want to default to using the cost-model command
  42. // cobra doesn't provide a way within the API to do this, so we'll prepend the command if it is omitted.
  43. if len(os.Args) > 1 {
  44. // try to find the sub-command from the arguments, if there's an error or the command _is_ the
  45. // root command, prepend the default command
  46. pCmd, _, err := rootCmd.Find(os.Args[1:])
  47. if err != nil || pCmd.Use == rootCmd.Use {
  48. rootCmd.SetArgs(append([]string{CommandCostModel}, os.Args[1:]...))
  49. }
  50. } else {
  51. rootCmd.SetArgs([]string{CommandCostModel})
  52. }
  53. return rootCmd.Execute()
  54. }
  55. // newRootCommand creates a new root command which will act as a sub-command router for the
  56. // cost-model application
  57. func newRootCommand(costModelCmd *cobra.Command) *cobra.Command {
  58. cmd := &cobra.Command{
  59. Use: commandRoot,
  60. SilenceUsage: true,
  61. }
  62. // add the modes of operation
  63. cmd.AddCommand(
  64. costModelCmd,
  65. newAgentCommand(),
  66. )
  67. return cmd
  68. }
  69. // default open-source cost-model command
  70. func newCostModelCommand() *cobra.Command {
  71. opts := &costmodel.CostModelOpts{}
  72. cmCmd := &cobra.Command{
  73. Use: CommandCostModel,
  74. Short: "Cost-Model metric exporter and data aggregator.",
  75. RunE: func(cmd *cobra.Command, args []string) error {
  76. return costmodel.Execute(opts)
  77. },
  78. }
  79. // TODO: We could introduce a way of mapping input command-line parameters to a configuration
  80. // TODO: object, and pass that object to the agent Execute()
  81. // cmCmd.Flags().<Type>VarP(&opts.<Property>, "<flag>", "<short>", <default>, "<usage>")
  82. return cmCmd
  83. }
  84. func newAgentCommand() *cobra.Command {
  85. opts := &agent.AgentOpts{}
  86. agentCmd := &cobra.Command{
  87. Use: CommandAgent,
  88. Short: "Agent mode operates as a metric exporter only.",
  89. RunE: func(cmd *cobra.Command, args []string) error {
  90. return agent.Execute(opts)
  91. },
  92. }
  93. // TODO: We could introduce a way of mapping input command-line parameters to a configuration
  94. // TODO: object, and pass that object to the agent Execute()
  95. // agentCmd.Flags().<Type>VarP(&opts.<Property>, "<flag>", "<short>", <default>, "<usage>")
  96. return agentCmd
  97. }
  98. // validate will check to ensure that the cost model command passed in has a use equal to the
  99. // CommandCostModel to ensure that the default command matches.
  100. func validate(costModelCommand *cobra.Command) error {
  101. if costModelCommand.Use != CommandCostModel {
  102. return fmt.Errorf("Incompatible 'cost-model' command provided to run-time.")
  103. }
  104. return nil
  105. }