Skip to content

Commit e50f6f6

Browse files
committed
prometheus: add externalPrometheus configuration
1 parent 1a2f92b commit e50f6f6

File tree

5 files changed

+176
-6
lines changed

5 files changed

+176
-6
lines changed

api/v1/coroot_types.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,33 @@ type PrometheusSpec struct {
8787
// Annotations for prometheus pods.
8888
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`
8989
// Metrics retention time (e.g. 4h, 3d, 2w, 1y; default 2d)
90-
// +kubebuilder:validation:Pattern=^\d+[mhdwy]$
90+
// +kubebuilder:validation:Pattern="^[0-9]+[mhdwy]$"
9191
Retention string `json:"retention,omitempty"`
9292
Image ImageSpec `json:"image,omitempty"`
9393
}
9494

95+
type ExternalPrometheusSpec struct {
96+
// http(s)://IP:Port or http(s)://Domain:Port or http(s)://Service Name:Port
97+
// +kubebuilder:validation:Pattern="^https?://.+$"
98+
URL string `json:"url,omitempty"`
99+
// Whether to skip verification of the Prometheus server's TLS certificate.
100+
TLSSkipVerify bool `json:"tlsSkipVerify,omitempty"`
101+
// Basic auth credentials.
102+
BasicAuth *BasicAuthSpec `json:"basicAuth,omitempty"`
103+
// Custom headers to include in requests to the Prometheus server.
104+
CustomHeaders map[string]string `json:"customHeaders,omitempty"`
105+
// The URL for metric ingestion though the Prometheus Remote Write protocol (optional).
106+
// +kubebuilder:validation:Pattern="^https?://.+$"
107+
RemoteWriteUrl string `json:"remoteWriteURL,omitempty"`
108+
}
109+
110+
type BasicAuthSpec struct {
111+
Username string `json:"username,omitempty"`
112+
Password string `json:"password,omitempty"`
113+
// Secret containing password. If specified, this takes precedence over the Password field.
114+
PasswordSecret *corev1.SecretKeySelector `json:"passwordSecret,omitempty"`
115+
}
116+
95117
type ClickhouseSpec struct {
96118
Shards int `json:"shards,omitempty"`
97119
Replicas int `json:"replicas,omitempty"`
@@ -233,6 +255,8 @@ type CorootSpec struct {
233255

234256
// Prometheus configuration.
235257
Prometheus PrometheusSpec `json:"prometheus,omitempty"`
258+
// Use an external Prometheus instance instead of deploying one.
259+
ExternalPrometheus *ExternalPrometheusSpec `json:"externalPrometheus,omitempty"`
236260

237261
// Clickhouse configuration.
238262
Clickhouse ClickhouseSpec `json:"clickhouse,omitempty"`

api/v1/zz_generated.deepcopy.go

Lines changed: 52 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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4640,6 +4640,64 @@ spec:
46404640
description: Username for accessing the external ClickHouse.
46414641
type: string
46424642
type: object
4643+
externalPrometheus:
4644+
description: Use an external Prometheus instance instead of deploying
4645+
one.
4646+
properties:
4647+
basicAuth:
4648+
description: Basic auth credentials.
4649+
properties:
4650+
password:
4651+
type: string
4652+
passwordSecret:
4653+
description: Secret containing password. If specified, this
4654+
takes precedence over the Password field.
4655+
properties:
4656+
key:
4657+
description: The key of the secret to select from. Must
4658+
be a valid secret key.
4659+
type: string
4660+
name:
4661+
default: ""
4662+
description: |-
4663+
Name of the referent.
4664+
This field is effectively required, but due to backwards compatibility is
4665+
allowed to be empty. Instances of this type with an empty value here are
4666+
almost certainly wrong.
4667+
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
4668+
type: string
4669+
optional:
4670+
description: Specify whether the Secret or its key must
4671+
be defined
4672+
type: boolean
4673+
required:
4674+
- key
4675+
type: object
4676+
x-kubernetes-map-type: atomic
4677+
username:
4678+
type: string
4679+
type: object
4680+
customHeaders:
4681+
additionalProperties:
4682+
type: string
4683+
description: Custom headers to include in requests to the Prometheus
4684+
server.
4685+
type: object
4686+
remoteWriteURL:
4687+
description: The URL for metric ingestion though the Prometheus
4688+
Remote Write protocol (optional).
4689+
pattern: ^https?://.+$
4690+
type: string
4691+
tlsSkipVerify:
4692+
description: Whether to skip verification of the Prometheus server's
4693+
TLS certificate.
4694+
type: boolean
4695+
url:
4696+
description: http(s)://IP:Port or http(s)://Domain:Port or http(s)://Service
4697+
Name:Port
4698+
pattern: ^https?://.+$
4699+
type: string
4700+
type: object
46434701
ingress:
46444702
description: Ingress configuration for Coroot.
46454703
properties:

controller/controller.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,14 @@ func (r *CorootReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
139139
}
140140
r.CreateOrUpdateIngress(ctx, cr, r.corootIngress(cr), cr.Spec.Ingress == nil)
141141

142-
r.CreateOrUpdateServiceAccount(ctx, cr, "prometheus", sccNonroot)
143-
r.CreateOrUpdatePVC(ctx, cr, r.prometheusPVC(cr), cr.Spec.Prometheus.Storage.ReclaimPolicy)
144-
r.CreateOrUpdateDeployment(ctx, cr, r.prometheusDeployment(cr))
145-
r.CreateOrUpdateService(ctx, cr, r.prometheusService(cr))
142+
if cr.Spec.ExternalPrometheus == nil {
143+
r.CreateOrUpdateServiceAccount(ctx, cr, "prometheus", sccNonroot)
144+
r.CreateOrUpdatePVC(ctx, cr, r.prometheusPVC(cr), cr.Spec.Prometheus.Storage.ReclaimPolicy)
145+
r.CreateOrUpdateDeployment(ctx, cr, r.prometheusDeployment(cr))
146+
r.CreateOrUpdateService(ctx, cr, r.prometheusService(cr))
147+
} else {
148+
// TODO: delete
149+
}
146150

147151
if cr.Spec.ExternalClickhouse == nil {
148152
r.CreateSecret(ctx, cr, r.clickhouseSecret(cr))

controller/coroot.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ func (r *CorootReconciler) corootStatefulSet(cr *corootv1.Coroot) *appsv1.Statef
157157

158158
env := []corev1.EnvVar{
159159
{Name: "GLOBAL_REFRESH_INTERVAL", Value: refreshInterval},
160-
{Name: "GLOBAL_PROMETHEUS_URL", Value: fmt.Sprintf("http://%s-prometheus.%s:9090", cr.Name, cr.Namespace)},
161160
{Name: "INSTALLATION_TYPE", Value: "k8s-operator"},
162161
}
163162
if cr.Spec.CacheTTL.Duration > 0 {
@@ -179,6 +178,39 @@ func (r *CorootReconciler) corootStatefulSet(cr *corootv1.Coroot) *appsv1.Statef
179178
env = append(env, corev1.EnvVar{Name: "LICENSE_KEY", Value: cr.Spec.EnterpriseEdition.LicenseKey})
180179
}
181180

181+
if ep := cr.Spec.ExternalPrometheus; ep != nil {
182+
env = append(env,
183+
corev1.EnvVar{Name: "GLOBAL_PROMETHEUS_URL", Value: ep.URL},
184+
)
185+
if ep.TLSSkipVerify {
186+
env = append(env, corev1.EnvVar{Name: "GLOBAL_PROMETHEUS_TLS_SKIP_VERIFY", Value: "true"})
187+
}
188+
if customHeaders := ep.CustomHeaders; len(customHeaders) > 0 {
189+
var headers []string
190+
for name, value := range customHeaders {
191+
headers = append(headers, fmt.Sprintf("%s=%s", name, value))
192+
}
193+
env = append(env, corev1.EnvVar{Name: "GLOBAL_PROMETHEUS_CUSTOM_HEADERS", Value: strings.Join(headers, "\n")})
194+
}
195+
if basicAuth := ep.BasicAuth; basicAuth != nil {
196+
env = append(env, corev1.EnvVar{Name: "GLOBAL_PROMETHEUS_USER", Value: basicAuth.Username})
197+
password := corev1.EnvVar{Name: "GLOBAL_PROMETHEUS_PASSWORD"}
198+
if basicAuth.PasswordSecret != nil {
199+
password.ValueFrom = &corev1.EnvVarSource{SecretKeyRef: basicAuth.PasswordSecret}
200+
} else {
201+
password.Value = basicAuth.Password
202+
}
203+
env = append(env, password)
204+
}
205+
if ep.RemoteWriteUrl != "" {
206+
env = append(env, corev1.EnvVar{Name: "GLOBAL_PROMETHEUS_REMOTE_WRITE_URL", Value: ep.RemoteWriteUrl})
207+
}
208+
} else {
209+
env = append(env,
210+
corev1.EnvVar{Name: "GLOBAL_PROMETHEUS_URL", Value: fmt.Sprintf("http://%s-prometheus.%s:9090", cr.Name, cr.Namespace)},
211+
)
212+
}
213+
182214
if ec := cr.Spec.ExternalClickhouse; ec != nil {
183215
env = append(env,
184216
corev1.EnvVar{Name: "GLOBAL_CLICKHOUSE_ADDRESS", Value: ec.Address},

0 commit comments

Comments
 (0)