2019-02-05 04:28:43 +08:00
|
|
|
package kube_inventory
|
|
|
|
|
|
|
|
|
|
import (
|
2020-06-19 03:08:52 +08:00
|
|
|
"reflect"
|
|
|
|
|
"strings"
|
2019-02-05 04:28:43 +08:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
v1 "k8s.io/api/apps/v1"
|
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
|
|
|
|
|
2019-02-05 04:28:43 +08:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestDeployment(t *testing.T) {
|
|
|
|
|
cli := &client{}
|
2020-06-19 03:08:52 +08:00
|
|
|
selectInclude := []string{}
|
|
|
|
|
selectExclude := []string{}
|
2019-02-05 04:28:43 +08:00
|
|
|
now := time.Now()
|
|
|
|
|
now = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 1, 36, 0, now.Location())
|
|
|
|
|
outputMetric := &testutil.Metric{
|
|
|
|
|
Fields: map[string]interface{}{
|
|
|
|
|
"replicas_available": int32(1),
|
|
|
|
|
"replicas_unavailable": int32(4),
|
|
|
|
|
"created": now.UnixNano(),
|
|
|
|
|
},
|
|
|
|
|
Tags: map[string]string{
|
2020-06-19 03:08:52 +08:00
|
|
|
"namespace": "ns1",
|
|
|
|
|
"deployment_name": "deploy1",
|
|
|
|
|
"selector_select1": "s1",
|
|
|
|
|
"selector_select2": "s2",
|
2019-02-05 04:28:43 +08:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
handler *mockHandler
|
|
|
|
|
output *testutil.Accumulator
|
|
|
|
|
hasError bool
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "no deployments",
|
|
|
|
|
handler: &mockHandler{
|
|
|
|
|
responseMap: map[string]interface{}{
|
2019-10-24 06:35:37 +08:00
|
|
|
"/deployments/": &v1.DeploymentList{},
|
2019-02-05 04:28:43 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
hasError: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "collect deployments",
|
|
|
|
|
handler: &mockHandler{
|
|
|
|
|
responseMap: map[string]interface{}{
|
2019-10-24 06:35:37 +08:00
|
|
|
"/deployments/": &v1.DeploymentList{
|
2021-03-18 05:35:25 +08:00
|
|
|
Items: []v1.Deployment{
|
2019-02-05 04:28:43 +08:00
|
|
|
{
|
2021-03-18 05:35:25 +08:00
|
|
|
Status: v1.DeploymentStatus{
|
|
|
|
|
Replicas: 3,
|
|
|
|
|
AvailableReplicas: 1,
|
|
|
|
|
UnavailableReplicas: 4,
|
|
|
|
|
UpdatedReplicas: 2,
|
|
|
|
|
ObservedGeneration: 9121,
|
2019-02-05 04:28:43 +08:00
|
|
|
},
|
2021-03-18 05:35:25 +08:00
|
|
|
Spec: v1.DeploymentSpec{
|
|
|
|
|
Strategy: v1.DeploymentStrategy{
|
2019-10-24 06:35:37 +08:00
|
|
|
RollingUpdate: &v1.RollingUpdateDeployment{
|
2019-02-05 04:28:43 +08:00
|
|
|
MaxUnavailable: &intstr.IntOrString{
|
2021-03-18 05:35:25 +08:00
|
|
|
IntVal: 30,
|
2019-02-05 04:28:43 +08:00
|
|
|
},
|
|
|
|
|
MaxSurge: &intstr.IntOrString{
|
2021-03-18 05:35:25 +08:00
|
|
|
IntVal: 20,
|
2019-02-05 04:28:43 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Replicas: toInt32Ptr(4),
|
2020-06-19 03:08:52 +08:00
|
|
|
Selector: &metav1.LabelSelector{
|
|
|
|
|
MatchLabels: map[string]string{
|
|
|
|
|
"select1": "s1",
|
|
|
|
|
"select2": "s2",
|
|
|
|
|
},
|
|
|
|
|
},
|
2019-02-05 04:28:43 +08:00
|
|
|
},
|
2021-03-18 05:35:25 +08:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Generation: 11221,
|
|
|
|
|
Namespace: "ns1",
|
|
|
|
|
Name: "deploy1",
|
2019-02-05 04:28:43 +08:00
|
|
|
Labels: map[string]string{
|
|
|
|
|
"lab1": "v1",
|
|
|
|
|
"lab2": "v2",
|
|
|
|
|
},
|
2021-03-18 05:35:25 +08:00
|
|
|
CreationTimestamp: metav1.Time{Time: now},
|
2019-02-05 04:28:43 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
output: &testutil.Accumulator{
|
|
|
|
|
Metrics: []*testutil.Metric{
|
|
|
|
|
outputMetric,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
hasError: false,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, v := range tests {
|
|
|
|
|
ks := &KubernetesInventory{
|
2020-06-19 03:08:52 +08:00
|
|
|
client: cli,
|
|
|
|
|
SelectorInclude: selectInclude,
|
|
|
|
|
SelectorExclude: selectExclude,
|
2019-02-05 04:28:43 +08:00
|
|
|
}
|
2020-06-19 03:08:52 +08:00
|
|
|
ks.createSelectorFilters()
|
2019-02-05 04:28:43 +08:00
|
|
|
acc := new(testutil.Accumulator)
|
2019-10-24 06:35:37 +08:00
|
|
|
for _, deployment := range ((v.handler.responseMap["/deployments/"]).(*v1.DeploymentList)).Items {
|
2021-03-23 01:21:36 +08:00
|
|
|
ks.gatherDeployment(deployment, acc)
|
2019-02-05 04:28:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := acc.FirstError()
|
|
|
|
|
if err == nil && v.hasError {
|
|
|
|
|
t.Fatalf("%s failed, should have error", v.name)
|
|
|
|
|
} else if err != nil && !v.hasError {
|
|
|
|
|
t.Fatalf("%s failed, err: %v", v.name, err)
|
|
|
|
|
}
|
|
|
|
|
if v.output == nil && len(acc.Metrics) > 0 {
|
|
|
|
|
t.Fatalf("%s: collected extra data", v.name)
|
|
|
|
|
} else if v.output != nil && len(v.output.Metrics) > 0 {
|
|
|
|
|
for i := range v.output.Metrics {
|
|
|
|
|
for k, m := range v.output.Metrics[i].Tags {
|
|
|
|
|
if acc.Metrics[i].Tags[k] != m {
|
|
|
|
|
t.Fatalf("%s: tag %s metrics unmatch Expected %s, got '%v'\n", v.name, k, m, acc.Metrics[i].Tags[k])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for k, m := range v.output.Metrics[i].Fields {
|
|
|
|
|
if acc.Metrics[i].Fields[k] != m {
|
|
|
|
|
t.Fatalf("%s: field %s metrics unmatch Expected %v(%T), got %v(%T)\n", v.name, k, m, m, acc.Metrics[i].Fields[k], acc.Metrics[i].Fields[k])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-19 03:08:52 +08:00
|
|
|
|
|
|
|
|
func TestDeploymentSelectorFilter(t *testing.T) {
|
|
|
|
|
cli := &client{}
|
|
|
|
|
now := time.Now()
|
|
|
|
|
now = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 1, 36, 0, now.Location())
|
|
|
|
|
|
|
|
|
|
responseMap := map[string]interface{}{
|
|
|
|
|
"/deployments/": &v1.DeploymentList{
|
2021-03-18 05:35:25 +08:00
|
|
|
Items: []v1.Deployment{
|
2020-06-19 03:08:52 +08:00
|
|
|
{
|
2021-03-18 05:35:25 +08:00
|
|
|
Status: v1.DeploymentStatus{
|
|
|
|
|
Replicas: 3,
|
|
|
|
|
AvailableReplicas: 1,
|
|
|
|
|
UnavailableReplicas: 4,
|
|
|
|
|
UpdatedReplicas: 2,
|
|
|
|
|
ObservedGeneration: 9121,
|
2020-06-19 03:08:52 +08:00
|
|
|
},
|
2021-03-18 05:35:25 +08:00
|
|
|
Spec: v1.DeploymentSpec{
|
|
|
|
|
Strategy: v1.DeploymentStrategy{
|
2020-06-19 03:08:52 +08:00
|
|
|
RollingUpdate: &v1.RollingUpdateDeployment{
|
|
|
|
|
MaxUnavailable: &intstr.IntOrString{
|
2021-03-18 05:35:25 +08:00
|
|
|
IntVal: 30,
|
2020-06-19 03:08:52 +08:00
|
|
|
},
|
|
|
|
|
MaxSurge: &intstr.IntOrString{
|
2021-03-18 05:35:25 +08:00
|
|
|
IntVal: 20,
|
2020-06-19 03:08:52 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Replicas: toInt32Ptr(4),
|
|
|
|
|
Selector: &metav1.LabelSelector{
|
|
|
|
|
MatchLabels: map[string]string{
|
|
|
|
|
"select1": "s1",
|
|
|
|
|
"select2": "s2",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-03-18 05:35:25 +08:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Generation: 11221,
|
|
|
|
|
Namespace: "ns1",
|
|
|
|
|
Name: "deploy1",
|
2020-06-19 03:08:52 +08:00
|
|
|
Labels: map[string]string{
|
|
|
|
|
"lab1": "v1",
|
|
|
|
|
"lab2": "v2",
|
|
|
|
|
},
|
2021-03-18 05:35:25 +08:00
|
|
|
CreationTimestamp: metav1.Time{Time: now},
|
2020-06-19 03:08:52 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
handler *mockHandler
|
|
|
|
|
hasError bool
|
|
|
|
|
include []string
|
|
|
|
|
exclude []string
|
|
|
|
|
expected map[string]string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "nil filters equals all selectors",
|
|
|
|
|
handler: &mockHandler{
|
|
|
|
|
responseMap: responseMap,
|
|
|
|
|
},
|
|
|
|
|
hasError: false,
|
|
|
|
|
include: nil,
|
|
|
|
|
exclude: nil,
|
|
|
|
|
expected: map[string]string{
|
|
|
|
|
"selector_select1": "s1",
|
|
|
|
|
"selector_select2": "s2",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "empty filters equals all selectors",
|
|
|
|
|
handler: &mockHandler{
|
|
|
|
|
responseMap: responseMap,
|
|
|
|
|
},
|
|
|
|
|
hasError: false,
|
|
|
|
|
include: []string{},
|
|
|
|
|
exclude: []string{},
|
|
|
|
|
expected: map[string]string{
|
|
|
|
|
"selector_select1": "s1",
|
|
|
|
|
"selector_select2": "s2",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "include filter equals only include-matched selectors",
|
|
|
|
|
handler: &mockHandler{
|
|
|
|
|
responseMap: responseMap,
|
|
|
|
|
},
|
|
|
|
|
hasError: false,
|
|
|
|
|
include: []string{"select1"},
|
|
|
|
|
exclude: []string{},
|
|
|
|
|
expected: map[string]string{
|
|
|
|
|
"selector_select1": "s1",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "exclude filter equals only non-excluded selectors (overrides include filter)",
|
|
|
|
|
handler: &mockHandler{
|
|
|
|
|
responseMap: responseMap,
|
|
|
|
|
},
|
|
|
|
|
hasError: false,
|
|
|
|
|
include: []string{},
|
|
|
|
|
exclude: []string{"select2"},
|
|
|
|
|
expected: map[string]string{
|
|
|
|
|
"selector_select1": "s1",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "include glob filter equals only include-matched selectors",
|
|
|
|
|
handler: &mockHandler{
|
|
|
|
|
responseMap: responseMap,
|
|
|
|
|
},
|
|
|
|
|
hasError: false,
|
|
|
|
|
include: []string{"*1"},
|
|
|
|
|
exclude: []string{},
|
|
|
|
|
expected: map[string]string{
|
|
|
|
|
"selector_select1": "s1",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "exclude glob filter equals only non-excluded selectors",
|
|
|
|
|
handler: &mockHandler{
|
|
|
|
|
responseMap: responseMap,
|
|
|
|
|
},
|
|
|
|
|
hasError: false,
|
|
|
|
|
include: []string{},
|
|
|
|
|
exclude: []string{"*2"},
|
|
|
|
|
expected: map[string]string{
|
|
|
|
|
"selector_select1": "s1",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "exclude glob filter equals only non-excluded selectors",
|
|
|
|
|
handler: &mockHandler{
|
|
|
|
|
responseMap: responseMap,
|
|
|
|
|
},
|
|
|
|
|
hasError: false,
|
|
|
|
|
include: []string{},
|
|
|
|
|
exclude: []string{"*2"},
|
|
|
|
|
expected: map[string]string{
|
|
|
|
|
"selector_select1": "s1",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
for _, v := range tests {
|
|
|
|
|
ks := &KubernetesInventory{
|
|
|
|
|
client: cli,
|
|
|
|
|
}
|
|
|
|
|
ks.SelectorInclude = v.include
|
|
|
|
|
ks.SelectorExclude = v.exclude
|
|
|
|
|
ks.createSelectorFilters()
|
|
|
|
|
acc := new(testutil.Accumulator)
|
|
|
|
|
for _, deployment := range ((v.handler.responseMap["/deployments/"]).(*v1.DeploymentList)).Items {
|
2021-03-23 01:21:36 +08:00
|
|
|
ks.gatherDeployment(deployment, acc)
|
2020-06-19 03:08:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Grab selector tags
|
|
|
|
|
actual := map[string]string{}
|
|
|
|
|
for _, metric := range acc.Metrics {
|
|
|
|
|
for key, val := range metric.Tags {
|
|
|
|
|
if strings.Contains(key, "selector_") {
|
|
|
|
|
actual[key] = val
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(v.expected, actual) {
|
|
|
|
|
t.Fatalf("actual selector tags (%v) do not match expected selector tags (%v)", actual, v.expected)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|