mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-13 09:04:06 -08:00
8ceb8f2ae8
- Do initial listing and syncing to scrape manager, then register event handlers may lost events happening in listing and syncing (if it lasted a long time). We should register event handlers at the very begining, before processing just wait until informers synced (sync in informer will list all objects and call OnUpdate event handler). - Use a queue then we don't block event callbacks and an object will be processed only once if added multiple times before it being processed. - Fix bug in `serviceUpdate` in endpoints.go, we should build endpoints when `exists && err == nil`. Add `^TestEndpointsDiscoveryWithService` tests to test this feature. Testing: - Use `k8s.io/client-go` testing framework and fake implementations which are more robust and reliable for testing. - `Test\w+DiscoveryBeforeRun` are used to test objects created before discoverer runs - `Test\w+DiscoveryAdd\w+` are used to test adding objects - `Test\w+DiscoveryDelete\w+` are used to test deleting objects - `Test\w+DiscoveryUpdate\w+` are used to test updating objects - `TestEndpointsDiscoveryWithService\w+` are used to test endpoints events triggered by services - `cache.DeletedFinalStateUnknown` related stuffs are removed, because we don't care deleted objects in store, we only need its name to send a specical `targetgroup.Group` to scrape manager Signed-off-by: Yecheng Fu <cofyc.jackson@gmail.com>
174 lines
4.3 KiB
Go
174 lines
4.3 KiB
Go
// 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"
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/client-go/pkg/api/v1"
|
|
)
|
|
|
|
func makeNode(name, address string, labels map[string]string, annotations map[string]string) *v1.Node {
|
|
return &v1.Node{
|
|
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, w := makeDiscovery(RoleNode, NamespaceDiscovery{})
|
|
|
|
k8sDiscoveryTest{
|
|
discovery: n,
|
|
beforeRun: func() {
|
|
obj := makeNode(
|
|
"test",
|
|
"1.2.3.4",
|
|
map[string]string{"testlabel": "testvalue"},
|
|
map[string]string{"testannotation": "testannotationvalue"},
|
|
)
|
|
c.CoreV1().Nodes().Create(obj)
|
|
w.Nodes().Add(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_testlabel": "testvalue",
|
|
"__meta_kubernetes_node_annotation_testannotation": "testannotationvalue",
|
|
},
|
|
Source: "node/test",
|
|
},
|
|
},
|
|
}.Run(t)
|
|
}
|
|
|
|
func TestNodeDiscoveryAdd(t *testing.T) {
|
|
n, c, w := makeDiscovery(RoleNode, NamespaceDiscovery{})
|
|
|
|
k8sDiscoveryTest{
|
|
discovery: n,
|
|
afterStart: func() {
|
|
obj := makeEnumeratedNode(1)
|
|
c.CoreV1().Nodes().Create(obj)
|
|
w.Nodes().Add(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, w := makeDiscovery(RoleNode, NamespaceDiscovery{}, obj)
|
|
|
|
k8sDiscoveryTest{
|
|
discovery: n,
|
|
afterStart: func() {
|
|
c.CoreV1().Nodes().Delete(obj.Name, &metav1.DeleteOptions{})
|
|
w.Nodes().Delete(obj)
|
|
},
|
|
expectedMaxItems: 2,
|
|
expectedRes: map[string]*targetgroup.Group{
|
|
"node/test0": {
|
|
Source: "node/test0",
|
|
},
|
|
},
|
|
}.Run(t)
|
|
}
|
|
|
|
func TestNodeDiscoveryUpdate(t *testing.T) {
|
|
n, c, w := makeDiscovery(RoleNode, NamespaceDiscovery{})
|
|
|
|
k8sDiscoveryTest{
|
|
discovery: n,
|
|
afterStart: func() {
|
|
obj1 := makeEnumeratedNode(0)
|
|
c.CoreV1().Nodes().Create(obj1)
|
|
w.Nodes().Add(obj1)
|
|
obj2 := makeNode(
|
|
"test0",
|
|
"1.2.3.4",
|
|
map[string]string{"Unschedulable": "true"},
|
|
map[string]string{},
|
|
)
|
|
c.CoreV1().Nodes().Update(obj2)
|
|
w.Nodes().Modify(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_name": "test0",
|
|
},
|
|
Source: "node/test0",
|
|
},
|
|
},
|
|
}.Run(t)
|
|
}
|