Skip to content

Commit

Permalink
[chore] add app.kubernetes.io/component label to monolithic objects (#…
Browse files Browse the repository at this point in the history
…788)

* also update Grafana operator package
* specify TypeMeta in ServiceMonitor

Signed-off-by: Andreas Gerstmayr <agerstmayr@redhat.com>
  • Loading branch information
andreasgerstmayr committed Feb 12, 2024
1 parent 5d01d0a commit 6612f3a
Show file tree
Hide file tree
Showing 20 changed files with 75 additions and 37 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions controllers/tempo/tempomonolithic_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion controllers/tempo/tempostack_create_or_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion internal/manifests/grafana/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion internal/manifests/grafana/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions internal/manifests/manifestutils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
4 changes: 2 additions & 2 deletions internal/manifests/monolithic/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
},
Expand Down
8 changes: 4 additions & 4 deletions internal/manifests/monolithic/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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,
},
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion internal/manifests/monolithic/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -24,7 +25,7 @@ func TestBuildTempoIngress(t *testing.T) {
},
},
}
labels := Labels("sample")
labels := ComponentLabels(manifestutils.TempoMonolithComponentName, "sample")

tests := []struct {
name string
Expand Down
15 changes: 13 additions & 2 deletions internal/manifests/monolithic/labels.go
Original file line number Diff line number Diff line change
@@ -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:<component> label.
func ComponentLabels(component, instanceName string) labels.Set {
return labels.Merge(CommonLabels(instanceName), map[string]string{
"app.kubernetes.io/component": component,
})
}
4 changes: 2 additions & 2 deletions internal/manifests/monolithic/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
Expand Down
3 changes: 2 additions & 1 deletion internal/manifests/monolithic/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions internal/manifests/monolithic/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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,
},
Expand Down Expand Up @@ -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),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/manifests/monolithic/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion internal/manifests/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion internal/manifests/naming/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
9 changes: 7 additions & 2 deletions internal/manifests/servicemonitor/servicemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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{{
Expand All @@ -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,
},
},
}
Expand Down
32 changes: 24 additions & 8 deletions internal/manifests/servicemonitor/servicemonitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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])
Expand All @@ -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",
Expand Down Expand Up @@ -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])
Expand All @@ -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",
Expand All @@ -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])
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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])
Expand Down

0 comments on commit 6612f3a

Please sign in to comment.