From 3ab5fe9d7c3b53ff3556f2795e1d5a69cbc19592 Mon Sep 17 00:00:00 2001 From: servak Date: Wed, 30 Jun 2021 20:04:53 +0900 Subject: [PATCH 1/2] fixed disappeared target groups in http_sd #9019 Signed-off-by: servak --- discovery/http/http.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/discovery/http/http.go b/discovery/http/http.go index df093b6578..deacc77f07 100644 --- a/discovery/http/http.go +++ b/discovery/http/http.go @@ -104,6 +104,7 @@ type Discovery struct { url string client *http.Client refreshInterval time.Duration + tgLastLength int } // NewDiscovery returns a new HTTP discovery for the given config. @@ -183,6 +184,15 @@ func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) { tg.Labels[httpSDURLLabel] = model.LabelValue(d.url) } + // Generate empty updates for sources that disappeared. + l := len(targetGroups) + if d.tgLastLength > l { + for i := l; i < d.tgLastLength; i++ { + targetGroups = append(targetGroups, &targetgroup.Group{Source: urlSource(d.url, i)}) + } + } + d.tgLastLength = l + return targetGroups, nil } From b027452c1e487a0a17887b582cfdf3bf8bcc3e08 Mon Sep 17 00:00:00 2001 From: servak Date: Wed, 30 Jun 2021 22:44:29 +0900 Subject: [PATCH 2/2] Add a testcase for http-sd Signed-off-by: servak --- discovery/http/http_test.go | 238 ++++++++++++++++++++++++++++++++++++ 1 file changed, 238 insertions(+) diff --git a/discovery/http/http_test.go b/discovery/http/http_test.go index 7adcb7c70c..65ba36262b 100644 --- a/discovery/http/http_test.go +++ b/discovery/http/http_test.go @@ -162,3 +162,241 @@ func TestContentTypeRegex(t *testing.T) { }) } } + +func TestSourceDisappeared(t *testing.T) { + var stubResponse string + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + fmt.Fprintln(w, stubResponse) + })) + t.Cleanup(ts.Close) + + cases := []struct { + responses []string + expectedTargets [][]*targetgroup.Group + }{ + { + responses: []string{ + `[]`, + `[]`, + }, + expectedTargets: [][]*targetgroup.Group{{}, {}}, + }, + { + responses: []string{ + `[{"labels": {"k": "1"}, "targets": ["127.0.0.1"]}]`, + `[{"labels": {"k": "1"}, "targets": ["127.0.0.1"]}, {"labels": {"k": "2"}, "targets": ["127.0.0.1"]}]`, + }, + expectedTargets: [][]*targetgroup.Group{ + { + { + Targets: []model.LabelSet{ + { + model.AddressLabel: model.LabelValue("127.0.0.1"), + }, + }, + Labels: model.LabelSet{ + model.LabelName("k"): model.LabelValue("1"), + model.LabelName("__meta_url"): model.LabelValue(ts.URL + "/http_disappeared.json"), + }, + Source: urlSource(ts.URL+"/http_disappeared.json", 0), + }, + }, + { + { + Targets: []model.LabelSet{ + { + model.AddressLabel: model.LabelValue("127.0.0.1"), + }, + }, + Labels: model.LabelSet{ + model.LabelName("k"): model.LabelValue("1"), + model.LabelName("__meta_url"): model.LabelValue(ts.URL + "/http_disappeared.json"), + }, + Source: urlSource(ts.URL+"/http_disappeared.json", 0), + }, + { + Targets: []model.LabelSet{ + { + model.AddressLabel: model.LabelValue("127.0.0.1"), + }, + }, + Labels: model.LabelSet{ + model.LabelName("k"): model.LabelValue("2"), + model.LabelName("__meta_url"): model.LabelValue(ts.URL + "/http_disappeared.json"), + }, + Source: urlSource(ts.URL+"/http_disappeared.json", 1), + }, + }, + }, + }, + { + responses: []string{ + `[{"labels": {"k": "1"}, "targets": ["127.0.0.1"]}, {"labels": {"k": "2"}, "targets": ["127.0.0.1"]}]`, + `[{"labels": {"k": "1"}, "targets": ["127.0.0.1"]}]`, + }, + expectedTargets: [][]*targetgroup.Group{ + { + { + Targets: []model.LabelSet{ + { + model.AddressLabel: model.LabelValue("127.0.0.1"), + }, + }, + Labels: model.LabelSet{ + model.LabelName("k"): model.LabelValue("1"), + model.LabelName("__meta_url"): model.LabelValue(ts.URL + "/http_disappeared.json"), + }, + Source: urlSource(ts.URL+"/http_disappeared.json", 0), + }, + { + Targets: []model.LabelSet{ + { + model.AddressLabel: model.LabelValue("127.0.0.1"), + }, + }, + Labels: model.LabelSet{ + model.LabelName("k"): model.LabelValue("2"), + model.LabelName("__meta_url"): model.LabelValue(ts.URL + "/http_disappeared.json"), + }, + Source: urlSource(ts.URL+"/http_disappeared.json", 1), + }, + }, + { + { + Targets: []model.LabelSet{ + { + model.AddressLabel: model.LabelValue("127.0.0.1"), + }, + }, + Labels: model.LabelSet{ + model.LabelName("k"): model.LabelValue("1"), + model.LabelName("__meta_url"): model.LabelValue(ts.URL + "/http_disappeared.json"), + }, + Source: urlSource(ts.URL+"/http_disappeared.json", 0), + }, + { + Targets: nil, + Labels: nil, + Source: urlSource(ts.URL+"/http_disappeared.json", 1), + }, + }, + }, + }, + { + responses: []string{ + `[{"labels": {"k": "1"}, "targets": ["127.0.0.1"]}, {"labels": {"k": "2"}, "targets": ["127.0.0.1"]}, {"labels": {"k": "3"}, "targets": ["127.0.0.1"]}]`, + `[{"labels": {"k": "1"}, "targets": ["127.0.0.1"]}]`, + `[{"labels": {"k": "v"}, "targets": ["127.0.0.2"]}, {"labels": {"k": "vv"}, "targets": ["127.0.0.3"]}]`, + }, + expectedTargets: [][]*targetgroup.Group{ + { + { + Targets: []model.LabelSet{ + { + model.AddressLabel: model.LabelValue("127.0.0.1"), + }, + }, + Labels: model.LabelSet{ + model.LabelName("k"): model.LabelValue("1"), + model.LabelName("__meta_url"): model.LabelValue(ts.URL + "/http_disappeared.json"), + }, + Source: urlSource(ts.URL+"/http_disappeared.json", 0), + }, + { + Targets: []model.LabelSet{ + { + model.AddressLabel: model.LabelValue("127.0.0.1"), + }, + }, + Labels: model.LabelSet{ + model.LabelName("k"): model.LabelValue("2"), + model.LabelName("__meta_url"): model.LabelValue(ts.URL + "/http_disappeared.json"), + }, + Source: urlSource(ts.URL+"/http_disappeared.json", 1), + }, + { + Targets: []model.LabelSet{ + { + model.AddressLabel: model.LabelValue("127.0.0.1"), + }, + }, + Labels: model.LabelSet{ + model.LabelName("k"): model.LabelValue("3"), + model.LabelName("__meta_url"): model.LabelValue(ts.URL + "/http_disappeared.json"), + }, + Source: urlSource(ts.URL+"/http_disappeared.json", 2), + }, + }, + { + { + Targets: []model.LabelSet{ + { + model.AddressLabel: model.LabelValue("127.0.0.1"), + }, + }, + Labels: model.LabelSet{ + model.LabelName("k"): model.LabelValue("1"), + model.LabelName("__meta_url"): model.LabelValue(ts.URL + "/http_disappeared.json"), + }, + Source: urlSource(ts.URL+"/http_disappeared.json", 0), + }, + { + Targets: nil, + Labels: nil, + Source: urlSource(ts.URL+"/http_disappeared.json", 1), + }, + { + Targets: nil, + Labels: nil, + Source: urlSource(ts.URL+"/http_disappeared.json", 2), + }, + }, + { + { + Targets: []model.LabelSet{ + { + model.AddressLabel: model.LabelValue("127.0.0.2"), + }, + }, + Labels: model.LabelSet{ + model.LabelName("k"): model.LabelValue("v"), + model.LabelName("__meta_url"): model.LabelValue(ts.URL + "/http_disappeared.json"), + }, + Source: urlSource(ts.URL+"/http_disappeared.json", 0), + }, + { + Targets: []model.LabelSet{ + { + model.AddressLabel: model.LabelValue("127.0.0.3"), + }, + }, + Labels: model.LabelSet{ + model.LabelName("k"): model.LabelValue("vv"), + model.LabelName("__meta_url"): model.LabelValue(ts.URL + "/http_disappeared.json"), + }, + Source: urlSource(ts.URL+"/http_disappeared.json", 1), + }, + }, + }, + }, + } + + cfg := SDConfig{ + HTTPClientConfig: config.DefaultHTTPClientConfig, + URL: ts.URL + "/http_disappeared.json", + RefreshInterval: model.Duration(1 * time.Second), + } + d, err := NewDiscovery(&cfg, log.NewNopLogger()) + require.NoError(t, err) + for _, test := range cases { + ctx := context.Background() + for i, res := range test.responses { + stubResponse = res + tgs, err := d.refresh(ctx) + require.NoError(t, err) + require.Equal(t, test.expectedTargets[i], tgs) + } + } + +}