Skip to content

Commit

Permalink
chore(kumactl) disable prometheus/grafana in install metrics (#1447) (#…
Browse files Browse the repository at this point in the history
…1449)

Signed-off-by: Nikolay Nikolaev <[email protected]>
(cherry picked from commit a28e5e5)

Co-authored-by: Nikolay Nikolaev <[email protected]>
  • Loading branch information
mergify[bot] and Nikolay Nikolaev authored Jan 20, 2021
1 parent a5d5542 commit 2b80ba9
Show file tree
Hide file tree
Showing 12 changed files with 9,357 additions and 12 deletions.
2 changes: 2 additions & 0 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ github.com/envoyproxy/protoc-gen-validate v0.4.1/go.mod h1:E+IEazqdaWv3FrnGtZIu3
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
Expand Down Expand Up @@ -145,6 +146,7 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
4 changes: 4 additions & 0 deletions app/kumactl/cmd/completion/testdata/bash.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2152,6 +2152,10 @@ _kumactl_install_metrics()
flags+=("--namespace=")
two_word_flags+=("--namespace")
local_nonpersistent_flags+=("--namespace=")
flags+=("--without-grafana")
local_nonpersistent_flags+=("--without-grafana")
flags+=("--without-prometheus")
local_nonpersistent_flags+=("--without-prometheus")
flags+=("--config-file=")
two_word_flags+=("--config-file")
flags+=("--log-level=")
Expand Down
2 changes: 2 additions & 0 deletions app/kumactl/cmd/completion/testdata/zsh.golden
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,8 @@ function _kumactl_install_metrics {
'--kuma-prometheus-sd-image[image name of Kuma Prometheus SD]:' \
'--kuma-prometheus-sd-version[version of Kuma Prometheus SD]:' \
'--namespace[namespace to install metrics to]:' \
'--without-grafana[disable Grafana resources generation]' \
'--without-prometheus[disable Prometheus resources generation]' \
'--config-file[path to the configuration file to use]:' \
'--log-level[log level: one of off|info|debug]:' \
'(-m --mesh)'{-m,--mesh}'[mesh to use]:'
Expand Down
26 changes: 25 additions & 1 deletion app/kumactl/cmd/install/install_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type metricsTemplateArgs struct {
KumaPrometheusSdImage string
KumaPrometheusSdVersion string
KumaCpAddress string
WithoutPrometheus bool
WithoutGrafana bool
DashboardDataplane string
DashboardMesh string
DashboardServiceToService string
Expand All @@ -31,6 +33,8 @@ var DefaultMetricsTemplateArgs = metricsTemplateArgs{
KumaPrometheusSdImage: "kong-docker-kuma-docker.bintray.io/kuma-prometheus-sd",
KumaPrometheusSdVersion: kuma_version.Build.Version,
KumaCpAddress: "grpc://kuma-control-plane.kuma-system:5676",
WithoutPrometheus: false,
WithoutGrafana: false,
}

func newInstallMetrics(pctx *kumactl_cmd.RootContext) *cobra.Command {
Expand Down Expand Up @@ -74,7 +78,9 @@ func newInstallMetrics(pctx *kumactl_cmd.RootContext) *cobra.Command {
}
args.DashboardCP = dashboard.String()

renderedFiles, err := renderFiles(yamlTemplateFiles, args, simpleTemplateRenderer)
filter := getExcludePrefixesFilter(args.WithoutPrometheus, args.WithoutGrafana)

renderedFiles, err := renderFilesWithFilter(yamlTemplateFiles, args, simpleTemplateRenderer, filter)
if err != nil {
return errors.Wrap(err, "Failed to render template files")
}
Expand All @@ -96,5 +102,23 @@ func newInstallMetrics(pctx *kumactl_cmd.RootContext) *cobra.Command {
cmd.Flags().StringVar(&args.KumaPrometheusSdImage, "kuma-prometheus-sd-image", args.KumaPrometheusSdImage, "image name of Kuma Prometheus SD")
cmd.Flags().StringVar(&args.KumaPrometheusSdVersion, "kuma-prometheus-sd-version", args.KumaPrometheusSdVersion, "version of Kuma Prometheus SD")
cmd.Flags().StringVar(&args.KumaCpAddress, "kuma-cp-address", args.KumaCpAddress, "the address of Kuma CP")
cmd.Flags().BoolVar(&args.WithoutPrometheus, "without-prometheus", args.WithoutPrometheus, "disable Prometheus resources generation")
cmd.Flags().BoolVar(&args.WithoutGrafana, "without-grafana", args.WithoutGrafana, "disable Grafana resources generation")
return cmd
}

func getExcludePrefixesFilter(withoutPrometheus, withoutGrafana bool) ExcludePrefixesFilter {
prefixes := []string{}

if withoutPrometheus {
prefixes = append(prefixes, "/prometheus")
}

if withoutGrafana {
prefixes = append(prefixes, "/grafana")
}

return ExcludePrefixesFilter{
Prefixes: prefixes,
}
}
12 changes: 12 additions & 0 deletions app/kumactl/cmd/install/install_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,17 @@ var _ = Describe("kumactl install metrics", func() {
},
goldenFile: "install-metrics.overrides.golden.yaml",
}),
Entry("should generate Kubernetes resources without prometheus", testCase{
extraArgs: []string{
"--without-prometheus",
},
goldenFile: "install-metrics.no-prometheus.golden.yaml",
}),
Entry("should generate Kubernetes resources without grafana", testCase{
extraArgs: []string{
"--without-grafana",
},
goldenFile: "install-metrics.no-grafana.golden.yaml",
}),
)
})
48 changes: 40 additions & 8 deletions app/kumactl/cmd/install/render_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,29 @@ import (
"github.com/kumahq/kuma/app/kumactl/pkg/install/data"
)

type templateFilter interface {
Filter(name string) bool
}

func renderFiles(templates []data.File, args interface{}, newRenderer func(data.File) (templateRenderer, error)) ([]data.File, error) {
return renderFilesWithFilter(templates, args, newRenderer, NoneFilter{})
}

func renderFilesWithFilter(templates []data.File, args interface{}, newRenderer func(data.File) (templateRenderer, error), filter templateFilter) ([]data.File, error) {
renderedFiles := make([]data.File, len(templates))

for i, template := range templates {
renderer, err := newRenderer(template)
if err != nil {
return nil, err
if filter.Filter(template.FullPath) {
renderer, err := newRenderer(template)
if err != nil {
return nil, err
}
var buf bytes.Buffer
if err := renderer.Execute(&buf, args); err != nil {
return nil, err
}
renderedFiles[i].Data = buf.Bytes()
}
var buf bytes.Buffer
if err := renderer.Execute(&buf, args); err != nil {
return nil, err
}
renderedFiles[i].Data = buf.Bytes()
}

return renderedFiles, nil
Expand All @@ -40,3 +50,25 @@ func simpleTemplateRenderer(text data.File) (templateRenderer, error) {
}
return tmpl, nil
}

// Template filters

type ExcludePrefixesFilter struct {
Prefixes []string
}

func (f ExcludePrefixesFilter) Filter(name string) bool {
for _, prefix := range f.Prefixes {
if len(name) > len(prefix) && name[:len(prefix)] == prefix {
return false
}
}
return true
}

type NoneFilter struct {
}

func (f NoneFilter) Filter(name string) bool {
return true
}
Loading

0 comments on commit 2b80ba9

Please sign in to comment.