From b1bfc3bd5adf75e038fad76ca681086b69a312f3 Mon Sep 17 00:00:00 2001 From: Joe Borg Date: Fri, 28 Jun 2024 12:23:00 -0400 Subject: [PATCH 1/2] Allow passing of metrics server secret to flagger Signed-off-by: Joe Borg --- cmd/flagger/main.go | 4 +++- pkg/metrics/observers/factory.go | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/cmd/flagger/main.go b/cmd/flagger/main.go index 6438187fd..f918660be 100644 --- a/cmd/flagger/main.go +++ b/cmd/flagger/main.go @@ -59,6 +59,7 @@ var ( kubeconfigQPS int kubeconfigBurst int metricsServer string + metricsServerSecretRef string controlLoopInterval time.Duration logLevel string port string @@ -94,6 +95,7 @@ func init() { flag.IntVar(&kubeconfigBurst, "kubeconfig-burst", 250, "Set Burst for kubeconfig.") flag.StringVar(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") flag.StringVar(&metricsServer, "metrics-server", "http://prometheus:9090", "Prometheus URL.") + flag.StringVar(&metricsServerSecretRef, "metrics-server-secret-ref", "", "Prometheus Secret Reference.") flag.DurationVar(&controlLoopInterval, "control-loop-interval", 10*time.Second, "Kubernetes API sync interval.") flag.StringVar(&logLevel, "log-level", "debug", "Log level can be: debug, info, warning, error.") flag.StringVar(&port, "port", "8080", "Port to listen on.") @@ -196,7 +198,7 @@ func main() { logger.Infof("Watching namespace %s", namespace) } - observerFactory, err := observers.NewFactory(metricsServer) + observerFactory, err := observers.NewFactoryWithSecret(metricsServer, metricsServerSecretRef) if err != nil { logger.Fatalf("Error building prometheus client: %s", err.Error()) } diff --git a/pkg/metrics/observers/factory.go b/pkg/metrics/observers/factory.go index 044f68f50..0e6ad5b65 100644 --- a/pkg/metrics/observers/factory.go +++ b/pkg/metrics/observers/factory.go @@ -21,6 +21,7 @@ import ( flaggerv1 "github.com/fluxcd/flagger/pkg/apis/flagger/v1beta1" "github.com/fluxcd/flagger/pkg/metrics/providers" + corev1 "k8s.io/api/core/v1" ) type Factory struct { @@ -42,6 +43,27 @@ func NewFactory(metricsServer string) (*Factory, error) { }, nil } +func NewFactoryWithSecret(metricsServer string, metricsServerSecretRef string) (*Factory, error) { + var secretRef *corev1.LocalObjectReference + if metricsServerSecretRef != "" { + secretRef = &corev1.LocalObjectReference{Name: metricsServerSecretRef} + } else { + secretRef = nil + } + client, err := providers.NewPrometheusProvider(flaggerv1.MetricTemplateProvider{ + Type: "prometheus", + Address: metricsServer, + SecretRef: secretRef, + }, nil) + if err != nil { + return nil, err + } + + return &Factory{ + Client: client, + }, nil +} + func (factory Factory) Observer(provider string) Interface { switch { case strings.HasPrefix(provider, flaggerv1.AppMeshProvider): From c14063be777e1520bd369cafcc5d59d17613ece5 Mon Sep 17 00:00:00 2001 From: Joe Borg Date: Fri, 28 Jun 2024 13:40:35 -0400 Subject: [PATCH 2/2] Code cleanup Signed-off-by: Joe Borg --- pkg/metrics/observers/factory.go | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/pkg/metrics/observers/factory.go b/pkg/metrics/observers/factory.go index 0e6ad5b65..8e71055e7 100644 --- a/pkg/metrics/observers/factory.go +++ b/pkg/metrics/observers/factory.go @@ -29,26 +29,13 @@ type Factory struct { } func NewFactory(metricsServer string) (*Factory, error) { - client, err := providers.NewPrometheusProvider(flaggerv1.MetricTemplateProvider{ - Type: "prometheus", - Address: metricsServer, - SecretRef: nil, - }, nil) - if err != nil { - return nil, err - } - - return &Factory{ - Client: client, - }, nil + return NewFactoryWithSecret(metricsServer, "") } func NewFactoryWithSecret(metricsServer string, metricsServerSecretRef string) (*Factory, error) { var secretRef *corev1.LocalObjectReference if metricsServerSecretRef != "" { secretRef = &corev1.LocalObjectReference{Name: metricsServerSecretRef} - } else { - secretRef = nil } client, err := providers.NewPrometheusProvider(flaggerv1.MetricTemplateProvider{ Type: "prometheus",