From 25f453ce1a4461ef43c8b4a5ca3b32d4820736ca Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 3 Mar 2021 10:30:39 +0200 Subject: [PATCH] lib/promscrape/discovery/kubernetes: properly check for nil pointer inside interface See https://mangatmodi.medium.com/go-check-nil-interface-the-right-way-d142776edef1 This fixes a panic when the ScrapeWork is filtered out in swcFunc. Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1108 --- lib/promscrape/discovery/kubernetes/api.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/promscrape/discovery/kubernetes/api.go b/lib/promscrape/discovery/kubernetes/api.go index da858bd274..1d319a671a 100644 --- a/lib/promscrape/discovery/kubernetes/api.go +++ b/lib/promscrape/discovery/kubernetes/api.go @@ -12,6 +12,7 @@ import ( "net/http" "net/url" "os" + "reflect" "strconv" "strings" "sync" @@ -380,7 +381,8 @@ func getScrapeWorkObjectsForLabels(swcFunc ScrapeWorkConstructorFunc, labelss [] swos := make([]interface{}, 0, len(labelss)) for _, labels := range labelss { swo := swcFunc(labels) - if swo != nil { + // The reflect check is needed because of https://mangatmodi.medium.com/go-check-nil-interface-the-right-way-d142776edef1 + if swo != nil && !reflect.ValueOf(swo).IsNil() { swos = append(swos, swo) } }