This is an automated email from the ASF dual-hosted git repository.

squakez pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/main by this push:
     new 79522d0e3 feat(operator): support HTTPS operator metrics via 
METRICS_CERT_DIR (#6969)
79522d0e3 is described below

commit 79522d0e301467f76e58be68a59046484f976941
Author: Anshul Jagota <[email protected]>
AuthorDate: Sat Nov 21 12:56:21 2026 +0530

    feat(operator): support HTTPS operator metrics via METRICS_CERT_DIR (#6969)
---
 .../ROOT/pages/installation/advanced/advanced.adoc     |  6 ++++++
 .../ROOT/pages/observability/monitoring/operator.adoc  | 16 ++++++++++++++++
 pkg/cmd/operator/operator.go                           | 10 +++++++++-
 pkg/cmd/operator/operator_test.go                      | 18 ++++++++++++++++++
 pkg/platform/operator.go                               |  6 ++++++
 pkg/platform/operator_test.go                          | 16 ++++++++++++++++
 6 files changed, 71 insertions(+), 1 deletion(-)

diff --git a/docs/modules/ROOT/pages/installation/advanced/advanced.adoc 
b/docs/modules/ROOT/pages/installation/advanced/advanced.adoc
index 0be19db94..b0497ab50 100644
--- a/docs/modules/ROOT/pages/installation/advanced/advanced.adoc
+++ b/docs/modules/ROOT/pages/installation/advanced/advanced.adoc
@@ -27,6 +27,8 @@ There is a default liveness service that is available at the 
operator Pod _/heal
 
 There is a default metrics service as well available at the operator Pod 
_/metrics_ endpoint on port _8080_. The port can be also changed. In this case 
you only need to change the Deployment container operator, changing the 
_--monitoring-port_ value to the port you want the operator to expose.
 
+By default, the metrics endpoint is served over HTTP. To enable HTTPS, mount a 
Kubernetes TLS `Secret` (containing `tls.crt` and `tls.key`) into the operator 
Pod and set the `METRICS_CERT_DIR` environment variable on the operator 
Deployment to the mount directory.
+
 The service exposed on the _/metrics_ endpoint is compatible with Prometheus. 
You can learn more about xref:observability/monitoring/operator.adoc[how to 
monitor Camel K Operator].
 
 == Logging
@@ -65,6 +67,10 @@ The following environment variables can be configured on the 
operator Deployment
 |`camel.apache.org/monitor`
 |If it exists, the operator add this label beside regular Camel K labels in 
order to let the Camel Monitor operator discover and monitor the application.
 
+|`METRICS_CERT_DIR`
+|_empty_
+|Directory containing `tls.crt` and `tls.key` for the operator metrics 
endpoint. When set, the metrics endpoint is served over HTTPS; when not set or 
empty, it falls back to HTTP.
+
 |`LOG_LEVEL`
 |`info`
 |The log level for the operator. Valid values: `debug`, `info`, `warn`, 
`error`.
diff --git a/docs/modules/ROOT/pages/observability/monitoring/operator.adoc 
b/docs/modules/ROOT/pages/observability/monitoring/operator.adoc
index bff7df586..f53a3f641 100644
--- a/docs/modules/ROOT/pages/observability/monitoring/operator.adoc
+++ b/docs/modules/ROOT/pages/observability/monitoring/operator.adoc
@@ -21,6 +21,22 @@ NOTE: change the ref value with the installation version tag 
you want to install
 
 IMPORTANT: The provided Prometheus configuration does not create a 
`NetworkPolicy`. As a post-installation step, cluster administrators should 
https://kubernetes.io/docs/concepts/services-networking/network-policies/[configure
 a network policy] that only allows the Prometheus scraper to access the 
operator metrics port. Adapt the namespace and pod selectors to your Prometheus 
deployment, and verify that the cluster networking solution supports 
`NetworkPolicy` resources.
 
+[[security-configuration]]
+== Security configuration
+
+By default, the operator metrics endpoint is served over HTTP. To enable 
HTTPS, mount a Kubernetes TLS `Secret` (containing `tls.crt` and `tls.key`) 
into the operator Pod and set the `METRICS_CERT_DIR` environment variable on 
the operator Deployment to the mount directory:
+
+[cols="2,1,3"]
+|===
+|Variable |Default |Description
+
+|`METRICS_CERT_DIR`
+|_empty_
+|Directory containing `tls.crt` and `tls.key` used to serve the `/metrics` 
endpoint over HTTPS. When empty, the endpoint falls back to HTTP.
+|===
+
+When `METRICS_CERT_DIR` is configured, update the `PodMonitor` endpoint to use 
`scheme: https` and configure `tlsConfig` to verify the certificate used by the 
operator.
+
 However, most of the time you want to probably configure and add different 
thresholds and KPIs in order to monitor in a properly manner your installation.
 
 [[metrics]]
diff --git a/pkg/cmd/operator/operator.go b/pkg/cmd/operator/operator.go
index a801fbeea..71324cfb8 100644
--- a/pkg/cmd/operator/operator.go
+++ b/pkg/cmd/operator/operator.go
@@ -223,7 +223,7 @@ func Run(healthPort, monitoringPort int32, leaderElection 
bool, leaderElectionID
                LeaderElectionResourceLock:    resourcelock.LeasesResourceLock,
                LeaderElectionReleaseOnCancel: true,
                HealthProbeBindAddress:        ":" + 
strconv.Itoa(int(healthPort)),
-               Metrics:                       
metricsserver.Options{BindAddress: ":" + strconv.Itoa(int(monitoringPort))},
+               Metrics:                       
newMetricsServerOptions(monitoringPort, platform.GetMetricsCertDir()),
                Cache:                         options,
        })
        exitOnError(err, "")
