Skip to content
This repository was archived by the owner on Jul 28, 2026. It is now read-only.
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
44 changes: 38 additions & 6 deletions pkg/operator/config/logs_templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ import (
)

func TestLogsClientConfig(t *testing.T) {
agent := &gragent.GrafanaAgent{
ObjectMeta: metav1.ObjectMeta{
Namespace: "telemetry",
Name: "grafana-agent",
},
}

tt := []struct {
name string
input map[string]interface{}
Expand All @@ -27,18 +34,22 @@ func TestLogsClientConfig(t *testing.T) {
{
name: "all-in-one URL",
input: map[string]interface{}{
"agent": agent,
"namespace": "operator",
"spec": &gragent.LogsClientSpec{
URL: "http://username:password@localhost:3100/loki/api/v1/push",
},
},
expect: util.Untab(`
url: http://username:password@localhost:3100/loki/api/v1/push
external_labels:
cluster: telemetry/grafana-agent
`),
},
{
name: "full basic config",
input: map[string]interface{}{
"agent": agent,
"namespace": "operator",
"spec": &gragent.LogsClientSpec{
URL: "http://localhost:3100/loki/api/v1/push",
Expand Down Expand Up @@ -69,6 +80,7 @@ func TestLogsClientConfig(t *testing.T) {
max_period: 5m
max_retries: 100
external_labels:
cluster: telemetry/grafana-agent
foo: bar
fizz: buzz
timeout: 5m
Expand All @@ -77,6 +89,7 @@ func TestLogsClientConfig(t *testing.T) {
{
name: "tls config",
input: map[string]interface{}{
"agent": agent,
"namespace": "operator",
"spec": &gragent.LogsClientSpec{
URL: "http://localhost:3100/loki/api/v1/push",
Expand All @@ -93,11 +106,14 @@ func TestLogsClientConfig(t *testing.T) {
ca_file: ca
key_file: key
cert_file: cert
external_labels:
cluster: telemetry/grafana-agent
`),
},
{
name: "bearer tokens",
input: map[string]interface{}{
"agent": agent,
"namespace": "operator",
"spec": &gragent.LogsClientSpec{
URL: "http://localhost:3100/loki/api/v1/push",
Expand All @@ -109,11 +125,14 @@ func TestLogsClientConfig(t *testing.T) {
url: http://localhost:3100/loki/api/v1/push
bearer_token: tok
bearer_token_file: tokfile
external_labels:
cluster: telemetry/grafana-agent
`),
},
{
name: "basic auth",
input: map[string]interface{}{
"agent": agent,
"namespace": "operator",
"spec": &gragent.LogsClientSpec{
URL: "http://localhost:3100/loki/api/v1/push",
Expand All @@ -134,6 +153,8 @@ func TestLogsClientConfig(t *testing.T) {
basic_auth:
username: secretkey
password: secretkey
external_labels:
cluster: telemetry/grafana-agent
`),
},
}
Expand Down Expand Up @@ -494,6 +515,13 @@ func TestPodLogsConfig(t *testing.T) {
}

func TestLogsConfig(t *testing.T) {
agent := &gragent.GrafanaAgent{
ObjectMeta: metav1.ObjectMeta{
Namespace: "operator",
Name: "grafana-agent",
},
}

tt := []struct {
name string
input map[string]interface{}
Expand All @@ -502,7 +530,7 @@ func TestLogsConfig(t *testing.T) {
{
name: "global clients",
input: map[string]interface{}{
"agentNamespace": "operator",
"agent": agent,
"global": &gragent.LogsSubsystemSpec{
Clients: []gragent.LogsClientSpec{{URL: "global"}},
},
Expand All @@ -524,12 +552,14 @@ func TestLogsConfig(t *testing.T) {
name: inst/default
clients:
- url: global
external_labels:
cluster: operator/grafana-agent
`),
},
{
name: "local clients",
input: map[string]interface{}{
"agentNamespace": "operator",
"agent": agent,
"global": &gragent.LogsSubsystemSpec{
Clients: []gragent.LogsClientSpec{{URL: "global"}},
},
Expand All @@ -553,13 +583,15 @@ func TestLogsConfig(t *testing.T) {
name: inst/default
clients:
- url: local
external_labels:
cluster: operator/grafana-agent
`),
},
{
name: "pod logs",
input: map[string]interface{}{
"agentNamespace": "operator",
"global": &gragent.LogsSubsystemSpec{},
"agent": agent,
"global": &gragent.LogsSubsystemSpec{},
"instance": &LogInstance{
Instance: &gragent.LogsInstance{
ObjectMeta: metav1.ObjectMeta{Namespace: "inst", Name: "default"},
Expand Down Expand Up @@ -605,8 +637,8 @@ func TestLogsConfig(t *testing.T) {
{
name: "additional scrape configs",
input: map[string]interface{}{
"agentNamespace": "operator",
"global": &gragent.LogsSubsystemSpec{},
"agent": agent,
"global": &gragent.LogsSubsystemSpec{},
"instance": &LogInstance{
Instance: &gragent.LogsInstance{
ObjectMeta: metav1.ObjectMeta{Namespace: "inst", Name: "default"},
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/config/templates/agent-logs.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function(ctx) marshal.YAML(optionals.trim({
positions_directory: '/var/lib/grafana-agent/data',
configs: optionals.array(std.map(
function(logs_inst) new_logs_instance(
agentNamespace=ctx.Agent.ObjectMeta.Namespace,
agent=ctx.Agent,
global=logs,
instance=logs_inst,
apiServer=spec.APIServerConfig,
Expand Down
6 changes: 4 additions & 2 deletions pkg/operator/config/templates/component/logs/client.libsonnet
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
local optionals = import 'ext/optionals.libsonnet';
local secrets = import 'ext/secrets.libsonnet';

local new_external_labels = import './external_labels.libsonnet';
local new_tls_config = import 'component/metrics/tls_config.libsonnet';

// Generates the content of a client object to send logs to Loki.
//
// @param {GrafanaAgent} agent
// @param {string} namespace - namespace of spec.
// @param {LogsClientSpec} spec
function(namespace, spec) {
function(agent, namespace, spec) {
url: spec.URL,
tls_config:
if spec.TLSConfig != null then new_tls_config(namespace, spec.TLSConfig),
Expand All @@ -33,5 +35,5 @@ function(namespace, spec) {
max_retries: optionals.number(spec.BackoffConfig.MaxRetries),
},

external_labels: optionals.object(spec.ExternalLabels),
external_labels: optionals.object(new_external_labels(agent, spec)),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Generates an external_label mapping. This includes the user-provided labels
// as well as the injected cluster label.
//
// @param {GrafanaAgent} agent
// @param {LogsClientSpec} client
function(agent, client) (
local meta = agent.ObjectMeta;
local logs = agent.Spec.Logs;

// Provide the cluster label first. Doing it this way allows the user to
// override with a value they choose.
(
local clusterValue = '%s/%s' % [meta.Namespace, meta.Name];
local clusterLabel = logs.LogsExternalLabelName;

if clusterLabel == null then { cluster: clusterValue }
else if clusterLabel != '' then { [clusterLabel]: clusterValue }
else {}
) +

// Finally add in any user-configured labels.
(
if client.ExternalLabels != null
then client.ExternalLabels
else {}
)
)
7 changes: 4 additions & 3 deletions pkg/operator/config/templates/logs.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ local new_pod_logs = import 'component/logs/pod_logs.libsonnet';

// Generates a logs_instance.
//
// @param {string} agentNamespace - namespace of the GrafanaAgent
// @param {GrafanaAgent} agent
// @param {LogsSubsystemSpec} global - global logs settings & defaults
// @param {LogInstance} instance
// @param {APIServerConfig} apiServer
// @param {boolean} ignoreNamespaceSelectors
// @param {string} enforcedNamespaceLabel
function(
agentNamespace,
agent,
global,
instance,
apiServer,
ignoreNamespaceSelectors,
enforcedNamespaceLabel,
) {
local agentNamespace = agent.ObjectMeta.Namespace,
local meta = instance.Instance.ObjectMeta,
local spec = instance.Instance.Spec,

Expand All @@ -39,7 +40,7 @@ function(
else { ns: agentNamespace, list: global.Clients },

clients: optionals.array(std.map(
function(spec) new_client(clients.ns, spec),
function(spec) new_client(agent, clients.ns, spec),
clients.list,
)),

Expand Down