|
1 | 1 | package metrics |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "crypto/x509" |
| 5 | + "encoding/pem" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
4 | 10 | promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" |
| 11 | + v1 "k8s.io/api/core/v1" |
5 | 12 | rbac "k8s.io/api/rbac/v1" |
6 | 13 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
7 | 14 | "k8s.io/utils/ptr" |
8 | | - |
9 | 15 | "kubevirt.io/ssp-operator/pkg/monitoring/rules" |
10 | 16 | ) |
11 | 17 |
|
12 | 18 | const ( |
13 | | - MonitorNamespace = "openshift-monitoring" |
14 | | - defaultRunbookURLTemplate = "https://kubevirt.io/monitoring/runbooks/%s" |
15 | | - runbookURLTemplateEnv = "RUNBOOK_URL_TEMPLATE" |
16 | | - PrometheusLabelKey = "prometheus.ssp.kubevirt.io" |
17 | | - PrometheusLabelValue = "true" |
18 | | - PrometheusClusterRoleName = "prometheus-k8s-ssp" |
19 | | - PrometheusServiceAccountName = "prometheus-k8s" |
20 | | - MetricsPortName = "http-metrics" |
| 19 | + MonitorNamespace = "openshift-monitoring" |
| 20 | + defaultRunbookURLTemplate = "https://kubevirt.io/monitoring/runbooks/%s" |
| 21 | + runbookURLTemplateEnv = "RUNBOOK_URL_TEMPLATE" |
| 22 | + PrometheusLabelKey = "prometheus.ssp.kubevirt.io" |
| 23 | + PrometheusLabelValue = "true" |
| 24 | + PrometheusClusterRoleName = "prometheus-k8s-ssp" |
| 25 | + PrometheusServiceAccountName = "prometheus-k8s" |
| 26 | + MetricsPortName = "http-metrics" |
| 27 | + CertFilename = "tls.crt" |
| 28 | + DefaultCertsDirectory = "/tmp/k8s-webhook-server/serving-certs" |
| 29 | + TemplateValidatorMetricsServiceName = "template-validator-metrics" |
| 30 | + MetricsServiceName = "ssp-operator-metrics" |
| 31 | + MetricsServiceKey = "metrics.ssp.kubevirt.io" |
21 | 32 | ) |
22 | 33 |
|
| 34 | +// Variable to store OLM deployment info (set from main) |
| 35 | +var isOLMDeployment bool |
| 36 | + |
| 37 | +func SetOLMDeployment(isOLM bool) { |
| 38 | + isOLMDeployment = isOLM |
| 39 | +} |
| 40 | + |
23 | 41 | func newMonitoringClusterRole() *rbac.ClusterRole { |
24 | 42 | return &rbac.ClusterRole{ |
25 | 43 | ObjectMeta: metav1.ObjectMeta{ |
@@ -61,31 +79,122 @@ func ServiceMonitorLabels() map[string]string { |
61 | 79 | } |
62 | 80 | } |
63 | 81 |
|
64 | | -func newServiceMonitorCR(namespace string) *promv1.ServiceMonitor { |
65 | | - return &promv1.ServiceMonitor{ |
| 82 | +func extractHostnameFromCert(certPath string) (string, error) { |
| 83 | + certBytes, err := os.ReadFile(certPath) |
| 84 | + if err != nil { |
| 85 | + return "", fmt.Errorf("failed to read certificate file: %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + block, _ := pem.Decode(certBytes) |
| 89 | + if block == nil { |
| 90 | + return "", fmt.Errorf("failed to parse certificate PEM") |
| 91 | + } |
| 92 | + |
| 93 | + cert, err := x509.ParseCertificate(block.Bytes) |
| 94 | + if err != nil { |
| 95 | + return "", fmt.Errorf("failed to parse certificate: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + if cert.Subject.CommonName != "" { |
| 99 | + return cert.Subject.CommonName, nil |
| 100 | + } |
| 101 | + |
| 102 | + if len(cert.DNSNames) > 0 { |
| 103 | + return cert.DNSNames[0], nil |
| 104 | + } |
| 105 | + |
| 106 | + return "", fmt.Errorf("no hostname found in certificate") |
| 107 | +} |
| 108 | + |
| 109 | +// getCAConfigForServiceMonitor returns the appropriate CA configuration |
| 110 | +func getCAConfigForServiceMonitor() *promv1.SecretOrConfigMap { |
| 111 | + if isOLMDeployment { |
| 112 | + // OLM deployment: use ssp-operator-service-cert secret with olmCAKey |
| 113 | + return &promv1.SecretOrConfigMap{ |
| 114 | + Secret: &v1.SecretKeySelector{ |
| 115 | + LocalObjectReference: v1.LocalObjectReference{ |
| 116 | + Name: "ssp-operator-service-cert", |
| 117 | + }, |
| 118 | + Key: "olmCAKey", |
| 119 | + }, |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Service-CA deployment: use openshift-service-ca.crt configmap |
| 124 | + return &promv1.SecretOrConfigMap{ |
| 125 | + ConfigMap: &v1.ConfigMapKeySelector{ |
| 126 | + LocalObjectReference: v1.LocalObjectReference{ |
| 127 | + Name: "openshift-service-ca.crt", |
| 128 | + }, |
| 129 | + Key: "service-ca.crt", |
| 130 | + }, |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +func newValidatorServiceMonitor(namespace string) *promv1.ServiceMonitor { |
| 135 | + tlsConfig := &promv1.TLSConfig{ |
| 136 | + SafeTLSConfig: promv1.SafeTLSConfig{ |
| 137 | + InsecureSkipVerify: ptr.To(false), |
| 138 | + CA: promv1.SecretOrConfigMap{ |
| 139 | + ConfigMap: &v1.ConfigMapKeySelector{ |
| 140 | + LocalObjectReference: v1.LocalObjectReference{ |
| 141 | + Name: "openshift-service-ca.crt", |
| 142 | + }, |
| 143 | + Key: "service-ca.crt", |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + } |
| 148 | + tlsConfig.ServerName = ptr.To("virt-template-validator.kubevirt.svc") |
| 149 | + |
| 150 | + serviceMonitor := newServiceMonitor(TemplateValidatorMetricsServiceName, namespace, tlsConfig, metav1.LabelSelector{ |
| 151 | + MatchLabels: map[string]string{ |
| 152 | + MetricsServiceKey: TemplateValidatorMetricsServiceName, |
| 153 | + }, |
| 154 | + }) |
| 155 | + return &serviceMonitor |
| 156 | +} |
| 157 | + |
| 158 | +func newSspServiceMonitor(namespace string) *promv1.ServiceMonitor { |
| 159 | + certPath := filepath.Join(DefaultCertsDirectory, CertFilename) |
| 160 | + hostname, _ := extractHostnameFromCert(certPath) |
| 161 | + |
| 162 | + tlsConfig := &promv1.TLSConfig{ |
| 163 | + SafeTLSConfig: promv1.SafeTLSConfig{ |
| 164 | + InsecureSkipVerify: ptr.To(false), |
| 165 | + // Use appropriate CA based on deployment type |
| 166 | + CA: *getCAConfigForServiceMonitor(), |
| 167 | + }, |
| 168 | + } |
| 169 | + |
| 170 | + if hostname != "" { |
| 171 | + tlsConfig.ServerName = &hostname |
| 172 | + } |
| 173 | + |
| 174 | + serviceMonitor := newServiceMonitor(rules.RuleName, namespace, tlsConfig, metav1.LabelSelector{}) |
| 175 | + return &serviceMonitor |
| 176 | +} |
| 177 | + |
| 178 | +func newServiceMonitor(name, |
| 179 | + namespace string, |
| 180 | + tlsConfig *promv1.TLSConfig, |
| 181 | + selector metav1.LabelSelector) promv1.ServiceMonitor { |
| 182 | + return promv1.ServiceMonitor{ |
66 | 183 | ObjectMeta: metav1.ObjectMeta{ |
67 | 184 | Namespace: namespace, |
68 | | - Name: rules.RuleName, |
| 185 | + Name: name, |
69 | 186 | Labels: ServiceMonitorLabels(), |
70 | 187 | }, |
71 | 188 | Spec: promv1.ServiceMonitorSpec{ |
72 | 189 | NamespaceSelector: promv1.NamespaceSelector{ |
73 | 190 | Any: true, |
74 | 191 | }, |
75 | | - Selector: metav1.LabelSelector{ |
76 | | - MatchLabels: map[string]string{ |
77 | | - PrometheusLabelKey: PrometheusLabelValue, |
78 | | - }, |
79 | | - }, |
| 192 | + Selector: selector, |
80 | 193 | Endpoints: []promv1.Endpoint{ |
81 | 194 | { |
82 | | - Port: MetricsPortName, |
83 | | - Scheme: "https", |
84 | | - TLSConfig: &promv1.TLSConfig{ |
85 | | - SafeTLSConfig: promv1.SafeTLSConfig{ |
86 | | - InsecureSkipVerify: ptr.To(true), |
87 | | - }, |
88 | | - }, |
| 195 | + Port: MetricsPortName, |
| 196 | + Scheme: "https", |
| 197 | + TLSConfig: tlsConfig, |
89 | 198 | HonorLabels: true, |
90 | 199 | }, |
91 | 200 | }, |
|
0 commit comments