Skip to content

Commit 7291ba9

Browse files
committed
agents: API key from a Secret (#14)
1 parent b154853 commit 7291ba9

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

api/v1/coroot_types.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,11 @@ type CorootSpec struct {
307307
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`
308308

309309
// The API key used by agents when sending telemetry to Coroot.
310-
ApiKey string `json:"apiKey,omitempty"`
311-
NodeAgent NodeAgentSpec `json:"nodeAgent,omitempty"`
312-
ClusterAgent ClusterAgentSpec `json:"clusterAgent,omitempty"`
310+
ApiKey string `json:"apiKey,omitempty"`
311+
// Secret containing API key.
312+
ApiKeySecret *corev1.SecretKeySelector `json:"apiKeySecret,omitempty"`
313+
NodeAgent NodeAgentSpec `json:"nodeAgent,omitempty"`
314+
ClusterAgent ClusterAgentSpec `json:"clusterAgent,omitempty"`
313315

314316
// Prometheus configuration.
315317
Prometheus PrometheusSpec `json:"prometheus,omitempty"`

api/v1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/coroot.com_coroots.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,29 @@ spec:
978978
description: The API key used by agents when sending telemetry to
979979
Coroot.
980980
type: string
981+
apiKeySecret:
982+
description: Secret containing API key.
983+
properties:
984+
key:
985+
description: The key of the secret to select from. Must be a
986+
valid secret key.
987+
type: string
988+
name:
989+
default: ""
990+
description: |-
991+
Name of the referent.
992+
This field is effectively required, but due to backwards compatibility is
993+
allowed to be empty. Instances of this type with an empty value here are
994+
almost certainly wrong.
995+
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
996+
type: string
997+
optional:
998+
description: Specify whether the Secret or its key must be defined
999+
type: boolean
1000+
required:
1001+
- key
1002+
type: object
1003+
x-kubernetes-map-type: atomic
9811004
authAnonymousRole:
9821005
description: Allows access to Coroot without authentication if set
9831006
(one of Admin, Editor, or Viewer).

controller/cluster_agent.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,19 @@ func (r *CorootReconciler) clusterAgentDeployment(cr *corootv1.Coroot) *appsv1.D
8484
}
8585
scrapeInterval := cmp.Or(cr.Spec.MetricsRefreshInterval, corootv1.DefaultMetricRefreshInterval)
8686
env := []corev1.EnvVar{
87-
{Name: "API_KEY", Value: cr.Spec.ApiKey},
8887
{Name: "COROOT_URL", Value: corootURL},
8988
{Name: "METRICS_SCRAPE_INTERVAL", Value: scrapeInterval},
9089
{Name: "KUBE_STATE_METRICS_ADDRESS", Value: "127.0.0.1:10302"},
9190
}
91+
92+
apiKey := corev1.EnvVar{Name: "API_KEY"}
93+
if cr.Spec.ApiKeySecret != nil {
94+
apiKey.ValueFrom = &corev1.EnvVarSource{SecretKeyRef: cr.Spec.ApiKeySecret}
95+
} else {
96+
apiKey.Value = cr.Spec.ApiKey
97+
}
98+
env = append(env, apiKey)
99+
92100
if tlsSkipVerify {
93101
env = append(env, corev1.EnvVar{Name: "INSECURE_SKIP_VERIFY", Value: "true"})
94102
}

controller/node_agent.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,17 @@ func (r *CorootReconciler) nodeAgentDaemonSet(cr *corootv1.Coroot) *appsv1.Daemo
3131
}
3232
scrapeInterval := cmp.Or(cr.Spec.MetricsRefreshInterval, corootv1.DefaultMetricRefreshInterval)
3333
env := []corev1.EnvVar{
34-
{Name: "API_KEY", Value: cr.Spec.ApiKey},
3534
{Name: "SCRAPE_INTERVAL", Value: scrapeInterval},
3635
}
3736

37+
apiKey := corev1.EnvVar{Name: "API_KEY"}
38+
if cr.Spec.ApiKeySecret != nil {
39+
apiKey.ValueFrom = &corev1.EnvVarSource{SecretKeyRef: cr.Spec.ApiKeySecret}
40+
} else {
41+
apiKey.Value = cr.Spec.ApiKey
42+
}
43+
env = append(env, apiKey)
44+
3845
if tlsSkipVerify {
3946
env = append(env, corev1.EnvVar{Name: "INSECURE_SKIP_VERIFY", Value: "true"})
4047
}

0 commit comments

Comments
 (0)