Skip to content

Commit 7c78722

Browse files
Service monitor scrap tempo metrics for monolithic (#1274)
* Service monitor scrap tempo metrics for monolithic Signed-off-by: Ruben Vargas <ruben.vp8510@gmail.com> * Add mTLS to monolithic tempo Signed-off-by: Ruben Vargas <ruben.vp8510@gmail.com> * more work on cert rotation Signed-off-by: Ruben Vargas <ruben.vp8510@gmail.com> * secure gateway with mtls Signed-off-by: Ruben Vargas <ruben.vp8510@gmail.com> * fix tests Signed-off-by: Ruben Vargas <ruben.vp8510@gmail.com> * Update .chloggen/fix_monolithic_metrics.yaml Co-authored-by: Andreas Gerstmayr <andreas@gerstmayr.me> * fix service monitor for monolithic with mtls Signed-off-by: Ruben Vargas <ruben.vp8510@gmail.com> * fix test Signed-off-by: Ruben Vargas <ruben.vp8510@gmail.com> * address comments Signed-off-by: Ruben Vargas <ruben.vp8510@gmail.com> * fix openshift tests Signed-off-by: Ruben Vargas <ruben.vp8510@gmail.com> --------- Signed-off-by: Ruben Vargas <ruben.vp8510@gmail.com> Co-authored-by: Andreas Gerstmayr <andreas@gerstmayr.me>
1 parent e36a8e0 commit 7c78722

28 files changed

+564
-124
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: bug_fix
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. tempostack, tempomonolithic, github action)
5+
component: tempomonolithic
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Scrape tempo metrics for monolithic.
9+
10+
# One or more tracking issues related to the change
11+
issues: [1275]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

cmd/start/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ func start(c *cobra.Command, args []string) {
6060
setupLog.Error(err, "unable to create controller", "controller", "certrotation")
6161
os.Exit(1)
6262
}
63+
64+
if err = (&controllers.CertRotationMonolithicReconciler{
65+
Client: mgr.GetClient(),
66+
Scheme: mgr.GetScheme(),
67+
FeatureGates: ctrlConfig.Gates,
68+
}).SetupWithManager(mgr); err != nil {
69+
setupLog.Error(err, "unable to create controller", "controller", "certrotationmonolithic")
70+
os.Exit(1)
71+
}
6372
}
6473

