Skip to content

Commit 27a5717

Browse files
authored
Encode the behavior into the promtail converter that promtail does internally to limit k8s discovery (#5046)
* Encode the behavior into the promtail converter that promtail does internally to limit k8s discovery * Update doc * Update static convert that uses promtail conversion * Fix spacing in generated config
1 parent ff849f4 commit 27a5717

17 files changed

+102
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ Main (unreleased)
6161

6262
- Fix Docker log corruption for multiplexed long lines. (@axd1x8a)
6363

64+
- Fix the promtail converter behavior to mimic promtail behavior by default and limit kubernetes discovery to the same node. (@dehaansa)
65+
6466
- Allow configuration of `force_attempt_http2` and default it to `true` for otelcol exporters with HTTP client configurations. (@dehaansa)
6567

6668
v1.12.0

docs/sources/set-up/migrate/from-promtail.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ The following list is specific to the convert command and not {{< param "PRODUCT
179179
Make sure that you use the new metric names, for example, in your alerts and dashboards queries.
180180
* The logs produced by {{< param "PRODUCT_NAME" >}} differ from those produced by Promtail.
181181
* {{< param "PRODUCT_NAME" >}} exposes the {{< param "PRODUCT_NAME" >}} [UI][], which differs from the Promtail Web UI.
182-
* If you are converting a Promtail configuration for a Kubernetes daemonset, [modify the generated configuration][single-node-discovery] to ensure `discovery.kubernetes` only discovers Pods residing on the same node as the {{< param "PRODUCT_NAME" >}} Pod.
182+
* If you are converting a Promtail configuration and not deploying as a Kubernetes daemonset, [modify the generated configuration][single-node-discovery] to ensure `discovery.kubernetes` discovery behaves as expected. The converter makes the same assumption as promtail that any `pod` discovery is for a daemonset deployment.
183183

184184
[Promtail]: https://www.grafana.com/docs/loki/latest/clients/promtail/
185185
[debugging]: #debugging

internal/converter/internal/promtailconvert/internal/build/service_discovery.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/grafana/alloy/internal/loki/promtail/scrapeconfig"
1313
"github.com/prometheus/common/model"
1414
prom_discover "github.com/prometheus/prometheus/discovery"
15+
"github.com/prometheus/prometheus/discovery/kubernetes"
1516
)
1617

1718
func (s *ScrapeConfigBuilder) AppendSDs() {
@@ -86,6 +87,13 @@ func toDiscoveryConfig(cfg *scrapeconfig.Config) prom_discover.Configs {
8687
}
8788

8889
for _, sd := range cfg.ServiceDiscoveryConfig.KubernetesSDConfigs {
90+
// See https://github.com/grafana/loki/blob/main/clients/pkg/promtail/targets/file/filetargetmanager.go#L126
91+
if sd.Role == kubernetes.RolePod {
92+
sd.Selectors = append(sd.Selectors, kubernetes.SelectorConfig{
93+
Role: kubernetes.RolePod,
94+
Field: `"spec.nodeName=" + coalesce(sys.env("HOSTNAME"), constants.hostname)`,
95+
})
96+
}
8997
sdConfigs = append(sdConfigs, sd)
9098
}
9199

internal/converter/internal/promtailconvert/promtailconvert.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"flag"
66
"fmt"
7+
"strings"
78

89
promtailcfg "github.com/grafana/alloy/internal/loki/promtail/config"
910
"github.com/grafana/alloy/internal/loki/promtail/file"
@@ -84,11 +85,19 @@ func Convert(in []byte, extraArgs []string) ([]byte, diag.Diagnostics) {
8485
return nil, diags
8586
}
8687

87-
prettyByte, newDiags := common.PrettyPrint(buf.Bytes())
88+
prettyByte, newDiags := common.PrettyPrint([]byte(fixFunctionUse(buf.String())))
8889
diags.AddAll(newDiags)
8990
return prettyByte, diags
9091
}
9192

93+
// There is not a way to express a function call in a struct to encode back into Alloy config, so we need to fix it
94+
func fixFunctionUse(buf string) string {
95+
return strings.ReplaceAll(buf,
96+
`"\"spec.nodeName=\" + coalesce(sys.env(\"HOSTNAME\"), constants.hostname)"`,
97+
`"spec.nodeName=" + coalesce(sys.env("HOSTNAME"), constants.hostname)`,
98+
)
99+
}
100+
92101
// AppendAll analyzes the entire promtail config in memory and transforms it
93102
// into Alloy components. It then appends each argument to the file builder.
94103
func AppendAll(f *builder.File, cfg *promtailcfg.Config, labelPrefix string, diags diag.Diagnostics) diag.Diagnostics {

internal/converter/internal/promtailconvert/promtailconvert_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
_ "github.com/grafana/alloy/internal/static/metrics/instance" // Imported to override default values via the init function.
1212
)
1313

14-
// Set this flag to update snapshots e.g. `go test -v ./interal/converter/internal/promtailconverter/...` -fix-tests
14+
// Set this flag to update snapshots e.g. `go test -v ./internal/converter/internal/promtailconvert/... -fix-tests`
1515
var fixTestsFlag = flag.Bool("fix-tests", false, "update the test files with the current generated content")
1616

1717
func TestConvert(t *testing.T) {

internal/converter/internal/promtailconvert/testdata/kubernetes.alloy

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@ discovery.kubernetes "fun" {
1515
server_name = "example.local"
1616
insecure_skip_verify = true
1717
}
18+
19+
selectors {
20+
role = "pod"
21+
field = "spec.nodeName=" + coalesce(sys.env("HOSTNAME"), constants.hostname)
22+
}
1823
}
1924

2025
discovery.kubernetes "fun_2" {
2126
role = "pod"
2227
kubeconfig_file = "/home/toby/.kube/config"
28+
29+
selectors {
30+
role = "pod"
31+
field = "spec.nodeName=" + coalesce(sys.env("HOSTNAME"), constants.hostname)
32+
}
2333
}
2434

2535
discovery.kubernetes "fun_3" {
@@ -30,6 +40,11 @@ discovery.kubernetes "fun_3" {
3040
type = "Bearer"
3141
credentials_file = "/home/robin/.special_token"
3242
}
43+
44+
selectors {
45+
role = "pod"
46+
field = "spec.nodeName=" + coalesce(sys.env("HOSTNAME"), constants.hostname)
47+
}
3348
}
3449

3550
discovery.kubernetes "fun_4" {
@@ -40,6 +55,11 @@ discovery.kubernetes "fun_4" {
4055
type = "Bearer"
4156
credentials_file = "/home/toby/.token"
4257
}
58+
59+
selectors {
60+
role = "pod"
61+
field = "spec.nodeName=" + coalesce(sys.env("HOSTNAME"), constants.hostname)
62+
}
4363
}
4464

4565
discovery.kubernetes "fun_5" {

internal/converter/internal/promtailconvert/testdata/mixed_pipeline.alloy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
discovery.kubernetes "uber_pipeline" {
22
role = "pod"
33
kubeconfig_file = "/home/toby/.kube/config"
4+
5+
selectors {
6+
role = "pod"
7+
field = "spec.nodeName=" + coalesce(sys.env("HOSTNAME"), constants.hostname)
8+
}
49
}
510

611
discovery.consulagent "uber_pipeline" {

internal/converter/internal/promtailconvert/testdata/pipeline_stages_cri_empty.alloy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
discovery.kubernetes "example" {
22
role = "pod"
33
kubeconfig_file = "/home/toby/.kube/config"
4+
5+
selectors {
6+
role = "pod"
7+
field = "spec.nodeName=" + coalesce(sys.env("HOSTNAME"), constants.hostname)
8+
}
49
}
510

611
loki.process "example" {

internal/converter/internal/promtailconvert/testdata/pipeline_stages_drop.alloy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
discovery.kubernetes "example" {
22
role = "pod"
33
kubeconfig_file = "/home/toby/.kube/config"
4+
5+
selectors {
6+
role = "pod"
7+
field = "spec.nodeName=" + coalesce(sys.env("HOSTNAME"), constants.hostname)
8+
}
49
}
510

611
loki.process "example" {

internal/converter/internal/promtailconvert/testdata/pipeline_stages_match_nested.alloy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
discovery.kubernetes "example" {
22
role = "pod"
33
kubeconfig_file = "/home/toby/.kube/config"
4+
5+
selectors {
6+
role = "pod"
7+
field = "spec.nodeName=" + coalesce(sys.env("HOSTNAME"), constants.hostname)
8+
}
49
}
510

611
loki.process "example" {

0 commit comments

Comments
 (0)