@@ -6,22 +6,20 @@ import (
66 "errors"
77 "flag"
88 "fmt"
9- "net/http"
109 "os"
1110 "reflect"
1211 "strconv"
1312 "sync"
1413 "sync/atomic"
1514
16- "github.com/prometheus/client_golang/prometheus"
17- "github.com/prometheus/client_golang/prometheus/promhttp"
1815 "github.com/spf13/cobra"
1916 "github.com/spf13/pflag"
2017 v1 "k8s.io/api/core/v1"
2118 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2219 "k8s.io/apimachinery/pkg/runtime"
2320 "k8s.io/client-go/kubernetes"
2421 coreclientsetv1 "k8s.io/client-go/kubernetes/typed/core/v1"
22+ "k8s.io/client-go/rest"
2523 "k8s.io/client-go/tools/cache"
2624 "k8s.io/client-go/tools/leaderelection"
2725 "k8s.io/client-go/tools/record"
@@ -32,18 +30,22 @@ import (
3230 osclientset "github.com/openshift/client-go/config/clientset/versioned"
3331 utiltls "github.com/openshift/controller-runtime-common/pkg/tls"
3432 "github.com/openshift/library-go/pkg/operator/events"
35- "github.com/openshift/machine-api-operator/pkg/metrics"
33+ maometrics "github.com/openshift/machine-api-operator/pkg/metrics"
3634 "github.com/openshift/machine-api-operator/pkg/operator"
3735 "github.com/openshift/machine-api-operator/pkg/util"
3836 "github.com/openshift/machine-api-operator/pkg/version"
3937 "sigs.k8s.io/controller-runtime/pkg/client"
38+ ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
39+ "sigs.k8s.io/controller-runtime/pkg/metrics/filters"
40+ metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
4041)
4142
4243const (
4344 // defaultMetricsPort is the default port to expose metrics.
4445 defaultMetricsPort = 8443
45- metricsCertFile = "/etc/tls/private/tls.crt"
46- metricsKeyFile = "/etc/tls/private/tls.key"
46+ metricsCertDir = "/etc/tls/private"
47+ metricsCertFile = "tls.crt"
48+ metricsKeyFile = "tls.key"
4749)
4850
4951var (
@@ -209,11 +211,11 @@ func startControllersOrDie(ctx *ControllerContext) {
209211func startMetricsCollectionAndServer (ctx * ControllerContext ) {
210212 machineInformer := ctx .MachineInformerFactory .Machine ().V1beta1 ().Machines ()
211213 machinesetInformer := ctx .MachineInformerFactory .Machine ().V1beta1 ().MachineSets ()
212- machineMetricsCollector := metrics .NewMachineCollector (
214+ machineMetricsCollector := maometrics .NewMachineCollector (
213215 machineInformer ,
214216 machinesetInformer ,
215217 componentNamespace )
216- prometheus .MustRegister (machineMetricsCollector )
218+ ctrlmetrics . Registry .MustRegister (machineMetricsCollector )
217219 metricsPort := defaultMetricsPort
218220 if port , ok := os .LookupEnv ("METRICS_PORT" ); ok {
219221 v , err := strconv .Atoi (port )
@@ -222,15 +224,34 @@ func startMetricsCollectionAndServer(ctx *ControllerContext) {
222224 }
223225 metricsPort = v
224226 }
225- klog .V (4 ).Info ("Starting server to serve prometheus metrics" )
226- tlsConfig , err := metricsTLSConfig (ctx )
227+ klog .V (4 ).Info ("Starting secure metrics server " )
228+ tlsOpts , err := metricsTLSOptions (ctx )
227229 if err != nil {
228230 klog .Fatalf ("Unable to configure metrics TLS: %v" , err )
229231 }
230- go startHTTPSMetricServer (fmt .Sprintf (":%d" , metricsPort ), tlsConfig )
232+ metricsServer , err := newSecureMetricsServer (
233+ ctx ,
234+ fmt .Sprintf (":%d" , metricsPort ),
235+ tlsOpts ,
236+ )
237+ if err != nil {
238+ klog .Fatalf ("Unable to initialize secure metrics server: %v" , err )
239+ }
240+
241+ metricsServerCtx , cancel := context .WithCancel (context .Background ())
242+ go func () {
243+ <- ctx .Stop
244+ cancel ()
245+ }()
246+
247+ go func () {
248+ if err := metricsServer .Start (metricsServerCtx ); err != nil {
249+ klog .Fatalf ("Unable to start secure metrics server: %v" , err )
250+ }
251+ }()
231252}
232253
233- func metricsTLSConfig (ctx * ControllerContext ) (* tls.Config , error ) {
254+ func metricsTLSOptions (ctx * ControllerContext ) ([] func ( * tls.Config ) , error ) {
234255 scheme := runtime .NewScheme ()
235256 if err := osconfigv1 .Install (scheme ); err != nil {
236257 return nil , fmt .Errorf ("unable to add config.openshift.io scheme: %w" , err )
@@ -251,10 +272,24 @@ func metricsTLSConfig(ctx *ControllerContext) (*tls.Config, error) {
251272 klog .Infof ("TLS configuration contains unsupported ciphers that will be ignored: %v" , unsupportedCiphers )
252273 }
253274
254- tlsConfig := & tls.Config {}
255- tlsConfigFn (tlsConfig )
275+ return []func (* tls.Config ){tlsConfigFn }, nil
276+ }
277+
278+ func newSecureMetricsServer (ctx * ControllerContext , metricsAddr string , tlsOpts []func (* tls.Config )) (metricsserver.Server , error ) {
279+ httpClient , err := rest .HTTPClientFor (ctx .ClientBuilder .config )
280+ if err != nil {
281+ return nil , fmt .Errorf ("unable to create HTTP client for metrics authn/authz: %w" , err )
282+ }
256283
257- return tlsConfig , nil
284+ return metricsserver .NewServer (metricsserver.Options {
285+ BindAddress : metricsAddr ,
286+ SecureServing : true ,
287+ FilterProvider : filters .WithAuthenticationAndAuthorization ,
288+ CertDir : metricsCertDir ,
289+ CertName : metricsCertFile ,
290+ KeyName : metricsKeyFile ,
291+ TLSOpts : tlsOpts ,
292+ }, ctx .ClientBuilder .config , httpClient )
258293}
259294
260295func setupTLSProfileWatcher (ctx * ControllerContext , shutdown func ()) error {
@@ -322,15 +357,3 @@ func handleTLSProfileEvent(obj interface{}, initialProfile osconfigv1.TLSProfile
322357 )
323358 shutdown ()
324359}
325-
326- func startHTTPSMetricServer (metricsAddr string , tlsConfig * tls.Config ) {
327- mux := http .NewServeMux ()
328- mux .Handle ("/metrics" , promhttp .Handler ())
329-
330- server := & http.Server {
331- Addr : metricsAddr ,
332- Handler : mux ,
333- TLSConfig : tlsConfig ,
334- }
335- klog .Fatal (server .ListenAndServeTLS (metricsCertFile , metricsKeyFile ))
336- }
0 commit comments