diff --git a/docs/CHANGELOG.MD b/docs/CHANGELOG.MD index 9db4dbc5..4b77ccbe 100644 --- a/docs/CHANGELOG.MD +++ b/docs/CHANGELOG.MD @@ -8,7 +8,7 @@ ### Features -- TODO +- [vmoperator](https://docs.victoriametrics.com/operator/) add ability to print default values for all [operator variables](https://docs.victoriametrics.com/operator/vars.html). See [this issue](https://github.com/VictoriaMetrics/operator/issues/675) for details. ## [v0.37.1](https://github.com/VictoriaMetrics/operator/releases/tag/v0.37.1) - 02 Sep 2023 diff --git a/e2e/suite/suite.go b/e2e/suite/suite.go index f55ec218..e4f6e078 100644 --- a/e2e/suite/suite.go +++ b/e2e/suite/suite.go @@ -6,6 +6,7 @@ import ( "path/filepath" victoriametricsv1beta1 "github.com/VictoriaMetrics/operator/api/v1beta1" + "github.com/VictoriaMetrics/operator/internal/config" "github.com/VictoriaMetrics/operator/internal/manager" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -89,7 +90,8 @@ func Before() { ctx, cancel := context.WithCancel(context.Background()) go func(ctx context.Context) { defer GinkgoRecover() - err := manager.RunManager(ctx) + baseConfig := config.MustGetBaseConfig() + err := manager.RunManager(ctx, baseConfig) close(stopped) Expect(err).NotTo(HaveOccurred()) }(ctx) diff --git a/internal/config/config.go b/internal/config/config.go index 1ee43707..fd4cd453 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -6,6 +6,7 @@ import ( "regexp" "strings" "sync" + "text/tabwriter" "time" "github.com/kelseyhightower/envconfig" @@ -325,6 +326,33 @@ func (boc BaseOperatorConf) Validate() error { return nil } +// PrintDefaults prints default values for all config variables. +// format can be one of: table, list, json, yaml. +func (boc BaseOperatorConf) PrintDefaults(format string) error { + tabs := tabwriter.NewWriter(os.Stdout, 1, 0, 4, ' ', 0) + + var formatter = "unknown" + switch format { + case "table": + formatter = envconfig.DefaultTableFormat + case "list": + formatter = envconfig.DefaultListFormat + case "json": + formatter = `{{$last := (len (slice . 1))}}{ +{{range $index, $item := .}} '{{usage_key $item}}': '{{usage_default $item}}'{{ if lt $index $last}},{{end}} +{{end}}}` + case "yaml": + formatter = `{{range $index, $item := .}}{{usage_key $item}}: '{{usage_default $item}}' +{{end}}` + default: + return fmt.Errorf("unknown print format %q", format) + } + + err := envconfig.Usagef(prefixVar, &boc, tabs, formatter) + _ = tabs.Flush() + return err +} + func MustGetBaseConfig() *BaseOperatorConf { initConf.Do(func() { c := &BaseOperatorConf{} diff --git a/internal/manager/manager.go b/internal/manager/manager.go index c005e7bd..df858fa4 100644 --- a/internal/manager/manager.go +++ b/internal/manager/manager.go @@ -72,7 +72,7 @@ func init() { } -func RunManager(ctx context.Context) error { +func RunManager(ctx context.Context, baseConfig *config.BaseOperatorConf) error { // Add flags registered by imported packages (e.g. glog and // controller-runtime) opts := zap.Options{} @@ -170,7 +170,6 @@ func RunManager(ctx context.Context) error { return err } } - baseConfig := config.MustGetBaseConfig() victoriametricsv1beta1.SetLabelAndAnnotationPrefixes(baseConfig.FilterChildLabelPrefixes, baseConfig.FilterChildAnnotationPrefixes) if err = (&controllers.VMAgentReconciler{ diff --git a/main.go b/main.go index 2c003fd2..8ab9a8e4 100644 --- a/main.go +++ b/main.go @@ -18,16 +18,20 @@ package main import ( "context" + "flag" "os" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/manager/signals" + "github.com/VictoriaMetrics/operator/internal/config" "github.com/VictoriaMetrics/operator/internal/manager" ) var ( - setupLog = ctrl.Log.WithName("setup") + setupLog = ctrl.Log.WithName("setup") + printDefaults = flag.Bool("printDefaults", false, "print all variables with their default values and exit") + printFormat = flag.String("printFormat", "table", "output format for --printDefaults. Can be table, json, yaml or list") ) func main() { @@ -38,7 +42,18 @@ func main() { cancel() }() - err := manager.RunManager(ctx) + flag.Parse() + baseConfig := config.MustGetBaseConfig() + if *printDefaults { + err := baseConfig.PrintDefaults(*printFormat) + if err != nil { + setupLog.Error(err, "cannot print variables") + os.Exit(1) + } + return + } + + err := manager.RunManager(ctx, baseConfig) if err != nil { setupLog.Error(err, "cannot setup manager") os.Exit(1)