Skip to content

Commit

Permalink
redo compare
Browse files Browse the repository at this point in the history
  • Loading branch information
jiuker committed Feb 6, 2025
1 parent dc1bcb3 commit 732b95c
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 72 deletions.
6 changes: 6 additions & 0 deletions docs/tenant_crd.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,12 @@ Directs the MinIO Operator to use prometheus operator. +

Tenant scrape configuration will be added to prometheus managed by the prometheus-operator.

|*`prometheusOperatorScrapeMetricsPath`* __string array__
|*Optional* +


The name of the Prometheus instance to scrape metrics from.

|*`serviceAccountName`* __string__
|*Optional* +

Expand Down
4 changes: 4 additions & 0 deletions helm/operator/templates/minio.min.io_tenants.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3682,6 +3682,10 @@ spec:
type: string
prometheusOperator:
type: boolean
prometheusOperatorScrapeMetricsPath:
items:
type: string
type: array
readiness:
properties:
exec:
Expand Down
3 changes: 0 additions & 3 deletions pkg/apis/minio.min.io/v2/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,6 @@ const StatefulSetPrefix = "ss"
// StatefulSetLegacyPrefix by old operators
const StatefulSetLegacyPrefix = "zone"

// MinIOPrometheusPathCluster is the path where MinIO tenant exposes cluster Prometheus metrics
const MinIOPrometheusPathCluster = "/minio/v2/metrics/cluster"

