2020-04-13 18:02:27 +00:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
2020-04-24 14:50:21 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2020-04-13 18:02:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseServiceListFailure(t *testing.T) {
|
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
2021-02-26 14:54:03 +00:00
|
|
|
objectsByKey, _, err := parseServiceList([]byte(s))
|
2020-04-13 18:02:27 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
|
|
|
}
|
2021-02-26 14:54:03 +00:00
|
|
|
if len(objectsByKey) != 0 {
|
|
|
|
t.Fatalf("unexpected non-empty objectsByKey: %v", objectsByKey)
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f(``)
|
|
|
|
f(`[1,23]`)
|
|
|
|
f(`{"items":[{"metadata":1}]}`)
|
|
|
|
f(`{"items":[{"metadata":{"labels":[1]}}]}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseServiceListSuccess(t *testing.T) {
|
|
|
|
data := `{
|
|
|
|
"kind": "ServiceList",
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"metadata": {
|
|
|
|
"selfLink": "/api/v1/services",
|
|
|
|
"resourceVersion": "60485"
|
|
|
|
},
|
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"metadata": {
|
|
|
|
"name": "kube-dns",
|
|
|
|
"namespace": "kube-system",
|
|
|
|
"selfLink": "/api/v1/namespaces/kube-system/services/kube-dns",
|
|
|
|
"uid": "38a396f1-17fe-46c2-a5f4-3b225c18dcdf",
|
|
|
|
"resourceVersion": "177",
|
|
|
|
"creationTimestamp": "2020-03-16T20:44:26Z",
|
|
|
|
"labels": {
|
|
|
|
"k8s-app": "kube-dns",
|
|
|
|
"kubernetes.io/cluster-service": "true",
|
|
|
|
"kubernetes.io/name": "KubeDNS"
|
|
|
|
},
|
|
|
|
"annotations": {
|
|
|
|
"prometheus.io/port": "9153",
|
|
|
|
"prometheus.io/scrape": "true"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"ports": [
|
|
|
|
{
|
|
|
|
"name": "dns",
|
|
|
|
"protocol": "UDP",
|
|
|
|
"port": 53,
|
|
|
|
"targetPort": 53
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "dns-tcp",
|
|
|
|
"protocol": "TCP",
|
|
|
|
"port": 53,
|
|
|
|
"targetPort": 53
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "metrics",
|
|
|
|
"protocol": "TCP",
|
|
|
|
"port": 9153,
|
|
|
|
"targetPort": 9153
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"selector": {
|
|
|
|
"k8s-app": "kube-dns"
|
|
|
|
},
|
|
|
|
"clusterIP": "10.96.0.10",
|
|
|
|
"type": "ClusterIP",
|
|
|
|
"sessionAffinity": "None"
|
|
|
|
},
|
|
|
|
"status": {
|
|
|
|
"loadBalancer": {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
2021-02-26 14:54:03 +00:00
|
|
|
objectsByKey, meta, err := parseServiceList([]byte(data))
|
2020-04-13 18:02:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2021-02-26 14:54:03 +00:00
|
|
|
expectedResourceVersion := "60485"
|
|
|
|
if meta.ResourceVersion != expectedResourceVersion {
|
|
|
|
t.Fatalf("unexpected resource version; got %s; want %s", meta.ResourceVersion, expectedResourceVersion)
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
2021-02-26 18:21:27 +00:00
|
|
|
sortedLabelss := getSortedLabelss(objectsByKey)
|
2020-04-13 18:02:27 +00:00
|
|
|
expectedLabelss := [][]prompbmarshal.Label{
|
2020-04-24 14:50:21 +00:00
|
|
|
discoveryutils.GetSortedLabels(map[string]string{
|
2020-04-13 18:02:27 +00:00
|
|
|
"__address__": "kube-dns.kube-system.svc:53",
|
|
|
|
"__meta_kubernetes_namespace": "kube-system",
|
|
|
|
"__meta_kubernetes_service_name": "kube-dns",
|
|
|
|
"__meta_kubernetes_service_type": "ClusterIP",
|
|
|
|
"__meta_kubernetes_service_port_name": "dns",
|
|
|
|
"__meta_kubernetes_service_port_protocol": "UDP",
|
|
|
|
"__meta_kubernetes_service_cluster_ip": "10.96.0.10",
|
|
|
|
|
|
|
|
"__meta_kubernetes_service_label_k8s_app": "kube-dns",
|
|
|
|
"__meta_kubernetes_service_label_kubernetes_io_cluster_service": "true",
|
|
|
|
"__meta_kubernetes_service_label_kubernetes_io_name": "KubeDNS",
|
|
|
|
|
|
|
|
"__meta_kubernetes_service_labelpresent_k8s_app": "true",
|
|
|
|
"__meta_kubernetes_service_labelpresent_kubernetes_io_cluster_service": "true",
|
|
|
|
"__meta_kubernetes_service_labelpresent_kubernetes_io_name": "true",
|
|
|
|
|
|
|
|
"__meta_kubernetes_service_annotation_prometheus_io_port": "9153",
|
|
|
|
"__meta_kubernetes_service_annotation_prometheus_io_scrape": "true",
|
|
|
|
|
|
|
|
"__meta_kubernetes_service_annotationpresent_prometheus_io_port": "true",
|
|
|
|
"__meta_kubernetes_service_annotationpresent_prometheus_io_scrape": "true",
|
|
|
|
}),
|
2020-04-24 14:50:21 +00:00
|
|
|
discoveryutils.GetSortedLabels(map[string]string{
|
2020-04-13 18:02:27 +00:00
|
|
|
"__address__": "kube-dns.kube-system.svc:53",
|
|
|
|
"__meta_kubernetes_namespace": "kube-system",
|
|
|
|
"__meta_kubernetes_service_name": "kube-dns",
|
|
|
|
"__meta_kubernetes_service_type": "ClusterIP",
|
|
|
|
"__meta_kubernetes_service_port_name": "dns-tcp",
|
|
|
|
"__meta_kubernetes_service_port_protocol": "TCP",
|
|
|
|
"__meta_kubernetes_service_cluster_ip": "10.96.0.10",
|
|
|
|
|
|
|
|
"__meta_kubernetes_service_label_k8s_app": "kube-dns",
|
|
|
|
"__meta_kubernetes_service_label_kubernetes_io_cluster_service": "true",
|
|
|
|
"__meta_kubernetes_service_label_kubernetes_io_name": "KubeDNS",
|
|
|
|
|
|
|
|
"__meta_kubernetes_service_labelpresent_k8s_app": "true",
|
|
|
|
"__meta_kubernetes_service_labelpresent_kubernetes_io_cluster_service": "true",
|
|
|
|
"__meta_kubernetes_service_labelpresent_kubernetes_io_name": "true",
|
|
|
|
|
|
|
|
"__meta_kubernetes_service_annotation_prometheus_io_port": "9153",
|
|
|
|
"__meta_kubernetes_service_annotation_prometheus_io_scrape": "true",
|
|
|
|
|
|
|
|
"__meta_kubernetes_service_annotationpresent_prometheus_io_port": "true",
|
|
|
|
"__meta_kubernetes_service_annotationpresent_prometheus_io_scrape": "true",
|
|
|
|
}),
|
2020-04-24 14:50:21 +00:00
|
|
|
discoveryutils.GetSortedLabels(map[string]string{
|
2020-04-13 18:02:27 +00:00
|
|
|
"__address__": "kube-dns.kube-system.svc:9153",
|
|
|
|
"__meta_kubernetes_namespace": "kube-system",
|
|
|
|
"__meta_kubernetes_service_name": "kube-dns",
|
|
|
|
"__meta_kubernetes_service_type": "ClusterIP",
|
|
|
|
"__meta_kubernetes_service_port_name": "metrics",
|
|
|
|
"__meta_kubernetes_service_port_protocol": "TCP",
|
|
|
|
"__meta_kubernetes_service_cluster_ip": "10.96.0.10",
|
|
|
|
|
|
|
|
"__meta_kubernetes_service_label_k8s_app": "kube-dns",
|
|
|
|
"__meta_kubernetes_service_label_kubernetes_io_cluster_service": "true",
|
|
|
|
"__meta_kubernetes_service_label_kubernetes_io_name": "KubeDNS",
|
|
|
|
|
|
|
|
"__meta_kubernetes_service_labelpresent_k8s_app": "true",
|
|
|
|
"__meta_kubernetes_service_labelpresent_kubernetes_io_cluster_service": "true",
|
|
|
|
"__meta_kubernetes_service_labelpresent_kubernetes_io_name": "true",
|
|
|
|
|
|
|
|
"__meta_kubernetes_service_annotation_prometheus_io_port": "9153",
|
|
|
|
"__meta_kubernetes_service_annotation_prometheus_io_scrape": "true",
|
|
|
|
|
|
|
|
"__meta_kubernetes_service_annotationpresent_prometheus_io_port": "true",
|
|
|
|
"__meta_kubernetes_service_annotationpresent_prometheus_io_scrape": "true",
|
|
|
|
}),
|
|
|
|
}
|
2021-03-01 12:31:28 +00:00
|
|
|
if !areEqualLabelss(sortedLabelss, expectedLabelss) {
|
2020-04-13 18:02:27 +00:00
|
|
|
t.Fatalf("unexpected labels:\ngot\n%v\nwant\n%v", sortedLabelss, expectedLabelss)
|
|
|
|
}
|
|
|
|
}
|