diff --git a/CHANGELOG.md b/CHANGELOG.md index 007f263c..fca6ad35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - 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 diff --git a/pkg/resource/events-logger-config/alloy-events-config_test.go b/pkg/resource/events-logger-config/alloy-events-config_test.go new file mode 100644 index 00000000..9766fc99 --- /dev/null +++ b/pkg/resource/events-logger-config/alloy-events-config_test.go @@ -0,0 +1,73 @@ +package eventsloggerconfig + +import ( + _ "embed" + "os" + "path/filepath" + "testing" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + capi "sigs.k8s.io/cluster-api/api/v1beta1" + + "github.com/google/go-cmp/cmp" + + loggedcluster "github.com/giantswarm/logging-operator/pkg/logged-cluster" + "github.com/giantswarm/logging-operator/pkg/logged-cluster/capicluster" +) + +func TestGenerateAlloyEventsConfig(t *testing.T) { + testCases := []struct { + goldenFile string + defaultNamespaces []string + installationName string + clusterName string + }{ + { + goldenFile: "alloy/test/events-logger-config.alloy.MC.yaml", + installationName: "test-installation", + clusterName: "test-installation", + }, + { + goldenFile: "alloy/test/events-logger-config.alloy.WC.yaml", + installationName: "test-installation", + clusterName: "test-cluster", + }, + } + + for _, tc := range testCases { + t.Run(filepath.Base(tc.goldenFile), func(t *testing.T) { + golden, err := os.ReadFile(tc.goldenFile) + if err != nil { + t.Fatalf("Failed to read golden file: %v", err) + } + + loggedCluster := &capicluster.Object{ + Object: &capi.Cluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: tc.clusterName, + }, + }, + Options: loggedcluster.Options{ + InstallationName: tc.installationName, + KubeEventsLogger: "alloy", + }, + } + + config, err := generateAlloyEventsConfig(loggedCluster, []string{"kube-system", "giantswarm"}) + if err != nil { + t.Fatalf("Failed to generate alloy config: %v", err) + } + + if string(golden) != config { + t.Logf("Generated config differs from %s, diff:\n%s", tc.goldenFile, cmp.Diff(string(golden), config)) + t.Fail() + if *update { + //nolint:gosec + if err := os.WriteFile(tc.goldenFile, []byte(config), 0644); err != nil { + t.Fatalf("Failed to update golden file: %v", err) + } + } + } + }) + } +} diff --git a/pkg/resource/events-logger-config/alloy/test/events-logger-config.alloy.MC.yaml b/pkg/resource/events-logger-config/alloy/test/events-logger-config.alloy.MC.yaml new file mode 100644 index 00000000..6b062d2d --- /dev/null +++ b/pkg/resource/events-logger-config/alloy/test/events-logger-config.alloy.MC.yaml @@ -0,0 +1,62 @@ +# This file was generated by logging-operator. +# It configures Alloy to be used as events logger. +# - configMap is generated from events-logger.alloy.template and passed as a string +# here and will be created by Alloy's chart. +# - Alloy runs as a deployment, with only 1 replica. +alloy: + alloy: + configMap: + create: true + content: |- + loki.source.kubernetes_events "local" { + namespaces = [] + forward_to = [loki.write.default.receiver] + } + + // Loki target configuration + loki.write "default" { + endpoint { + url = env("LOKI_URL") + max_backoff_period = "10m" + tenant_id = env("TENANT_ID") + + basic_auth { + username = env("BASIC_AUTH_USERNAME") + password = env("BASIC_AUTH_PASSWORD") + } + + tls_config { + insecure_skip_verify = false + } + } + external_labels = { + cluster_id = "test-installation", + installation = "test-installation", + scrape_job = "kubernetes-events", + } + } + + logging { + level = "info" + format = "logfmt" + } + + envFrom: + - secretRef: + name: alloy-events + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + readOnlyRootFilesystem: false + runAsUser: 10 + runAsGroup: 10 + runAsNonRoot: true + seccompProfile: + type: RuntimeDefault + controller: + type: deployment + replicas: 1 + crds: + create: false diff --git a/pkg/resource/events-logger-config/alloy/test/events-logger-config.alloy.WC.yaml b/pkg/resource/events-logger-config/alloy/test/events-logger-config.alloy.WC.yaml new file mode 100644 index 00000000..a0302c5b --- /dev/null +++ b/pkg/resource/events-logger-config/alloy/test/events-logger-config.alloy.WC.yaml @@ -0,0 +1,62 @@ +# This file was generated by logging-operator. +# It configures Alloy to be used as events logger. +# - configMap is generated from events-logger.alloy.template and passed as a string +# here and will be created by Alloy's chart. +# - Alloy runs as a deployment, with only 1 replica. +alloy: + alloy: + configMap: + create: true + content: |- + loki.source.kubernetes_events "local" { + namespaces = ["kube-system", "giantswarm"] + forward_to = [loki.write.default.receiver] + } + + // Loki target configuration + loki.write "default" { + endpoint { + url = env("LOKI_URL") + max_backoff_period = "10m" + tenant_id = env("TENANT_ID") + + basic_auth { + username = env("BASIC_AUTH_USERNAME") + password = env("BASIC_AUTH_PASSWORD") + } + + tls_config { + insecure_skip_verify = false + } + } + external_labels = { + cluster_id = "test-cluster", + installation = "test-installation", + scrape_job = "kubernetes-events", + } + } + + logging { + level = "info" + format = "logfmt" + } + + envFrom: + - secretRef: + name: alloy-events + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + readOnlyRootFilesystem: false + runAsUser: 10 + runAsGroup: 10 + runAsNonRoot: true + seccompProfile: + type: RuntimeDefault + controller: + type: deployment + replicas: 1 + crds: + create: false