@@ -251,6 +251,14 @@ func Run(healthPort, monitoringPort int32, leaderElection 
bool, leaderElectionID
        exitOnError(mgr.Start(ctx), "manager exited non-zero")
 }
 
+func newMetricsServerOptions(monitoringPort int32, certDir string) 
metricsserver.Options {
+       return metricsserver.Options{
+               BindAddress:   ":" + strconv.Itoa(int(monitoringPort)),
+               SecureServing: certDir != "",
+               CertDir:       certDir,
+       }
+}
+
 func getNamespacesSelector(operatorNamespace string, watchNamespace string) 
map[string]cache.Config {
        namespacesSelector := map[string]cache.Config{
                // The same operator namespace is needed while the operator 
stores
diff --git a/pkg/cmd/operator/operator_test.go 
b/pkg/cmd/operator/operator_test.go
index 364271ce6..93c42a603 100644
--- a/pkg/cmd/operator/operator_test.go
+++ b/pkg/cmd/operator/operator_test.go
@@ -31,6 +31,24 @@ import (
        "sigs.k8s.io/controller-runtime/pkg/cache"
 )
 
+func TestNewMetricsServerOptions(t *testing.T) {
+       t.Run("defaults to HTTP when certDir is empty", func(t *testing.T) {
+               options := newMetricsServerOptions(8080, "")
+
+               assert.Equal(t, ":8080", options.BindAddress)
+               assert.False(t, options.SecureServing)
+               assert.Empty(t, options.CertDir)
+       })
+
+       t.Run("enables HTTPS when certDir is provided", func(t *testing.T) {
+               options := newMetricsServerOptions(9090, 
"/etc/camel-k/metrics-certs")
+
+               assert.Equal(t, ":9090", options.BindAddress)
+               assert.True(t, options.SecureServing)
+               assert.Equal(t, "/etc/camel-k/metrics-certs", options.CertDir)
+       })
+}
+
 func TestGetNamespacesSelector(t *testing.T) {
        tests := []struct {
                name              string
diff --git a/pkg/platform/operator.go b/pkg/platform/operator.go
index e2ae42be6..ea7124db4 100644
--- a/pkg/platform/operator.go
+++ b/pkg/platform/operator.go
@@ -42,6 +42,7 @@ const (
        OperatorPipeMaxConcurrentReconcilesEnvVar        = 
"PIPE_MAX_CONCURRENT_RECONCILES"
        OperatorClientQPSEnvVar                          = "CLIENT_QPS"
        OperatorClientBurstEnvVar                        = "CLIENT_BURST"
+       OperatorMetricsCertDirEnvVar                     = "METRICS_CERT_DIR"
 
        defaultIntegrationMaxConcurrentReconciles = 1
        defaultPipeMaxConcurrentReconciles        = 1
@@ -274,4 +275,9 @@ func GetClientBurst() int {
        return GetEnvOrDefaultAsInt(OperatorClientBurstEnvVar, 
defaultClientBurst)
 }
 
+// GetMetricsCertDir returns the configured certificate directory for the 
operator metrics endpoint.
+func GetMetricsCertDir() string {
+       return strings.TrimSpace(GetEnvOrDefault(OperatorMetricsCertDirEnvVar, 
""))
+}
+
 var _ predicate.Predicate = FilteringFuncs[ctrl.Object]{}
diff --git a/pkg/platform/operator_test.go b/pkg/platform/operator_test.go
index 17ef20a8d..eb1497a91 100644
--- a/pkg/platform/operator_test.go
+++ b/pkg/platform/operator_test.go
@@ -118,3 +118,19 @@ func TestGetClientBurst(t *testing.T) {
                assert.Equal(t, 200, GetClientBurst())
        })
 }
+
+func TestGetMetricsCertDir(t *testing.T) {
+       t.Run("default empty value", func(t *testing.T) {
+               assert.Empty(t, GetMetricsCertDir())
+       })
+
+       t.Run("custom cert dir", func(t *testing.T) {
+               t.Setenv(OperatorMetricsCertDirEnvVar, 
"/etc/camel-k/metrics-certs")
+               assert.Equal(t, "/etc/camel-k/metrics-certs", 
GetMetricsCertDir())
+       })
+
+       t.Run("trims whitespace", func(t *testing.T) {
+               t.Setenv(OperatorMetricsCertDirEnvVar, "  
/etc/camel-k/metrics-certs  ")
+               assert.Equal(t, "/etc/camel-k/metrics-certs", 
GetMetricsCertDir())
+       })
+}

Reply via email to