Path: blob/main/pkg/operator/reconciler_integrations.go
4094 views
package operator12import (3"context"4"fmt"56"github.com/go-kit/log"7"github.com/go-kit/log/level"8gragent "github.com/grafana/agent/pkg/operator/apis/monitoring/v1alpha1"9"github.com/grafana/agent/pkg/operator/clientutil"10"github.com/grafana/agent/pkg/operator/config"11apps_v1 "k8s.io/api/apps/v1"12"k8s.io/apimachinery/pkg/types"13)1415func (r *reconciler) newIntegrationsDeploymentSecret(16ctx context.Context,17l log.Logger,18d gragent.Deployment,19) error {2021// The Deployment for integrations only has integrations where AllNodes is22// false.23d = deploymentIntegrationSubset(d, false)2425name := fmt.Sprintf("%s-integrations-deploy-config", d.Agent.Name)26return r.createTelemetryConfigurationSecret(ctx, l, name, d, config.IntegrationsType)27}2829func (r *reconciler) newIntegrationsDaemonSetSecret(30ctx context.Context,31l log.Logger,32d gragent.Deployment,33) error {3435// The DaemonSet for integrations only has integrations where AllNodes is36// true.37d = deploymentIntegrationSubset(d, true)3839name := fmt.Sprintf("%s-integrations-ds-config", d.Agent.Name)40return r.createTelemetryConfigurationSecret(ctx, l, name, d, config.IntegrationsType)41}4243func deploymentIntegrationSubset(d gragent.Deployment, allNodes bool) gragent.Deployment {44res := *d.DeepCopy()4546filteredIntegrations := make([]gragent.IntegrationsDeployment, 0, len(d.Integrations))47for _, i := range d.Integrations {48if i.Instance.Spec.Type.AllNodes == allNodes {49filteredIntegrations = append(filteredIntegrations, i)50}51}5253res.Integrations = filteredIntegrations54return res55}5657func (r *reconciler) newIntegrationsDeployment(58ctx context.Context,59l log.Logger,60d gragent.Deployment,61) error {6263// The Deployment for integrations only has integrations where AllNodes is64// false.65d = deploymentIntegrationSubset(d, false)6667name := fmt.Sprintf("%s-integrations-deploy", d.Agent.Name)68deploy, err := newIntegrationsDeployment(r.config, name, d)69if err != nil {70return fmt.Errorf("failed to generate integrations Deployment: %w", err)71}72key := types.NamespacedName{Namespace: deploy.Namespace, Name: deploy.Name}7374if len(d.Integrations) == 0 {75// There's nothing to deploy; delete anything that might've been deployed76// from a previous reconcile.77level.Info(l).Log("msg", "deleting integrations Deployment", "deploy", key)78var deploy apps_v1.Deployment79return deleteManagedResource(ctx, r.Client, key, &deploy)80}8182level.Info(l).Log("msg", "reconciling integrations Deployment", "deploy", key)83err = clientutil.CreateOrUpdateDeployment(ctx, r.Client, deploy, l)84if err != nil {85return fmt.Errorf("failed to reconcile integrations Deployment: %w", err)86}87return nil88}8990func (r *reconciler) newIntegrationsDaemonSet(91ctx context.Context,92l log.Logger,93d gragent.Deployment,94) error {9596// The DaemonSet for integrations only has integrations where AllNodes is97// true.98d = deploymentIntegrationSubset(d, true)99100name := fmt.Sprintf("%s-integrations-ds", d.Agent.Name)101ds, err := newIntegrationsDaemonSet(r.config, name, d)102if err != nil {103return fmt.Errorf("failed to generate integrations DaemonSet: %w", err)104}105key := types.NamespacedName{Namespace: ds.Namespace, Name: ds.Name}106107if len(d.Integrations) == 0 {108// There's nothing to deploy; delete anything that might've been deployed109// from a previous reconcile.110level.Info(l).Log("msg", "deleting integrations DaemonSet", "ds", key)111var ds apps_v1.DaemonSet112return deleteManagedResource(ctx, r.Client, key, &ds)113}114115level.Info(l).Log("msg", "reconciling integrations DaemonSet", "ds", key)116err = clientutil.CreateOrUpdateDaemonSet(ctx, r.Client, ds, l)117if err != nil {118return fmt.Errorf("failed to reconcile integrations DaemonSet: %w", err)119}120return nil121}122123124