// MinIOPrometheusScrapeInterval defines how frequently to scrape targets.
const MinIOPrometheusScrapeInterval = 30 * time.Second

Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/minio.min.io/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ type TenantSpec struct {
PrometheusOperator bool `json:"prometheusOperator,omitempty"`
// *Optional* +
//
// The name of the Prometheus instance to scrape metrics from.
//
// +optional
PrometheusOperatorScrapeMetricsPath []string `json:"prometheusOperatorScrapeMetricsPath,omitempty"`
// *Optional* +
//
// The https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/[Kubernetes Service Account] to use for running MinIO pods created as part of the Tenant. +
// +optional
ServiceAccountName string `json:"serviceAccountName,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/minio.min.io/v2/zz_generated.deepcopy.go

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

81 changes: 46 additions & 35 deletions pkg/client/applyconfiguration/minio.min.io/v2/tenantspec.go

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

49 changes: 29 additions & 20 deletions pkg/controller/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package controller
import (
"context"
"errors"
"reflect"
"strings"

promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
promv1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1"
Expand Down Expand Up @@ -161,20 +163,34 @@ func (c *Controller) checkAndCreatePrometheusAddlConfig(ctx context.Context, ten
return err
}
} else {
var scrapeConfigs []configmaps.ScrapeConfig
var scrapeConfigs, exceptedScrapeConfigs []configmaps.ScrapeConfig
err := yaml.Unmarshal(secret.Data[miniov2.PrometheusAddlScrapeConfigKey], &scrapeConfigs)
if err != nil {
return err
}
// Check if the scrape config is already present
hasScrapeConfig := false
// get other scrape configs
for _, sc := range scrapeConfigs {
if sc.JobName == tenant.PrometheusOperatorAddlConfigJobName() {
hasScrapeConfig = true
break
if !strings.HasPrefix(sc.JobName, tenant.PrometheusOperatorAddlConfigJobName()) {
exceptedScrapeConfigs = append(exceptedScrapeConfigs, sc)
}
}
if !hasScrapeConfig {
exceptedScrapeConfigs = append(exceptedScrapeConfigs, promCfg.ScrapeConfigs...)
updateScrapeConfig := false
if len(scrapeConfigs) != len(exceptedScrapeConfigs) {
updateScrapeConfig = true
} else {
for i := range scrapeConfigs {
if scrapeConfigs[i].JobName != exceptedScrapeConfigs[i].JobName ||
scrapeConfigs[i].MetricsPath != exceptedScrapeConfigs[i].MetricsPath ||
scrapeConfigs[i].Scheme != exceptedScrapeConfigs[i].Scheme ||
!reflect.DeepEqual(scrapeConfigs[i].TLSConfig, exceptedScrapeConfigs[i].TLSConfig) ||
!reflect.DeepEqual(scrapeConfigs[i].StaticConfigs, exceptedScrapeConfigs[i].StaticConfigs) {
updateScrapeConfig = true
break
}
}
}
if updateScrapeConfig {
klog.Infof("Adding MinIO tenant Prometheus scrape config")
scrapeConfigs = append(scrapeConfigs, promCfg.ScrapeConfigs...)
scrapeCfgYaml, err := yaml.Marshal(scrapeConfigs)
Expand Down Expand Up @@ -224,27 +240,20 @@ func (c *Controller) deletePrometheusAddlConfig(ctx context.Context, tenant *min
return err
}

var scrapeConfigs []configmaps.ScrapeConfig
var scrapeConfigs, exceptedScrapeConfigs []configmaps.ScrapeConfig
err = yaml.Unmarshal(secret.Data[miniov2.PrometheusAddlScrapeConfigKey], &scrapeConfigs)
if err != nil {
return err
}
// Check if the scrape config is present
hasScrapeConfig := false
scIndex := -1
for i, sc := range scrapeConfigs {
if sc.JobName == tenant.PrometheusOperatorAddlConfigJobName() {
hasScrapeConfig = true
scIndex = i
break
for _, sc := range scrapeConfigs {
if !strings.HasPrefix(sc.JobName, tenant.PrometheusOperatorAddlConfigJobName()) {
exceptedScrapeConfigs = append(exceptedScrapeConfigs, sc)
}
}
if hasScrapeConfig {
if !reflect.DeepEqual(scrapeConfigs, exceptedScrapeConfigs) {
klog.Infof("Deleting MinIO tenant Prometheus scrape config")
// Delete the config
newScrapeConfigs := append(scrapeConfigs[:scIndex], scrapeConfigs[scIndex+1:]...)
// Update the secret
scrapeCfgYaml, err := yaml.Marshal(newScrapeConfigs)
scrapeCfgYaml, err := yaml.Marshal(exceptedScrapeConfigs)
if err != nil {
return err
}
Expand Down
35 changes: 21 additions & 14 deletions pkg/resources/configmaps/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,29 @@ func GetPrometheusConfig(t *miniov2.Tenant, accessKey, secretKey string) *Promet
ScrapeInterval: miniov2.MinIOPrometheusScrapeInterval,
EvaluationInterval: 30 * time.Second,
},
ScrapeConfigs: []ScrapeConfig{
{
JobName: t.PrometheusConfigJobName(),
BearerToken: bearerToken,
MetricsPath: miniov2.MinIOPrometheusPathCluster,
Scheme: minioScheme,
TLSConfig: tlsConfig{
CAFile: "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt",
},
StaticConfigs: []staticConfig{
{
Targets: []string{minioTargets},
},
ScrapeConfigs: []ScrapeConfig{},
}

if len(t.Spec.PrometheusOperatorScrapeMetricsPath) == 0 {
t.Spec.PrometheusOperatorScrapeMetricsPath = []string{"/minio/v2/metrics/cluster"}
}

for index, scrape := range t.Spec.PrometheusOperatorScrapeMetricsPath {
promConfig.ScrapeConfigs = append(promConfig.ScrapeConfigs, ScrapeConfig{
JobName: fmt.Sprintf("%s-%d", t.PrometheusOperatorAddlConfigJobName(), index),
BearerToken: bearerToken,
MetricsPath: scrape,
Scheme: minioScheme,
TLSConfig: tlsConfig{
CAFile: "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt",
},
StaticConfigs: []staticConfig{
{
Targets: []string{minioTargets},
},
},
},
})
}

return promConfig
}
4 changes: 4 additions & 0 deletions resources/base/crds/minio.min.io_tenants.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3682,6 +3682,10 @@ spec:
type: string
prometheusOperator:
type: boolean
prometheusOperatorScrapeMetricsPath:
items:
type: string
type: array
readiness:
properties:
exec:
Expand Down

0 comments on commit 732b95c

Please sign in to comment.