Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/operator/reconciler.go
4093 views
1
package operator
2
3
import (
4
"context"
5
"fmt"
6
7
"github.com/go-kit/log"
8
"github.com/go-kit/log/level"
9
gragent "github.com/grafana/agent/pkg/operator/apis/monitoring/v1alpha1"
10
"github.com/grafana/agent/pkg/operator/clientutil"
11
"github.com/grafana/agent/pkg/operator/config"
12
"github.com/grafana/agent/pkg/operator/hierarchy"
13
"github.com/grafana/agent/pkg/operator/logutil"
14
"github.com/prometheus/prometheus/model/labels"
15
core_v1 "k8s.io/api/core/v1"
16
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
17
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18
"k8s.io/apimachinery/pkg/runtime"
19
controller "sigs.k8s.io/controller-runtime"
20
"sigs.k8s.io/controller-runtime/pkg/client"
21
)
22
23
type reconciler struct {
24
client.Client
25
scheme *runtime.Scheme
26
config *Config
27
28
notifier *hierarchy.Notifier
29
}
30
31
func (r *reconciler) Reconcile(ctx context.Context, req controller.Request) (controller.Result, error) {
32
l := logutil.FromContext(ctx)
33
level.Info(l).Log("msg", "reconciling grafana-agent")
34
defer level.Debug(l).Log("msg", "done reconciling grafana-agent")
35
36
// Reset our notifications while we re-handle the reconcile.
37
r.notifier.StopNotify(req.NamespacedName)
38
39
var agent gragent.GrafanaAgent
40
if err := r.Get(ctx, req.NamespacedName, &agent); k8s_errors.IsNotFound(err) {
41
level.Debug(l).Log("msg", "detected deleted agent")
42
return controller.Result{}, nil
43
} else if err != nil {
44
level.Error(l).Log("msg", "unable to get grafana-agent", "err", err)
45
return controller.Result{}, nil
46
}
47
48
if agent.Spec.Paused {
49
return controller.Result{}, nil
50
}
51
52
if r.config.agentLabelSelector != nil && !r.config.agentLabelSelector.Matches(labels.FromMap(agent.ObjectMeta.Labels)) {
53
level.Debug(l).Log("msg", "grafana-agent does not match agent selector. Skipping reconcile")
54
return controller.Result{}, nil
55
}
56
57
deployment, watchers, err := buildHierarchy(ctx, l, r.Client, &agent)
58
if err != nil {
59
level.Error(l).Log("msg", "unable to build hierarchy", "err", err)
60
return controller.Result{}, nil
61
}
62
if err := r.notifier.Notify(watchers...); err != nil {
63
level.Error(l).Log("msg", "unable to update notifier", "err", err)
64
return controller.Result{}, nil
65
}
66
67
type reconcileFunc func(context.Context, log.Logger, gragent.Deployment) error
68
actors := []reconcileFunc{
69
// Operator-wide resources
70
r.createSecrets,
71
72
// Metrics resources (may be a no-op if no metrics configured)
73
r.createMetricsConfigurationSecret,
74
r.createMetricsGoverningService,
75
r.createMetricsStatefulSets,
76
77
// Logs resources (may be a no-op if no logs configured)
78
r.createLogsConfigurationSecret,
79
r.createLogsDaemonSet,
80
81
// Integration resources (may be a no-op if no integrations configured)
82
r.newIntegrationsDeploymentSecret,
83
r.newIntegrationsDaemonSetSecret,
84
r.newIntegrationsDeployment,
85
r.newIntegrationsDaemonSet,
86
}
87
for _, actor := range actors {
88
err := actor(ctx, l, deployment)
89
if err != nil {
90
level.Error(l).Log("msg", "error during reconciling", "err", err)
91
return controller.Result{Requeue: true}, nil
92
}
93
}
94
95
return controller.Result{}, nil
96
}
97
98
// createSecrets creates secrets from the secret store.
99
func (r *reconciler) createSecrets(
100
ctx context.Context,
101
l log.Logger,
102
d gragent.Deployment,
103
) error {
104
105
blockOwnerDeletion := true
106
107
data := make(map[string][]byte)
108
for k, value := range d.Secrets {
109
data[config.SanitizeLabelName(string(k))] = []byte(value)
110
}
111
112
secret := core_v1.Secret{
113
ObjectMeta: v1.ObjectMeta{
114
Namespace: d.Agent.Namespace,
115
Name: fmt.Sprintf("%s-secrets", d.Agent.Name),
116
OwnerReferences: []v1.OwnerReference{{
117
APIVersion: d.Agent.APIVersion,
118
BlockOwnerDeletion: &blockOwnerDeletion,
119
Kind: d.Agent.Kind,
120
Name: d.Agent.Name,
121
UID: d.Agent.UID,
122
}},
123
Labels: map[string]string{
124
managedByOperatorLabel: managedByOperatorLabelValue,
125
},
126
},
127
Data: data,
128
}
129
130
level.Info(l).Log("msg", "reconciling secret", "secret", secret.Name)
131
err := clientutil.CreateOrUpdateSecret(ctx, r.Client, &secret)
132
if err != nil {
133
return fmt.Errorf("failed to reconcile secret: %w", err)
134
}
135
return nil
136
}
137
138