Skip to content

Commit

Permalink
chore(kuma-cp) extensible kumactl install metrics (#1550)
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Dyszkiewicz <jakub.dyszkiewicz@gmail.com>
  • Loading branch information
jakubdyszkiewicz authored Feb 11, 2021
1 parent c4b1fc4 commit 0e42763
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 66 deletions.
36 changes: 36 additions & 0 deletions app/kumactl/cmd/install/context/install_metrics_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package context

import kuma_version "github.com/kumahq/kuma/pkg/version"

type Dashboard struct {
FileName string
Content string
}

type MetricsTemplateArgs struct {
Namespace string
Mesh string
KumaPrometheusSdImage string
KumaPrometheusSdVersion string
KumaCpAddress string
WithoutPrometheus bool
WithoutGrafana bool
Dashboards []Dashboard
}

type InstallMetricsContext struct {
TemplateArgs MetricsTemplateArgs
}

func DefaultInstallMetricsContext() InstallMetricsContext {
return InstallMetricsContext{
TemplateArgs: MetricsTemplateArgs{
Namespace: "kuma-metrics",
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,
},
}
}
47 changes: 18 additions & 29 deletions app/kumactl/cmd/install/install_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package install
import (
"strings"

"github.com/kumahq/kuma/app/kumactl/cmd/install/context"
kumactl_cmd "github.com/kumahq/kuma/app/kumactl/pkg/cmd"

"github.com/pkg/errors"
Expand All @@ -11,34 +12,10 @@ import (
"github.com/kumahq/kuma/app/kumactl/pkg/install/data"
"github.com/kumahq/kuma/app/kumactl/pkg/install/k8s"
"github.com/kumahq/kuma/app/kumactl/pkg/install/k8s/metrics"
kuma_version "github.com/kumahq/kuma/pkg/version"
)

type metricsTemplateArgs struct {
Namespace string
Mesh string
KumaPrometheusSdImage string
KumaPrometheusSdVersion string
KumaCpAddress string
WithoutPrometheus bool
WithoutGrafana bool
DashboardDataplane string
DashboardMesh string
DashboardServiceToService string
DashboardCP string
}

var DefaultMetricsTemplateArgs = metricsTemplateArgs{
Namespace: "kuma-metrics",
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 {
args := DefaultMetricsTemplateArgs
args := pctx.InstallMetricsContext.TemplateArgs
cmd := &cobra.Command{
Use: "metrics",
Short: "Install Metrics backend in Kubernetes cluster (Prometheus + Grafana)",
Expand All @@ -58,25 +35,37 @@ func newInstallMetrics(pctx *kumactl_cmd.RootContext) *cobra.Command {
if err != nil {
return err
}
args.DashboardDataplane = dashboard.String()
args.Dashboards = append(args.Dashboards, context.Dashboard{
FileName: "kuma-dataplane.json",
Content: dashboard.String(),
})

dashboard, err = data.ReadFile(metrics.Templates, "/grafana/kuma-mesh.json")
if err != nil {
return err
}
args.DashboardMesh = dashboard.String()
args.Dashboards = append(args.Dashboards, context.Dashboard{
FileName: "kuma-mesh.json",
Content: dashboard.String(),
})

dashboard, err = data.ReadFile(metrics.Templates, "/grafana/kuma-service-to-service.json")
if err != nil {
return err
}
args.DashboardServiceToService = dashboard.String()
args.Dashboards = append(args.Dashboards, context.Dashboard{
FileName: "kuma-service-to-service.json",
Content: dashboard.String(),
})

dashboard, err = data.ReadFile(metrics.Templates, "/grafana/kuma-cp.json")
if err != nil {
return err
}
args.DashboardCP = dashboard.String()
args.Dashboards = append(args.Dashboards, context.Dashboard{
FileName: "kuma-cp.json",
Content: dashboard.String(),
})

filter := getExcludePrefixesFilter(args.WithoutPrometheus, args.WithoutGrafana)

Expand Down
7 changes: 4 additions & 3 deletions app/kumactl/cmd/install/install_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
. "github.com/onsi/gomega"

"github.com/kumahq/kuma/app/kumactl/cmd"
"github.com/kumahq/kuma/app/kumactl/cmd/install"
kumactl_cmd "github.com/kumahq/kuma/app/kumactl/pkg/cmd"
kuma_version "github.com/kumahq/kuma/pkg/version"
)

Expand All @@ -35,13 +35,14 @@ var _ = Describe("kumactl install metrics", func() {
GitCommit: "91ce236824a9d875601679aa80c63783fb0e8725",
BuildDate: "2019-08-07T11:26:06Z",
}
install.DefaultMetricsTemplateArgs.KumaPrometheusSdVersion = "0.0.1"
})

DescribeTable("should generate Kubernetes resources",
func(given testCase) {
// given
rootCmd := cmd.DefaultRootCmd()
rootCtx := kumactl_cmd.DefaultRootContext()
rootCtx.InstallMetricsContext.TemplateArgs.KumaPrometheusSdVersion = "0.0.1"
rootCmd := cmd.NewRootCmd(rootCtx)
rootCmd.SetArgs(append([]string{"install", "metrics"}, given.extraArgs...))
rootCmd.SetOut(stdout)
rootCmd.SetErr(stderr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ data:
editable: true
options:
path: /etc/grafana/provisioning/dashboards
kuma-dataplane.json: |
{
"__inputs": [
Expand Down Expand Up @@ -2516,6 +2517,7 @@ data:
"description": "Statistics of a single Dataplane in Kuma Service Mesh"
}
kuma-mesh.json: |
{
"__inputs": [
Expand Down Expand Up @@ -3266,6 +3268,7 @@ data:
"description": "Statistics of the single Mesh in Kuma Service Mesh"
}
kuma-service-to-service.json: |
{
"__inputs": [
Expand Down Expand Up @@ -4110,6 +4113,7 @@ data:
"version": 1,
"description": "Statistics of the traffic between services in Kuma Service Mesh"
}
kuma-cp.json: |
{
"annotations": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ data:
editable: true
options:
path: /etc/grafana/provisioning/dashboards
kuma-dataplane.json: |
{
"__inputs": [
Expand Down Expand Up @@ -2452,6 +2453,7 @@ data:
"description": "Statistics of a single Dataplane in Kuma Service Mesh"
}
kuma-mesh.json: |
{
"__inputs": [
Expand Down Expand Up @@ -3202,6 +3204,7 @@ data:
"description": "Statistics of the single Mesh in Kuma Service Mesh"
}
kuma-service-to-service.json: |
{
"__inputs": [
Expand Down Expand Up @@ -4046,6 +4049,7 @@ data:
"version": 1,
"description": "Statistics of the traffic between services in Kuma Service Mesh"
}
kuma-cp.json: |
{
"annotations": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ data:
editable: true
options:
path: /etc/grafana/provisioning/dashboards
kuma-dataplane.json: |
{
"__inputs": [
Expand Down Expand Up @@ -2516,6 +2517,7 @@ data:
"description": "Statistics of a single Dataplane in Kuma Service Mesh"
}
kuma-mesh.json: |
{
"__inputs": [
Expand Down Expand Up @@ -3266,6 +3268,7 @@ data:
"description": "Statistics of the single Mesh in Kuma Service Mesh"
}
kuma-service-to-service.json: |
{
"__inputs": [
Expand Down Expand Up @@ -4110,6 +4113,7 @@ data:
"version": 1,
"description": "Statistics of the traffic between services in Kuma Service Mesh"
}
kuma-cp.json: |
{
"annotations": {
Expand Down
12 changes: 4 additions & 8 deletions app/kumactl/data/install/k8s/metrics/grafana/grafana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,10 @@ data:
editable: true
options:
path: /etc/grafana/provisioning/dashboards
kuma-dataplane.json: |
{{ .DashboardDataplane | replace "${DS_PROMETHEUS}" "Prometheus" | indent 4 }}
kuma-mesh.json: |
{{ .DashboardMesh | replace "${DS_PROMETHEUS}" "Prometheus" | indent 4 }}
kuma-service-to-service.json: |
{{ .DashboardServiceToService | replace "${DS_PROMETHEUS}" "Prometheus" | indent 4 }}
kuma-cp.json: |
{{ .DashboardCP | replace "${DS_PROMETHEUS}" "Prometheus" | indent 4 }}
{{ range $dashboard := .Dashboards }}
{{ $dashboard.FileName | indent 2 }}: |
{{ $dashboard.Content | replace "${DS_PROMETHEUS}" "Prometheus" | indent 4 }}
{{ end }}
---
apiVersion: v1
kind: ServiceAccount
Expand Down
22 changes: 12 additions & 10 deletions app/kumactl/pkg/cmd/root_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ type RootRuntime struct {
// rootCmd := cmd.NewRootCmd(rootCtx)
// err := rootCmd.Execute()
type RootContext struct {
TypeArgs map[string]core_model.ResourceType
Args RootArgs
Runtime RootRuntime
GetContext get_context.GetContext
ListContext get_context.ListContext
InspectContext inspect_context.InspectContext
InstallCpContext install_context.InstallCpContext
InstallCRDContext install_context.InstallCrdsContext
TypeArgs map[string]core_model.ResourceType
Args RootArgs
Runtime RootRuntime
GetContext get_context.GetContext
ListContext get_context.ListContext
InspectContext inspect_context.InspectContext
InstallCpContext install_context.InstallCpContext
InstallMetricsContext install_context.InstallMetricsContext
InstallCRDContext install_context.InstallCrdsContext
}

func DefaultRootContext() *RootContext {
Expand Down Expand Up @@ -80,8 +81,9 @@ func DefaultRootContext() *RootContext {
"secret": system.SecretType,
"zone": system.ZoneType,
},
InstallCpContext: install_context.DefaultInstallCpContext(),
InstallCRDContext: install_context.DefaultInstallCrdsContext(),
InstallCpContext: install_context.DefaultInstallCpContext(),
InstallCRDContext: install_context.DefaultInstallCrdsContext(),
InstallMetricsContext: install_context.DefaultInstallMetricsContext(),
}
}

Expand Down
Loading

0 comments on commit 0e42763

Please sign in to comment.