Skip to content

Commit

Permalink
add common format scraped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinBisson committed Nov 20, 2024
1 parent 469cc7e commit 0c0b167
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 28 deletions.
10 changes: 10 additions & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package common

import (
"context"
"fmt"
"strings"

"github.com/pkg/errors"
netv1 "k8s.io/api/networking/v1"
Expand Down Expand Up @@ -76,3 +78,11 @@ func ReadProxyIngressURL(ctx context.Context, lc loggedcluster.Interface, client

return ingressURL, nil
}

func FormatScrapedNamespaces(lc loggedcluster.Interface, namespaces []string) string {
if IsWorkloadCluster(lc) {
return fmt.Sprintf("[\"%s\"]", strings.Join(namespaces, "\", \""))
}

return "[]"
}
6 changes: 2 additions & 4 deletions pkg/resource/events-logger-config/grafana-agent-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,13 @@ func generateGrafanaAgentInnerConfig(lc loggedcluster.Interface, defaultWorkload
Installation string
InsecureSkipVerify string
SecretName string
ScrapedNamespaces []string
IsWorkloadCluster bool
ScrapedNamespaces string
}{
ClusterID: lc.GetClusterName(),
Installation: lc.GetInstallationName(),
InsecureSkipVerify: fmt.Sprintf("%t", lc.IsInsecureCA()),
SecretName: fmt.Sprintf("%s-%s", lc.GetClusterName(), common.GrafanaAgentExtraSecretName()),
ScrapedNamespaces: defaultWorkloadClusterNamespaces,
IsWorkloadCluster: common.IsWorkloadCluster(lc),
ScrapedNamespaces: common.FormatScrapedNamespaces(lc, defaultWorkloadClusterNamespaces),
}

err := grafanaAgentConfigTemplate.Execute(&values, data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@ logging {
}

loki.source.kubernetes_events "local" {
{{- if and (.IsWorkloadCluster) (gt (len (.ScrapedNamespaces | join ",")) 0 )}}
namespaces = [
{{- range .ScrapedNamespaces }}
"{{ . }}",
{{- end }}
]
{{- else }}
namespaces = []
{{- end }}
namespaces = {{ .ScrapedNamespaces }}
forward_to = [loki.write.default.receiver]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ grafana-agent:
}
loki.source.kubernetes_events "local" {
namespaces = [
"kube-system",
"giantswarm",
]
namespaces = ["kube-system", "giantswarm"]
forward_to = [loki.write.default.receiver]
}
Expand Down
22 changes: 11 additions & 11 deletions pkg/resource/events-logger-config/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ func (r *Reconciler) ReconcileCreate(ctx context.Context, lc loggedcluster.Inter
return ctrl.Result{}, errors.WithStack(err)
}

desired := v1.ConfigMap{
desiredEventsLoggerConfig := v1.ConfigMap{
ObjectMeta: configMeta(lc),
Data: map[string]string{
"values": values,
},
}

// Check if config already exists.
logger.Info("grafana-agent-config - getting", "namespace", desired.GetNamespace(), "name", desired.GetName())
var currentGrafanaAgentConfig v1.ConfigMap
err = r.Client.Get(ctx, types.NamespacedName{Name: desired.GetName(), Namespace: desired.GetNamespace()}, &currentGrafanaAgentConfig)
logger.Info("grafana-agent-config - getting", "namespace", desiredEventsLoggerConfig.GetNamespace(), "name", desiredEventsLoggerConfig.GetName())
var currentEventsLoggerConfig v1.ConfigMap
err = r.Client.Get(ctx, types.NamespacedName{Name: desiredEventsLoggerConfig.GetName(), Namespace: desiredEventsLoggerConfig.GetNamespace()}, &currentEventsLoggerConfig)
if err != nil {
if apimachineryerrors.IsNotFound(err) {
logger.Info("grafana-agent-config not found, creating")
err = r.Client.Create(ctx, &desired)
err = r.Client.Create(ctx, &desiredEventsLoggerConfig)
if err != nil {
return ctrl.Result{}, errors.WithStack(err)
}
Expand All @@ -74,13 +74,13 @@ func (r *Reconciler) ReconcileCreate(ctx context.Context, lc loggedcluster.Inter
return ctrl.Result{}, errors.WithStack(err)
}

if !needUpdate(currentGrafanaAgentConfig, desired) {
if !needUpdate(currentEventsLoggerConfig, desiredEventsLoggerConfig) {
logger.Info("grafana-agent-config up to date")
return ctrl.Result{}, nil
}

logger.Info("grafana-agent-config - updating")
err = r.Client.Update(ctx, &desired)
err = r.Client.Update(ctx, &desiredEventsLoggerConfig)
if err != nil {
return ctrl.Result{}, errors.WithStack(err)
}
Expand All @@ -95,8 +95,8 @@ func (r *Reconciler) ReconcileDelete(ctx context.Context, lc loggedcluster.Inter
logger.Info("grafana-agent-config delete")

// Get expected configmap.
var currentGrafanaAgentConfig v1.ConfigMap
err := r.Client.Get(ctx, types.NamespacedName{Name: getGrafanaAgentConfigName(lc), Namespace: lc.GetAppsNamespace()}, &currentGrafanaAgentConfig)
var currentEventsLoggerConfig v1.ConfigMap
err := r.Client.Get(ctx, types.NamespacedName{Name: getGrafanaAgentConfigName(lc), Namespace: lc.GetAppsNamespace()}, &currentEventsLoggerConfig)
if err != nil {
if apimachineryerrors.IsNotFound(err) {
logger.Info("grafana-agent-config not found, stop here")
Expand All @@ -106,8 +106,8 @@ func (r *Reconciler) ReconcileDelete(ctx context.Context, lc loggedcluster.Inter
}

// Delete configmap.
logger.Info("grafana-agent-config deleting", "namespace", currentGrafanaAgentConfig.GetNamespace(), "name", currentGrafanaAgentConfig.GetName())
err = r.Client.Delete(ctx, &currentGrafanaAgentConfig)
logger.Info("grafana-agent-config deleting", "namespace", currentEventsLoggerConfig.GetNamespace(), "name", currentEventsLoggerConfig.GetName())
err = r.Client.Delete(ctx, &currentEventsLoggerConfig)
if err != nil {
if apimachineryerrors.IsNotFound(err) {
// Do no throw error in case it was not found, as this means
Expand Down

0 comments on commit 0c0b167

Please sign in to comment.