Skip to content

Commit

Permalink
Remove resources when mimir is enabled (#1652)
Browse files Browse the repository at this point in the history
* Remove resources when mimir is enabled

* fix tests
  • Loading branch information
QuentinBisson authored Jun 11, 2024
1 parent c595f16 commit 2cea0f0
Show file tree
Hide file tree
Showing 25 changed files with 187 additions and 138 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 28 additions & 8 deletions service/controller/clusterapi/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -334,13 +347,16 @@ func New(config Config) ([]resource.Interface, error) {
Provider: config.Provider,
Region: config.Region,
Installation: config.Installation,

MimirEnabled: config.MimirEnabled,
}

scrapeConfigResource, err = scrapeconfigs.New(c)
if err != nil {
return nil, microerror.Mask(err)
}
}

var ingressResource resource.Interface
{
c := ingress.Config{
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
25 changes: 20 additions & 5 deletions service/controller/managementcluster/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ var (
type Config struct {
K8sClient k8sclient.Interface
Logger micrologger.Logger

MimirEnabled bool
}

func New(config Config) (*generic.Resource, error) {
Expand All @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions service/controller/resource/certificates/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand All @@ -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
}
6 changes: 3 additions & 3 deletions service/controller/resource/certificates/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading

0 comments on commit 2cea0f0

Please sign in to comment.