From 2cea0f003a86fe2071b2c5086504533c685c6d5f Mon Sep 17 00:00:00 2001 From: Quentin Bisson Date: Tue, 11 Jun 2024 16:24:03 +0200 Subject: [PATCH] Remove resources when mimir is enabled (#1652) * Remove resources when mimir is enabled * fix tests --- CHANGELOG.md | 6 ++- service/controller/clusterapi/resource.go | 36 ++++++++++---- .../controller/managementcluster/resource.go | 25 ++++++++-- .../alerting/alertmanagerwiring/resource.go | 15 +++--- .../resource/certificates/create.go | 10 ++-- .../resource/certificates/delete.go | 6 +-- .../resource/certificates/resource.go | 47 +++++++------------ .../resource/ciliumnetpol/create.go | 15 ++++-- .../resource/ciliumnetpol/delete.go | 6 +-- .../resource/ciliumnetpol/resource.go | 12 ++--- .../resource/etcd-certificates/resource.go | 5 +- .../resource/monitoring/ingress/resource.go | 5 +- .../monitoring/prometheus/resource.go | 3 +- .../monitoring/remotewriteingress/create.go | 5 ++ .../monitoring/remotewriteingress/resource.go | 9 ++-- .../remotewriteingressauth/create.go | 5 ++ .../remotewriteingressauth/resource.go | 1 + .../monitoring/scrapeconfigs/resource.go | 5 +- .../verticalpodautoscaler/create.go | 23 +++++---- .../verticalpodautoscaler/delete.go | 6 +-- .../verticalpodautoscaler/resource.go | 27 ++++------- .../controller/resource/namespace/resource.go | 18 +++---- service/controller/resource/rbac/create.go | 14 ++++-- service/controller/resource/rbac/delete.go | 6 +-- service/controller/resource/rbac/resource.go | 15 ++---- 25 files changed, 187 insertions(+), 138 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3072f32f..91c9cdc0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,9 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- Internal rework to remove the use generic resource to ease out the migration to Mimir. -- Update generic resource so we can delete resources if mimir is enabled. - Remove unused scrape_timeout inhibition. +- Some improvements towards Mimir: + - Internal rework to remove the use generic resource to ease out the migration to Mimir. + - Update generic resource so we can delete resources when mimir is enabled. + - Remove legacy prometheus resources when Mimir is enabled. ## [4.76.0] - 2024-06-03 diff --git a/service/controller/clusterapi/resource.go b/service/controller/clusterapi/resource.go index ae850f832..fef4d74ea 100644 --- a/service/controller/clusterapi/resource.go +++ b/service/controller/clusterapi/resource.go @@ -87,6 +87,8 @@ func New(config Config) ([]resource.Interface, error) { c := namespace.Config{ K8sClient: config.K8sClient, Logger: config.Logger, + + MimirEnabled: config.MimirEnabled, } namespaceResource, err = namespace.New(c) @@ -98,10 +100,11 @@ func New(config Config) ([]resource.Interface, error) { var apiCertificatesResource resource.Interface { c := certificates.Config{ - Name: "api-certificates", - Provider: config.Provider, - K8sClient: config.K8sClient, - Logger: config.Logger, + Name: "api-certificates", + Provider: config.Provider, + K8sClient: config.K8sClient, + Logger: config.Logger, + MimirEnabled: config.MimirEnabled, Sources: []certificates.CertificateSource{ { NameFunc: key.Namespace, @@ -130,6 +133,8 @@ func New(config Config) ([]resource.Interface, error) { c := ciliumnetpol.Config{ DynamicK8sClient: config.DynamicK8sClient, Logger: config.Logger, + + MimirEnabled: config.MimirEnabled, } ciliumnetpolResource, err = ciliumnetpol.New(c) @@ -164,6 +169,8 @@ func New(config Config) ([]resource.Interface, error) { PasswordManager: passwordManager, Installation: config.Installation, Provider: config.Provider, + + MimirEnabled: config.MimirEnabled, } remoteWriteIngressAuthResource, err = remotewriteingressauth.New(c) @@ -179,6 +186,8 @@ func New(config Config) ([]resource.Interface, error) { Logger: config.Logger, BaseDomain: config.PrometheusBaseDomain, ExternalDNS: config.ExternalDNS, + + MimirEnabled: config.MimirEnabled, } remoteWriteIngressResource, err = remotewriteingress.New(c) @@ -268,6 +277,8 @@ func New(config Config) ([]resource.Interface, error) { c := rbac.Config{ K8sClient: config.K8sClient, Logger: config.Logger, + + MimirEnabled: config.MimirEnabled, } rbacResource, err = rbac.New(c) @@ -312,6 +323,8 @@ func New(config Config) ([]resource.Interface, error) { VpaClient: config.VpaClient, Installation: config.Installation, Provider: config.Provider, + + MimirEnabled: config.MimirEnabled, } verticalPodAutoScalerResource, err = verticalpodautoscaler.New(c) @@ -334,6 +347,8 @@ func New(config Config) ([]resource.Interface, error) { Provider: config.Provider, Region: config.Region, Installation: config.Installation, + + MimirEnabled: config.MimirEnabled, } scrapeConfigResource, err = scrapeconfigs.New(c) @@ -341,6 +356,7 @@ func New(config Config) ([]resource.Interface, error) { return nil, microerror.Mask(err) } } + var ingressResource resource.Interface { c := ingress.Config{ @@ -350,6 +366,8 @@ func New(config Config) ([]resource.Interface, error) { RestrictedAccessEnabled: config.RestrictedAccessEnabled, WhitelistedSubnets: config.WhitelistedSubnets, ExternalDNS: config.ExternalDNS, + + MimirEnabled: config.MimirEnabled, } ingressResource, err = ingress.New(c) @@ -377,12 +395,12 @@ func New(config Config) ([]resource.Interface, error) { var alertmanagerWiringResource resource.Interface // This resource creates a static secret to connect Prometheus to Alertmanager. When using mimir, this is not needed anymore - if config.MimirEnabled { - alertmanagerWiringResource = noop.New(noop.Config{Logger: config.Logger}) - } else { + { c := alertmanagerwiring.Config{ K8sClient: config.K8sClient, Logger: config.Logger, + + MimirEnabled: config.MimirEnabled, } alertmanagerWiringResource, err = alertmanagerwiring.New(c) @@ -392,7 +410,9 @@ func New(config Config) ([]resource.Interface, error) { } var pvcResizeResource resource.Interface - { + if config.MimirEnabled { + pvcResizeResource = noop.New(noop.Config{Logger: config.Logger}) + } else { c := pvcresizingresource.Config{ K8sClient: config.K8sClient, Logger: config.Logger, diff --git a/service/controller/managementcluster/resource.go b/service/controller/managementcluster/resource.go index a02363495..f8281e8d9 100644 --- a/service/controller/managementcluster/resource.go +++ b/service/controller/managementcluster/resource.go @@ -95,6 +95,8 @@ func newResources(config resourcesConfig) ([]resource.Interface, error) { c := namespace.Config{ K8sClient: config.K8sClient, Logger: config.Logger, + + MimirEnabled: config.MimirEnabled, } namespaceResource, err = namespace.New(c) @@ -110,6 +112,8 @@ func newResources(config resourcesConfig) ([]resource.Interface, error) { Logger: config.Logger, Installation: config.Installation, Provider: config.Provider, + + MimirEnabled: config.MimirEnabled, } etcdCertificatesResource, err = etcdcertificates.New(c) @@ -123,6 +127,8 @@ func newResources(config resourcesConfig) ([]resource.Interface, error) { c := rbac.Config{ K8sClient: config.K8sClient, Logger: config.Logger, + + MimirEnabled: config.MimirEnabled, } rbacResource, err = rbac.New(c) @@ -155,13 +161,12 @@ func newResources(config resourcesConfig) ([]resource.Interface, error) { } var alertmanagerWiringResource resource.Interface - // This resource creates a static secret to connect Prometheus to Alertmanager. When using mimir, this is not needed anymore - if config.MimirEnabled { - alertmanagerWiringResource = noop.New(noop.Config{Logger: config.Logger}) - } else { + { c := alertmanagerwiring.Config{ K8sClient: config.K8sClient, Logger: config.Logger, + + MimirEnabled: config.MimirEnabled, } alertmanagerWiringResource, err = alertmanagerwiring.New(c) @@ -175,6 +180,8 @@ func newResources(config resourcesConfig) ([]resource.Interface, error) { c := ciliumnetpol.Config{ DynamicK8sClient: config.DynamicK8sClient, Logger: config.Logger, + + MimirEnabled: config.MimirEnabled, } ciliumnetpolResource, err = ciliumnetpol.New(c) @@ -237,6 +244,8 @@ func newResources(config resourcesConfig) ([]resource.Interface, error) { VpaClient: config.VpaClient, Installation: config.Installation, Provider: config.Provider, + + MimirEnabled: config.MimirEnabled, } verticalPodAutoScalerResource, err = verticalpodautoscaler.New(c) @@ -260,6 +269,8 @@ func newResources(config resourcesConfig) ([]resource.Interface, error) { Region: config.Region, Installation: config.Installation, Vault: config.Vault, + + MimirEnabled: config.MimirEnabled, } scrapeConfigResource, err = scrapeconfigs.New(c) @@ -277,6 +288,8 @@ func newResources(config resourcesConfig) ([]resource.Interface, error) { RestrictedAccessEnabled: config.RestrictedAccessEnabled, WhitelistedSubnets: config.WhitelistedSubnets, ExternalDNS: config.ExternalDNS, + + MimirEnabled: config.MimirEnabled, } monitoringIngressResource, err = ingress.New(c) @@ -303,7 +316,9 @@ func newResources(config resourcesConfig) ([]resource.Interface, error) { } var pvcResizeResource resource.Interface - { + if config.MimirEnabled { + pvcResizeResource = noop.New(noop.Config{Logger: config.Logger}) + } else { c := pvcresizingresource.Config{ Logger: config.Logger, K8sClient: config.K8sClient, diff --git a/service/controller/resource/alerting/alertmanagerwiring/resource.go b/service/controller/resource/alerting/alertmanagerwiring/resource.go index 8803195f0..27a76fbe0 100644 --- a/service/controller/resource/alerting/alertmanagerwiring/resource.go +++ b/service/controller/resource/alerting/alertmanagerwiring/resource.go @@ -31,6 +31,8 @@ var ( type Config struct { K8sClient k8sclient.Interface Logger micrologger.Logger + + MimirEnabled bool } func New(config Config) (*generic.Resource, error) { @@ -46,12 +48,13 @@ func New(config Config) (*generic.Resource, error) { } c := generic.Config{ - ClientFunc: clientFunc, - Logger: config.Logger, - Name: Name, - GetObjectMeta: getObjectMeta, - GetDesiredObject: toSecret, - HasChangedFunc: hasChanged, + ClientFunc: clientFunc, + Logger: config.Logger, + Name: Name, + GetObjectMeta: getObjectMeta, + GetDesiredObject: toSecret, + HasChangedFunc: hasChanged, + DeleteIfMimirEnabled: config.MimirEnabled, } r, err := generic.New(c) if err != nil { diff --git a/service/controller/resource/certificates/create.go b/service/controller/resource/certificates/create.go index ea8808e4a..495ba48c6 100644 --- a/service/controller/resource/certificates/create.go +++ b/service/controller/resource/certificates/create.go @@ -11,13 +11,17 @@ import ( ) func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error { + if r.config.MimirEnabled { + r.config.Logger.Debugf(ctx, "mimir is enabled, deleting heartbeat if it exists") + return r.EnsureDeleted(ctx, obj) + } desired, err := r.getDesiredObject(ctx, obj) if err != nil { return microerror.Mask(err) } - r.logger.Debugf(ctx, "creating") - c := r.k8sClient.K8sClient().CoreV1().Secrets(desired.GetNamespace()) + r.config.Logger.Debugf(ctx, "creating") + c := r.config.K8sClient.K8sClient().CoreV1().Secrets(desired.GetNamespace()) current, err := c.Get(ctx, desired.GetName(), metav1.GetOptions{}) if apierrors.IsNotFound(err) { current, err = c.Create(ctx, desired, metav1.CreateOptions{}) @@ -33,7 +37,7 @@ func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error { return microerror.Mask(err) } } - r.logger.Debugf(ctx, "created") + r.config.Logger.Debugf(ctx, "created") return nil } diff --git a/service/controller/resource/certificates/delete.go b/service/controller/resource/certificates/delete.go index 1f53d8ce8..f172dcb03 100644 --- a/service/controller/resource/certificates/delete.go +++ b/service/controller/resource/certificates/delete.go @@ -14,15 +14,15 @@ func (r *Resource) EnsureDeleted(ctx context.Context, obj interface{}) error { return microerror.Mask(err) } - r.logger.Debugf(ctx, "deleting") - c := r.k8sClient.K8sClient().CoreV1().Secrets(object.GetNamespace()) + r.config.Logger.Debugf(ctx, "deleting") + c := r.config.K8sClient.K8sClient().CoreV1().Secrets(object.GetNamespace()) err = c.Delete(ctx, object.GetName(), metav1.DeleteOptions{}) if apierrors.IsNotFound(err) { // fall through } else if err != nil { return microerror.Mask(err) } - r.logger.Debugf(ctx, "deleted") + r.config.Logger.Debugf(ctx, "deleted") return nil } diff --git a/service/controller/resource/certificates/resource.go b/service/controller/resource/certificates/resource.go index 555c0e991..e2a1e9a40 100644 --- a/service/controller/resource/certificates/resource.go +++ b/service/controller/resource/certificates/resource.go @@ -19,12 +19,13 @@ import ( ) type Config struct { - Name string - Provider cluster.Provider - Sources []CertificateSource - Target string - K8sClient k8sclient.Interface - Logger micrologger.Logger + Name string + Provider cluster.Provider + Sources []CertificateSource + Target string + K8sClient k8sclient.Interface + Logger micrologger.Logger + MimirEnabled bool } type NameFunc func(metav1.Object) string @@ -35,12 +36,7 @@ type CertificateSource struct { } type Resource struct { - name string - provider cluster.Provider - sources []CertificateSource - target string - k8sClient k8sclient.Interface - logger micrologger.Logger + config Config } func New(config Config) (*Resource, error) { @@ -63,20 +59,11 @@ func New(config Config) (*Resource, error) { return nil, microerror.Maskf(invalidConfigError, "%T.Target must not be empty", config) } - r := &Resource{ - name: config.Name, - provider: config.Provider, - logger: config.Logger, - k8sClient: config.K8sClient, - sources: config.Sources, - target: config.Target, - } - - return r, nil + return &Resource{config}, nil } func (r *Resource) Name() string { - return r.name + return r.config.Name } func (r *Resource) getObjectMeta(v interface{}) (metav1.ObjectMeta, error) { @@ -86,7 +73,7 @@ func (r *Resource) getObjectMeta(v interface{}) (metav1.ObjectMeta, error) { } return metav1.ObjectMeta{ - Name: r.target, + Name: r.config.Target, Namespace: key.Namespace(cluster), }, nil } @@ -108,7 +95,7 @@ func (r *Resource) getDesiredObject(ctx context.Context, v interface{}) (*v1.Sec } secretData := sourceSecret.Data - if key.IsCAPIManagementCluster(r.provider) { + if key.IsCAPIManagementCluster(r.config.Provider) { // CAPI Secret is a kubeconfig so we need to extract the certs from it if kubeconfig, ok := secretData["value"]; ok { capiKubeconfig, err := clientcmd.Load(kubeconfig) @@ -147,16 +134,16 @@ func (r *Resource) getSource(ctx context.Context, v interface{}) (*v1.Secret, er } var secret *v1.Secret - for _, source := range r.sources { + for _, source := range r.config.Sources { secretName := source.NameFunc(cluster) secretNamespace := source.NamespaceFunc(cluster) - r.logger.Debugf(ctx, "searching for secret %v/%v", secretNamespace, secretName) + r.config.Logger.Debugf(ctx, "searching for secret %v/%v", secretNamespace, secretName) - secret, err = r.k8sClient.K8sClient().CoreV1().Secrets(secretNamespace).Get(ctx, secretName, metav1.GetOptions{}) + secret, err = r.config.K8sClient.K8sClient().CoreV1().Secrets(secretNamespace).Get(ctx, secretName, metav1.GetOptions{}) if apierrors.IsNotFound(err) { // fallthrough - r.logger.Debugf(ctx, "did not find secret %v/%v", secretNamespace, secretName) + r.config.Logger.Debugf(ctx, "did not find secret %v/%v", secretNamespace, secretName) secret = nil } else if err != nil { return nil, microerror.Mask(err) @@ -164,7 +151,7 @@ func (r *Resource) getSource(ctx context.Context, v interface{}) (*v1.Secret, er if secret != nil { // We return the first secret we find - r.logger.Debugf(ctx, "found secret %v/%v", secretNamespace, secretName) + r.config.Logger.Debugf(ctx, "found secret %v/%v", secretNamespace, secretName) return secret, nil } } diff --git a/service/controller/resource/ciliumnetpol/create.go b/service/controller/resource/ciliumnetpol/create.go index 8a1f10c52..ab275b736 100644 --- a/service/controller/resource/ciliumnetpol/create.go +++ b/service/controller/resource/ciliumnetpol/create.go @@ -12,7 +12,12 @@ import ( ) func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error { - r.logger.Debugf(ctx, "creating") + if r.config.MimirEnabled { + r.config.Logger.Debugf(ctx, "mimir is enabled, deleting heartbeat if it exists") + return r.EnsureDeleted(ctx, obj) + } + + r.config.Logger.Debugf(ctx, "creating") { resource := schema.GroupVersionResource{ Group: "cilium.io", @@ -25,9 +30,9 @@ func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error { return microerror.Mask(err) } - current, err := r.dynamicK8sClient.Resource(resource).Namespace(desired.GetNamespace()).Get(ctx, desired.GetName(), metav1.GetOptions{}) + current, err := r.config.DynamicK8sClient.Resource(resource).Namespace(desired.GetNamespace()).Get(ctx, desired.GetName(), metav1.GetOptions{}) if apierrors.IsNotFound(err) { - current, err = r.dynamicK8sClient.Resource(resource).Namespace(desired.GetNamespace()).Create(ctx, desired, metav1.CreateOptions{}) + current, err = r.config.DynamicK8sClient.Resource(resource).Namespace(desired.GetNamespace()).Create(ctx, desired, metav1.CreateOptions{}) } if err != nil { return microerror.Mask(err) @@ -35,13 +40,13 @@ func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error { if hasCiliumNetworkPolicyChanged(current, desired) { resourceutils.UpdateMeta(current, desired) - _, err = r.dynamicK8sClient.Resource(resource).Namespace(desired.GetNamespace()).Update(ctx, desired, metav1.UpdateOptions{}) + _, err = r.config.DynamicK8sClient.Resource(resource).Namespace(desired.GetNamespace()).Update(ctx, desired, metav1.UpdateOptions{}) if err != nil { return microerror.Mask(err) } } } - r.logger.Debugf(ctx, "created") + r.config.Logger.Debugf(ctx, "created") return nil } diff --git a/service/controller/resource/ciliumnetpol/delete.go b/service/controller/resource/ciliumnetpol/delete.go index fb46e21e1..5dc44467a 100644 --- a/service/controller/resource/ciliumnetpol/delete.go +++ b/service/controller/resource/ciliumnetpol/delete.go @@ -10,7 +10,7 @@ import ( ) func (r *Resource) EnsureDeleted(ctx context.Context, obj interface{}) error { - r.logger.Debugf(ctx, "deleting") + r.config.Logger.Debugf(ctx, "deleting") { resource := schema.GroupVersionResource{ Group: "cilium.io", @@ -23,14 +23,14 @@ func (r *Resource) EnsureDeleted(ctx context.Context, obj interface{}) error { return microerror.Mask(err) } - err = r.dynamicK8sClient.Resource(resource).Namespace(desired.GetNamespace()).Delete(ctx, desired.GetName(), metav1.DeleteOptions{}) + err = r.config.DynamicK8sClient.Resource(resource).Namespace(desired.GetNamespace()).Delete(ctx, desired.GetName(), metav1.DeleteOptions{}) if apierrors.IsNotFound(err) { // fall through } else if err != nil { return microerror.Mask(err) } } - r.logger.Debugf(ctx, "deleted") + r.config.Logger.Debugf(ctx, "deleted") return nil } diff --git a/service/controller/resource/ciliumnetpol/resource.go b/service/controller/resource/ciliumnetpol/resource.go index 7279c5e5d..981060e61 100644 --- a/service/controller/resource/ciliumnetpol/resource.go +++ b/service/controller/resource/ciliumnetpol/resource.go @@ -22,20 +22,16 @@ const ( type Config struct { DynamicK8sClient dynamic.Interface Logger micrologger.Logger + + MimirEnabled bool } type Resource struct { - dynamicK8sClient dynamic.Interface - logger micrologger.Logger + config Config } func New(config Config) (*Resource, error) { - r := &Resource{ - dynamicK8sClient: config.DynamicK8sClient, - logger: config.Logger, - } - - return r, nil + return &Resource{config}, nil } func (r *Resource) Name() string { diff --git a/service/controller/resource/etcd-certificates/resource.go b/service/controller/resource/etcd-certificates/resource.go index 890a75932..e8eaa2135 100644 --- a/service/controller/resource/etcd-certificates/resource.go +++ b/service/controller/resource/etcd-certificates/resource.go @@ -24,6 +24,8 @@ type Config struct { K8sClient k8sclient.Interface Logger micrologger.Logger Provider cluster.Provider + + MimirEnabled bool } // secretCopier provides a way to create a new secret from different data source. @@ -68,7 +70,8 @@ func New(config Config) (*generic.Resource, error) { GetDesiredObject: func(ctx context.Context, v interface{}) (metav1.Object, error) { return sc.ToSecret(ctx, v, config) }, - HasChangedFunc: hasChanged, + HasChangedFunc: hasChanged, + DeleteIfMimirEnabled: config.MimirEnabled, } r, err := generic.New(c) if err != nil { diff --git a/service/controller/resource/monitoring/ingress/resource.go b/service/controller/resource/monitoring/ingress/resource.go index 6856e73b8..2ffb6022d 100644 --- a/service/controller/resource/monitoring/ingress/resource.go +++ b/service/controller/resource/monitoring/ingress/resource.go @@ -26,6 +26,8 @@ type Config struct { RestrictedAccessEnabled bool WhitelistedSubnets string ExternalDNS bool + + MimirEnabled bool } func New(config Config) (*generic.Resource, error) { @@ -44,7 +46,8 @@ func New(config Config) (*generic.Resource, error) { GetDesiredObject: func(ctx context.Context, v interface{}) (metav1.Object, error) { return toIngress(v, config) }, - HasChangedFunc: hasChanged, + HasChangedFunc: hasChanged, + DeleteIfMimirEnabled: config.MimirEnabled, } r, err := generic.New(c) if err != nil { diff --git a/service/controller/resource/monitoring/prometheus/resource.go b/service/controller/resource/monitoring/prometheus/resource.go index b3049502c..5bbe68474 100644 --- a/service/controller/resource/monitoring/prometheus/resource.go +++ b/service/controller/resource/monitoring/prometheus/resource.go @@ -63,7 +63,8 @@ func New(config Config) (*generic.Resource, error) { GetDesiredObject: func(ctx context.Context, v interface{}) (metav1.Object, error) { return toPrometheus(ctx, v, config) }, - HasChangedFunc: hasChanged, + HasChangedFunc: hasChanged, + DeleteIfMimirEnabled: config.MimirEnabled, } r, err := generic.New(c) if err != nil { diff --git a/service/controller/resource/monitoring/remotewriteingress/create.go b/service/controller/resource/monitoring/remotewriteingress/create.go index 37733f9df..916ac8f87 100644 --- a/service/controller/resource/monitoring/remotewriteingress/create.go +++ b/service/controller/resource/monitoring/remotewriteingress/create.go @@ -11,6 +11,11 @@ import ( ) func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error { + if r.config.MimirEnabled { + r.config.Logger.Debugf(ctx, "mimir is enabled, deleting heartbeat if it exists") + return r.EnsureDeleted(ctx, obj) + } + desired, err := r.toIngress(obj) if err != nil { return microerror.Mask(err) diff --git a/service/controller/resource/monitoring/remotewriteingress/resource.go b/service/controller/resource/monitoring/remotewriteingress/resource.go index 02d905cc4..5678d1ed4 100644 --- a/service/controller/resource/monitoring/remotewriteingress/resource.go +++ b/service/controller/resource/monitoring/remotewriteingress/resource.go @@ -18,10 +18,11 @@ const ( ) type Config struct { - K8sClient k8sclient.Interface - Logger micrologger.Logger - BaseDomain string - ExternalDNS bool + K8sClient k8sclient.Interface + Logger micrologger.Logger + BaseDomain string + ExternalDNS bool + MimirEnabled bool } type Resource struct { diff --git a/service/controller/resource/monitoring/remotewriteingressauth/create.go b/service/controller/resource/monitoring/remotewriteingressauth/create.go index 61a0730dc..3ca89c505 100644 --- a/service/controller/resource/monitoring/remotewriteingressauth/create.go +++ b/service/controller/resource/monitoring/remotewriteingressauth/create.go @@ -11,6 +11,11 @@ import ( ) func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error { + if r.config.MimirEnabled { + r.config.Logger.Debugf(ctx, "mimir is enabled, deleting heartbeat if it exists") + return r.EnsureDeleted(ctx, obj) + } + desired, err := r.toSecret(ctx, obj) if err != nil { return microerror.Mask(err) diff --git a/service/controller/resource/monitoring/remotewriteingressauth/resource.go b/service/controller/resource/monitoring/remotewriteingressauth/resource.go index 6e764c079..58742d1eb 100644 --- a/service/controller/resource/monitoring/remotewriteingressauth/resource.go +++ b/service/controller/resource/monitoring/remotewriteingressauth/resource.go @@ -27,6 +27,7 @@ type Config struct { PasswordManager password.Manager Installation string Provider cluster.Provider + MimirEnabled bool } type Resource struct { diff --git a/service/controller/resource/monitoring/scrapeconfigs/resource.go b/service/controller/resource/monitoring/scrapeconfigs/resource.go index 2305851d9..978ed1932 100644 --- a/service/controller/resource/monitoring/scrapeconfigs/resource.go +++ b/service/controller/resource/monitoring/scrapeconfigs/resource.go @@ -47,6 +47,8 @@ type Config struct { Vault string TemplatePath string WorkloadClusterETCDDomain string + + MimirEnabled bool } type TemplateData struct { @@ -100,7 +102,8 @@ func New(config Config) (*generic.Resource, error) { GetDesiredObject: func(ctx context.Context, v interface{}) (metav1.Object, error) { return toSecret(ctx, v, config) }, - HasChangedFunc: hasChanged, + HasChangedFunc: hasChanged, + DeleteIfMimirEnabled: config.MimirEnabled, } r, err := generic.New(c) if err != nil { diff --git a/service/controller/resource/monitoring/verticalpodautoscaler/create.go b/service/controller/resource/monitoring/verticalpodautoscaler/create.go index a0706a488..5d5515f22 100644 --- a/service/controller/resource/monitoring/verticalpodautoscaler/create.go +++ b/service/controller/resource/monitoring/verticalpodautoscaler/create.go @@ -11,34 +11,39 @@ import ( ) func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error { + if r.config.MimirEnabled { + r.config.Logger.Debugf(ctx, "mimir is enabled, deleting heartbeat if it exists") + return r.EnsureDeleted(ctx, obj) + } + desired, err := r.getObject(ctx, obj) if err != nil { return microerror.Mask(err) } - r.logger.Debugf(ctx, "checking if vpa cr already exists") - current, err := r.vpaClient.AutoscalingV1().VerticalPodAutoscalers(desired.GetNamespace()).Get(ctx, desired.GetName(), metav1.GetOptions{}) + r.config.Logger.Debugf(ctx, "checking if vpa cr already exists") + current, err := r.config.VpaClient.AutoscalingV1().VerticalPodAutoscalers(desired.GetNamespace()).Get(ctx, desired.GetName(), metav1.GetOptions{}) if apierrors.IsNotFound(err) { - r.logger.Debugf(ctx, "creating") - _, err = r.vpaClient.AutoscalingV1().VerticalPodAutoscalers(desired.GetNamespace()).Create(ctx, desired, metav1.CreateOptions{}) + r.config.Logger.Debugf(ctx, "creating") + _, err = r.config.VpaClient.AutoscalingV1().VerticalPodAutoscalers(desired.GetNamespace()).Create(ctx, desired, metav1.CreateOptions{}) if err != nil { return microerror.Mask(err) } - r.logger.Debugf(ctx, "created") + r.config.Logger.Debugf(ctx, "created") return nil } else if err != nil { return microerror.Mask(err) } - r.logger.Debugf(ctx, "checking if vpa cr needs to be updated") + r.config.Logger.Debugf(ctx, "checking if vpa cr needs to be updated") if hasChanged(current, desired) { - r.logger.Debugf(ctx, "updating") + r.config.Logger.Debugf(ctx, "updating") resourceutils.UpdateMeta(current, desired) - _, err = r.vpaClient.AutoscalingV1().VerticalPodAutoscalers(desired.GetNamespace()).Update(ctx, desired, metav1.UpdateOptions{}) + _, err = r.config.VpaClient.AutoscalingV1().VerticalPodAutoscalers(desired.GetNamespace()).Update(ctx, desired, metav1.UpdateOptions{}) if err != nil { return microerror.Mask(err) } - r.logger.Debugf(ctx, "updated") + r.config.Logger.Debugf(ctx, "updated") } return nil diff --git a/service/controller/resource/monitoring/verticalpodautoscaler/delete.go b/service/controller/resource/monitoring/verticalpodautoscaler/delete.go index 7203c36d4..95de2db8a 100644 --- a/service/controller/resource/monitoring/verticalpodautoscaler/delete.go +++ b/service/controller/resource/monitoring/verticalpodautoscaler/delete.go @@ -14,14 +14,14 @@ func (r *Resource) EnsureDeleted(ctx context.Context, obj interface{}) error { return microerror.Mask(err) } - r.logger.Debugf(ctx, "deleting") - err = r.vpaClient.AutoscalingV1().VerticalPodAutoscalers(object.GetNamespace()).Delete(ctx, object.GetName(), metav1.DeleteOptions{}) + r.config.Logger.Debugf(ctx, "deleting") + err = r.config.VpaClient.AutoscalingV1().VerticalPodAutoscalers(object.GetNamespace()).Delete(ctx, object.GetName(), metav1.DeleteOptions{}) if apierrors.IsNotFound(err) { // fall through } else if err != nil { return microerror.Mask(err) } - r.logger.Debugf(ctx, "deleted") + r.config.Logger.Debugf(ctx, "deleted") return nil } diff --git a/service/controller/resource/monitoring/verticalpodautoscaler/resource.go b/service/controller/resource/monitoring/verticalpodautoscaler/resource.go index 42cb14df4..fc248b94d 100644 --- a/service/controller/resource/monitoring/verticalpodautoscaler/resource.go +++ b/service/controller/resource/monitoring/verticalpodautoscaler/resource.go @@ -35,15 +35,12 @@ type Config struct { Installation string Provider cluster.Provider + + MimirEnabled bool } type Resource struct { - k8sClient k8sclient.Interface - vpaClient vpa_clientset.Interface - logger micrologger.Logger - - installation string - provider cluster.Provider + config Config } func New(config Config) (*Resource, error) { @@ -61,15 +58,7 @@ func New(config Config) (*Resource, error) { return nil, microerror.Maskf(invalidConfigError, "%T.Installation must not be empty", config) } - r := &Resource{ - k8sClient: config.K8sClient, - vpaClient: config.VpaClient, - logger: config.Logger, - installation: config.Installation, - provider: config.Provider, - } - - return r, nil + return &Resource{config}, nil } func (r *Resource) Name() string { @@ -179,8 +168,8 @@ func (r *Resource) getObject(ctx context.Context, v interface{}) (*vpa_types.Ver func (r *Resource) getManagementClusterObservabilityBundleAppVersion(ctx context.Context, cluster metav1.Object) (string, error) { var appName string var appNamespace string - if key.IsCAPIManagementCluster(r.provider) { - appName = fmt.Sprintf("%s-observability-bundle", r.installation) + if key.IsCAPIManagementCluster(r.config.Provider) { + appName = fmt.Sprintf("%s-observability-bundle", r.config.Installation) appNamespace = "org-giantswarm" } else { appName = "observability-bundle" @@ -189,7 +178,7 @@ func (r *Resource) getManagementClusterObservabilityBundleAppVersion(ctx context app := &appsv1alpha1.App{} objectKey := types.NamespacedName{Namespace: appNamespace, Name: appName} - err := r.k8sClient.CtrlClient().Get(ctx, objectKey, app) + err := r.config.K8sClient.CtrlClient().Get(ctx, objectKey, app) if err != nil { if apierrors.IsNotFound(err) { return unknownObservabilityBundleVersion, nil @@ -207,7 +196,7 @@ func (r *Resource) getManagementClusterObservabilityBundleAppVersion(ctx context func (r *Resource) listWorkerNodes(ctx context.Context) (*v1.NodeList, error) { // Selects only worker nodes selector := "node-role.kubernetes.io/control-plane!=" - nodeList, err := r.k8sClient.K8sClient().CoreV1().Nodes().List(ctx, metav1.ListOptions{LabelSelector: selector}) + nodeList, err := r.config.K8sClient.K8sClient().CoreV1().Nodes().List(ctx, metav1.ListOptions{LabelSelector: selector}) if err != nil { return nil, microerror.Mask(err) } diff --git a/service/controller/resource/namespace/resource.go b/service/controller/resource/namespace/resource.go index 068ddcf71..53ddc0fe6 100644 --- a/service/controller/resource/namespace/resource.go +++ b/service/controller/resource/namespace/resource.go @@ -18,8 +18,9 @@ const ( ) type Config struct { - K8sClient k8sclient.Interface - Logger micrologger.Logger + K8sClient k8sclient.Interface + Logger micrologger.Logger + MimirEnabled bool } func New(config Config) (*generic.Resource, error) { @@ -29,12 +30,13 @@ func New(config Config) (*generic.Resource, error) { } c := generic.Config{ - ClientFunc: clientFunc, - Logger: config.Logger, - Name: Name, - GetObjectMeta: getObjectMeta, - GetDesiredObject: toNamespace, - HasChangedFunc: hasChanged, + ClientFunc: clientFunc, + Logger: config.Logger, + Name: Name, + GetObjectMeta: getObjectMeta, + GetDesiredObject: toNamespace, + HasChangedFunc: hasChanged, + DeleteIfMimirEnabled: config.MimirEnabled, } r, err := generic.New(c) if err != nil { diff --git a/service/controller/resource/rbac/create.go b/service/controller/resource/rbac/create.go index 812da3a98..e326a7cb0 100644 --- a/service/controller/resource/rbac/create.go +++ b/service/controller/resource/rbac/create.go @@ -11,16 +11,20 @@ import ( ) func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error { - r.logger.Debugf(ctx, "creating") + if r.config.MimirEnabled { + r.config.Logger.Debugf(ctx, "mimir is enabled, deleting heartbeat if it exists") + return r.EnsureDeleted(ctx, obj) + } + r.config.Logger.Debugf(ctx, "creating") { desired, err := toClusterRoleBinding(obj) if err != nil { return microerror.Mask(err) } - current, err := r.k8sClient.K8sClient().RbacV1().ClusterRoleBindings().Get(ctx, desired.GetName(), metav1.GetOptions{}) + current, err := r.config.K8sClient.K8sClient().RbacV1().ClusterRoleBindings().Get(ctx, desired.GetName(), metav1.GetOptions{}) if apierrors.IsNotFound(err) { - current, err = r.k8sClient.K8sClient().RbacV1().ClusterRoleBindings().Create(ctx, desired, metav1.CreateOptions{}) + current, err = r.config.K8sClient.K8sClient().RbacV1().ClusterRoleBindings().Create(ctx, desired, metav1.CreateOptions{}) } if err != nil { return microerror.Mask(err) @@ -28,13 +32,13 @@ func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error { if hasClusterRoleBindingChanged(current, desired) { resourceutils.UpdateMeta(current, desired) - _, err = r.k8sClient.K8sClient().RbacV1().ClusterRoleBindings().Update(ctx, desired, metav1.UpdateOptions{}) + _, err = r.config.K8sClient.K8sClient().RbacV1().ClusterRoleBindings().Update(ctx, desired, metav1.UpdateOptions{}) if err != nil { return microerror.Mask(err) } } } - r.logger.Debugf(ctx, "created") + r.config.Logger.Debugf(ctx, "created") return nil } diff --git a/service/controller/resource/rbac/delete.go b/service/controller/resource/rbac/delete.go index c4fedbc6f..99ea051a4 100644 --- a/service/controller/resource/rbac/delete.go +++ b/service/controller/resource/rbac/delete.go @@ -9,21 +9,21 @@ import ( ) func (r *Resource) EnsureDeleted(ctx context.Context, obj interface{}) error { - r.logger.Debugf(ctx, "deleting") + r.config.Logger.Debugf(ctx, "deleting") { desired, err := toClusterRoleBinding(obj) if err != nil { return microerror.Mask(err) } - err = r.k8sClient.K8sClient().RbacV1().ClusterRoleBindings().Delete(ctx, desired.GetName(), metav1.DeleteOptions{}) + err = r.config.K8sClient.K8sClient().RbacV1().ClusterRoleBindings().Delete(ctx, desired.GetName(), metav1.DeleteOptions{}) if apierrors.IsNotFound(err) { // fall through } else if err != nil { return microerror.Mask(err) } } - r.logger.Debugf(ctx, "deleted") + r.config.Logger.Debugf(ctx, "deleted") return nil } diff --git a/service/controller/resource/rbac/resource.go b/service/controller/resource/rbac/resource.go index b418ef23d..9f23ad6e3 100644 --- a/service/controller/resource/rbac/resource.go +++ b/service/controller/resource/rbac/resource.go @@ -17,22 +17,17 @@ const ( ) type Config struct { - K8sClient k8sclient.Interface - Logger micrologger.Logger + K8sClient k8sclient.Interface + Logger micrologger.Logger + MimirEnabled bool } type Resource struct { - k8sClient k8sclient.Interface - logger micrologger.Logger + config Config } func New(config Config) (*Resource, error) { - r := &Resource{ - k8sClient: config.K8sClient, - logger: config.Logger, - } - - return r, nil + return &Resource{config}, nil } func (r *Resource) Name() string {