Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for alloy-events in events-logger-config #277

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
73 changes: 73 additions & 0 deletions pkg/resource/events-logger-config/alloy-events-config_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
})
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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