Skip to content

Commit f91b1b8

Browse files
committed
Merge pull request #2 from mgirgisf/cloudkitty-prom-collector
update prometheus_collector config
2 parents 294361a + d803db4 commit f91b1b8

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

api/v1beta1/cloudkitty_types.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ type CloudKittySpecBase struct {
9090
// TopologyRef to apply the Topology defined by the associated CR referenced
9191
// by name
9292
TopologyRef *topologyv1.TopoRef `json:"topologyRef,omitempty"`
93+
94+
// Host of user deployed prometheus
95+
// +kubebuilder:validation:Optional
96+
PrometheusHost string `json:"prometheusHost,omitempty"`
97+
98+
// Port of user deployed prometheus
99+
// +kubebuilder:validation:Minimum=1
100+
// +kubebuilder:validation:Maximum=65535
101+
// +kubebuilder:validation:Optional
102+
PrometheusPort int32 `json:"prometheusPort,omitempty"`
103+
104+
// If defined, specifies which CA certificate to use for user deployed prometheus
105+
// +kubebuilder:validation:Optional
106+
// +nullable
107+
PrometheusTLSCaCertSecret *corev1.SecretKeySelector `json:"prometheusTLSCaCertSecret,omitempty"`
93108
}
94109

95110
// CloudKittySpecCore the same as CloudKittySpec without ContainerImage references
@@ -211,6 +226,15 @@ type CloudKittyStatus struct {
211226
// controller has not started processing the latest changes, and the status
212227
// and its conditions are likely stale.
213228
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
229+
230+
// PrometheusHost - Hostname for prometheus used for autoscaling
231+
PrometheusHost string `json:"prometheusHostname,omitempty"`
232+
233+
// PrometheusPort - Port for prometheus used for autoscaling
234+
PrometheusPort int32 `json:"prometheusPort,omitempty"`
235+
236+
// PrometheusTLS - Determines if TLS should be used for accessing prometheus
237+
PrometheusTLS bool `json:"prometheusTLS,omitempty"`
214238
}
215239

216240
//+kubebuilder:object:root=true

api/v1beta1/telemetry_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ type CloudKittySection struct {
194194

195195
// +kubebuilder:validation:Optional
196196
//+operator-sdk:csv:customresourcedefinitions:type=spec
197+
// +kubebuilder:default={apiTimeout: 60}
197198
// Template - Overrides to use when creating the OpenStack CloudKitty service
198199
CloudKittySpec `json:",inline"`
199200
}

controllers/cloudkitty_controller.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22+
"strconv"
23+
2224
"github.com/openstack-k8s-operators/telemetry-operator/pkg/cloudkitty"
25+
"github.com/openstack-k8s-operators/telemetry-operator/pkg/metricstorage"
2326

2427
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
2528
"k8s.io/apimachinery/pkg/runtime"
@@ -813,6 +816,7 @@ func (r *CloudKittyReconciler) generateServiceConfigs(
813816
memcached *memcachedv1.Memcached,
814817
db *mariadbv1.Database,
815818
) error {
819+
Log := r.GetLogger(ctx)
816820
//
817821
// create Secret required for cloudkitty input
818822
// - %-scripts holds scripts to e.g. bootstrap the service
@@ -855,6 +859,46 @@ func (r *CloudKittyReconciler) generateServiceConfigs(
855859
return err
856860
}
857861

862+
if instance.Spec.PrometheusHost == "" {
863+
// We're using MetricStorage for Prometheus.
864+
prometheusEndpointSecret := &corev1.Secret{}
865+
err = r.Client.Get(ctx, client.ObjectKey{
866+
Name: cloudkitty.PrometheusEndpointSecret,
867+
Namespace: instance.Namespace,
868+
}, prometheusEndpointSecret)
869+
if err != nil {
870+
Log.Info("Prometheus Endpoint Secret not found")
871+
}
872+
if prometheusEndpointSecret.Data != nil {
873+
instance.Status.PrometheusHost = string(prometheusEndpointSecret.Data[metricstorage.PrometheusHost])
874+
port, err := strconv.Atoi(string(prometheusEndpointSecret.Data[metricstorage.PrometheusPort]))
875+
if err != nil {
876+
return err
877+
}
878+
instance.Status.PrometheusPort = int32(port)
879+
880+
metricStorage := &telemetryv1.MetricStorage{}
881+
err = r.Client.Get(ctx, client.ObjectKey{
882+
Namespace: instance.Namespace,
883+
Name: telemetryv1.DefaultServiceName,
884+
}, metricStorage)
885+
if err != nil {
886+
instance.Status.Conditions.Set(condition.FalseCondition(
887+
condition.ServiceConfigReadyCondition,
888+
condition.ErrorReason,
889+
condition.SeverityWarning,
890+
condition.ServiceConfigReadyErrorMessage,
891+
err.Error()))
892+
}
893+
instance.Status.PrometheusTLS = metricStorage.Spec.PrometheusTLS.Enabled()
894+
}
895+
} else {
896+
// We're using user-deployed Prometheus.
897+
instance.Status.PrometheusHost = instance.Spec.PrometheusHost
898+
instance.Status.PrometheusPort = instance.Spec.PrometheusPort
899+
instance.Status.PrometheusTLS = instance.Spec.PrometheusTLSCaCertSecret != nil
900+
}
901+
858902
databaseAccount := db.GetAccount()
859903
dbSecret := db.GetSecret()
860904

@@ -864,6 +908,8 @@ func (r *CloudKittyReconciler) generateServiceConfigs(
864908
templateParameters["KeystoneInternalURL"] = keystoneInternalURL
865909
templateParameters["KeystonePublicURL"] = keystonePublicURL
866910
templateParameters["TransportURL"] = string(transportURLSecret.Data["transport_url"])
911+
templateParameters["PrometheusHost"] = instance.Status.PrometheusHost
912+
templateParameters["PrometheusPort"] = instance.Status.PrometheusPort
867913
templateParameters["DatabaseConnection"] = fmt.Sprintf("mysql+pymysql://%s:%s@%s/%s?read_default_file=/etc/my.cnf",
868914
databaseAccount.Spec.UserName,
869915
string(dbSecret.Data[mariadbv1.DatabasePasswordSelector]),

pkg/cloudkitty/const.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ const (
4949

5050
ShortDuration = time.Duration(5) * time.Second
5151
NormalDuration = time.Duration(10) * time.Second
52+
53+
// PrometheusEndpointSecret - The name of the secret that contains the Prometheus endpoint configuration.
54+
PrometheusEndpointSecret = "metric-storage-prometheus-endpoint"
5255
)
5356

5457
var ResultRequeue = ctrl.Result{RequeueAfter: NormalDuration}

templates/cloudkitty/config/cloudkitty.conf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@ collector = prometheus
4545
scope_key = project
4646

4747
[collector_prometheus]
48-
prometheus_url = https://metric-storage-prometheus.openstack.svc:9090
48+
{{- if .TLS }}
49+
prometheus_url = https://{{ .PrometheusHost }}:{{ .PrometheusPort }}
50+
cafile = {{ .CAFile }}
51+
insecure = false
52+
{{- else }}
53+
prometheus_url = http://{{ .PrometheusHost }}:{{ .PrometheusPort }}
4954
insecure = true
55+
{{- end }}
5056

5157
[output]
5258
pipeline = osrf

0 commit comments

Comments
 (0)