Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parameter encoding issue #930

Merged
merged 8 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions cmd/otel-allocator/allocation/allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"math"
"testing"

"github.com/go-logr/logr"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/assert"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

var logger = logf.Log.WithName("unit-tests")

// Tests least connection - The expected collector after running findNextCollector should be the collector with the least amount of workload
func TestFindNextCollector(t *testing.T) {
var log logr.Logger
s := NewAllocator(log)
s := NewAllocator(logger)

defaultCol := collector{Name: "default-col", NumTargets: 1}
maxCol := collector{Name: "max-col", NumTargets: 2}
Expand All @@ -25,9 +26,7 @@ func TestFindNextCollector(t *testing.T) {
}

func TestSetCollectors(t *testing.T) {

var log logr.Logger
s := NewAllocator(log)
s := NewAllocator(logger)

cols := []string{"col-1", "col-2", "col-3"}
s.SetCollectors(cols)
Expand All @@ -42,8 +41,7 @@ func TestSetCollectors(t *testing.T) {

func TestAddingAndRemovingTargets(t *testing.T) {
// prepare allocator with initial targets and collectors
var log logr.Logger
s := NewAllocator(log)
s := NewAllocator(logger)

cols := []string{"col-1", "col-2", "col-3"}
s.SetCollectors(cols)
Expand Down Expand Up @@ -88,8 +86,7 @@ func TestAddingAndRemovingTargets(t *testing.T) {
func TestCollectorBalanceWhenAddingAndRemovingAtRandom(t *testing.T) {

// prepare allocator with 3 collectors and 'random' amount of targets
var log logr.Logger
s := NewAllocator(log)
s := NewAllocator(logger)

cols := []string{"col-1", "col-2", "col-3"}
s.SetCollectors(cols)
Expand Down
118 changes: 118 additions & 0 deletions cmd/otel-allocator/allocation/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package allocation

import (
"github.com/prometheus/common/model"
"reflect"
"testing"
)

func TestGetAllTargetsByCollectorAndJob(t *testing.T) {
baseAllocator := NewAllocator(logger)
baseAllocator.SetCollectors([]string{"test-collector"})
statefulAllocator := NewAllocator(logger)
statefulAllocator.SetCollectors([]string{"test-collector-0"})
type args struct {
collector string
job string
cMap map[string][]TargetItem
allocator *Allocator
}
var tests = []struct {
name string
args args
want []targetGroupJSON
}{
{
name: "Empty target map",
args: args{
collector: "test-collector",
job: "test-job",
cMap: map[string][]TargetItem{},
allocator: baseAllocator,
},
want: nil,
},
{
name: "Single entry target map",
args: args{
collector: "test-collector",
job: "test-job",
cMap: map[string][]TargetItem{
"test-collectortest-job": {
TargetItem{
JobName: "test-job",
Label: model.LabelSet{
"test-label": "test-value",
},
TargetURL: "test-url",
Collector: &collector{
Name: "test-collector",
NumTargets: 1,
},
},
},
},
allocator: baseAllocator,
},
want: []targetGroupJSON{
{
Targets: []string{"test-url"},
Labels: map[model.LabelName]model.LabelValue{
"test-label": "test-value",
},
},
},
},
{
name: "Multiple entry target map",
args: args{
collector: "test-collector",
job: "test-job",
cMap: map[string][]TargetItem{
"test-collectortest-job": {
TargetItem{
JobName: "test-job",
Label: model.LabelSet{
"test-label": "test-value",
},
TargetURL: "test-url",
Collector: &collector{
Name: "test-collector",
NumTargets: 1,
},
},
},
"test-collectortest-job2": {
TargetItem{
JobName: "test-job2",
Label: model.LabelSet{
"test-label": "test-value",
},
TargetURL: "test-url",
Collector: &collector{
Name: "test-collector",
NumTargets: 1,
},
},
},
},
allocator: baseAllocator,
},
want: []targetGroupJSON{
{
Targets: []string{"test-url"},
Labels: map[model.LabelName]model.LabelValue{
"test-label": "test-value",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetAllTargetsByCollectorAndJob(tt.args.collector, tt.args.job, tt.args.cMap, tt.args.allocator); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetAllTargetsByCollectorAndJob() = %v, want %v", got, tt.want)
}
})
}
}
12 changes: 6 additions & 6 deletions cmd/otel-allocator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,18 @@ func (s *server) TargetsHandler(w http.ResponseWriter, r *http.Request) {
compareMap[v.Collector.Name+v.JobName] = append(compareMap[v.Collector.Name+v.JobName], *v)
}
params := mux.Vars(r)
jobId, err := url.QueryUnescape(params["job_id"])
if err != nil {
errorHandler(err, w, r)
return
}

if len(q) == 0 {
jobId, err := url.QueryUnescape(params["job_id"])
if err != nil {
errorHandler(err, w, r)
return
}
displayData := allocation.GetAllTargetsByJob(jobId, compareMap, s.allocator)
jsonHandler(w, r, displayData)

} else {
tgs := allocation.GetAllTargetsByCollectorAndJob(q[0], params["job_id"], compareMap, s.allocator)
tgs := allocation.GetAllTargetsByCollectorAndJob(q[0], jobId, compareMap, s.allocator)
// Displays empty list if nothing matches
if len(tgs) == 0 {
jsonHandler(w, r, []interface{}{})
Expand Down
4 changes: 3 additions & 1 deletion pkg/collector/reconcile/config_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package reconcile

import (
"fmt"
"net/url"

"github.com/mitchellh/mapstructure"
promconfig "github.com/prometheus/prometheus/config"
Expand Down Expand Up @@ -61,9 +62,10 @@ func ReplaceConfig(params Params) (string, error) {
}

for i := range cfg.PromConfig.ScrapeConfigs {
escapedJob := url.QueryEscape(cfg.PromConfig.ScrapeConfigs[i].JobName)
cfg.PromConfig.ScrapeConfigs[i].ServiceDiscoveryConfigs = discovery.Configs{
&http.SDConfig{
URL: fmt.Sprintf("http://%s:80/jobs/%s/targets?collector_id=$POD_NAME", naming.TAService(params.Instance), cfg.PromConfig.ScrapeConfigs[i].JobName),
URL: fmt.Sprintf("http://%s:80/jobs/%s/targets?collector_id=$POD_NAME", naming.TAService(params.Instance), escapedJob),
},
}
}
Expand Down
52 changes: 52 additions & 0 deletions pkg/collector/reconcile/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,58 @@ service:

})

t.Run("should return expected escaped collector config map with http_sd_config", func(t *testing.T) {
expectedLables["app.kubernetes.io/component"] = "opentelemetry-collector"
expectedLables["app.kubernetes.io/name"] = "test-collector"
expectedLables["app.kubernetes.io/version"] = "latest"

expectedData := map[string]string{
"collector.yaml": `exporters:
logging: null
processors: null
receivers:
prometheus:
config:
global:
scrape_interval: 1m
scrape_timeout: 10s
evaluation_interval: 1m
scrape_configs:
- job_name: serviceMonitor/test/test/0
honor_timestamps: true
scrape_interval: 1m
scrape_timeout: 10s
metrics_path: /metrics
scheme: http
follow_redirects: true
http_sd_configs:
- follow_redirects: false
url: http://test-targetallocator:80/jobs/serviceMonitor%2Ftest%2Ftest%2F0/targets?collector_id=$POD_NAME
service:
pipelines:
metrics:
exporters:
- logging
processors: []
receivers:
- prometheus
`,
}

param, err := newParams("test/test-img", "../testdata/http_sd_config_servicemonitor_test.yaml")
assert.NoError(t, err)
param.Instance.Spec.TargetAllocator.Enabled = true
actual := desiredConfigMap(context.Background(), param)

assert.Equal(t, "test-collector", actual.Name)
assert.Equal(t, expectedLables, actual.Labels)
assert.Equal(t, expectedData, actual.Data)

// Reset the value
expectedLables["app.kubernetes.io/version"] = "0.47.0"

})

t.Run("should return expected target allocator config map", func(t *testing.T) {
expectedLables["app.kubernetes.io/component"] = "opentelemetry-targetallocator"
expectedLables["app.kubernetes.io/name"] = "test-targetallocator"
Expand Down
25 changes: 25 additions & 0 deletions pkg/collector/testdata/http_sd_config_servicemonitor_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
processors:
receivers:
prometheus:
config:
scrape_configs:
- job_name: serviceMonitor/test/test/0

static_configs:
- targets: ["prom.domain:1001", "prom.domain:1002", "prom.domain:1003"]
labels:
my: label

file_sd_configs:
- files:
- file2.json

exporters:
logging:

service:
pipelines:
metrics:
receivers: [prometheus]
processors: []
exporters: [logging]