From 6612f3a195ff824c15b80858c6c8aa716d3ffc9f Mon Sep 17 00:00:00 2001 From: Andreas Gerstmayr Date: Mon, 12 Feb 2024 13:27:34 +0100 Subject: [PATCH] [chore] add app.kubernetes.io/component label to monolithic objects (#788) * also update Grafana operator package * specify TypeMeta in ServiceMonitor Signed-off-by: Andreas Gerstmayr --- cmd/root.go | 2 +- .../tempo/tempomonolithic_controller.go | 4 +-- .../tempo/tempostack_create_or_update.go | 2 +- go.mod | 2 +- go.sum | 4 +-- internal/manifests/grafana/datasource.go | 2 +- internal/manifests/grafana/datasource_test.go | 2 +- internal/manifests/manifestutils/constants.go | 4 +++ internal/manifests/monolithic/configmap.go | 4 +-- internal/manifests/monolithic/ingress.go | 8 ++--- internal/manifests/monolithic/ingress_test.go | 3 +- internal/manifests/monolithic/labels.go | 15 +++++++-- internal/manifests/monolithic/service.go | 4 +-- internal/manifests/monolithic/service_test.go | 3 +- internal/manifests/monolithic/statefulset.go | 6 ++-- .../manifests/monolithic/statefulset_test.go | 2 +- internal/manifests/mutate.go | 2 +- internal/manifests/naming/naming.go | 2 +- .../servicemonitor/servicemonitor.go | 9 ++++-- .../servicemonitor/servicemonitor_test.go | 32 ++++++++++++++----- 20 files changed, 75 insertions(+), 37 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 5e42a9cc1..c664d68a6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,7 +5,7 @@ import ( "fmt" "os" - grafanav1 "github.com/grafana-operator/grafana-operator/v5/api/v1beta1" + grafanav1 "github.com/grafana/grafana-operator/v5/api/v1beta1" configv1 "github.com/openshift/api/config/v1" openshiftoperatorv1 "github.com/openshift/api/operator/v1" routev1 "github.com/openshift/api/route/v1" diff --git a/controllers/tempo/tempomonolithic_controller.go b/controllers/tempo/tempomonolithic_controller.go index a0012218f..0983e5370 100644 --- a/controllers/tempo/tempomonolithic_controller.go +++ b/controllers/tempo/tempomonolithic_controller.go @@ -4,7 +4,7 @@ import ( "context" "fmt" - grafanav1 "github.com/grafana-operator/grafana-operator/v5/api/v1beta1" + grafanav1 "github.com/grafana/grafana-operator/v5/api/v1beta1" routev1 "github.com/openshift/api/route/v1" monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" appsv1 "k8s.io/api/apps/v1" @@ -106,7 +106,7 @@ func (r *TempoMonolithicReconciler) getOwnedObjects(ctx context.Context, tempo v ownedObjects := map[types.UID]client.Object{} listOps := &client.ListOptions{ Namespace: tempo.GetNamespace(), - LabelSelector: labels.SelectorFromSet(monolithic.Labels(tempo.Name)), + LabelSelector: labels.SelectorFromSet(monolithic.CommonLabels(tempo.Name)), } // Add all resources where the operator can conditionally create an object. diff --git a/controllers/tempo/tempostack_create_or_update.go b/controllers/tempo/tempostack_create_or_update.go index 646391968..a190f72ee 100644 --- a/controllers/tempo/tempostack_create_or_update.go +++ b/controllers/tempo/tempostack_create_or_update.go @@ -5,7 +5,7 @@ import ( "fmt" "github.com/go-logr/logr" - grafanav1 "github.com/grafana-operator/grafana-operator/v5/api/v1beta1" + grafanav1 "github.com/grafana/grafana-operator/v5/api/v1beta1" routev1 "github.com/openshift/api/route/v1" monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" networkingv1 "k8s.io/api/networking/v1" diff --git a/go.mod b/go.mod index faf9f57d0..1749d648b 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/go-logr/logr v1.4.1 github.com/go-logr/zapr v1.3.0 github.com/google/go-cmp v0.6.0 - github.com/grafana-operator/grafana-operator/v5 v5.5.2 + github.com/grafana/grafana-operator/v5 v5.6.0 github.com/imdario/mergo v0.3.16 github.com/novln/docker-parser v1.0.0 github.com/onsi/ginkgo/v2 v2.15.0 diff --git a/go.sum b/go.sum index 593361a36..51525bb30 100644 --- a/go.sum +++ b/go.sum @@ -54,8 +54,8 @@ github.com/google/pprof v0.0.0-20230510103437-eeec1cb781c3 h1:2XF1Vzq06X+inNqgJ9 github.com/google/pprof v0.0.0-20230510103437-eeec1cb781c3/go.mod h1:79YE0hCXdHag9sBkw2o+N/YnZtTkXi0UT9Nnixa5eYk= github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/grafana-operator/grafana-operator/v5 v5.5.2 h1:jGjNn1VhxX96FK8NTaTuHfzpIENAwbY6iYCSDtYuxI8= -github.com/grafana-operator/grafana-operator/v5 v5.5.2/go.mod h1:eO0pkUx77TofCzzYNVLNFvm04de123ox+gljuZzqS+4= +github.com/grafana/grafana-operator/v5 v5.6.0 h1:B7i1kA82i59vdyG4s5RZY4Qo9nY+7F/SVGoGWa0TWeE= +github.com/grafana/grafana-operator/v5 v5.6.0/go.mod h1:HCcL2coITTaefbixzafoeRU4URj/Z2EkBgFoyF3s9fs= github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4= github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= diff --git a/internal/manifests/grafana/datasource.go b/internal/manifests/grafana/datasource.go index 4817823a2..83e7c1165 100644 --- a/internal/manifests/grafana/datasource.go +++ b/internal/manifests/grafana/datasource.go @@ -4,7 +4,7 @@ import ( "encoding/json" "fmt" - grafanav1 "github.com/grafana-operator/grafana-operator/v5/api/v1beta1" + grafanav1 "github.com/grafana/grafana-operator/v5/api/v1beta1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/grafana/tempo-operator/internal/manifests/manifestutils" diff --git a/internal/manifests/grafana/datasource_test.go b/internal/manifests/grafana/datasource_test.go index b5ba6733b..9dfe70a02 100644 --- a/internal/manifests/grafana/datasource_test.go +++ b/internal/manifests/grafana/datasource_test.go @@ -5,7 +5,7 @@ import ( "fmt" "testing" - grafanav1 "github.com/grafana-operator/grafana-operator/v5/api/v1beta1" + grafanav1 "github.com/grafana/grafana-operator/v5/api/v1beta1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" diff --git a/internal/manifests/manifestutils/constants.go b/internal/manifests/manifestutils/constants.go index 725c4d198..4e54819c7 100644 --- a/internal/manifests/manifestutils/constants.go +++ b/internal/manifests/manifestutils/constants.go @@ -104,6 +104,10 @@ const ( IngesterComponentName = "ingester" // GatewayComponentName declares the internal name of the gateway component. GatewayComponentName = "gateway" + + // TempoMonolithComponentName declares the internal name of the Tempo Monolith component. + TempoMonolithComponentName = "tempo" + // TenantHeader is the header name that contains tenant name. TenantHeader = "x-scope-orgid" diff --git a/internal/manifests/monolithic/configmap.go b/internal/manifests/monolithic/configmap.go index 39812528e..6da624bf0 100644 --- a/internal/manifests/monolithic/configmap.go +++ b/internal/manifests/monolithic/configmap.go @@ -88,7 +88,7 @@ type tempoQueryConfig struct { // BuildConfigMap creates the Tempo ConfigMap for a monolithic deployment. func BuildConfigMap(opts Options) (*corev1.ConfigMap, string, error) { tempo := opts.Tempo - labels := Labels(tempo.Name) + labels := ComponentLabels(manifestutils.TempoMonolithComponentName, tempo.Name) tempoConfig, err := buildTempoConfig(opts) if err != nil { @@ -101,7 +101,7 @@ func BuildConfigMap(opts Options) (*corev1.ConfigMap, string, error) { Kind: "ConfigMap", }, ObjectMeta: metav1.ObjectMeta{ - Name: naming.Name("", tempo.Name), + Name: naming.Name(manifestutils.TempoMonolithComponentName, tempo.Name), Namespace: tempo.Namespace, Labels: labels, }, diff --git a/internal/manifests/monolithic/ingress.go b/internal/manifests/monolithic/ingress.go index 6455ef024..ef7e59eb1 100644 --- a/internal/manifests/monolithic/ingress.go +++ b/internal/manifests/monolithic/ingress.go @@ -38,7 +38,7 @@ func BuildTempoIngress(opts Options) ([]client.Object, error) { func buildJaegerUIIngress(opts Options) *networkingv1.Ingress { tempo := opts.Tempo - labels := Labels(tempo.Name) + labels := ComponentLabels(manifestutils.TempoMonolithComponentName, tempo.Name) ingress := &networkingv1.Ingress{ TypeMeta: metav1.TypeMeta{ APIVersion: networkingv1.SchemeGroupVersion.String(), @@ -57,7 +57,7 @@ func buildJaegerUIIngress(opts Options) *networkingv1.Ingress { backend := networkingv1.IngressBackend{ Service: &networkingv1.IngressServiceBackend{ - Name: naming.Name("", tempo.Name), + Name: naming.Name(manifestutils.TempoMonolithComponentName, tempo.Name), Port: networkingv1.ServiceBackendPort{ Name: manifestutils.JaegerUIPortName, }, @@ -90,7 +90,7 @@ func buildJaegerUIIngress(opts Options) *networkingv1.Ingress { func buildJaegerUIRoute(opts Options) (*routev1.Route, error) { tempo := opts.Tempo - labels := Labels(tempo.Name) + labels := ComponentLabels(manifestutils.TempoMonolithComponentName, tempo.Name) var tlsCfg *routev1.TLSConfig switch tempo.Spec.JaegerUI.Route.Termination { @@ -121,7 +121,7 @@ func buildJaegerUIRoute(opts Options) (*routev1.Route, error) { Host: tempo.Spec.JaegerUI.Route.Host, To: routev1.RouteTargetReference{ Kind: "Service", - Name: naming.Name("", tempo.Name), + Name: naming.Name(manifestutils.TempoMonolithComponentName, tempo.Name), }, Port: &routev1.RoutePort{ TargetPort: intstr.FromString(manifestutils.JaegerUIPortName), diff --git a/internal/manifests/monolithic/ingress_test.go b/internal/manifests/monolithic/ingress_test.go index eaaae8789..03bcd6bbe 100644 --- a/internal/manifests/monolithic/ingress_test.go +++ b/internal/manifests/monolithic/ingress_test.go @@ -13,6 +13,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "github.com/grafana/tempo-operator/apis/tempo/v1alpha1" + "github.com/grafana/tempo-operator/internal/manifests/manifestutils" ) func TestBuildTempoIngress(t *testing.T) { @@ -24,7 +25,7 @@ func TestBuildTempoIngress(t *testing.T) { }, }, } - labels := Labels("sample") + labels := ComponentLabels(manifestutils.TempoMonolithComponentName, "sample") tests := []struct { name string diff --git a/internal/manifests/monolithic/labels.go b/internal/manifests/monolithic/labels.go index 9469ba3ef..5c79ff68a 100644 --- a/internal/manifests/monolithic/labels.go +++ b/internal/manifests/monolithic/labels.go @@ -1,10 +1,21 @@ package monolithic -// Labels returns common labels for each TempoMonolithic object created by the operator. -func Labels(instanceName string) map[string]string { +import ( + "k8s.io/apimachinery/pkg/labels" +) + +// CommonLabels returns common labels for each TempoMonolithic object created by the operator. +func CommonLabels(instanceName string) map[string]string { return map[string]string{ "app.kubernetes.io/name": "tempo-monolithic", "app.kubernetes.io/instance": instanceName, "app.kubernetes.io/managed-by": "tempo-operator", } } + +// ComponentLabels is a list of all commonLabels including the app.kubernetes.io/component: label. +func ComponentLabels(component, instanceName string) labels.Set { + return labels.Merge(CommonLabels(instanceName), map[string]string{ + "app.kubernetes.io/component": component, + }) +} diff --git a/internal/manifests/monolithic/service.go b/internal/manifests/monolithic/service.go index 6cdeda695..bd0a37fa8 100644 --- a/internal/manifests/monolithic/service.go +++ b/internal/manifests/monolithic/service.go @@ -13,7 +13,7 @@ import ( // BuildTempoService creates the service for a monolithic deployment. func BuildTempoService(opts Options) *corev1.Service { tempo := opts.Tempo - labels := Labels(tempo.Name) + labels := ComponentLabels(manifestutils.TempoMonolithComponentName, tempo.Name) ports := []corev1.ServicePort{ { Name: manifestutils.HttpPortName, @@ -69,7 +69,7 @@ func BuildTempoService(opts Options) *corev1.Service { Kind: "Service", }, ObjectMeta: metav1.ObjectMeta{ - Name: naming.Name("", tempo.Name), + Name: naming.Name(manifestutils.TempoMonolithComponentName, tempo.Name), Namespace: tempo.Namespace, Labels: labels, }, diff --git a/internal/manifests/monolithic/service_test.go b/internal/manifests/monolithic/service_test.go index 9e2bbfe7a..63302c258 100644 --- a/internal/manifests/monolithic/service_test.go +++ b/internal/manifests/monolithic/service_test.go @@ -9,6 +9,7 @@ import ( "k8s.io/apimachinery/pkg/util/intstr" "github.com/grafana/tempo-operator/apis/tempo/v1alpha1" + "github.com/grafana/tempo-operator/internal/manifests/manifestutils" ) func TestBuildTempoService(t *testing.T) { @@ -123,7 +124,7 @@ func TestBuildTempoService(t *testing.T) { }, } - labels := Labels("sample") + labels := ComponentLabels(manifestutils.TempoMonolithComponentName, "sample") for _, test := range tests { t.Run(test.name, func(t *testing.T) { opts.Tempo.Spec = test.input diff --git a/internal/manifests/monolithic/statefulset.go b/internal/manifests/monolithic/statefulset.go index 55a7ddd93..7809fc0c2 100644 --- a/internal/manifests/monolithic/statefulset.go +++ b/internal/manifests/monolithic/statefulset.go @@ -23,7 +23,7 @@ var ( // BuildTempoStatefulset creates the Tempo statefulset for a monolithic deployment. func BuildTempoStatefulset(opts Options) (*appsv1.StatefulSet, error) { tempo := opts.Tempo - labels := Labels(tempo.Name) + labels := ComponentLabels(manifestutils.TempoMonolithComponentName, tempo.Name) annotations := manifestutils.CommonAnnotations(opts.ConfigChecksum) sts := &appsv1.StatefulSet{ @@ -32,7 +32,7 @@ func BuildTempoStatefulset(opts Options) (*appsv1.StatefulSet, error) { Kind: "StatefulSet", }, ObjectMeta: metav1.ObjectMeta{ - Name: naming.Name("", tempo.Name), + Name: naming.Name(manifestutils.TempoMonolithComponentName, tempo.Name), Namespace: tempo.Namespace, Labels: labels, }, @@ -84,7 +84,7 @@ func BuildTempoStatefulset(opts Options) (*appsv1.StatefulSet, error) { VolumeSource: corev1.VolumeSource{ ConfigMap: &corev1.ConfigMapVolumeSource{ LocalObjectReference: corev1.LocalObjectReference{ - Name: naming.Name("", tempo.Name), + Name: naming.Name(manifestutils.TempoMonolithComponentName, tempo.Name), }, }, }, diff --git a/internal/manifests/monolithic/statefulset_test.go b/internal/manifests/monolithic/statefulset_test.go index 5ca18be14..89ad8b7d1 100644 --- a/internal/manifests/monolithic/statefulset_test.go +++ b/internal/manifests/monolithic/statefulset_test.go @@ -57,7 +57,7 @@ func TestStatefulsetMemoryStorage(t *testing.T) { sts, err := BuildTempoStatefulset(opts) require.NoError(t, err) - labels := Labels("sample") + labels := ComponentLabels(manifestutils.TempoMonolithComponentName, "sample") annotations := manifestutils.CommonAnnotations("") require.Equal(t, &appsv1.StatefulSet{ TypeMeta: metav1.TypeMeta{ diff --git a/internal/manifests/mutate.go b/internal/manifests/mutate.go index 029a4f120..155db8255 100644 --- a/internal/manifests/mutate.go +++ b/internal/manifests/mutate.go @@ -6,7 +6,7 @@ import ( "github.com/ViaQ/logerr/v2/kverrors" "github.com/google/go-cmp/cmp" - grafanav1 "github.com/grafana-operator/grafana-operator/v5/api/v1beta1" + grafanav1 "github.com/grafana/grafana-operator/v5/api/v1beta1" "github.com/imdario/mergo" routev1 "github.com/openshift/api/route/v1" monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" diff --git a/internal/manifests/naming/naming.go b/internal/manifests/naming/naming.go index 82787ec44..b6d29b65e 100644 --- a/internal/manifests/naming/naming.go +++ b/internal/manifests/naming/naming.go @@ -7,7 +7,7 @@ import ( // Name returns the manifest name of a component. // Example: tempo-simplest-compactor. func Name(component string, tempoStackName string) string { - if component == "" { + if component == "" || component == "tempo" { return DNSName(fmt.Sprintf("tempo-%s", tempoStackName)) } return DNSName(fmt.Sprintf("tempo-%s-%s", tempoStackName, component)) diff --git a/internal/manifests/servicemonitor/servicemonitor.go b/internal/manifests/servicemonitor/servicemonitor.go index 7e0cf1e67..aec322806 100644 --- a/internal/manifests/servicemonitor/servicemonitor.go +++ b/internal/manifests/servicemonitor/servicemonitor.go @@ -34,6 +34,7 @@ func BuildServiceMonitors(params manifestutils.Params) []client.Object { func buildServiceMonitor(params manifestutils.Params, component string, port string) *monitoringv1.ServiceMonitor { tempo := params.Tempo + labels := manifestutils.ComponentLabels(component, tempo.Name) scheme := "http" var tlsConfig *monitoringv1.TLSConfig @@ -72,10 +73,14 @@ func buildServiceMonitor(params manifestutils.Params, component string, port str } return &monitoringv1.ServiceMonitor{ + TypeMeta: metav1.TypeMeta{ + APIVersion: monitoringv1.SchemeGroupVersion.String(), + Kind: monitoringv1.ServiceMonitorsKind, + }, ObjectMeta: metav1.ObjectMeta{ Namespace: tempo.Namespace, Name: naming.Name(component, tempo.Name), - Labels: manifestutils.CommonLabels(tempo.Name), + Labels: labels, }, Spec: monitoringv1.ServiceMonitorSpec{ Endpoints: []monitoringv1.Endpoint{{ @@ -101,7 +106,7 @@ func buildServiceMonitor(params manifestutils.Params, component string, port str MatchNames: []string{tempo.Namespace}, }, Selector: metav1.LabelSelector{ - MatchLabels: manifestutils.ComponentLabels(component, tempo.Name), + MatchLabels: labels, }, }, } diff --git a/internal/manifests/servicemonitor/servicemonitor_test.go b/internal/manifests/servicemonitor/servicemonitor_test.go index 74526048a..596c4e1eb 100644 --- a/internal/manifests/servicemonitor/servicemonitor_test.go +++ b/internal/manifests/servicemonitor/servicemonitor_test.go @@ -23,9 +23,13 @@ func TestBuildServiceMonitors(t *testing.T) { Spec: v1alpha1.TempoStackSpec{}, }}) - labels := manifestutils.CommonLabels("test") + labels := manifestutils.ComponentLabels(manifestutils.CompactorComponentName, "test") assert.Len(t, objects, 5) assert.Equal(t, &monitoringv1.ServiceMonitor{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "monitoring.coreos.com/v1", + Kind: "ServiceMonitor", + }, ObjectMeta: metav1.ObjectMeta{ Name: "tempo-test-compactor", Namespace: "project1", @@ -52,7 +56,7 @@ func TestBuildServiceMonitors(t *testing.T) { MatchNames: []string{"project1"}, }, Selector: metav1.LabelSelector{ - MatchLabels: manifestutils.ComponentLabels(manifestutils.CompactorComponentName, "test"), + MatchLabels: labels, }, }, }, objects[0]) @@ -74,9 +78,13 @@ func TestBuildServiceMonitorsTLS(t *testing.T) { }, }) - labels := manifestutils.CommonLabels("test") + labels := manifestutils.ComponentLabels(manifestutils.CompactorComponentName, "test") assert.Len(t, objects, 5) assert.Equal(t, &monitoringv1.ServiceMonitor{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "monitoring.coreos.com/v1", + Kind: "ServiceMonitor", + }, ObjectMeta: metav1.ObjectMeta{ Name: "tempo-test-compactor", Namespace: "project1", @@ -130,7 +138,7 @@ func TestBuildServiceMonitorsTLS(t *testing.T) { MatchNames: []string{"project1"}, }, Selector: metav1.LabelSelector{ - MatchLabels: manifestutils.ComponentLabels(manifestutils.CompactorComponentName, "test"), + MatchLabels: labels, }, }, }, objects[0]) @@ -151,9 +159,13 @@ func TestBuildGatewayServiceMonitor(t *testing.T) { }, }}) - labels := manifestutils.CommonLabels("test") + labels := manifestutils.ComponentLabels(manifestutils.GatewayComponentName, "test") assert.Len(t, objects, 6) assert.Equal(t, &monitoringv1.ServiceMonitor{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "monitoring.coreos.com/v1", + Kind: "ServiceMonitor", + }, ObjectMeta: metav1.ObjectMeta{ Name: "tempo-test-gateway", Namespace: "project1", @@ -180,7 +192,7 @@ func TestBuildGatewayServiceMonitor(t *testing.T) { MatchNames: []string{"project1"}, }, Selector: metav1.LabelSelector{ - MatchLabels: manifestutils.ComponentLabels(manifestutils.GatewayComponentName, "test"), + MatchLabels: labels, }, }, }, objects[5]) @@ -211,9 +223,13 @@ func TestBuildGatewayServiceMonitorsTLS(t *testing.T) { }, }) - labels := manifestutils.CommonLabels("test") + labels := manifestutils.ComponentLabels(manifestutils.GatewayComponentName, "test") assert.Len(t, objects, 6) assert.Equal(t, &monitoringv1.ServiceMonitor{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "monitoring.coreos.com/v1", + Kind: "ServiceMonitor", + }, ObjectMeta: metav1.ObjectMeta{ Name: "tempo-test-gateway", Namespace: "project1", @@ -267,7 +283,7 @@ func TestBuildGatewayServiceMonitorsTLS(t *testing.T) { MatchNames: []string{"project1"}, }, Selector: metav1.LabelSelector{ - MatchLabels: manifestutils.ComponentLabels(manifestutils.GatewayComponentName, "test"), + MatchLabels: labels, }, }, }, objects[5])