Skip to content

Commit

Permalink
roachprod: secure clusters in dynamic admin url port
Browse files Browse the repository at this point in the history
A recent change to support dynamic cluster port was introduced.
But, that was not considering secured clusters
This change supports secure cluster

Fixes: cockroachdb#117125
Epic: none
  • Loading branch information
nameisbhaskar committed May 13, 2024
1 parent 16d4175 commit d92e5cb
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
12 changes: 7 additions & 5 deletions pkg/roachprod/promhelperclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ func NewPromClient() *PromClient {
// instanceConfigRequest is the HTTP request received for generating instance config
type instanceConfigRequest struct {
//Config is the content of the yaml file
Config string `json:"config"`
Config string `json:"config"`
Insecure bool `json:"insecure"`
}

// UpdatePrometheusTargets updates the cluster config in the promUrl
Expand All @@ -76,9 +77,10 @@ func (c *PromClient) UpdatePrometheusTargets(
promUrl, clusterName string,
forceFetchCreds bool,
nodes []string,
insecure bool,
l *logger.Logger,
) error {
req, err := buildCreateRequest(nodes)
req, err := buildCreateRequest(nodes, insecure)
if err != nil {
return err
}
Expand All @@ -99,7 +101,7 @@ func (c *PromClient) UpdatePrometheusTargets(
defer func() { _ = response.Body.Close() }()
if response.StatusCode == http.StatusUnauthorized && !forceFetchCreds {
l.Printf("request failed - this may be due to a stale token. retrying with forceFetchCreds true ...")
return c.UpdatePrometheusTargets(ctx, promUrl, clusterName, true, nodes, l)
return c.UpdatePrometheusTargets(ctx, promUrl, clusterName, true, nodes, insecure, l)
}
body, err := io.ReadAll(response.Body)
if err != nil {
Expand Down Expand Up @@ -160,7 +162,7 @@ const clusterConfFileTemplate = `- targets:
`

// createClusterConfigFile creates the cluster config file per node
func buildCreateRequest(nodes []string) (io.Reader, error) {
func buildCreateRequest(nodes []string, insecure bool) (io.Reader, error) {
buffer := bytes.NewBufferString("---\n")
for i, n := range nodes {
if n == "" {
Expand All @@ -181,7 +183,7 @@ func buildCreateRequest(nodes []string) (io.Reader, error) {
}
}

b, err := json.Marshal(&instanceConfigRequest{Config: buffer.String()})
b, err := json.Marshal(&instanceConfigRequest{Config: buffer.String(), Insecure: insecure})
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/roachprod/promhelperclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestUpdatePrometheusTargets(t *testing.T) {
Body: io.NopCloser(strings.NewReader("failed")),
}, nil
}
err := c.UpdatePrometheusTargets(ctx, promUrl, "c1", false, []string{"n1"}, l)
err := c.UpdatePrometheusTargets(ctx, promUrl, "c1", false, []string{"n1"}, true, l)
require.NotNil(t, err)
require.Equal(t, "request failed with status 400 and error failed", err.Error())
})
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestUpdatePrometheusTargets(t *testing.T) {
StatusCode: 200,
}, nil
}
err := c.UpdatePrometheusTargets(ctx, promUrl, "c1", false, []string{"n1", "", "n3"}, l)
err := c.UpdatePrometheusTargets(ctx, promUrl, "c1", false, []string{"n1", "", "n3"}, true, l)
require.Nil(t, err)
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/roachprod/promhelperclient/promhelper_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

var (
// promCredFile is where the prom helper credentials are stored
promCredFile = os.TempDir() + "promhelpers-secrets"
promCredFile = os.TempDir() + "/promhelpers-secrets"
)

// FetchedFrom indicates where the credentials have been fetched from.
Expand Down
2 changes: 1 addition & 1 deletion pkg/roachprod/roachprod.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ func updatePrometheusTargets(ctx context.Context, l *logger.Logger, c *install.S
if len(nodeIPPorts) > 0 {
if err := promhelperclient.NewPromClient().UpdatePrometheusTargets(ctx,
envutil.EnvOrDefaultString(prometheusHostUrlEnv, defaultPrometheusHostUrl),
c.Name, false, nodeIPPorts, l); err != nil {
c.Name, false, nodeIPPorts, !c.Secure, l); err != nil {
l.Errorf("creating cluster config failed for the ip:ports %v: %v", nodeIPPorts, err)
}
}
Expand Down

0 comments on commit d92e5cb

Please sign in to comment.