Skip to content

Commit 2523c61

Browse files
committed
address review
1 parent 5fdc509 commit 2523c61

File tree

9 files changed

+281
-184
lines changed

9 files changed

+281
-184
lines changed

charts/loki/templates/_helpers.tpl

Lines changed: 0 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,173 +1163,3 @@ env:
11631163
{{- end -}}
11641164
{{- end -}}
11651165

1166-
{{/*
1167-
Gateway API version detection
1168-
*/}}
1169-
{{- define "loki.gatewayApi.apiVersion" -}}
1170-
{{- if .Capabilities.APIVersions.Has "gateway.networking.k8s.io/v1" }}
1171-
{{- print "gateway.networking.k8s.io/v1" }}
1172-
{{- else if .Capabilities.APIVersions.Has "gateway.networking.k8s.io/v1beta1" }}
1173-
{{- print "gateway.networking.k8s.io/v1beta1" }}
1174-
{{- else }}
1175-
{{- print "gateway.networking.k8s.io/v1" }}
1176-
{{- end }}
1177-
{{- end -}}
1178-
1179-
{{/*
1180-
Gateway API route manifest shared between the nginx gateway route and direct Loki service routes.
1181-
Params:
1182-
ctx - chart root context ($)
1183-
route - route values object
1184-
name - resource name for the HTTPRoute object
1185-
rules - pre-rendered rules string to embed under spec.rules
1186-
labelsTemplate - optional: name of the labels helper to use (default "loki.labels")
1187-
*/}}
1188-
{{- define "loki.route" -}}
1189-
{{- $route := .route -}}
1190-
{{- $ctx := .ctx }}
1191-
---
1192-
apiVersion: {{ $route.apiVersion | default (include "loki.gatewayApi.apiVersion" $ctx) }}
1193-
kind: {{ $route.kind | default "HTTPRoute" }}
1194-
metadata:
1195-
name: {{ .name }}
1196-
namespace: {{ include "loki.namespace" $ctx }}
1197-
labels:
1198-
{{- include (.labelsTemplate | default "loki.labels") $ctx | nindent 4 }}
1199-
{{- with $route.labels }}
1200-
{{- toYaml . | nindent 4 }}
1201-
{{- end }}
1202-
{{- with $route.annotations }}
1203-
annotations:
1204-
{{- toYaml . | nindent 4 }}
1205-
{{- end }}
1206-
spec:
1207-
{{- with $route.parentRefs }}
1208-
parentRefs:
1209-
{{- toYaml . | nindent 4 }}
1210-
{{- end }}
1211-
{{- with $route.hostnames }}
1212-
hostnames:
1213-
{{- tpl (toYaml .) $ctx | nindent 4 }}
1214-
{{- end }}
1215-
rules:
1216-
{{- with $route.additionalRules }}
1217-
{{- tpl (toYaml .) $ctx | nindent 4 }}
1218-
{{- end }}
1219-
{{- .rules | nindent 4 }}
1220-
{{- end -}}
1221-
1222-
{{/*
1223-
HTTPRoute rule that points to the nginx gateway backend.
1224-
Params:
1225-
ctx - chart root context ($)
1226-
route - route values object
1227-
name - backend service name
1228-
port - backend service port
1229-
*/}}
1230-
{{- define "loki.route.gatewayRule" -}}
1231-
- backendRefs:
1232-
- group: ""
1233-
kind: Service
1234-
name: {{ .name }}
1235-
port: {{ .port }}
1236-
weight: 1
1237-
{{- with .route.filters }}
1238-
filters:
1239-
{{- toYaml . | nindent 2 }}
1240-
{{- end }}
1241-
{{- with .route.matches }}
1242-
matches:
1243-
{{- toYaml . | nindent 2 }}
1244-
{{- end }}
1245-
{{- with .route.timeouts }}
1246-
timeouts:
1247-
{{- toYaml . | nindent 2 }}
1248-
{{- end }}
1249-
{{- end -}}
1250-
1251-
{{/*
1252-
Generate HTTPRoute rules based on deployment type for direct routing to Loki services.
1253-
Params:
1254-
ctx - chart root context ($)
1255-
paths - paths object from the route entry (e.g. .Values.route.main.paths)
1256-
*/}}
1257-
{{- define "loki.route.rules" -}}
1258-
{{- $ctx := .ctx -}}
1259-
{{- $paths := .paths -}}
1260-
{{- if (eq (include "loki.deployment.isSingleBinary" $ctx) "true") -}}
1261-
{{- include "loki.route.singleBinaryRules" (dict "ctx" $ctx "paths" $paths) }}
1262-
{{- else if (eq (include "loki.deployment.isDistributed" $ctx) "true") -}}
1263-
{{- include "loki.route.distributedRules" (dict "ctx" $ctx "paths" $paths) }}
1264-
{{- else if and (eq (include "loki.deployment.isScalable" $ctx) "true") -}}
1265-
{{- include "loki.route.scalableRules" (dict "ctx" $ctx "paths" $paths) }}
1266-
{{- end -}}
1267-
{{- end -}}
1268-
1269-
{{/*
1270-
HTTPRoute rules for distributed deployment
1271-
*/}}
1272-
{{- define "loki.route.distributedRules" -}}
1273-
{{- $ctx := .ctx -}}
1274-
{{- $paths := .paths -}}
1275-
{{- $distributorServiceName := include "loki.resourceName" (dict "ctx" $ctx "component" "distributor") }}
1276-
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $distributorServiceName "paths" $paths.distributor) }}
1277-
{{- $queryFrontendServiceName := include "loki.resourceName" (dict "ctx" $ctx "component" "query-frontend") }}
1278-
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $queryFrontendServiceName "paths" $paths.queryFrontend) }}
1279-
{{- $rulerServiceName := include "loki.resourceName" (dict "ctx" $ctx "component" "ruler") }}
1280-
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $rulerServiceName "paths" $paths.ruler) }}
1281-
{{- $compactorServiceName := include "loki.resourceName" (dict "ctx" $ctx "component" "compactor") }}
1282-
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $compactorServiceName "paths" $paths.compactor) }}
1283-
{{- end -}}
1284-
1285-
{{/*
1286-
HTTPRoute rules for simple scalable deployment
1287-
*/}}
1288-
{{- define "loki.route.scalableRules" -}}
1289-
{{- $ctx := .ctx -}}
1290-
{{- $paths := .paths -}}
1291-
{{- $readServiceName := include "loki.resourceName" (dict "ctx" $ctx "component" "read") }}
1292-
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $readServiceName "paths" $paths.queryFrontend) }}
1293-
{{- $writeServiceName := include "loki.resourceName" (dict "ctx" $ctx "component" "write") }}
1294-
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $writeServiceName "paths" $paths.distributor) }}
1295-
{{- $backendServiceName := include "loki.resourceName" (dict "ctx" $ctx "component" "backend") }}
1296-
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $backendServiceName "paths" $paths.ruler) }}
1297-
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $backendServiceName "paths" $paths.compactor) }}
1298-
{{- end -}}
1299-
1300-
{{/*
1301-
HTTPRoute rules for single binary deployment
1302-
*/}}
1303-
{{- define "loki.route.singleBinaryRules" -}}
1304-
{{- $ctx := .ctx -}}
1305-
{{- $paths := .paths -}}
1306-
{{- $serviceName := include "loki.singleBinaryFullname" $ctx }}
1307-
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $serviceName "paths" $paths.distributor) }}
1308-
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $serviceName "paths" $paths.queryFrontend) }}
1309-
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $serviceName "paths" $paths.ruler) }}
1310-
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $serviceName "paths" $paths.compactor) }}
1311-
{{- end -}}
1312-
1313-
{{/*
1314-
HTTPRoute service rule helper.
1315-
Params:
1316-
ctx - chart root context ($)
1317-
serviceName - fully qualified k8s service name
1318-
paths - list of URL path prefixes to match
1319-
*/}}
1320-
{{- define "loki.route.serviceRule" -}}
1321-
{{- if .paths }}
1322-
- backendRefs:
1323-
- group: ""
1324-
kind: Service
1325-
name: {{ .serviceName }}
1326-
port: {{ .ctx.Values.loki.server.http_listen_port }}
1327-
weight: 1
1328-
matches:
1329-
{{- range .paths }}
1330-
- path:
1331-
type: PathPrefix
1332-
value: {{ . }}
1333-
{{- end }}
1334-
{{- end -}}
1335-
{{- end -}}

charts/loki/templates/_route.tpl

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
{{/*
2+
Detect the highest available Gateway API version from cluster capabilities.
3+
*/}}
4+
{{- define "loki.gatewayApi.apiVersion" -}}
5+
{{- if .Capabilities.APIVersions.Has "gateway.networking.k8s.io/v1" -}}
6+
{{- print "gateway.networking.k8s.io/v1" -}}
7+
{{- else if .Capabilities.APIVersions.Has "gateway.networking.k8s.io/v1beta1" -}}
8+
{{- print "gateway.networking.k8s.io/v1beta1" -}}
9+
{{- else -}}
10+
{{- print "gateway.networking.k8s.io/v1" -}}
11+
{{- end -}}
12+
{{- end -}}
13+
14+
{{/*
15+
Gateway API route manifest. Shared by the nginx gateway route and direct Loki service routes.
16+
Params:
17+
ctx - chart root context ($)
18+
route - route values object
19+
name - resource name for the route object
20+
rules - pre-rendered rules string to embed under spec.rules
21+
labelsTemplate - optional: name of the labels helper to use (default "loki.labels")
22+
*/}}
23+
{{- define "loki.route" -}}
24+
{{- $route := .route -}}
25+
{{- $ctx := .ctx }}
26+
---
27+
apiVersion: {{ $route.apiVersion | default (include "loki.gatewayApi.apiVersion" $ctx) }}
28+
kind: {{ $route.kind | default "HTTPRoute" }}
29+
metadata:
30+
name: {{ .name }}
31+
namespace: {{ include "loki.namespace" $ctx }}
32+
labels:
33+
{{- include (.labelsTemplate | default "loki.labels") $ctx | nindent 4 }}
34+
{{- with $route.labels }}
35+
{{- toYaml . | nindent 4 }}
36+
{{- end }}
37+
{{- with $route.annotations }}
38+
annotations:
39+
{{- toYaml . | nindent 4 }}
40+
{{- end }}
41+
spec:
42+
{{- with $route.parentRefs }}
43+
parentRefs:
44+
{{- toYaml . | nindent 4 }}
45+
{{- end }}
46+
{{- with $route.hostnames }}
47+
hostnames:
48+
{{- tpl (toYaml .) $ctx | nindent 4 }}
49+
{{- end }}
50+
{{- if or $route.additionalRules (ne (trim .rules) "") }}
51+
rules:
52+
{{- with $route.additionalRules }}
53+
{{- tpl (toYaml .) $ctx | nindent 4 }}
54+
{{- end }}
55+
{{- if ne (trim .rules) "" }}
56+
{{- .rules | nindent 4 }}
57+
{{- end }}
58+
{{- end }}
59+
{{- end -}}
60+
61+
{{/*
62+
HTTPRoute rule pointing to the nginx gateway backend.
63+
Params:
64+
ctx - chart root context ($)
65+
route - route values object
66+
serviceName - backend Service name (always the gateway service, regardless of route key)
67+
port - backend service port
68+
*/}}
69+
{{- define "loki.route.gatewayRule" -}}
70+
- backendRefs:
71+
- group: ""
72+
kind: Service
73+
name: {{ .serviceName }}
74+
port: {{ .port }}
75+
weight: 1
76+
{{- with .route.filters }}
77+
filters:
78+
{{- toYaml . | nindent 2 }}
79+
{{- end }}
80+
{{- with .route.matches }}
81+
matches:
82+
{{- toYaml . | nindent 2 }}
83+
{{- end }}
84+
{{- with .route.timeouts }}
85+
timeouts:
86+
{{- toYaml . | nindent 2 }}
87+
{{- end }}
88+
{{- end -}}
89+
90+
{{/*
91+
Generate HTTPRoute rules based on deployment mode.
92+
Params:
93+
ctx - chart root context ($)
94+
paths - paths map from the route entry (route.main.paths)
95+
port - optional backend port override; falls back to loki.server.http_listen_port
96+
*/}}
97+
{{- define "loki.route.rules" -}}
98+
{{- $ctx := .ctx -}}
99+
{{- $paths := .paths -}}
100+
{{- $port := .port -}}
101+
{{- if eq (include "loki.deployment.isSingleBinary" $ctx) "true" -}}
102+
{{- $svc := include "loki.fullname" $ctx -}}
103+
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $svc "paths" $paths.distributor "port" $port) }}
104+
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $svc "paths" $paths.queryFrontend "port" $port) }}
105+
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $svc "paths" $paths.ruler "port" $port) }}
106+
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $svc "paths" $paths.compactor "port" $port) }}
107+
{{- else if eq (include "loki.deployment.isDistributed" $ctx) "true" -}}
108+
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" (include "loki.resourceName" (dict "ctx" $ctx "component" "distributor")) "paths" $paths.distributor "port" $port) }}
109+
{{- /* query-frontend is optional; fall back to querier when it has no replicas and no autoscaler */ -}}
110+
{{- if or (gt (int $ctx.Values.queryFrontend.replicas) 0) $ctx.Values.queryFrontend.autoscaling.enabled $ctx.Values.queryFrontend.kedaAutoscaling.enabled -}}
111+
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" (include "loki.resourceName" (dict "ctx" $ctx "component" "query-frontend")) "paths" $paths.queryFrontend "port" $port) }}
112+
{{- else -}}
113+
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" (include "loki.resourceName" (dict "ctx" $ctx "component" "querier")) "paths" $paths.queryFrontend "port" $port) }}
114+
{{- end -}}
115+
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" (include "loki.resourceName" (dict "ctx" $ctx "component" "ruler")) "paths" $paths.ruler "port" $port) }}
116+
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" (include "loki.resourceName" (dict "ctx" $ctx "component" "compactor")) "paths" $paths.compactor "port" $port) }}
117+
{{- else if eq (include "loki.deployment.isScalable" $ctx) "true" -}}
118+
{{- $backend := include "loki.resourceName" (dict "ctx" $ctx "component" "backend") -}}
119+
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" (include "loki.resourceName" (dict "ctx" $ctx "component" "read")) "paths" $paths.queryFrontend "port" $port) }}
120+
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" (include "loki.resourceName" (dict "ctx" $ctx "component" "write")) "paths" $paths.distributor "port" $port) }}
121+
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $backend "paths" $paths.ruler "port" $port) }}
122+
{{- include "loki.route.serviceRule" (dict "ctx" $ctx "serviceName" $backend "paths" $paths.compactor "port" $port) }}
123+
{{- end -}}
124+
{{- end -}}
125+
126+
{{/*
127+
HTTPRoute rule for a single Loki service with path-prefix matches.
128+
Params:
129+
ctx - chart root context ($)
130+
serviceName - fully qualified k8s service name
131+
paths - list of URL path prefixes to match; rule is skipped when empty
132+
port - optional backend port; falls back to loki.server.http_listen_port
133+
*/}}
134+
{{- define "loki.route.serviceRule" -}}
135+
{{- if .paths }}
136+
- backendRefs:
137+
- group: ""
138+
kind: Service
139+
name: {{ .serviceName }}
140+
port: {{ .port | default .ctx.Values.loki.server.http_listen_port }}
141+
weight: 1
142+
matches:
143+
{{- range .paths }}
144+
- path:
145+
type: PathPrefix
146+
value: {{ . }}
147+
{{- end }}
148+
{{- end -}}
149+
{{- end -}}

charts/loki/templates/gateway/_helpers.tpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
gateway fullname
33
*/}}
44
{{- define "loki.gatewayFullname" -}}
5-
{{ include "loki.fullname" . }}-gateway
6-
{{- end }}
5+
{{- include "loki.fullname" . -}}-gateway
6+
{{- end -}}
77

88
{{/*
99
gateway common labels
1010
*/}}
1111
{{- define "loki.gatewayLabels" -}}
12-
{{ include "loki.labels" . }}
12+
{{- include "loki.labels" . }}
1313
app.kubernetes.io/component: gateway
14-
{{- end }}
14+
{{- end -}}
1515

1616
{{/*
1717
gateway selector labels

charts/loki/templates/gateway/route-gateway.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{{- range $name, $route := .Values.gateway.route -}}
33
{{- if $route.enabled -}}
44
{{- $routeName := ternary (include "loki.gatewayFullname" $) (printf "%s-%s" (include "loki.gatewayFullname" $) $name) (eq $name "main") -}}
5-
{{- $rules := include "loki.route.gatewayRule" (dict "ctx" $ "route" $route "name" $routeName "port" $.Values.gateway.service.port) -}}
5+
{{- $rules := include "loki.route.gatewayRule" (dict "ctx" $ "route" $route "serviceName" (include "loki.gatewayFullname" $) "port" $.Values.gateway.service.port) -}}
66
{{- include "loki.route" (dict "ctx" $ "route" $route "name" $routeName "rules" $rules "labelsTemplate" "loki.gatewayLabels") }}
77
{{- end -}}
88
{{- end -}}

charts/loki/templates/route.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{{- range $name, $route := .Values.route -}}
22
{{- if $route.enabled -}}
33
{{- $routeName := ternary (include "loki.fullname" $) (printf "%s-%s" (include "loki.fullname" $) $name) (eq $name "main") -}}
4-
{{- $rules := include "loki.route.rules" (dict "ctx" $ "paths" $route.paths) -}}
4+
{{- $kind := $route.kind | default "HTTPRoute" -}}
5+
{{- $port := $route.backendPort | default (ternary $.Values.loki.server.grpc_listen_port $.Values.loki.server.http_listen_port (eq $kind "GRPCRoute")) -}}
6+
{{- $rules := include "loki.route.rules" (dict "ctx" $ "paths" $route.paths "port" $port) -}}
57
{{- include "loki.route" (dict "ctx" $ "route" $route "name" $routeName "rules" $rules) }}
68
{{- end -}}
79
{{- end -}}

charts/loki/tests/gateway/route_test.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ tests:
5757
pattern: .*-loki-gateway-grpc$
5858
template: gateway/route-gateway.yaml
5959

60+
- it: should always route to the gateway service regardless of route key
61+
set:
62+
gateway.route.main.enabled: false
63+
gateway.route.grpc.enabled: true
64+
gateway.route.grpc.parentRefs:
65+
- name: my-gateway
66+
namespace: gateway-namespace
67+
asserts:
68+
- matchRegex:
69+
path: spec.rules[0].backendRefs[0].name
70+
pattern: .*-loki-gateway$
71+
template: gateway/route-gateway.yaml
72+
6073
- it: should render parentRefs
6174
asserts:
6275
- equal:

0 commit comments

Comments
 (0)