Skip to content

Commit

Permalink
Merge branch 'main' into remove-mtp
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinBisson authored Nov 27, 2024
2 parents 4be7e28 + 0ade0b4 commit f5e44b1
Show file tree
Hide file tree
Showing 23 changed files with 649 additions and 246 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.16.0] - 2024-11-26

### Added

- Add kubernetes events logging in Alloy.
- Add support for Private CAs in alloy logs.
- Add KubeEventsLogger option and related methods in loggedCLuster package.
- Add `events-logger` flag in the operator.
- Add toggle for `events-logger` in observability-bundle configmap.
- Add tests for `alloy-events` in events-logger-config.

### Changed

Expand Down Expand Up @@ -402,7 +406,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add '--vintage' toggle
- Add controller for Workload Management Cluster using cluster.x-k8s.io/v1beta1```

[Unreleased]: https://github.com/giantswarm/logging-operator/compare/v0.15.2...HEAD
[Unreleased]: https://github.com/giantswarm/logging-operator/compare/v0.16.0...HEAD
[0.16.0]: https://github.com/giantswarm/logging-operator/compare/v0.15.2...v0.16.0
[0.15.2]: https://github.com/giantswarm/logging-operator/compare/v0.15.1...v0.15.2
[0.15.1]: https://github.com/giantswarm/logging-operator/compare/v0.15.0...v0.15.1
[0.15.0]: https://github.com/giantswarm/logging-operator/compare/v0.14.0...v0.15.0
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/giantswarm/grafana-multi-tenant-proxy v0.8.0
github.com/google/go-cmp v0.6.0
github.com/onsi/ginkgo/v2 v2.22.0
github.com/onsi/gomega v1.35.1
github.com/onsi/gomega v1.36.0
github.com/pkg/errors v0.9.1
golang.org/x/crypto v0.28.0
golang.org/x/mod v0.22.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/onsi/ginkgo/v2 v2.22.0 h1:Yed107/8DjTr0lKCNt7Dn8yQ6ybuDRQoMGrNFKzMfHg=
github.com/onsi/ginkgo/v2 v2.22.0/go.mod h1:7Du3c42kxCUegi0IImZ1wUQzMBVecgIHjR1C+NkhLQo=
github.com/onsi/gomega v1.35.1 h1:Cwbd75ZBPxFSuZ6T+rN/WCb/gOc6YgFBXLlZLhC7Ds4=
github.com/onsi/gomega v1.35.1/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog=
github.com/onsi/gomega v1.36.0 h1:Pb12RlruUtj4XUuPUqeEWc6j5DkVVVA49Uf6YLfC95Y=
github.com/onsi/gomega v1.36.0/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
2 changes: 1 addition & 1 deletion helm/logging-operator/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
name: logging-operator
description: The logging-operator manages loki and logging related config.
home: https://github.com/giantswarm/logging-operator
version: 0.15.2
version: 0.16.0
appVersion: 0.0.1
annotations:
application.giantswarm.io/team: "atlas"
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ func main() {
DefaultWorkloadClusterNamespaces: defaultNamespaces,
}

eventsLoggerSecret := eventsloggersecret.Reconciler{
Client: mgr.GetClient(),
}

eventsLoggerConfig := eventsloggerconfig.Reconciler{
Client: mgr.GetClient(),
DefaultWorkloadClusterNamespaces: defaultNamespaces,
}

eventsLoggerSecret := eventsloggersecret.Reconciler{
Client: mgr.GetClient(),
}

loggedcluster.O.EnableLoggingFlag = enableLogging
loggedcluster.O.LoggingAgent = loggingAgent
loggedcluster.O.KubeEventsLogger = eventsLogger
Expand Down
4 changes: 4 additions & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ const (
AlloyLogAgentAppName = "alloy-logs"
AlloyLogAgentAppNamespace = "kube-system"

// Alloy app name and namespace when using Alloy as events logger.
AlloyEventsLoggerAppName = "alloy-events"
AlloyEventsLoggerAppNamespace = "kube-system"

MaxBackoffPeriod = "10m"
LokiURLFormat = "https://%s/loki/api/v1/push"
)
Expand Down
47 changes: 25 additions & 22 deletions pkg/resource/agents-toggle/observability_bundle_configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,33 @@ func GenerateObservabilityBundleConfigMap(ctx context.Context, lc loggedcluster.
return v1.ConfigMap{}, errors.Errorf("unsupported logging agent %q", lc.GetLoggingAgent())
}

// Enforce grafana-agent as events logger when observability-bundle version < 1.9.0 because this needs alloy 0.7.0.
if observabilityBundleVersion.LT(semver.MustParse("1.9.0")) && lc.GetKubeEventsLogger() == common.EventsLoggerAlloy {
logger := log.FromContext(ctx)
logger.Info("Alloy events logger is not supported by observability bundle, using grafana-agent instead.", "observability-bundle-version", observabilityBundleVersion, "events-logger", lc.GetKubeEventsLogger())
lc.SetKubeEventsLogger(common.EventsLoggerGrafanaAgent)
}

switch lc.GetKubeEventsLogger() {
case common.EventsLoggerGrafanaAgent:
appsToEnable["grafanaAgent"] = app{
Enabled: true,
}
appsToEnable["alloyEvents"] = app{
Enabled: false,
// If observability-bundle version >= 0.9.0, events loggers can be enabled.
if observabilityBundleVersion.GT(semver.MustParse("0.9.0")) {
// Enforce grafana-agent as events logger when observability-bundle version < 1.9.0 because this needs alloy 0.7.0.
if observabilityBundleVersion.LT(semver.MustParse("1.9.0")) && lc.GetKubeEventsLogger() == common.EventsLoggerAlloy {
logger := log.FromContext(ctx)
logger.Info("Alloy events logger is not supported by observability bundle, using grafana-agent instead.", "observability-bundle-version", observabilityBundleVersion, "events-logger", lc.GetKubeEventsLogger())
lc.SetKubeEventsLogger(common.EventsLoggerGrafanaAgent)
}
case common.EventsLoggerAlloy:
appsToEnable["grafanaAgent"] = app{
Enabled: false,
}
appsToEnable["alloyEvents"] = app{
Enabled: true,

switch lc.GetKubeEventsLogger() {
case common.EventsLoggerGrafanaAgent:
appsToEnable["grafanaAgent"] = app{
Enabled: true,
}
appsToEnable["alloyEvents"] = app{
Enabled: false,
}
case common.EventsLoggerAlloy:
appsToEnable["grafanaAgent"] = app{
Enabled: false,
}
appsToEnable["alloyEvents"] = app{
Enabled: true,
}
default:
return v1.ConfigMap{}, errors.Errorf("unsupported events logger %q", lc.GetKubeEventsLogger())
}
default:
return v1.ConfigMap{}, errors.Errorf("unsupported events logger %q", lc.GetKubeEventsLogger())
}

values := Values{
Expand Down
33 changes: 19 additions & 14 deletions pkg/resource/agents-toggle/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,36 @@ type Reconciler struct {
Scheme *runtime.Scheme
}

// ReconcileCreate ensure logging agents are enabled in the given cluster.
// ReconcileCreate ensure logging agents and events loggers are enabled in the given cluster.
func (r *Reconciler) ReconcileCreate(ctx context.Context, lc loggedcluster.Interface) (ctrl.Result, error) {
logger := log.FromContext(ctx)
logger.Info("Logging agents toggle create")
logger.Info("agents toggle create")

observabilityBundleVersion, err := common.GetObservabilityBundleAppVersion(lc, r.Client, ctx)
if err != nil {
// Handle case where the app is not found.
if apimachineryerrors.IsNotFound(err) {
logger.Info("logging-agents-toggle - observability bundle app not found, requeueing")
logger.Info("agents-toggle - observability bundle app not found, requeueing")
// If the app is not found we should requeue and try again later (5 minutes is the app platform default reconciliation time)
return ctrl.Result{RequeueAfter: time.Duration(5 * time.Minute)}, nil
}
return ctrl.Result{}, errors.WithStack(err)
}

// Get desired configmap to enable logging agents.
// Get desired configmap to enable logging agents and events loggers.
desiredConfigMap, err := GenerateObservabilityBundleConfigMap(ctx, lc, observabilityBundleVersion)
if err != nil {
return ctrl.Result{}, errors.WithStack(err)
}

// Check if configmap is already installed.
logger.Info("Logging agents toggle checking", "namespace", desiredConfigMap.GetNamespace(), "name", desiredConfigMap.GetName())
logger.Info("agents toggle checking", "namespace", desiredConfigMap.GetNamespace(), "name", desiredConfigMap.GetName())
var currentConfigMap v1.ConfigMap
err = r.Client.Get(ctx, types.NamespacedName{Name: desiredConfigMap.GetName(), Namespace: desiredConfigMap.GetNamespace()}, &currentConfigMap)
if err != nil {
if apimachineryerrors.IsNotFound(err) {
// Install configmap.
logger.Info("Logging agents toggle not found, creating")
logger.Info("agents toggle not found, creating")
err = r.Client.Create(ctx, &desiredConfigMap)
}
if err != nil {
Expand All @@ -66,24 +66,24 @@ func (r *Reconciler) ReconcileCreate(ctx context.Context, lc loggedcluster.Inter
}

if needUpdate(currentConfigMap, desiredConfigMap) {
logger.Info("Logging agents toggle updating")
logger.Info("agents toggle updating")
// Update configmap
// Configmap is installed and need to be updated.
err := r.Client.Update(ctx, &desiredConfigMap)
if err != nil {
return ctrl.Result{}, errors.WithStack(err)
}
} else {
logger.Info("Logging agents toggle up to date")
logger.Info("agents toggle up to date")
}

return ctrl.Result{}, nil
}

// ReconcileDelete ensure logging agents are disabled for the given cluster.
// ReconcileDelete ensure logging agents and events loggers are disabled for the given cluster.
func (r *Reconciler) ReconcileDelete(ctx context.Context, lc loggedcluster.Interface) (ctrl.Result, error) {
logger := log.FromContext(ctx)
logger.Info("Logging agents toggle delete")
logger.Info("agents toggle delete")

// Get observability bundle app metadata.
appMeta := common.ObservabilityBundleAppMeta(lc)
Expand All @@ -93,7 +93,7 @@ func (r *Reconciler) ReconcileDelete(ctx context.Context, lc loggedcluster.Inter
if err != nil {
// Handle case where the app is not found.
if apimachineryerrors.IsNotFound(err) {
logger.Info("logging-agents-toggle - observability bundle app not found, skipping deletion")
logger.Info("agents-toggle - observability bundle app not found, skipping deletion")
// If the app is not found we ignore the error and return, as this means the app was already deleted.
return ctrl.Result{}, nil
}
Expand All @@ -105,25 +105,30 @@ func (r *Reconciler) ReconcileDelete(ctx context.Context, lc loggedcluster.Inter
return ctrl.Result{}, errors.WithStack(err)
}

// If the observability-bundle version is too old, we don't need to do anything.
if observabilityBundleVersion.LT(semver.MustParse("0.9.0")) {
return ctrl.Result{}, nil
}

// Get expected configmap.
desiredConfigMap, err := GenerateObservabilityBundleConfigMap(ctx, lc, observabilityBundleVersion)
if err != nil {
return ctrl.Result{}, errors.WithStack(err)
}

// Delete configmap.
logger.Info("Logging agents toggle deleting", "namespace", desiredConfigMap.GetNamespace(), "name", desiredConfigMap.GetName())
logger.Info("agents toggle deleting", "namespace", desiredConfigMap.GetNamespace(), "name", desiredConfigMap.GetName())
err = r.Client.Delete(ctx, &desiredConfigMap)
if err != nil {
if apimachineryerrors.IsNotFound(err) {
// Do no throw error in case it was not found, as this means
// it was already deleted.
logger.Info("Logging agents toggle already deleted")
logger.Info("agents toggle already deleted")
} else if err != nil {
return ctrl.Result{}, errors.WithStack(err)
}
} else {
logger.Info("Logging agents toggle deleted")
logger.Info("agents toggle deleted")
}

return ctrl.Result{}, nil
Expand Down
86 changes: 86 additions & 0 deletions pkg/resource/events-logger-config/alloy-events-config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package eventsloggerconfig

import (
"bytes"
_ "embed"
"fmt"
"text/template"

"github.com/Masterminds/sprig/v3"

"github.com/giantswarm/logging-operator/pkg/common"
loggedcluster "github.com/giantswarm/logging-operator/pkg/logged-cluster"
loggingsecret "github.com/giantswarm/logging-operator/pkg/resource/logging-secret"
)

var (
//go:embed alloy/events-logger.alloy.template
alloyEvents string
alloyEventsTemplate *template.Template

//go:embed alloy/events-logger-config.alloy.yaml.template
alloyEventsConfig string
alloyEventsConfigTemplate *template.Template
)

func init() {
alloyEventsTemplate = template.Must(template.New("events-logger.alloy").Funcs(sprig.FuncMap()).Parse(alloyEvents))
alloyEventsConfigTemplate = template.Must(template.New("events-logger-config.alloy.yaml").Funcs(sprig.FuncMap()).Parse(alloyEventsConfig))
}

func generateAlloyEventsConfig(lc loggedcluster.Interface, defaultNamespaces []string) (string, error) {
var values bytes.Buffer

alloyConfig, err := generateAlloyConfig(lc, defaultNamespaces)
if err != nil {
return "", err
}

data := struct {
AlloyConfig string
SecretName string
}{
AlloyConfig: alloyConfig,
SecretName: common.AlloyEventsLoggerAppName,
}

err = alloyEventsConfigTemplate.Execute(&values, data)
if err != nil {
return "", err
}

return values.String(), nil
}

func generateAlloyConfig(lc loggedcluster.Interface, defaultNamespaces []string) (string, error) {
var values bytes.Buffer

data := struct {
ClusterID string
Installation string
InsecureSkipVerify string
MaxBackoffPeriod string
LokiURLEnvVarName string
TenantIDEnvVarName string
BasicAuthUsernameEnvVarName string
BasicAuthPasswordEnvVarName string
ScrapedNamespaces string
}{
ClusterID: lc.GetClusterName(),
Installation: lc.GetInstallationName(),
InsecureSkipVerify: fmt.Sprintf("%t", lc.IsInsecureCA()),
MaxBackoffPeriod: common.MaxBackoffPeriod,
LokiURLEnvVarName: loggingsecret.AlloyLokiURLEnvVarName,
TenantIDEnvVarName: loggingsecret.AlloyTenantIDEnvVarName,
BasicAuthUsernameEnvVarName: loggingsecret.AlloyBasicAuthUsernameEnvVarName,
BasicAuthPasswordEnvVarName: loggingsecret.AlloyBasicAuthPasswordEnvVarName,
ScrapedNamespaces: common.FormatScrapedNamespaces(lc, defaultNamespaces),
}

err := alloyEventsTemplate.Execute(&values, data)
if err != nil {
return "", err
}

return values.String(), nil
}
Loading

0 comments on commit f5e44b1

Please sign in to comment.