/
certrotation_discovery.go
45 lines (34 loc) · 1.31 KB
/
certrotation_discovery.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
package handlers
import (
"context"
"fmt"
"time"
"github.com/ViaQ/logerr/v2/kverrors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
v1alpha1 "github.com/grafana/tempo-operator/apis/tempo/v1alpha1"
)
const certRotationRequiredAtKey = "tempo.grafana.com/certRotationRequiredAt"
// AnnotateForRequiredCertRotation adds/updates the `tempo.grafana.com/certRotationRequiredAt` annotation
// to the named TempoStack if any of the managed client/serving/ca certificates expired. If no TempoStack
// is found, then skip reconciliation.
func AnnotateForRequiredCertRotation(ctx context.Context, k client.Client, name, namespace string) error {
var s v1alpha1.TempoStack
key := client.ObjectKey{Name: name, Namespace: namespace}
if err := k.Get(ctx, key, &s); err != nil {
if apierrors.IsNotFound(err) {
// Do nothing
return nil
}
return kverrors.Wrap(err, "failed to get tempo TempoStack", "key", key)
}
ss := s.DeepCopy()
if ss.Annotations == nil {
ss.Annotations = make(map[string]string)
}
ss.Annotations[certRotationRequiredAtKey] = time.Now().UTC().Format(time.RFC3339)
if err := k.Update(ctx, ss); err != nil {
return kverrors.Wrap(err, fmt.Sprintf("failed to update tempo TempoStack `%s` annotation", certRotationRequiredAtKey), "key", key)
}
return nil
}