mirror of
https://github.com/prometheus-operator/prometheus-operator.git
synced 2025-04-21 03:38:43 +00:00
chore: factorize ProxyConfig processing
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
parent
6c4545572a
commit
b9ae56238a
6 changed files with 67 additions and 70 deletions
pkg
|
@ -17,6 +17,7 @@ package v1
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
|
@ -106,18 +107,42 @@ type ProxyConfig struct {
|
|||
}
|
||||
|
||||
// Validate semantically validates the given ProxyConfig.
|
||||
func (c *ProxyConfig) Validate() error {
|
||||
if c == nil {
|
||||
func (pc *ProxyConfig) Validate() error {
|
||||
if pc == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, v := range c.ProxyConnectHeader {
|
||||
if reflect.ValueOf(pc).IsZero() {
|
||||
return nil
|
||||
}
|
||||
|
||||
proxyFromEnvironmentDefined := pc.ProxyFromEnvironment != nil && *pc.ProxyFromEnvironment
|
||||
proxyURLDefined := pc.ProxyURL != nil && *pc.ProxyURL != ""
|
||||
noProxyDefined := pc.NoProxy != nil && *pc.NoProxy != ""
|
||||
|
||||
if len(pc.ProxyConnectHeader) > 0 && (!proxyFromEnvironmentDefined && !proxyURLDefined) {
|
||||
return fmt.Errorf("if proxyConnectHeader is configured, proxyUrl or proxyFromEnvironment must also be configured")
|
||||
}
|
||||
|
||||
if proxyFromEnvironmentDefined && proxyURLDefined {
|
||||
return fmt.Errorf("if proxyFromEnvironment is configured, proxyUrl must not be configured")
|
||||
}
|
||||
|
||||
if proxyFromEnvironmentDefined && noProxyDefined {
|
||||
return fmt.Errorf("if proxyFromEnvironment is configured, noProxy must not be configured")
|
||||
}
|
||||
|
||||
if !proxyURLDefined && noProxyDefined {
|
||||
return fmt.Errorf("if noProxy is configured, proxyUrl must also be configured")
|
||||
}
|
||||
|
||||
for k, v := range pc.ProxyConnectHeader {
|
||||
if len(v) == 0 {
|
||||
return errors.New("ProxyConnectHeader selectors must not be empty")
|
||||
return fmt.Errorf("proxyConnetHeader[%s]: selector must not be empty", k)
|
||||
}
|
||||
for _, k := range v {
|
||||
if k == (v1.SecretKeySelector{}) {
|
||||
return errors.New("ProxyConnectHeader selector must be defined")
|
||||
for i, sel := range v {
|
||||
if sel == (v1.SecretKeySelector{}) {
|
||||
return fmt.Errorf("proxyConnectHeader[%s][%d]: selector must be defined", k, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -101,16 +101,11 @@ func (s *StoreBuilder) AddBasicAuth(ctx context.Context, ns string, ba *monitori
|
|||
}
|
||||
|
||||
// AddProxyConfig processes the given *ProxyConfig and adds the referenced credentials to the store.
|
||||
func (s *StoreBuilder) AddProxyConfig(ctx context.Context, ns string, pc monitoringv1.ProxyConfig) error {
|
||||
if len(pc.ProxyConnectHeader) <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *StoreBuilder) AddProxyConfig(ctx context.Context, namespace string, pc monitoringv1.ProxyConfig) error {
|
||||
for k, v := range pc.ProxyConnectHeader {
|
||||
for _, v1 := range v {
|
||||
_, err := s.GetSecretKey(ctx, ns, v1)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get proxy config connect header: %s %w", k, err)
|
||||
for index, sel := range v {
|
||||
if _, err := s.GetSecretKey(ctx, namespace, sel); err != nil {
|
||||
return fmt.Errorf("header[%s][%d]: %w", k, index, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -132,7 +132,7 @@ func validateRemoteWriteSpec(spec monitoringv1.RemoteWriteSpec) error {
|
|||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return spec.ProxyConfig.Validate()
|
||||
}
|
||||
|
||||
// Process will determine the Status of a Prometheus resource (server or agent) depending on its current state in the cluster.
|
||||
|
|
|
@ -16,7 +16,6 @@ package prometheus
|
|||
|
||||
import (
|
||||
"cmp"
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"math"
|
||||
|
@ -4562,42 +4561,6 @@ func (cg *ConfigGenerator) appendTracingConfig(cfg yaml.MapSlice, s assets.Store
|
|||
}), nil
|
||||
}
|
||||
|
||||
func validateProxyConfig(ctx context.Context, pc monitoringv1.ProxyConfig, store *assets.StoreBuilder, namespace string) error {
|
||||
if reflect.ValueOf(pc).IsZero() {
|
||||
return nil
|
||||
}
|
||||
|
||||
proxyFromEnvironmentDefined := ptr.Deref(pc.ProxyFromEnvironment, false)
|
||||
proxyURLDefined := ptr.Deref(pc.ProxyURL, "") != ""
|
||||
noProxyDefined := ptr.Deref(pc.NoProxy, "") != ""
|
||||
|
||||
if len(pc.ProxyConnectHeader) > 0 && (!proxyFromEnvironmentDefined && !proxyURLDefined) {
|
||||
return fmt.Errorf("if proxyConnectHeader is configured, proxyUrl or proxyFromEnvironment must also be configured")
|
||||
}
|
||||
|
||||
if proxyFromEnvironmentDefined && proxyURLDefined {
|
||||
return fmt.Errorf("if proxyFromEnvironment is configured, proxyUrl must not be configured")
|
||||
}
|
||||
|
||||
if proxyFromEnvironmentDefined && noProxyDefined {
|
||||
return fmt.Errorf("if proxyFromEnvironment is configured, noProxy must not be configured")
|
||||
}
|
||||
|
||||
if !proxyURLDefined && noProxyDefined {
|
||||
return fmt.Errorf("if noProxy is configured, proxyUrl must also be configured")
|
||||
}
|
||||
|
||||
for k, v := range pc.ProxyConnectHeader {
|
||||
for index, s := range v {
|
||||
if _, err := store.GetSecretKey(ctx, namespace, s); err != nil {
|
||||
return fmt.Errorf("header[%s]: index[%d] %w", k, index, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cg *ConfigGenerator) getScrapeClassOrDefault(name *string) monitoringv1.ScrapeClass {
|
||||
if name != nil {
|
||||
if scrapeClass, found := cg.scrapeClasses[*name]; found {
|
||||
|
|
|
@ -817,7 +817,7 @@ func (rs *ResourceSelector) SelectScrapeConfigs(ctx context.Context, listFn List
|
|||
continue
|
||||
}
|
||||
|
||||
if err = validateProxyConfig(ctx, sc.Spec.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
if err = addProxyConfigToStore(ctx, sc.Spec.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
rejectFn(sc, err)
|
||||
continue
|
||||
}
|
||||
|
@ -969,7 +969,7 @@ func (rs *ResourceSelector) validateKubernetesSDConfigs(ctx context.Context, sc
|
|||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
|
||||
if err := validateProxyConfig(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
if err := addProxyConfigToStore(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
|
||||
|
@ -1045,7 +1045,7 @@ func (rs *ResourceSelector) validateConsulSDConfigs(ctx context.Context, sc *mon
|
|||
}
|
||||
}
|
||||
|
||||
if err := validateProxyConfig(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
if err := addProxyConfigToStore(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
}
|
||||
|
@ -1079,7 +1079,7 @@ func (rs *ResourceSelector) validateHTTPSDConfigs(ctx context.Context, sc *monit
|
|||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
|
||||
if err := validateProxyConfig(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
if err := addProxyConfigToStore(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
}
|
||||
|
@ -1117,7 +1117,7 @@ func (rs *ResourceSelector) validateEC2SDConfigs(ctx context.Context, sc *monito
|
|||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
|
||||
if err := validateProxyConfig(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
if err := addProxyConfigToStore(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
}
|
||||
|
@ -1188,7 +1188,7 @@ func (rs *ResourceSelector) validateDigitalOceanSDConfigs(ctx context.Context, s
|
|||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
|
||||
if err := validateProxyConfig(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
if err := addProxyConfigToStore(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
}
|
||||
|
@ -1219,7 +1219,7 @@ func (rs *ResourceSelector) validateDockerSDConfigs(ctx context.Context, sc *mon
|
|||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
|
||||
if err := validateProxyConfig(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
if err := addProxyConfigToStore(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
}
|
||||
|
@ -1244,7 +1244,7 @@ func (rs *ResourceSelector) validateLinodeSDConfigs(ctx context.Context, sc *mon
|
|||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
|
||||
if err := validateProxyConfig(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
if err := addProxyConfigToStore(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
}
|
||||
|
@ -1273,7 +1273,7 @@ func (rs *ResourceSelector) validateKumaSDConfigs(ctx context.Context, sc *monit
|
|||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
|
||||
if err := validateProxyConfig(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
if err := addProxyConfigToStore(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
}
|
||||
|
@ -1299,7 +1299,7 @@ func (rs *ResourceSelector) validateEurekaSDConfigs(ctx context.Context, sc *mon
|
|||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
|
||||
if err := validateProxyConfig(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
if err := addProxyConfigToStore(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
}
|
||||
|
@ -1325,7 +1325,7 @@ func (rs *ResourceSelector) validateHetznerSDConfigs(ctx context.Context, sc *mo
|
|||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
|
||||
if err := validateProxyConfig(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
if err := addProxyConfigToStore(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
}
|
||||
|
@ -1351,7 +1351,7 @@ func (rs *ResourceSelector) validateNomadSDConfigs(ctx context.Context, sc *moni
|
|||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
|
||||
if err := validateProxyConfig(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
if err := addProxyConfigToStore(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
}
|
||||
|
@ -1385,7 +1385,7 @@ func (rs *ResourceSelector) validateDockerSwarmSDConfigs(ctx context.Context, sc
|
|||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
|
||||
if err := validateProxyConfig(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
if err := addProxyConfigToStore(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
}
|
||||
|
@ -1426,7 +1426,7 @@ func (rs *ResourceSelector) validatePuppetDBSDConfigs(ctx context.Context, sc *m
|
|||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
|
||||
if err := validateProxyConfig(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
if err := addProxyConfigToStore(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
}
|
||||
|
@ -1467,7 +1467,7 @@ func (rs *ResourceSelector) validateLightSailSDConfigs(ctx context.Context, sc *
|
|||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
|
||||
if err := validateProxyConfig(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
if err := addProxyConfigToStore(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
}
|
||||
|
@ -1528,7 +1528,7 @@ func (rs *ResourceSelector) validateIonosSDConfigs(ctx context.Context, sc *moni
|
|||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
|
||||
if err := validateProxyConfig(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
if err := addProxyConfigToStore(ctx, config.ProxyConfig, rs.store, sc.GetNamespace()); err != nil {
|
||||
return fmt.Errorf("[%d]: %w", i, err)
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
|
||||
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
|
||||
monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
|
||||
"github.com/prometheus-operator/prometheus-operator/pkg/assets"
|
||||
)
|
||||
|
@ -78,6 +79,10 @@ func AddRemoteReadsToStore(ctx context.Context, store *assets.StoreBuilder, name
|
|||
return fmt.Errorf("remote read %d: %w", i, err)
|
||||
}
|
||||
|
||||
if err := remote.ProxyConfig.Validate(); err != nil {
|
||||
return fmt.Errorf("remote read %d: %w", i, err)
|
||||
}
|
||||
|
||||
if err := store.AddProxyConfig(ctx, namespace, remote.ProxyConfig); err != nil {
|
||||
return fmt.Errorf("remote read %d: %w", i, err)
|
||||
}
|
||||
|
@ -85,6 +90,7 @@ func AddRemoteReadsToStore(ctx context.Context, store *assets.StoreBuilder, name
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func AddAPIServerConfigToStore(ctx context.Context, store *assets.StoreBuilder, namespace string, config *monv1.APIServerConfig) error {
|
||||
if config == nil {
|
||||
return nil
|
||||
|
@ -113,3 +119,11 @@ func AddScrapeClassesToStore(ctx context.Context, store *assets.StoreBuilder, na
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func addProxyConfigToStore(ctx context.Context, pc monitoringv1.ProxyConfig, store *assets.StoreBuilder, namespace string) error {
|
||||
if err := pc.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return store.AddProxyConfig(ctx, namespace, pc)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue