Skip to content

Commit 5ffd74d

Browse files
committed
Add LokiStack operator status integration
Integrate with LokiStack operator to use its status conditions instead of querying the Loki status endpoint. This adds support for detecting Loki readiness through the operator's status API when available. Changes: - Add github.com/grafana/loki/operator/apis/loki dependency - Add LokiStackStatus field to Loki config - Check operator status in getLokiStatus before querying status URL - Prevent status URL usage when using Loki operator
1 parent a2cf608 commit 5ffd74d

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ require (
6363
k8s.io/klog/v2 v2.130.1 // indirect
6464
k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b // indirect
6565
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397 // indirect
66+
sigs.k8s.io/controller-runtime v0.20.4 // indirect
6667
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
6768
sigs.k8s.io/randfill v1.0.0 // indirect
6869
sigs.k8s.io/structured-merge-diff/v6 v6.3.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b h1:MloQ9/bdJyIu9lb1PzujOP
233233
k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b/go.mod h1:UZ2yyWbFTpuhSbFhv24aGNOdoRdJZgsIObGBUaYVsts=
234234
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397 h1:hwvWFiBzdWw1FhfY1FooPn3kzWuJ8tmbZBHi4zVsl1Y=
235235
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
236+
sigs.k8s.io/controller-runtime v0.20.4 h1:X3c+Odnxz+iPTRobG4tp092+CvBU9UK0t/bRf+n0DGU=
237+
sigs.k8s.io/controller-runtime v0.20.4/go.mod h1:xg2XB0K5ShQzAgsoujxuKN4LNXR2LfwwHsPj7Iaw+XY=
236238
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 h1:gBQPwqORJ8d8/YNZWEjoZs7npUVDpVXUUOFfW6CgAqE=
237239
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8/go.mod h1:mdzfpAEoE6DHQEN0uh9ZbOCuHbLK5wOm7dK4ctXE9Tg=
238240
sigs.k8s.io/randfill v1.0.0 h1:JfjMILfT8A6RbawdsK2JXGBR5AQVfd+9TbzrlneTyrU=

pkg/config/loki.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Loki struct {
1818
TokenPath string `yaml:"tokenPath,omitempty" json:"tokenPath,omitempty"`
1919
SkipTLS bool `yaml:"skipTls,omitempty" json:"skipTls,omitempty"`
2020
CAPath string `yaml:"caPath,omitempty" json:"caPath,omitempty"`
21+
Status string `yaml:"status,omitempty" json:"status,omitempty"`
2122
StatusSkipTLS bool `yaml:"statusSkipTls,omitempty" json:"statusSkipTls,omitempty"`
2223
StatusCAPath string `yaml:"statusCaPath,omitempty" json:"statusCaPath,omitempty"`
2324
StatusUserCertPath string `yaml:"statusUserCertPath,omitempty" json:"statusUserCertPath,omitempty"`

pkg/handler/loki.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ func getLokiNamesForPrefix(cfg *config.Loki, lokiClient httpclient.Caller, filts
198198
}
199199

200200
func (h *Handlers) getLokiStatus(r *http.Request) ([]byte, int, error) {
201+
// Check if the status was provided by the operator
202+
if h.Cfg.Loki.Status != "" {
203+
return []byte(h.Cfg.Loki.Status), 200, nil
204+
}
201205
lokiClient := newLokiClient(&h.Cfg.Loki, r.Header, true)
202206
baseURL := strings.TrimRight(h.Cfg.Loki.GetStatusURL(), "/")
203207
return executeLokiQuery(fmt.Sprintf("%s/%s", baseURL, "ready"), lokiClient)
@@ -231,6 +235,10 @@ func (h *Handlers) LokiMetrics() func(w http.ResponseWriter, r *http.Request) {
231235
writeError(w, http.StatusBadRequest, "Loki is disabled")
232236
return
233237
}
238+
if h.Cfg.Loki.Status != nil {
239+
writeError(w, http.StatusBadRequest, "Loki status URL is not usable with Loki operator")
240+
return
241+
}
234242
lokiClient := newLokiClient(&h.Cfg.Loki, r.Header, true)
235243
baseURL := strings.TrimRight(h.Cfg.Loki.GetStatusURL(), "/")
236244

@@ -250,6 +258,10 @@ func (h *Handlers) LokiBuildInfos() func(w http.ResponseWriter, r *http.Request)
250258
writeError(w, http.StatusBadRequest, "Loki is disabled")
251259
return
252260
}
261+
if h.Cfg.Loki.Status != nil {
262+
writeError(w, http.StatusBadRequest, "Loki status URL is not usable with Loki operator")
263+
return
264+
}
253265
lokiClient := newLokiClient(&h.Cfg.Loki, r.Header, true)
254266
baseURL := strings.TrimRight(h.Cfg.Loki.GetStatusURL(), "/")
255267

@@ -264,6 +276,10 @@ func (h *Handlers) LokiBuildInfos() func(w http.ResponseWriter, r *http.Request)
264276
}
265277

266278
func (h *Handlers) fetchLokiConfig(cl httpclient.Caller, output any) error {
279+
if h.Cfg.Loki.Status != nil {
280+
return fmt.Errorf("loki status url is not usable with Loki operator")
281+
}
282+
267283
baseURL := strings.TrimRight(h.Cfg.Loki.GetStatusURL(), "/")
268284

269285
resp, _, err := executeLokiQuery(fmt.Sprintf("%s/%s", baseURL, "config"), cl)

vendor/modules.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,8 @@ k8s.io/utils/clock
500500
k8s.io/utils/internal/third_party/forked/golang/net
501501
k8s.io/utils/net
502502
k8s.io/utils/ptr
503+
# sigs.k8s.io/controller-runtime v0.20.4
504+
## explicit; go 1.23.0
503505
# sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8
504506
## explicit; go 1.23
505507
sigs.k8s.io/json

0 commit comments

Comments
 (0)