prometheus/discovery/kubernetes/node_test.go

172 lines
4.4 KiB
Go
Raw Normal View History

// Copyright 2016 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kubernetes
import (
"fmt"
"testing"
"github.com/prometheus/common/model"
Refactor SD configuration to remove `config` dependency (#3629) * refactor: move targetGroup struct and CheckOverflow() to their own package * refactor: move auth and security related structs to a utility package, fix import error in utility package * refactor: Azure SD, remove SD struct from config * refactor: DNS SD, remove SD struct from config into dns package * refactor: ec2 SD, move SD struct from config into the ec2 package * refactor: file SD, move SD struct from config to file discovery package * refactor: gce, move SD struct from config to gce discovery package * refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil * refactor: consul, move SD struct from config into consul discovery package * refactor: marathon, move SD struct from config into marathon discovery package * refactor: triton, move SD struct from config to triton discovery package, fix test * refactor: zookeeper, move SD structs from config to zookeeper discovery package * refactor: openstack, remove SD struct from config, move into openstack discovery package * refactor: kubernetes, move SD struct from config into kubernetes discovery package * refactor: notifier, use targetgroup package instead of config * refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup * refactor: retrieval, use targetgroup package instead of config.TargetGroup * refactor: storage, use config util package * refactor: discovery manager, use targetgroup package instead of config.TargetGroup * refactor: use HTTPClient and TLS config from configUtil instead of config * refactor: tests, use targetgroup package instead of config.TargetGroup * refactor: fix tagetgroup.Group pointers that were removed by mistake * refactor: openstack, kubernetes: drop prefixes * refactor: remove import aliases forced due to vscode bug * refactor: move main SD struct out of config into discovery/config * refactor: rename configUtil to config_util * refactor: rename yamlUtil to yaml_config * refactor: kubernetes, remove prefixes * refactor: move the TargetGroup package to discovery/ * refactor: fix order of imports
2017-12-29 20:01:34 +00:00
"github.com/prometheus/prometheus/discovery/targetgroup"
v1 "k8s.io/api/core/v1"
2017-05-11 08:29:10 +00:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func makeNode(name, address string, labels map[string]string, annotations map[string]string) *v1.Node {
return &v1.Node{
2017-05-11 08:29:10 +00:00
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: labels,
Annotations: annotations,
},
Status: v1.NodeStatus{
Addresses: []v1.NodeAddress{
{
Type: v1.NodeInternalIP,
Address: address,
},
},
DaemonEndpoints: v1.NodeDaemonEndpoints{
KubeletEndpoint: v1.DaemonEndpoint{
Port: 10250,
},
},
},
}
}
func makeEnumeratedNode(i int) *v1.Node {
return makeNode(fmt.Sprintf("test%d", i), "1.2.3.4", map[string]string{}, map[string]string{})
}
func TestNodeDiscoveryBeforeStart(t *testing.T) {
n, c := makeDiscovery(RoleNode, NamespaceDiscovery{})
k8sDiscoveryTest{
discovery: n,
beforeRun: func() {
obj := makeNode(
"test",
"1.2.3.4",
map[string]string{"test-label": "testvalue"},
map[string]string{"test-annotation": "testannotationvalue"},
)
c.CoreV1().Nodes().Create(obj)
},
expectedMaxItems: 1,
expectedRes: map[string]*targetgroup.Group{
"node/test": {
Targets: []model.LabelSet{
{
"__address__": "1.2.3.4:10250",
"instance": "test",
"__meta_kubernetes_node_address_InternalIP": "1.2.3.4",
},
},
Labels: model.LabelSet{
"__meta_kubernetes_node_name": "test",
"__meta_kubernetes_node_label_test_label": "testvalue",
"__meta_kubernetes_node_labelpresent_test_label": "true",
"__meta_kubernetes_node_annotation_test_annotation": "testannotationvalue",
"__meta_kubernetes_node_annotationpresent_test_annotation": "true",
},
Source: "node/test",
},
},
}.Run(t)
}
func TestNodeDiscoveryAdd(t *testing.T) {
n, c := makeDiscovery(RoleNode, NamespaceDiscovery{})
k8sDiscoveryTest{
discovery: n,
afterStart: func() {
obj := makeEnumeratedNode(1)
c.CoreV1().Nodes().Create(obj)
},
expectedMaxItems: 1,
expectedRes: map[string]*targetgroup.Group{
"node/test1": {
Targets: []model.LabelSet{
{
"__address__": "1.2.3.4:10250",
"instance": "test1",
"__meta_kubernetes_node_address_InternalIP": "1.2.3.4",
},
},
Labels: model.LabelSet{
"__meta_kubernetes_node_name": "test1",
},
Source: "node/test1",
},
},
}.Run(t)
}
func TestNodeDiscoveryDelete(t *testing.T) {
obj := makeEnumeratedNode(0)
n, c := makeDiscovery(RoleNode, NamespaceDiscovery{}, obj)
k8sDiscoveryTest{
discovery: n,
afterStart: func() {
c.CoreV1().Nodes().Delete(obj.Name, &metav1.DeleteOptions{})
},
expectedMaxItems: 2,
expectedRes: map[string]*targetgroup.Group{
"node/test0": {
Source: "node/test0",
},
},
}.Run(t)
}
func TestNodeDiscoveryUpdate(t *testing.T) {
n, c := makeDiscovery(RoleNode, NamespaceDiscovery{})
k8sDiscoveryTest{
discovery: n,
afterStart: func() {
obj1 := makeEnumeratedNode(0)
c.CoreV1().Nodes().Create(obj1)
obj2 := makeNode(
"test0",
"1.2.3.4",
map[string]string{"Unschedulable": "true"},
map[string]string{},
)
c.CoreV1().Nodes().Update(obj2)
},
expectedMaxItems: 2,
expectedRes: map[string]*targetgroup.Group{
"node/test0": {
Targets: []model.LabelSet{
{
"__address__": "1.2.3.4:10250",
"instance": "test0",
"__meta_kubernetes_node_address_InternalIP": "1.2.3.4",
},
},
Labels: model.LabelSet{
"__meta_kubernetes_node_label_Unschedulable": "true",
"__meta_kubernetes_node_labelpresent_Unschedulable": "true",
"__meta_kubernetes_node_name": "test0",
},
Source: "node/test0",
},
},
}.Run(t)
}