From 0a223707b03839931e3ff6080ffd7c4cf04228d9 Mon Sep 17 00:00:00 2001 From: Bhaskarjyoti Bora Date: Fri, 17 May 2024 08:51:50 +0530 Subject: [PATCH] roachprod: use yaml for creating the cluster config currently a template is used to generate the yaml for the prometheus cluster config. this is error-prone and adding new parameters also becomes risky. with this change, we are using go yaml to generate the yaml Fixes: #124320 Epic: none --- pkg/roachprod/promhelperclient/BUILD.bazel | 3 ++ pkg/roachprod/promhelperclient/client.go | 43 ++++++++----------- pkg/roachprod/promhelperclient/client_test.go | 15 ++++++- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/pkg/roachprod/promhelperclient/BUILD.bazel b/pkg/roachprod/promhelperclient/BUILD.bazel index c8e1f8402f76..21cbaf2cd4fc 100644 --- a/pkg/roachprod/promhelperclient/BUILD.bazel +++ b/pkg/roachprod/promhelperclient/BUILD.bazel @@ -9,10 +9,12 @@ go_library( importpath = "github.com/cockroachdb/cockroach/pkg/roachprod/promhelperclient", visibility = ["//visibility:public"], deps = [ + "//pkg/roachprod/install", "//pkg/roachprod/logger", "//pkg/util/httputil", "@com_github_cockroachdb_errors//:errors", "@com_google_cloud_go_storage//:storage", + "@in_gopkg_yaml_v2//:yaml_v2", "@org_golang_google_api//idtoken", "@org_golang_google_api//option", "@org_golang_x_oauth2//:oauth2", @@ -26,6 +28,7 @@ go_test( deps = [ "//pkg/roachprod/logger", "@com_github_stretchr_testify//require", + "@in_gopkg_yaml_v2//:yaml_v2", "@org_golang_google_api//idtoken", "@org_golang_x_oauth2//:oauth2", ], diff --git a/pkg/roachprod/promhelperclient/client.go b/pkg/roachprod/promhelperclient/client.go index 867a2f74abc1..139c62273812 100644 --- a/pkg/roachprod/promhelperclient/client.go +++ b/pkg/roachprod/promhelperclient/client.go @@ -23,13 +23,14 @@ import ( "os" "strconv" "strings" - "text/template" + "github.com/cockroachdb/cockroach/pkg/roachprod/install" "github.com/cockroachdb/cockroach/pkg/roachprod/logger" "github.com/cockroachdb/cockroach/pkg/util/httputil" "github.com/cockroachdb/errors" "golang.org/x/oauth2" "google.golang.org/api/idtoken" + "gopkg.in/yaml.v2" ) const ( @@ -150,38 +151,30 @@ func getUrl(promUrl, clusterName string) string { return fmt.Sprintf("%s/%s/%s/%s", promUrl, resourceVersion, resourceName, clusterName) } -// ccParams are the params for the clusterConfFileTemplate -type ccParams struct { - Targets []string - Labels []string +// CCParams are the params for the cluster configs +type CCParams struct { + Targets []string `yaml:"targets"` + Labels map[string]string `yaml:"labels"` } -const clusterConfFileTemplate = `- targets: -{{range $val := .Targets}} - {{$val}} -{{end}} labels: -{{range $val := .Labels}} {{$val}} -{{end}} -` - // createClusterConfigFile creates the cluster config file per node func buildCreateRequest(nodes map[int]string, insecure bool) (io.Reader, error) { - buffer := bytes.NewBufferString("---\n") + configs := make([]*CCParams, 0) for i, n := range nodes { - params := &ccParams{ + params := &CCParams{ Targets: []string{n}, - Labels: make([]string, 0), - } - params.Labels = append(params.Labels, - fmt.Sprintf("node: \"%s\"", strconv.Itoa(i)), - "tenant: system", - ) - t := template.Must(template.New("start").Parse(clusterConfFileTemplate)) - if err := t.Execute(buffer, params); err != nil { - return nil, err + Labels: map[string]string{ + "node": strconv.Itoa(i), + "tenant": install.SystemInterfaceName, + }, } + configs = append(configs, params) } - - b, err := json.Marshal(&instanceConfigRequest{Config: buffer.String(), Insecure: insecure}) + cb, err := yaml.Marshal(&configs) + if err != nil { + return nil, err + } + b, err := json.Marshal(&instanceConfigRequest{Config: string(cb), Insecure: insecure}) if err != nil { return nil, err } diff --git a/pkg/roachprod/promhelperclient/client_test.go b/pkg/roachprod/promhelperclient/client_test.go index fac3a29df8bf..e83c6c71c23e 100644 --- a/pkg/roachprod/promhelperclient/client_test.go +++ b/pkg/roachprod/promhelperclient/client_test.go @@ -18,6 +18,7 @@ import ( "net/http" "os" "path/filepath" + "strconv" "strings" "testing" @@ -25,6 +26,7 @@ import ( "github.com/stretchr/testify/require" "golang.org/x/oauth2" "google.golang.org/api/idtoken" + "gopkg.in/yaml.v2" ) func TestUpdatePrometheusTargets(t *testing.T) { @@ -52,18 +54,27 @@ func TestUpdatePrometheusTargets(t *testing.T) { require.Equal(t, "request failed with status 400 and error failed", err.Error()) }) t.Run("UpdatePrometheusTargets succeeds", func(t *testing.T) { + nodeInfos := map[int]string{1: "n1", 3: "n3"} c.httpPut = func(ctx context.Context, url string, h *http.Header, body io.Reader) ( resp *http.Response, err error) { require.Equal(t, getUrl(promUrl, "c1"), url) ir, err := getInstanceConfigRequest(io.NopCloser(body)) require.Nil(t, err) - // TODO (bhaskar): check for the correct yaml require.NotNil(t, ir.Config) + configs := make([]*CCParams, 0) + require.Nil(t, yaml.UnmarshalStrict([]byte(ir.Config), &configs)) + require.Len(t, configs, 2) + for _, c := range configs { + nodeID, err := strconv.Atoi(c.Labels["node"]) + require.NoError(t, err) + require.Equal(t, nodeInfos[nodeID], c.Targets[0]) + require.Equal(t, "system", c.Labels["tenant"]) + } return &http.Response{ StatusCode: 200, }, nil } - err := c.UpdatePrometheusTargets(ctx, promUrl, "c1", false, map[int]string{1: "n1", 3: "n3"}, true, l) + err := c.UpdatePrometheusTargets(ctx, promUrl, "c1", false, nodeInfos, true, l) require.Nil(t, err) }) }