Skip to content

Commit d5ef3a1

Browse files
committed
feat(Grafana): useKubeAuth enables using k8s serviceacccount
1 parent 5409d78 commit d5ef3a1

File tree

6 files changed

+40
-1
lines changed

6 files changed

+40
-1
lines changed

api/v1beta1/grafana_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ type JsonnetConfig struct {
125125

126126
// GrafanaClient contains the Grafana API client settings
127127
type GrafanaClient struct {
128+
// Use Kubernetes Serviceaccount as authentication
129+
// Requires configuring [auth.jwt] in the instance
130+
// +optional
131+
UseKubeAuth bool `json:"useKubeAuth,omitempty"`
128132
// +nullable
129133
TimeoutSeconds *int `json:"timeout,omitempty"`
130134
// +nullable

config/crd/bases/grafana.integreatly.org_grafanas.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ spec:
9090
x-kubernetes-validations:
9191
- message: insecureSkipVerify and certSecretRef cannot be set at the same time
9292
rule: (has(self.insecureSkipVerify) && !(has(self.certSecretRef))) || (has(self.certSecretRef) && !(has(self.insecureSkipVerify)))
93+
useKubeAuth:
94+
description: |-
95+
Use Kubernetes Serviceaccount as authentication
96+
Requires configuring [auth.jwt] in the instance
97+
type: boolean
9398
type: object
9499
config:
95100
additionalProperties:

controllers/client/grafana_client.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/http"
77
"net/url"
8+
"os"
89
"time"
910

1011
genapi "github.com/grafana/grafana-openapi-client-go/client"
@@ -62,6 +63,17 @@ func getExternalAdminPassword(ctx context.Context, c client.Client, cr *v1beta1.
6263
func getAdminCredentials(ctx context.Context, c client.Client, grafana *v1beta1.Grafana) (*grafanaAdminCredentials, error) {
6364
credentials := &grafanaAdminCredentials{}
6465

66+
if grafana.Spec.Client != nil && grafana.Spec.Client.UseKubeAuth {
67+
b, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token")
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
credentials.apikey = string(b)
73+
74+
return credentials, nil
75+
}
76+
6577
if grafana.IsExternal() {
6678
// prefer api key if present
6779
if grafana.Spec.External.APIKey != nil {
@@ -151,7 +163,7 @@ func InjectAuthHeaders(ctx context.Context, c client.Client, grafana *v1beta1.Gr
151163
}
152164

153165
if creds.apikey != "" {
154-
req.Header.Add("Authorization", "Bearer "+creds.apikey)
166+
req.Header.Set("Authorization", "Bearer "+creds.apikey)
155167
} else {
156168
req.SetBasicAuth(creds.adminUser, creds.adminPassword)
157169
}

deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanas.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ spec:
9090
x-kubernetes-validations:
9191
- message: insecureSkipVerify and certSecretRef cannot be set at the same time
9292
rule: (has(self.insecureSkipVerify) && !(has(self.certSecretRef))) || (has(self.certSecretRef) && !(has(self.insecureSkipVerify)))
93+
useKubeAuth:
94+
description: |-
95+
Use Kubernetes Serviceaccount as authentication
96+
Requires configuring [auth.jwt] in the instance
97+
type: boolean
9398
type: object
9499
config:
95100
additionalProperties:

deploy/kustomize/base/crds.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3407,6 +3407,11 @@ spec:
34073407
at the same time
34083408
rule: (has(self.insecureSkipVerify) && !(has(self.certSecretRef)))
34093409
|| (has(self.certSecretRef) && !(has(self.insecureSkipVerify)))
3410+
useKubeAuth:
3411+
description: |-
3412+
Use Kubernetes Serviceaccount as authentication
3413+
Requires configuring [auth.jwt] in the instance
3414+
type: boolean
34103415
type: object
34113416
config:
34123417
additionalProperties:

docs/docs/api.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6444,6 +6444,14 @@ Client defines how the grafana-operator talks to the grafana instance.
64446444
<i>Validations</i>:<li>(has(self.insecureSkipVerify) && !(has(self.certSecretRef))) || (has(self.certSecretRef) && !(has(self.insecureSkipVerify))): insecureSkipVerify and certSecretRef cannot be set at the same time</li>
64456445
</td>
64466446
<td>false</td>
6447+
</tr><tr>
6448+
<td><b>useKubeAuth</b></td>
6449+
<td>boolean</td>
6450+
<td>
6451+
Use Kubernetes Serviceaccount as authentication
6452+
Requires configuring [auth.jwt] in the instance<br/>
6453+
</td>
6454+
<td>false</td>
64476455
</tr></tbody>
64486456
</table>
64496457

0 commit comments

Comments
 (0)