Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/operator/reconciler_integrations.go
4094 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
apps_v1 "k8s.io/api/apps/v1"
13
"k8s.io/apimachinery/pkg/types"
14
)
15
16
func (r *reconciler) newIntegrationsDeploymentSecret(
17
ctx context.Context,
18
l log.Logger,
19
d gragent.Deployment,
20
) error {
21
22
// The Deployment for integrations only has integrations where AllNodes is
23
// false.
24
d = deploymentIntegrationSubset(d, false)
25
26
name := fmt.Sprintf("%s-integrations-deploy-config", d.Agent.Name)
27
return r.createTelemetryConfigurationSecret(ctx, l, name, d, config.IntegrationsType)
28
}
29
30
func (r *reconciler) newIntegrationsDaemonSetSecret(
31
ctx context.Context,
32
l log.Logger,
33
d gragent.Deployment,
34
) error {
35
36
// The DaemonSet for integrations only has integrations where AllNodes is
37
// true.
38
d = deploymentIntegrationSubset(d, true)
39
40
name := fmt.Sprintf("%s-integrations-ds-config", d.Agent.Name)
41
return r.createTelemetryConfigurationSecret(ctx, l, name, d, config.IntegrationsType)
42
}
43
44
func deploymentIntegrationSubset(d gragent.Deployment, allNodes bool) gragent.Deployment {
45
res := *d.DeepCopy()
46
47
filteredIntegrations := make([]gragent.IntegrationsDeployment, 0, len(d.Integrations))
48
for _, i := range d.Integrations {
49
if i.Instance.Spec.Type.AllNodes == allNodes {
50
filteredIntegrations = append(filteredIntegrations, i)
51
}
52
}
53
54
res.Integrations = filteredIntegrations
55
return res
56
}
57
58
func (r *reconciler) newIntegrationsDeployment(
59
ctx context.Context,
60
l log.Logger,
61
d gragent.Deployment,
62
) error {
63
64
// The Deployment for integrations only has integrations where AllNodes is
65
// false.
66
d = deploymentIntegrationSubset(d, false)
67
68
name := fmt.Sprintf("%s-integrations-deploy", d.Agent.Name)
69
deploy, err := newIntegrationsDeployment(r.config, name, d)
70
if err != nil {
71
return fmt.Errorf("failed to generate integrations Deployment: %w", err)
72
}
73
key := types.NamespacedName{Namespace: deploy.Namespace, Name: deploy.Name}
74
75
if len(d.Integrations) == 0 {
76
// There's nothing to deploy; delete anything that might've been deployed
77
// from a previous reconcile.
78
level.Info(l).Log("msg", "deleting integrations Deployment", "deploy", key)
79
var deploy apps_v1.Deployment
80
return deleteManagedResource(ctx, r.Client, key, &deploy)
81
}
82
83
level.Info(l).Log("msg", "reconciling integrations Deployment", "deploy", key)
84
err = clientutil.CreateOrUpdateDeployment(ctx, r.Client, deploy, l)
85
if err != nil {
86
return fmt.Errorf("failed to reconcile integrations Deployment: %w", err)
87
}
88
return nil
89
}
90
91
func (r *reconciler) newIntegrationsDaemonSet(
92
ctx context.Context,
93
l log.Logger,
94
d gragent.Deployment,
95
) error {
96
97
// The DaemonSet for integrations only has integrations where AllNodes is
98
// true.
99
d = deploymentIntegrationSubset(d, true)
100
101
name := fmt.Sprintf("%s-integrations-ds", d.Agent.Name)
102
ds, err := newIntegrationsDaemonSet(r.config, name, d)
103
if err != nil {
104
return fmt.Errorf("failed to generate integrations DaemonSet: %w", err)
105
}
106
key := types.NamespacedName{Namespace: ds.Namespace, Name: ds.Name}
107
108
if len(d.Integrations) == 0 {
109
// There's nothing to deploy; delete anything that might've been deployed
110
// from a previous reconcile.
111
level.Info(l).Log("msg", "deleting integrations DaemonSet", "ds", key)
112
var ds apps_v1.DaemonSet
113
return deleteManagedResource(ctx, r.Client, key, &ds)
114
}
115
116
level.Info(l).Log("msg", "reconciling integrations DaemonSet", "ds", key)
117
err = clientutil.CreateOrUpdateDaemonSet(ctx, r.Client, ds, l)
118
if err != nil {
119
return fmt.Errorf("failed to reconcile integrations DaemonSet: %w", err)
120
}
121
return nil
122
}
123
124