6574
if err = (&controllers.TempoStackReconciler{

internal/certrotation/build.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var defaultUserInfo = &user.DefaultInfo{Name: "system:tempostacks", Groups: []st
1414

1515
// BuildAll builds all secrets and configmaps containing
1616
// CA certificates, CA bundles and client certificates for
17-
// a TempoStack.
17+
// a Tempo CR.
1818
func BuildAll(opts Options) ([]client.Object, error) {
1919
res := make([]client.Object, 0)
2020

@@ -40,7 +40,7 @@ func BuildAll(opts Options) ([]client.Object, error) {
4040
}
4141

4242
// ApplyDefaultSettings merges the default options with the ones we give.
43-
func ApplyDefaultSettings(opts *Options, cfg configv1alpha1.BuiltInCertManagement) error {
43+
func ApplyDefaultSettings(opts *Options, cfg configv1alpha1.BuiltInCertManagement, components map[string]string) error {
4444
rotation, err := ParseRotation(cfg)
4545
if err != nil {
4646
return err
@@ -55,11 +55,12 @@ func ApplyDefaultSettings(opts *Options, cfg configv1alpha1.BuiltInCertManagemen
5555
if opts.Certificates == nil {
5656
opts.Certificates = make(map[string]SelfSignedCertKey)
5757
}
58-
for service, name := range ComponentCertSecretNames(opts.StackName) {
58+
for service, name := range components {
5959
r := certificateRotation{
6060
Clock: clock,
6161
UserInfo: defaultUserInfo,
6262
Hostnames: []string{
63+
"localhost",
6364
fmt.Sprintf("%s.%s.svc.cluster.local", service, opts.StackNamespace),
6465
fmt.Sprintf("%s.%s.svc", service, opts.StackNamespace),
6566
},

internal/certrotation/build_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestBuildAll(t *testing.T) {
2929
StackName: "dev",
3030
StackNamespace: "ns",
3131
}
32-
err := ApplyDefaultSettings(&opts, cfg)
32+
err := ApplyDefaultSettings(&opts, cfg, TempoStackComponentCertSecretNames(opts.StackName))
3333
require.NoError(t, err)
3434

3535
objs, err := BuildAll(opts)
@@ -69,17 +69,18 @@ func TestApplyDefaultSettings_EmptySecrets(t *testing.T) {
6969
StackNamespace: "ns",
7070
}
7171

72-
err := ApplyDefaultSettings(&opts, cfg)
72+
err := ApplyDefaultSettings(&opts, cfg, TempoStackComponentCertSecretNames(opts.StackName))
7373
require.NoError(t, err)
7474

75-
cs := ComponentCertSecretNames(opts.StackName)
75+
cs := TempoStackComponentCertSecretNames(opts.StackName)
7676

7777
for service, name := range cs {
7878
cert, ok := opts.Certificates[name]
7979
require.True(t, ok)
8080
require.NotEmpty(t, cert.Rotation)
8181

8282
hostnames := []string{
83+
"localhost",
8384
fmt.Sprintf("%s.%s.svc.cluster.local", service, opts.StackNamespace),
8485
fmt.Sprintf("%s.%s.svc", service, opts.StackNamespace),
8586
}
@@ -114,7 +115,7 @@ func TestApplyDefaultSettings_ExistingSecrets(t *testing.T) {
114115
Certificates: ComponentCertificates{},
115116
}
116117

117-
cs := ComponentCertSecretNames(opts.StackName)
118+
cs := TempoStackComponentCertSecretNames(opts.StackName)
118119

119120
for _, name := range cs {
120121
opts.Certificates[name] = SelfSignedCertKey{
@@ -131,7 +132,7 @@ func TestApplyDefaultSettings_ExistingSecrets(t *testing.T) {
131132
}
132133
}
133134

134-
err := ApplyDefaultSettings(&opts, cfg)
135+
err := ApplyDefaultSettings(&opts, cfg, TempoStackComponentCertSecretNames(opts.StackName))
135136
require.NoError(t, err)
136137

137138
for service, name := range cs {
@@ -140,6 +141,7 @@ func TestApplyDefaultSettings_ExistingSecrets(t *testing.T) {
140141
require.NotEmpty(t, cert.Rotation)
141142

142143
hostnames := []string{
144+
"localhost",
143145
fmt.Sprintf("%s.%s.svc.cluster.local", service, opts.StackNamespace),
144146
fmt.Sprintf("%s.%s.svc", service, opts.StackNamespace),
145147
}

internal/certrotation/handlers/certrotation_discovery.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414

1515
const certRotationRequiredAtKey = "tempo.grafana.com/certRotationRequiredAt"
1616

17-
// AnnotateForRequiredCertRotation adds/updates the `tempo.grafana.com/certRotationRequiredAt` annotation
17+
// AnnotateTempoStackForRequiredCertRotation adds/updates the `tempo.grafana.com/certRotationRequiredAt` annotation
1818
// to the named TempoStack if any of the managed client/serving/ca certificates expired. If no TempoStack
1919
// is found, then skip reconciliation.
20-
func AnnotateForRequiredCertRotation(ctx context.Context, k client.Client, name, namespace string) error {
20+
func AnnotateTempoStackForRequiredCertRotation(ctx context.Context, k client.Client, name, namespace string) error {
2121
var s v1alpha1.TempoStack
2222
key := client.ObjectKey{Name: name, Namespace: namespace}
2323

@@ -43,3 +43,33 @@ func AnnotateForRequiredCertRotation(ctx context.Context, k client.Client, name,
4343

4444
return nil
4545
}
46+
47+
// AnnotateMonolithicForRequiredCertRotation adds/updates the `tempo.grafana.com/certRotationRequiredAt` annotation
48+
// to the named TempoStack if any of the managed client/serving/ca certificates expired. If no TempoStack
49+
// is found, then skip reconciliation.
50+
func AnnotateMonolithicForRequiredCertRotation(ctx context.Context, k client.Client, name, namespace string) error {
51+
var s v1alpha1.TempoMonolithic
52+
key := client.ObjectKey{Name: name, Namespace: namespace}
53+
54+
if err := k.Get(ctx, key, &s); err != nil {
55+
if apierrors.IsNotFound(err) {
56+
// Do nothing
57+
return nil
58+
}
59+
60+
return kverrors.Wrap(err, "failed to get tempo TempoStack", "key", key)
61+
}
62+
63+
ss := s.DeepCopy()
64+
if ss.Annotations == nil {
65+
ss.Annotations = make(map[string]string)
66+
}
67+
68+
ss.Annotations[certRotationRequiredAtKey] = time.Now().UTC().Format(time.RFC3339)
69+
70+
if err := k.Update(ctx, ss); err != nil {
71+
return kverrors.Wrap(err, fmt.Sprintf("failed to update tempo TempoStack `%s` annotation", certRotationRequiredAtKey), "key", key)
72+
}
73+
74+
return nil
75+
}

internal/certrotation/handlers/check_cert_expiry.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,26 @@ import (
55

66
"github.com/ViaQ/logerr/v2/kverrors"
77
"github.com/go-logr/logr"
8-
apierrors "k8s.io/apimachinery/pkg/api/errors"
98
ctrl "sigs.k8s.io/controller-runtime"
109
"sigs.k8s.io/controller-runtime/pkg/client"
1110

1211
configv1alpha1 "github.com/grafana/tempo-operator/api/config/v1alpha1"
13-
v1alpha1 "github.com/grafana/tempo-operator/api/tempo/v1alpha1"
1412
"github.com/grafana/tempo-operator/internal/certrotation"
1513
)
1614

1715
// CheckCertExpiry handles the case if the TempoStack managed signing CA, client and/or serving
1816
// certificates expired. Returns true if any of those expired and an error representing the reason
1917
// of expiry.
20-
func CheckCertExpiry(ctx context.Context, log logr.Logger, req ctrl.Request, k client.Client, fg configv1alpha1.FeatureGates) error {
21-
ll := log.WithValues("tempostacks", req.String(), "event", "checkCertExpiry")
22-
23-
var stack v1alpha1.TempoStack
24-
if err := k.Get(ctx, req.NamespacedName, &stack); err != nil {
25-
if apierrors.IsNotFound(err) {
26-
// maybe the user deleted it before we could react? Either way this isn't an issue
27-
ll.Error(err, "could not find the requested tempo tempostacks", "name", req.String())
28-
return nil
29-
}
30-
return kverrors.Wrap(err, "failed to lookup tempostacks", "name", req.String())
31-
}
18+
func CheckCertExpiry(controllerName string, ctx context.Context, log logr.Logger, req ctrl.Request, k client.Client,
19+
fg configv1alpha1.FeatureGates, components map[string]string) error {
20+
ll := log.WithValues(controllerName, req.String(), "event", "checkCertExpiry")
3221

33-
opts, err := GetOptions(ctx, k, req)
22+
opts, err := GetOptions(ctx, k, req, components)
3423
if err != nil {
3524
return kverrors.Wrap(err, "failed to lookup certificates secrets", "name", req.String())
3625
}
3726

38-
if optErr := certrotation.ApplyDefaultSettings(&opts, fg.BuiltInCertManagement); optErr != nil {
27+
if optErr := certrotation.ApplyDefaultSettings(&opts, fg.BuiltInCertManagement, components); optErr != nil {
3928
ll.Error(optErr, "failed to conform options to build settings")
4029
return optErr
4130
}

internal/certrotation/handlers/options.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
// GetOptions return a certrotation options struct filled with all found client and serving certificate secrets if any found.
1616
// Return an error only if either the k8s client returns any other error except IsNotFound or if merging options fails.
17-
func GetOptions(ctx context.Context, k client.Client, req ctrl.Request) (certrotation.Options, error) {
17+
func GetOptions(ctx context.Context, k client.Client, req ctrl.Request, cs map[string]string) (certrotation.Options, error) {
1818
name := certrotation.SigningCASecretName(req.Name)
1919
ca, err := getSecret(ctx, k, name, req.Namespace)
2020
if err != nil {
@@ -31,7 +31,7 @@ func GetOptions(ctx context.Context, k client.Client, req ctrl.Request) (certrot
3131
}
3232
}
3333

34-
certs, err := getCertificateOptions(ctx, k, req)
34+
certs, err := getCertificateOptions(ctx, k, req, cs)
3535
if err != nil {
3636
return certrotation.Options{}, err
3737
}
@@ -47,8 +47,7 @@ func GetOptions(ctx context.Context, k client.Client, req ctrl.Request) (certrot
4747
}, nil
4848
}
4949

50-
func getCertificateOptions(ctx context.Context, k client.Client, req ctrl.Request) (certrotation.ComponentCertificates, error) {
51-
cs := certrotation.ComponentCertSecretNames(req.Name)
50+
func getCertificateOptions(ctx context.Context, k client.Client, req ctrl.Request, cs map[string]string) (certrotation.ComponentCertificates, error) {
5251
certs := make(certrotation.ComponentCertificates, len(cs))
5352

5453
for _, name := range cs {

internal/certrotation/handlers/rotate_certs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
// including the signing CA and a ca bundle or else returns an error. It returns only a degrade-condition-worthy
2424
// error if building the manifests fails for any reason.
2525
func CreateOrRotateCertificates(ctx context.Context, log logr.Logger,
26-
req ctrl.Request, k client.Client, s *runtime.Scheme, fg configv1alpha1.FeatureGates) error {
26+
req ctrl.Request, k client.Client, s *runtime.Scheme, fg configv1alpha1.FeatureGates, cs map[string]string) error {
2727
ll := log.WithValues("tempostacks", req.String(), "event", "createOrRotateCerts")
2828
var stack v1alpha1.TempoStack
2929
if err := k.Get(ctx, req.NamespacedName, &stack); err != nil {
@@ -35,12 +35,12 @@ func CreateOrRotateCertificates(ctx context.Context, log logr.Logger,
3535
return kverrors.Wrap(err, "failed to lookup tempostacks", "name", req.String())
3636
}
3737

38-
opts, err := GetOptions(ctx, k, req)
38+
opts, err := GetOptions(ctx, k, req, cs)
3939
if err != nil {
4040
return kverrors.Wrap(err, "failed to lookup certificates secrets", "name", req.String())
4141
}
4242

43-
if optErr := certrotation.ApplyDefaultSettings(&opts, fg.BuiltInCertManagement); optErr != nil {
43+
if optErr := certrotation.ApplyDefaultSettings(&opts, fg.BuiltInCertManagement, certrotation.TempoStackComponentCertSecretNames(opts.StackName)); optErr != nil {
4444
ll.Error(optErr, "failed to conform options to build settings")
4545
return kverrors.Wrap(err, "failed to conform options to build settings", "name", req.String())
4646
}

internal/certrotation/target.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func CertificatesExpired(opts Options) error {
5151
return &CertExpiredError{Message: "certificates expired", Reasons: reasons}
5252
}
5353

54-
// buildTargetCertKeyPairSecrets returns a slice of all rotated client and serving tempostacks certificates.
54+
// buildTargetCertKeyPairSecrets returns a slice of all rotated client and serving tempo certificates.
5555
func buildTargetCertKeyPairSecrets(opts Options) ([]client.Object, error) {
5656
var (
5757
res = make([]client.Object, 0)

internal/certrotation/target_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ func TestCertificatesExpired(t *testing.T) {
5555
},
5656
RawCACerts: rawCA.Config.Certs,
5757
}
58-
err = ApplyDefaultSettings(&opts, cfg)
58+
err = ApplyDefaultSettings(&opts, cfg, TempoStackComponentCertSecretNames(opts.StackName))
5959
require.NoError(t, err)
6060

61-
for _, name := range ComponentCertSecretNames(stackName) {
61+
for _, name := range TempoStackComponentCertSecretNames(stackName) {
6262
cert := opts.Certificates[name]
6363
cert.Secret = &corev1.Secret{
6464
ObjectMeta: metav1.ObjectMeta{
@@ -106,7 +106,7 @@ func TestBuildTargetCertKeyPairSecrets_Create(t *testing.T) {
106106
RawCACerts: rawCA.Config.Certs,
107107
}
108108

109-
err := ApplyDefaultSettings(&opts, cfg)
109+
err := ApplyDefaultSettings(&opts, cfg, TempoStackComponentCertSecretNames(opts.StackName))
110110
require.NoError(t, err)
111111

112112
objs, err := buildTargetCertKeyPairSecrets(opts)
@@ -154,7 +154,7 @@ func TestBuildTargetCertKeyPairSecrets_Rotate(t *testing.T) {
154154
},
155155
},
156156
}
157-
err := ApplyDefaultSettings(&opts, cfg)
157+
err := ApplyDefaultSettings(&opts, cfg, TempoStackComponentCertSecretNames(opts.StackName))
158158
require.NoError(t, err)
159159

160160
objs, err := buildTargetCertKeyPairSecrets(opts)

0 commit comments

Comments
 (0)