Skip to content

Commit caec744

Browse files
committed
Configure Prometheus data source in WatcherAPI controller
This patch is configuring prometheus data source in the watcherapi deployment based on the config values coming from the SubCrs secret and the values of TLS and PrometheusTLSCaCertSecret in the Spec. Note that the config parameters in the config file is taken from a watcher patch which is not merged yet although the config parameters seems to be already agreed [1]. [1] https://review.opendev.org/c/openstack/watcher/+/934423
1 parent c05a3c6 commit caec744

File tree

6 files changed

+65
-1
lines changed

6 files changed

+65
-1
lines changed

controllers/watcherapi_controller.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22+
"strconv"
2223
"time"
2324

2425
ctrl "sigs.k8s.io/controller-runtime"
@@ -155,6 +156,9 @@ func (r *WatcherAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request)
155156
[]string{
156157
instance.Spec.PasswordSelectors.Service,
157158
TransportURLSelector,
159+
PrometheusHostKey,
160+
PrometheusPortKey,
161+
PrometheusTLSKey,
158162
},
159163
helper.GetClient(),
160164
&instance.Status.Conditions,
@@ -268,6 +272,7 @@ func (r *WatcherAPIReconciler) generateServiceConfigs(
268272
databaseUsername := string(secret.Data[DatabaseUsername])
269273
databaseHostname := string(secret.Data[DatabaseHostname])
270274
databasePassword := string(secret.Data[DatabasePassword])
275+
prometheusTLS, _ := strconv.ParseBool(string(secret.Data[PrometheusTLSKey]))
271276
templateParameters := map[string]interface{}{
272277
"DatabaseConnection": fmt.Sprintf("mysql+pymysql://%s:%s@%s/%s?charset=utf8",
273278
databaseUsername,
@@ -282,6 +287,10 @@ func (r *WatcherAPIReconciler) generateServiceConfigs(
282287
"MemcachedServers": memcachedInstance.GetMemcachedServerListString(),
283288
"LogFile": fmt.Sprintf("%s%s.log", watcher.WatcherLogPath, instance.Name),
284289
"APIPublicPort": fmt.Sprintf("%d", watcher.WatcherPublicPort),
290+
"PrometheusHost": string(secret.Data[PrometheusHostKey]),
291+
"PrometheusPort": string(secret.Data[PrometheusPortKey]),
292+
"PrometheusTLS": prometheusTLS,
293+
"PrometheusCaCert": string(secret.Data[PrometheusCaCertKey]),
285294
}
286295

287296
// create httpd vhost template parameters

pkg/watcher/volumes.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,25 @@ func GetKollaConfigVolumeMount(serviceName string) corev1.VolumeMount {
110110
ReadOnly: true,
111111
}
112112
}
113+
114+
// getCustomPrometheusCaVolume - Volume for CA certificate of user deployed Prometheus
115+
func GetCustomPrometheusCaVolume(secretName string) corev1.Volume {
116+
return corev1.Volume{
117+
Name: "custom-prometheus-ca",
118+
VolumeSource: corev1.VolumeSource{
119+
Secret: &corev1.SecretVolumeSource{
120+
SecretName: secretName,
121+
},
122+
},
123+
}
124+
}
125+
126+
// getCustomPrometheusCaVolumeMount - VolumeMount for CA certificate of user deployed Prometheus
127+
func GetCustomPrometheusCaVolumeMount(fileName string) corev1.VolumeMount {
128+
return corev1.VolumeMount{
129+
Name: "custom-prometheus-ca",
130+
MountPath: CustomPrometheusCaCertFolderPath + fileName,
131+
SubPath: fileName,
132+
ReadOnly: true,
133+
}
134+
}

pkg/watcherapi/deployment.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ func Deployment(
6767
}
6868
apiVolumeMounts = append(apiVolumeMounts, watcher.GetLogVolumeMount()...)
6969

70+
// Create mount for bundle CA if defined in TLS.CaBundleSecretName
71+
if instance.Spec.TLS.CaBundleSecretName != "" {
72+
apiVolumes = append(apiVolumes, instance.Spec.TLS.CreateVolume())
73+
apiVolumeMounts = append(apiVolumeMounts, instance.Spec.TLS.CreateVolumeMounts(nil)...)
74+
}
75+
76+
// add prometheus CA cert if defined
77+
if instance.Spec.PrometheusTLSCaCertSecret != nil {
78+
apiVolumes = append(apiVolumes, watcher.GetCustomPrometheusCaVolume(instance.Spec.PrometheusTLSCaCertSecret.Name))
79+
apiVolumeMounts = append(apiVolumeMounts, watcher.GetCustomPrometheusCaVolumeMount(instance.Spec.PrometheusTLSCaCertSecret.Key))
80+
}
81+
7082
deployment := &appsv1.Deployment{
7183
ObjectMeta: metav1.ObjectMeta{
7284
Name: fmt.Sprintf("%s-api", instance.Name),

templates/watcher/config/00-default.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,13 @@ datasources = ceilometer
5959
[cache]
6060
memcached_servers = {{ .MemcachedServers }}
6161
{{ end }}
62+
63+
{{ if (index . "PrometheusHost") }}
64+
[prometheus_client]
65+
host = {{ .PrometheusHost }}
66+
port = {{ .PrometheusPort }}
67+
fqdn_label = fqdn
68+
{{ if (index . "PrometheusTLS") }}
69+
cafile = {{ .PrometheusCaCert }}
70+
{{ end }}
71+
{{ end }}

tests/functional/watcher_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ var _ = Describe("Watcher controller", func() {
860860
deployment := th.GetDeployment(watcherTest.WatcherAPIDeployment)
861861
Expect(deployment.Spec.Template.Spec.ServiceAccountName).To(Equal("watcher-watcher"))
862862
Expect(int(*deployment.Spec.Replicas)).To(Equal(2))
863-
Expect(deployment.Spec.Template.Spec.Volumes).To(HaveLen(3))
863+
Expect(deployment.Spec.Template.Spec.Volumes).To(HaveLen(5))
864864
Expect(deployment.Spec.Template.Spec.Containers).To(HaveLen(2))
865865
Expect(deployment.Spec.Selector.MatchLabels).To(Equal(map[string]string{"service": "watcher-api"}))
866866

tests/functional/watcherapi_controller_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ var _ = Describe("WatcherAPI controller with minimal spec values", func() {
3434
Expect(WatcherAPI.Spec.Secret).Should(Equal("osp-secret"))
3535
Expect(WatcherAPI.Spec.MemcachedInstance).Should(Equal("memcached"))
3636
Expect(WatcherAPI.Spec.PasswordSelectors).Should(Equal(watcherv1beta1.PasswordSelector{Service: "WatcherPassword"}))
37+
Expect(WatcherAPI.Spec.TLS.CaBundleSecretName).Should(Equal(""))
38+
Expect(WatcherAPI.Spec.PrometheusTLSCaCertSecret).Should(BeNil())
3739
})
3840

3941
It("should have the Status fields initialized", func() {
@@ -111,6 +113,9 @@ var _ = Describe("WatcherAPI controller", func() {
111113
map[string][]byte{
112114
"WatcherPassword": []byte("service-password"),
113115
"transport_url": []byte("url"),
116+
"prometheus_host": []byte("prometheus.example.com"),
117+
"prometheus_port": []byte("1234"),
118+
"prometheus_tls": []byte("false"),
114119
},
115120
)
116121
DeferCleanup(k8sClient.Delete, ctx, secret)
@@ -249,6 +254,9 @@ var _ = Describe("WatcherAPI controller", func() {
249254
"database_username": []byte("username"),
250255
"database_password": []byte("password"),
251256
"database_hostname": []byte("hostname"),
257+
"prometheus_host": []byte("prometheus.example.com"),
258+
"prometheus_port": []byte("1234"),
259+
"prometheus_tls": []byte("false"),
252260
},
253261
)
254262
DeferCleanup(k8sClient.Delete, ctx, secret)
@@ -291,6 +299,9 @@ var _ = Describe("WatcherAPI controller", func() {
291299
"database_username": []byte("username"),
292300
"database_password": []byte("password"),
293301
"database_hostname": []byte("hostname"),
302+
"prometheus_host": []byte("prometheus.example.com"),
303+
"prometheus_port": []byte("1234"),
304+
"prometheus_tls": []byte("false"),
294305
},
295306
)
296307
DeferCleanup(k8sClient.Delete, ctx, secret)

0 commit comments

Comments
 (0)