Skip to content

Commit

Permalink
Add flag to compress the Prometheus ConfigMap
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSpiritXIII committed Sep 19, 2023
1 parent e5bb288 commit 8ef0ecb
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ spec:
type: object
description: Features holds configuration for optional managed-collection features.
properties:
config:
type: object
description: Settings for the collector configuration propagation.
properties:
compression:
type: string
description: Compression enables compression of the config data propagated by the operator to collectors. It is recommended to use the gzip option when using a large number of ClusterPodMonitoring and/or PodMonitoring.
enum:
- none
- gzip
targetStatus:
type: object
description: Configuration of target status reporting.
Expand Down
15 changes: 15 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This Document documents the types introduced by the GMP CRDs to be consumed by u
* [ClusterRules](#clusterrules)
* [ClusterRulesList](#clusterruleslist)
* [CollectionSpec](#collectionspec)
* [ConfigSpec](#configspec)
* [ExportFilters](#exportfilters)
* [GlobalRules](#globalrules)
* [GlobalRulesList](#globalruleslist)
Expand Down Expand Up @@ -190,6 +191,19 @@ CollectionSpec specifies how the operator configures collection of metric data.

[Back to TOC](#table-of-contents)

## ConfigSpec

ConfigSpec holds configurations for the Prometheus configuration.


<em>appears in: [OperatorFeatures](#operatorfeatures)</em>

| Field | Description | Scheme | Required |
| ----- | ----------- | ------ | -------- |
| compression | Compression enables compression of the config data propagated by the operator to collectors. It is recommended to use the gzip option when using a large number of ClusterPodMonitoring and/or PodMonitoring. | CompressionType | false |

[Back to TOC](#table-of-contents)

## ExportFilters

ExportFilters provides mechanisms to filter the scraped data that's sent to GMP.
Expand Down Expand Up @@ -325,6 +339,7 @@ OperatorFeatures holds configuration for optional managed-collection features.
| Field | Description | Scheme | Required |
| ----- | ----------- | ------ | -------- |
| targetStatus | Configuration of target status reporting. | [TargetStatusSpec](#targetstatusspec) | false |
| config | Settings for the collector configuration propagation. | [ConfigSpec](#configspec) | false |

[Back to TOC](#table-of-contents)

Expand Down
10 changes: 10 additions & 0 deletions manifests/setup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,16 @@ spec:
type: object
description: Features holds configuration for optional managed-collection features.
properties:
config:
type: object
description: Settings for the collector configuration propagation.
properties:
compression:
type: string
description: Compression enables compression of the config data propagated by the operator to collectors. It is recommended to use the gzip option when using a large number of ClusterPodMonitoring and/or PodMonitoring.
enum:
- none
- gzip
targetStatus:
type: object
description: Configuration of target status reporting.
Expand Down
13 changes: 13 additions & 0 deletions pkg/operator/apis/monitoring/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ type CollectionSpec struct {
type OperatorFeatures struct {
// Configuration of target status reporting.
TargetStatus TargetStatusSpec `json:"targetStatus,omitempty"`
// Settings for the collector configuration propagation.
Config ConfigSpec `json:"config,omitempty"`
}

// ConfigSpec holds configurations for the Prometheus configuration.
type ConfigSpec struct {
// Compression enables compression of the config data propagated by the operator to collectors.
// It is recommended to use the gzip option when using a large number of ClusterPodMonitoring
// and/or PodMonitoring.
Compression CompressionType `json:"compression,omitempty"`
}

// TargetStatusSpec holds configuration for target status reporting.
Expand All @@ -128,6 +138,9 @@ type TargetStatusSpec struct {
// +kubebuilder:validation:Enum=none;gzip
type CompressionType string

const CompressionNone CompressionType = "none"
const CompressionGzip CompressionType = "gzip"

// KubeletScraping allows enabling scraping of the Kubelets' metric endpoints.
type KubeletScraping struct {
// The interval at which the metric endpoints are scraped.
Expand Down
17 changes: 17 additions & 0 deletions pkg/operator/apis/monitoring/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 36 additions & 5 deletions pkg/operator/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package operator

import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -174,7 +176,7 @@ func (r *collectionReconciler) Reconcile(ctx context.Context, req reconcile.Requ
return reconcile.Result{}, fmt.Errorf("ensure collector daemon set: %w", err)
}

if err := r.ensureCollectorConfig(ctx, &config.Collection); err != nil {
if err := r.ensureCollectorConfig(ctx, &config.Collection, config.Features.Config.Compression); err != nil {
return reconcile.Result{}, fmt.Errorf("ensure collector config: %w", err)
}

Expand Down Expand Up @@ -255,7 +257,7 @@ func (r *collectionReconciler) ensureCollectorDaemonSet(ctx context.Context, spe
flags = append(flags, fmt.Sprintf("--export.credentials-file=%q", p))
}

if len(spec.Compression) > 0 && spec.Compression != "none" {
if len(spec.Compression) > 0 && spec.Compression != monitoringv1.CompressionNone {
flags = append(flags, fmt.Sprintf("--export.compression=%s", spec.Compression))
}

Expand Down Expand Up @@ -300,8 +302,20 @@ func resolveLabels(opts Options, externalLabels map[string]string) (projectID st
return
}

func gzipData(data []byte) ([]byte, error) {
var b bytes.Buffer
gz := gzip.NewWriter(&b)
if _, err := gz.Write(data); err != nil {
return nil, err
}
if err := gz.Close(); err != nil {
return nil, err
}
return b.Bytes(), nil
}

// ensureCollectorConfig generates the collector config and creates or updates it.
func (r *collectionReconciler) ensureCollectorConfig(ctx context.Context, spec *monitoringv1.CollectionSpec) error {
func (r *collectionReconciler) ensureCollectorConfig(ctx context.Context, spec *monitoringv1.CollectionSpec, compression monitoringv1.CompressionType) error {
cfg, err := r.makeCollectorConfig(ctx, spec)
if err != nil {
return fmt.Errorf("generate Prometheus config: %w", err)
Expand All @@ -316,9 +330,26 @@ func (r *collectionReconciler) ensureCollectorConfig(ctx context.Context, spec *
Namespace: r.opts.OperatorNamespace,
Name: NameCollector,
},
Data: map[string]string{
}

// Thanos config-reloader detects gzip compression automatically, so no sync with
// config-reloaders is needed when switching between these.
switch compression {
case monitoringv1.CompressionGzip:
compressedCfg, err := gzipData(cfgEncoded)
if err != nil {
return fmt.Errorf("gzip Prometheus config: %w", err)
}

cm.BinaryData = map[string][]byte{
configFilename: compressedCfg,
}
case "", monitoringv1.CompressionNone:
cm.Data = map[string]string{
configFilename: string(cfgEncoded),
},
}
default:
return fmt.Errorf("unknown compression type: %q", compression)
}

if err := r.client.Update(ctx, cm); apierrors.IsNotFound(err) {
Expand Down

0 comments on commit 8ef0ecb

Please sign in to comment.