forked from grafana/tempo-operator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoperator_controller_test.go
120 lines (110 loc) · 3.19 KB
/
operator_controller_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
package controllers
import (
"context"
"testing"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
configv1alpha1 "github.com/grafana/tempo-operator/apis/config/v1alpha1"
"github.com/grafana/tempo-operator/internal/manifests/manifestutils"
)
func createOperatorDeployment(t *testing.T, nsn types.NamespacedName) {
labels := map[string]string{
"app.kubernetes.io/name": "tempo-operator",
}
tempo := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: nsn.Name,
Namespace: nsn.Namespace,
Labels: labels,
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: labels,
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "operator",
Image: "operator",
},
},
},
},
},
}
err := k8sClient.Create(context.Background(), tempo)
require.NoError(t, err)
}
func TestReconcileOperator(t *testing.T) {
nsn := types.NamespacedName{Name: "reconcile-operator", Namespace: "default"}
createOperatorDeployment(t, nsn)
reconciler := OperatorReconciler{
Client: k8sClient,
Scheme: testScheme,
}
err := reconciler.Reconcile(context.Background(), configv1alpha1.ProjectConfig{
Gates: configv1alpha1.FeatureGates{
PrometheusOperator: true,
Observability: configv1alpha1.ObservabilityFeatureGates{
Metrics: configv1alpha1.MetricsFeatureGates{
CreateServiceMonitors: true,
CreatePrometheusRules: true,
},
},
},
})
require.NoError(t, err)
// Check if objects of specific types were created and are managed by the operator
listOpts := []client.ListOption{
client.InNamespace(nsn.Namespace),
client.MatchingLabels(manifestutils.CommonOperatorLabels()),
}
{
list := &monitoringv1.ServiceMonitorList{}
err = k8sClient.List(context.Background(), list, listOpts...)
assert.NoError(t, err)
assert.Len(t, list.Items, 1)
}
{
list := &monitoringv1.PrometheusRuleList{}
err = k8sClient.List(context.Background(), list, listOpts...)
assert.NoError(t, err)
assert.Len(t, list.Items, 1)
}
// Update config and reconcile again
err = reconciler.Reconcile(context.Background(), configv1alpha1.ProjectConfig{
Gates: configv1alpha1.FeatureGates{
PrometheusOperator: true,
Observability: configv1alpha1.ObservabilityFeatureGates{
Metrics: configv1alpha1.MetricsFeatureGates{
CreateServiceMonitors: false,
CreatePrometheusRules: false,
},
},
},
})
require.NoError(t, err)
// Check if objects got pruned by the operator
{
list := &monitoringv1.ServiceMonitorList{}
err = k8sClient.List(context.Background(), list, listOpts...)
assert.NoError(t, err)
assert.Len(t, list.Items, 0)
}
{
list := &monitoringv1.PrometheusRuleList{}
err = k8sClient.List(context.Background(), list, listOpts...)
assert.NoError(t, err)
assert.Len(t, list.Items, 0)
}
}