Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
124322: roachprod: use yaml for creating the cluster config r=renatolabs a=nameisbhaskar

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

Co-authored-by: Bhaskarjyoti Bora <[email protected]>
  • Loading branch information
craig[bot] and nameisbhaskar committed May 20, 2024
2 parents 5d01328 + 0a22370 commit e4d7442
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
3 changes: 3 additions & 0 deletions pkg/roachprod/promhelperclient/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
],
Expand Down
43 changes: 18 additions & 25 deletions pkg/roachprod/promhelperclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 13 additions & 2 deletions pkg/roachprod/promhelperclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import (
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"testing"

"github.com/cockroachdb/cockroach/pkg/roachprod/logger"
"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 @@ -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)
})
}
Expand Down

0 comments on commit e4d7442

Please sign in to comment.