Skip to content

Commit

Permalink
fix: unmarshal bear_token/password of remote write/read config (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: RayHuangCN <raylhuang@tencent.com>
  • Loading branch information
RayHuangCN and RayHuangCN authored Dec 11, 2020
1 parent b2ed157 commit 89d23ce
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
21 changes: 21 additions & 0 deletions pkg/sidecar/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,27 @@ func (i *Injector) UpdateConfig() error {
}
}

for _, w := range cfg.RemoteWriteConfigs {
if w.HTTPClientConfig.BearerToken != "" {
bTokens = append(bTokens, string(w.HTTPClientConfig.BearerToken))
}

if w.HTTPClientConfig.BasicAuth != nil && w.HTTPClientConfig.BasicAuth.Password != "" {
password = append(password, string(w.HTTPClientConfig.BasicAuth.Password))
}

}

for _, w := range cfg.RemoteReadConfigs {
if w.HTTPClientConfig.BearerToken != "" {
bTokens = append(bTokens, string(w.HTTPClientConfig.BearerToken))
}

if w.HTTPClientConfig.BasicAuth != nil && w.HTTPClientConfig.BasicAuth.Password != "" {
password = append(password, string(w.HTTPClientConfig.BasicAuth.Password))
}
}

gen, err := yaml.Marshal(&cfg)
if err != nil {
return errors.Wrapf(err, "marshal config failed")
Expand Down
32 changes: 20 additions & 12 deletions pkg/sidecar/injector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package sidecar

import (
"github.com/prometheus/common/model"

"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/discovery"
"github.com/prometheus/prometheus/pkg/labels"
Expand All @@ -27,19 +28,21 @@ import (
"os"
"testing"
"tkestack.io/kvass/pkg/target"
"tkestack.io/kvass/pkg/utils/test"
)

func TestInjector_UpdateConfig(t *testing.T) {
job := &config.ScrapeConfig{
JobName: "job",
}
job.HTTPClientConfig.BearerToken = "123"

cfg := &config.Config{
ScrapeConfigs: []*config.ScrapeConfig{job},
}

cfg := `global: {}
scrape_configs:
- job_name: job
honor_timestamps: false
bearer_token: job
remote_write:
- url: http://127.0.0.1
bearer_token: write
remote_read:
- url: http://127.0.0.1
bearer_token: read
`
tar := &target.Target{
Hash: 1,
Labels: labels.Labels{
Expand All @@ -64,16 +67,18 @@ func TestInjector_UpdateConfig(t *testing.T) {

out := &config.Config{}
in.readFile = func(file string) (bytes []byte, e error) {
return []byte(test.MustYAMLV2(cfg)), nil
return []byte(cfg), nil
}
in.writeFile = func(filename string, data []byte, perm os.FileMode) error {
return yaml.Unmarshal(data, out)
}

r.NoError(in.UpdateTargets(map[string][]*target.Target{
job.JobName: {tar},
"job": {tar},
}))

r.NoError(in.UpdateConfig())

outJob := out.ScrapeConfigs[0]
outSD := outJob.ServiceDiscoveryConfigs[0].(discovery.StaticConfig)[0]
r.Equal("http://127.0.0.1:8008", outJob.HTTPClientConfig.ProxyURL.String())
Expand All @@ -83,4 +88,7 @@ func TestInjector_UpdateConfig(t *testing.T) {
r.Equal(model.LabelValue("https"), outSD.Labels[model.ParamLabelPrefix+paramScheme])
r.Equal(model.LabelValue("job"), outSD.Labels[model.ParamLabelPrefix+paramJobName])
r.Equal(model.LabelValue("1"), outSD.Labels[model.ParamLabelPrefix+paramHash])
r.Equal("job", string(outJob.HTTPClientConfig.BearerToken))
r.Equal("write", string(out.RemoteWriteConfigs[0].HTTPClientConfig.BearerToken))
r.Equal("read", string(out.RemoteReadConfigs[0].HTTPClientConfig.BearerToken))
}

0 comments on commit 89d23ce

Please sign in to comment.