forked from kubernetes/kube-state-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpointslice_test.go
206 lines (198 loc) · 6.81 KB
/
endpointslice_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
Copyright 2022 The Kubernetes Authors All rights reserved.
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 store
import (
"testing"
corev1 "k8s.io/api/core/v1"
discoveryv1 "k8s.io/api/discovery/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
)
func TestEndpointSliceStore(t *testing.T) {
startTime := 1501569018
metav1StartTime := metav1.Unix(int64(startTime), 0)
portname := "http"
portnumber := int32(80)
portprotocol := corev1.Protocol("TCP")
nodename := "node"
hostname := "host"
zone := "west"
ready := true
terminating := false
addresses := []string{"10.0.0.1", "192.168.1.10"}
cases := []generateMetricsTestCase{
{
Obj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Name: "test_endpointslice-info",
Namespace: "test",
},
AddressType: "IPv4",
},
Want: `
# HELP kube_endpointslice_info Information about endpointslice.
# TYPE kube_endpointslice_info gauge
kube_endpointslice_info{endpointslice="test_endpointslice-info",addresstype="IPv4",namespace="test"} 1
`,
MetricNames: []string{
"kube_endpointslice_info",
},
},
{
Obj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Name: "test_kube_endpointslice-created",
Namespace: "test",
CreationTimestamp: metav1StartTime,
},
AddressType: "IPv4",
},
Want: `
# HELP kube_endpointslice_created Unix creation timestamp
# TYPE kube_endpointslice_created gauge
kube_endpointslice_created{endpointslice="test_kube_endpointslice-created",namespace="test"} 1.501569018e+09
`,
MetricNames: []string{
"kube_endpointslice_created",
},
},
{
Obj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Name: "test_endpointslice-ports",
Namespace: "test",
},
AddressType: "IPv4",
Ports: []discoveryv1.EndpointPort{
{Name: &portname,
Port: &portnumber,
Protocol: &portprotocol,
},
},
},
Want: `
# HELP kube_endpointslice_ports Ports attached to the endpointslice.
# TYPE kube_endpointslice_ports gauge
kube_endpointslice_ports{endpointslice="test_endpointslice-ports",port_name="http",port_protocol="TCP",port_number="80",namespace="test"} 1
`,
MetricNames: []string{
"kube_endpointslice_ports",
},
},
{
Obj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Name: "test_endpointslice-endpoints",
Namespace: "test",
},
AddressType: "IPv4",
Endpoints: []discoveryv1.Endpoint{
{
NodeName: &nodename,
Conditions: discoveryv1.EndpointConditions{
Ready: &ready,
Terminating: &terminating,
},
Hostname: &hostname,
Zone: &zone,
Addresses: addresses,
},
},
},
Want: `
# HELP kube_endpointslice_endpoints Endpoints attached to the endpointslice.
# HELP kube_endpointslice_endpoints_hints Topology routing hints attached to endpoints
# TYPE kube_endpointslice_endpoints gauge
# TYPE kube_endpointslice_endpoints_hints gauge
kube_endpointslice_endpoints{address="10.0.0.1",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="host",ready="true",terminating="false",namespace="test"} 1
kube_endpointslice_endpoints{address="192.168.1.10",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="host",ready="true",terminating="false",namespace="test"} 1
`,
MetricNames: []string{
"kube_endpointslice_endpoints",
},
},
{
Obj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Name: "test_endpointslice-endpoints",
Namespace: "test",
},
AddressType: "IPv4",
Endpoints: []discoveryv1.Endpoint{
{
NodeName: &nodename,
Conditions: discoveryv1.EndpointConditions{
Ready: &ready,
Terminating: &terminating,
},
Hostname: &hostname,
Zone: &zone,
Addresses: addresses,
Hints: &discoveryv1.EndpointHints{
ForZones: []discoveryv1.ForZone{
{Name: "zone1"},
},
},
},
},
},
Want: `
# HELP kube_endpointslice_endpoints Endpoints attached to the endpointslice.
# HELP kube_endpointslice_endpoints_hints Topology routing hints attached to endpoints
# TYPE kube_endpointslice_endpoints gauge
# TYPE kube_endpointslice_endpoints_hints gauge
kube_endpointslice_endpoints_hints{address="10.0.0.1",endpointslice="test_endpointslice-endpoints",for_zone="zone1",namespace="test"} 1
kube_endpointslice_endpoints{address="10.0.0.1",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="host",ready="true",terminating="false",namespace="test"} 1
kube_endpointslice_endpoints{address="192.168.1.10",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="host",ready="true",terminating="false",namespace="test"} 1
`,
MetricNames: []string{
"kube_endpointslice_endpoints",
},
},
{
AllowAnnotationsList: []string{
"foo",
},
Obj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Name: "test_endpointslice-labels",
Namespace: "test",
Annotations: map[string]string{
"foo": "baz",
},
Labels: map[string]string{
"foo": "bar",
},
},
AddressType: "IPv4",
},
Want: `
# HELP kube_endpointslice_annotations Kubernetes annotations converted to Prometheus labels.
# HELP kube_endpointslice_labels Kubernetes labels converted to Prometheus labels.
# TYPE kube_endpointslice_annotations gauge
# TYPE kube_endpointslice_labels gauge
kube_endpointslice_annotations{endpointslice="test_endpointslice-labels",annotation_foo="baz",namespace="test"} 1
`,
MetricNames: []string{
"kube_endpointslice_annotations", "kube_endpointslice_labels",
},
},
}
for i, c := range cases {
c.Func = generator.ComposeMetricGenFuncs(endpointSliceMetricFamilies(c.AllowAnnotationsList, nil))
c.Headers = generator.ExtractMetricFamilyHeaders(endpointSliceMetricFamilies(c.AllowAnnotationsList, nil))
if err := c.run(); err != nil {
t.Errorf("unexpected collecting result in %vth run:\n%s", i, err)
}
}
}