Skip to content

Commit

Permalink
add ability to print default values for all operator variables (#675) (
Browse files Browse the repository at this point in the history
…#742)

* add ability to print default values for all operator variables (#675)

* fix review comment
  • Loading branch information
Amper authored Sep 2, 2023
1 parent c32931b commit add9045
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<a name="v0.37.1"></a>
## [v0.37.1](https://github.com/VictoriaMetrics/operator/releases/tag/v0.37.1) - 02 Sep 2023
Expand Down
4 changes: 3 additions & 1 deletion e2e/suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"
"strings"
"sync"
"text/tabwriter"
"time"

"github.com/kelseyhightower/envconfig"
Expand Down Expand Up @@ -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{}
Expand Down
3 changes: 1 addition & 2 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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{
Expand Down
19 changes: 17 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
Expand Down

0 comments on commit add9045

Please sign in to comment.