Skip to content

Commit

Permalink
roachprod: use yaml for creating the cluster config
Browse files Browse the repository at this point in the history
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: cockroachdb#124320
Epic: none
  • Loading branch information
nameisbhaskar committed May 17, 2024
1 parent 6918532 commit 78e0f98
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
2 changes: 2 additions & 0 deletions pkg/roachprod/promhelperclient/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_library(
"//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",
Expand All @@ -26,6 +27,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",
],
Expand Down
41 changes: 16 additions & 25 deletions pkg/roachprod/promhelperclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import (
"os"
"strconv"
"strings"
"text/template"

"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 (
Expand Down Expand Up @@ -150,38 +150,29 @@ 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: make(map[string]string, 0),
}
params.Labels["node"] = strconv.Itoa(i)
params.Labels["tenant"] = "system"
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
}
Expand Down
14 changes: 13 additions & 1 deletion pkg/roachprod/promhelperclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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) {
Expand Down Expand Up @@ -57,8 +58,19 @@ func TestUpdatePrometheusTargets(t *testing.T) {
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.Equal(t, 2, len(configs))
for _, c := range configs {
if c.Labels["node"] == "1" {
require.Equal(t, "n1", c.Targets[0])
} else {
require.Equal(t, "n3", c.Targets[0])
require.Equal(t, "3", c.Labels["node"])
}
c.Labels["tenant"] = "system"
}
return &http.Response{
StatusCode: 200,
}, nil
Expand Down

0 comments on commit 78e0f98

Please sign in to comment.