commands.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. pflag.CommandLine.AddGoFlag(flag.CommandLine.Lookup("v"))
  38. pflag.CommandLine.AddGoFlag(flag.CommandLine.Lookup("logtostderr"))
  39. pflag.CommandLine.Set("v", "3")
  40. // in the event that no directive/command is passed, we want to default to using the cost-model command
  41. // cobra doesn't provide a way within the API to do this, so we'll prepend the command if it is omitted.
  42. if len(os.Args) > 1 {
  43. // try to find the sub-command from the arguments, if there's an error or the command _is_ the
  44. // root command, prepend the default command
  45. pCmd, _, err := rootCmd.Find(os.Args[1:])
  46. if err != nil || pCmd.Use == rootCmd.Use {
  47. rootCmd.SetArgs(append([]string{CommandCostModel}, os.Args[1:]...))
  48. }
  49. } else {
  50. rootCmd.SetArgs([]string{CommandCostModel})
  51. }
  52. return rootCmd.Execute()
  53. }
  54. // newRootCommand creates a new root command which will act as a sub-command router for the
  55. // cost-model application
  56. func newRootCommand(costModelCmd *cobra.Command) *cobra.Command {
  57. cmd := &cobra.Command{
  58. Use: commandRoot,
  59. SilenceUsage: true,
  60. }
  61. // add the modes of operation
  62. cmd.AddCommand(
  63. costModelCmd,
  64. newAgentCommand(),
  65. )
  66. return cmd
  67. }
  68. // default open-source cost-model command
  69. func newCostModelCommand() *cobra.Command {
  70. opts := &costmodel.CostModelOpts{}
  71. cmCmd := &cobra.Command{
  72. Use: CommandCostModel,
  73. Short: "Cost-Model metric exporter and data aggregator.",
  74. RunE: func(cmd *cobra.Command, args []string) error {
  75. return costmodel.Execute(opts)
  76. },
  77. }
  78. // TODO: We could introduce a way of mapping input command-line parameters to a configuration
  79. // TODO: object, and pass that object to the agent Execute()
  80. // cmCmd.Flags().<Type>VarP(&opts.<Property>, "<flag>", "<short>", <default>, "<usage>")
  81. return cmCmd
  82. }
  83. func newAgentCommand() *cobra.Command {
  84. opts := &agent.AgentOpts{}
  85. agentCmd := &cobra.Command{
  86. Use: CommandAgent,
  87. Short: "Agent mode operates as a metric exporter only.",
  88. RunE: func(cmd *cobra.Command, args []string) error {
  89. return agent.Execute(opts)
  90. },
  91. }
  92. // TODO: We could introduce a way of mapping input command-line parameters to a configuration
  93. // TODO: object, and pass that object to the agent Execute()
  94. // agentCmd.Flags().<Type>VarP(&opts.<Property>, "<flag>", "<short>", <default>, "<usage>")
  95. return agentCmd
  96. }
  97. // validate will check to ensure that the cost model command passed in has a use equal to the
  98. // CommandCostModel to ensure that the default command matches.
  99. func validate(costModelCommand *cobra.Command) error {
  100. if costModelCommand.Use != CommandCostModel {
  101. return fmt.Errorf("Incompatible 'cost-model' command provided to run-time.")
  102. }
  103. return nil
  104. }