Skip to content

Commit cfe4dcd

Browse files
committed
replacing camel case matchExpression and matchLabel to lower case in ta config unmarshalling
1 parent dbdb7db commit cfe4dcd

File tree

3 files changed

+144
-186
lines changed

3 files changed

+144
-186
lines changed

cmd/otel-allocator/config/config.go

+29-72
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ import (
2121
"fmt"
2222
"io/fs"
2323
"os"
24-
"reflect"
24+
"regexp"
2525
"time"
2626

2727
"github.com/go-logr/logr"
28-
"github.com/mitchellh/mapstructure"
2928
"github.com/prometheus/common/model"
3029
promconfig "github.com/prometheus/prometheus/config"
3130
_ "github.com/prometheus/prometheus/discovery/install"
@@ -48,24 +47,29 @@ const (
4847
)
4948

5049
type Config struct {
51-
ListenAddr string `yaml:"listen_addr,omitempty"`
52-
KubeConfigFilePath string `yaml:"kube_config_file_path,omitempty"`
53-
ClusterConfig *rest.Config `yaml:"-"`
54-
RootLogger logr.Logger `yaml:"-"`
55-
CollectorSelector *metav1.LabelSelector `yaml:"collector_selector,omitempty"`
56-
PromConfig *promconfig.Config `yaml:"config"`
57-
AllocationStrategy string `yaml:"allocation_strategy,omitempty"`
58-
FilterStrategy string `yaml:"filter_strategy,omitempty"`
59-
PrometheusCR PrometheusCRConfig `yaml:"prometheus_cr,omitempty"`
60-
HTTPS HTTPSServerConfig `yaml:"https,omitempty"`
50+
ListenAddr string `yaml:"listen_addr,omitempty"`
51+
KubeConfigFilePath string `yaml:"kube_config_file_path,omitempty"`
52+
ClusterConfig *rest.Config `yaml:"-"`
53+
RootLogger logr.Logger `yaml:"-"`
54+
CollectorSelector *metav1.LabelSelector `yaml:"collector_selector,omitempty"`
55+
PromConfig *promconfig.Config `yaml:"config"`
56+
AllocationStrategy string `yaml:"allocation_strategy,omitempty"`
57+
AllocationFallbackStrategy string `yaml:"allocation_fallback_strategy,omitempty"`
58+
FilterStrategy string `yaml:"filter_strategy,omitempty"`
59+
PrometheusCR PrometheusCRConfig `yaml:"prometheus_cr,omitempty"`
60+
HTTPS HTTPSServerConfig `yaml:"https,omitempty"`
6161
}
6262

6363
type PrometheusCRConfig struct {
6464
Enabled bool `yaml:"enabled,omitempty"`
6565
PodMonitorSelector *metav1.LabelSelector `yaml:"pod_monitor_selector,omitempty"`
66+
PodMonitorNamespaceSelector *metav1.LabelSelector `yaml:"pod_monitor_namespace_selector,omitempty"`
6667
ServiceMonitorSelector *metav1.LabelSelector `yaml:"service_monitor_selector,omitempty"`
6768
ServiceMonitorNamespaceSelector *metav1.LabelSelector `yaml:"service_monitor_namespace_selector,omitempty"`
68-
PodMonitorNamespaceSelector *metav1.LabelSelector `yaml:"pod_monitor_namespace_selector,omitempty"`
69+
ScrapeConfigSelector *metav1.LabelSelector `yaml:"scrape_config_selector,omitempty"`
70+
ScrapeConfigNamespaceSelector *metav1.LabelSelector `yaml:"scrape_config_namespace_selector,omitempty"`
71+
ProbeSelector *metav1.LabelSelector `yaml:"probe_selector,omitempty"`
72+
ProbeNamespaceSelector *metav1.LabelSelector `yaml:"probe_namespace_selector,omitempty"`
6973
ScrapeInterval model.Duration `yaml:"scrape_interval,omitempty"`
7074
}
7175

@@ -150,78 +154,31 @@ func LoadFromCLI(target *Config, flagSet *pflag.FlagSet) error {
150154
return nil
151155
}
152156

153-
func StringToModelDurationHookFunc() mapstructure.DecodeHookFunc {
154-
return func(
155-
f reflect.Type,
156-
t reflect.Type,
157-
data interface{},
158-
) (interface{}, error) {
159-
if f.Kind() != reflect.String {
160-
return data, nil
161-
}
162-
if t != reflect.TypeOf(model.Duration(5)) {
163-
return data, nil
164-
}
165-
166-
// Convert it by parsing
167-
return time.ParseDuration(data.(string))
168-
}
169-
}
170-
171-
func decodeSubConfig(t interface{}, dc mapstructure.DecoderConfig) error {
172-
dec, decError := mapstructure.NewDecoder(&dc)
173-
if decError != nil {
174-
return decError
175-
}
176-
if err := dec.Decode(t); err != nil {
177-
return err
178-
}
179-
return nil
180-
}
181-
182-
func flexibleUnmarshal(yamlFile []byte, cfg *Config) error {
183-
t := make(map[string]interface{})
184-
if err := yaml.Unmarshal(yamlFile, &t); err != nil {
185-
return fmt.Errorf("error unmarshaling YAML: %w", err)
186-
}
187-
188-
if t["collector_selector"] != nil {
189-
dc := mapstructure.DecoderConfig{TagName: "yaml", Result: cfg.CollectorSelector}
190-
if err := decodeSubConfig(t["collector_selector"], dc); err != nil {
191-
return err
192-
}
193-
}
194-
195-
if t["prometheus_cr"] != nil {
196-
dc := mapstructure.DecoderConfig{TagName: "yaml", Result: &cfg.PrometheusCR, DecodeHook: StringToModelDurationHookFunc()}
197-
if err := decodeSubConfig(t["prometheus_cr"], dc); err != nil {
198-
return err
199-
}
200-
}
201-
202-
return nil
203-
}
204-
205157
func unmarshal(cfg *Config, configFile string) error {
206158
yamlFile, err := os.ReadFile(configFile)
207159
if err != nil {
208160
return err
209161
}
162+
163+
// Changing matchLabels and matchExpressions from camel case to lower case
164+
// because we use yaml unmarshaling that supports lower case field names if no `yaml` tag is defined
165+
// and metav1.LabelSelector uses `json` tags.
166+
reLabels := regexp.MustCompile(`([ \t\f\v]*)matchLabels:([ \t\f\v]*\n)`)
167+
yamlFile = reLabels.ReplaceAll(yamlFile, []byte("${1}matchlabels:${2}"))
168+
reExpressions := regexp.MustCompile(`([ \t\f\v]*)matchExpressions:([ \t\f\v]*\n)`)
169+
yamlFile = reExpressions.ReplaceAll(yamlFile, []byte("${1}matchexpressions:${2}"))
170+
210171
if err = yaml.Unmarshal(yamlFile, cfg); err != nil {
211172
return fmt.Errorf("error unmarshaling YAML: %w", err)
212173
}
213-
214-
if err := flexibleUnmarshal(yamlFile, cfg); err != nil {
215-
return err
216-
}
217-
218174
return nil
219175
}
220176

221177
func CreateDefaultConfig() Config {
222178
return Config{
223-
AllocationStrategy: DefaultAllocationStrategy,
224-
FilterStrategy: DefaultFilterStrategy,
179+
AllocationStrategy: DefaultAllocationStrategy,
180+
AllocationFallbackStrategy: "",
181+
FilterStrategy: DefaultFilterStrategy,
225182
PrometheusCR: PrometheusCRConfig{
226183
ScrapeInterval: DefaultCRScrapeInterval,
227184
},

go.mod

+39-39
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
module github.com/open-telemetry/opentelemetry-operator
22

3-
go 1.22.0
3+
go 1.22.7
44

55
retract v1.51.0
66

77
require (
88
dario.cat/mergo v1.0.1
9-
github.com/Masterminds/semver/v3 v3.3.0
9+
github.com/Masterminds/semver/v3 v3.3.1
1010
github.com/blang/semver/v4 v4.0.0
1111
github.com/buraksezer/consistent v0.10.0
1212
github.com/cespare/xxhash/v2 v2.3.0
@@ -22,35 +22,34 @@ require (
2222
github.com/openshift/api v0.0.0-20240124164020-e2ce40831f2e
2323
github.com/operator-framework/api v0.27.0
2424
github.com/operator-framework/operator-lib v0.15.0
25-
github.com/prometheus-operator/prometheus-operator v0.76.0
25+
github.com/prometheus-operator/prometheus-operator v0.76.2
2626
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.76.2
2727
github.com/prometheus-operator/prometheus-operator/pkg/client v0.76.2
2828
github.com/prometheus/client_golang v1.20.5
29-
github.com/prometheus/common v0.60.1
30-
github.com/prometheus/prometheus v0.55.0
29+
github.com/prometheus/common v0.61.0
30+
github.com/prometheus/prometheus v0.55.1
3131
github.com/shirou/gopsutil v3.21.11+incompatible
3232
github.com/spf13/pflag v1.0.5
33-
github.com/stretchr/testify v1.9.0
34-
go.opentelemetry.io/collector/featuregate v1.18.0
35-
go.opentelemetry.io/otel v1.31.0
36-
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.31.0
37-
go.opentelemetry.io/otel/exporters/prometheus v0.53.0
38-
go.opentelemetry.io/otel/metric v1.31.0
39-
go.opentelemetry.io/otel/sdk v1.31.0
40-
go.opentelemetry.io/otel/sdk/metric v1.31.0
33+
github.com/stretchr/testify v1.10.0
34+
go.opentelemetry.io/collector/featuregate v1.22.0
35+
go.opentelemetry.io/otel v1.33.0
36+
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.33.0
37+
go.opentelemetry.io/otel/exporters/prometheus v0.55.0
38+
go.opentelemetry.io/otel/metric v1.33.0
39+
go.opentelemetry.io/otel/sdk v1.33.0
40+
go.opentelemetry.io/otel/sdk/metric v1.33.0
4141
go.uber.org/multierr v1.11.0
4242
go.uber.org/zap v1.27.0
4343
gopkg.in/yaml.v2 v2.4.0
4444
gopkg.in/yaml.v3 v3.0.1
45-
k8s.io/api v0.31.2
46-
k8s.io/apiextensions-apiserver v0.31.2
47-
k8s.io/apimachinery v0.31.2
48-
k8s.io/client-go v0.31.2
49-
k8s.io/component-base v0.31.2
45+
k8s.io/api v0.31.3
46+
k8s.io/apiextensions-apiserver v0.31.3
47+
k8s.io/apimachinery v0.31.3
48+
k8s.io/client-go v0.31.3
49+
k8s.io/component-base v0.31.3
5050
k8s.io/klog/v2 v2.130.1
51-
k8s.io/kubectl v0.31.2
5251
k8s.io/utils v0.0.0-20240921022957-49e7df575cb6
53-
sigs.k8s.io/controller-runtime v0.19.1
52+
sigs.k8s.io/controller-runtime v0.19.3
5453
sigs.k8s.io/gateway-api v1.1.0 // indirect
5554
sigs.k8s.io/yaml v1.4.0
5655
)
@@ -75,10 +74,10 @@ require (
7574
github.com/bytedance/sonic v1.11.6 // indirect
7675
github.com/bytedance/sonic/loader v0.1.1 // indirect
7776
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
78-
github.com/cert-manager/cert-manager v1.16.1
77+
github.com/cert-manager/cert-manager v1.16.2
7978
github.com/cloudwego/base64x v0.1.4 // indirect
8079
github.com/cloudwego/iasm v0.2.0 // indirect
81-
github.com/cncf/xds/go v0.0.0-20240723142845-024c85f92f20 // indirect
80+
github.com/cncf/xds/go v0.0.0-20240905190251-b4127c9b8d78 // indirect
8281
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
8382
github.com/dennwc/varint v1.0.0 // indirect
8483
github.com/digitalocean/godo v1.125.0 // indirect
@@ -133,7 +132,7 @@ require (
133132
github.com/gophercloud/gophercloud v1.14.0 // indirect
134133
github.com/gorilla/websocket v1.5.1 // indirect
135134
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc // indirect
136-
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
135+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.24.0 // indirect
137136
github.com/hashicorp/consul/api v1.29.4 // indirect
138137
github.com/hashicorp/cronexpr v1.1.2 // indirect
139138
github.com/hashicorp/errwrap v1.1.0 // indirect
@@ -199,28 +198,29 @@ require (
199198
github.com/yusufpapurcu/wmi v1.2.3 // indirect
200199
go.mongodb.org/mongo-driver v1.14.0 // indirect
201200
go.opencensus.io v0.24.0 // indirect
201+
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
202202
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect
203-
go.opentelemetry.io/otel/trace v1.31.0 // indirect
204-
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
203+
go.opentelemetry.io/otel/trace v1.33.0 // indirect
204+
go.opentelemetry.io/proto/otlp v1.4.0 // indirect
205205
go.uber.org/atomic v1.11.0 // indirect
206206
golang.org/x/arch v0.8.0 // indirect
207-
golang.org/x/crypto v0.28.0 // indirect
208-
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
209-
golang.org/x/mod v0.20.0 // indirect
210-
golang.org/x/net v0.30.0 // indirect
211-
golang.org/x/oauth2 v0.23.0 // indirect
212-
golang.org/x/sync v0.8.0 // indirect
213-
golang.org/x/sys v0.26.0 // indirect
214-
golang.org/x/term v0.25.0 // indirect
215-
golang.org/x/text v0.19.0 // indirect
207+
golang.org/x/crypto v0.31.0 // indirect
208+
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
209+
golang.org/x/mod v0.21.0 // indirect
210+
golang.org/x/net v0.32.0 // indirect
211+
golang.org/x/oauth2 v0.24.0 // indirect
212+
golang.org/x/sync v0.10.0 // indirect
213+
golang.org/x/sys v0.28.0 // indirect
214+
golang.org/x/term v0.27.0 // indirect
215+
golang.org/x/text v0.21.0 // indirect
216216
golang.org/x/time v0.6.0 // indirect
217-
golang.org/x/tools v0.24.0 // indirect
217+
golang.org/x/tools v0.25.0 // indirect
218218
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
219219
google.golang.org/api v0.198.0 // indirect
220-
google.golang.org/genproto/googleapis/api v0.0.0-20241007155032-5fefd90f89a9 // indirect
221-
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9 // indirect
222-
google.golang.org/grpc v1.67.1 // indirect
223-
google.golang.org/protobuf v1.35.1 // indirect
220+
google.golang.org/genproto/googleapis/api v0.0.0-20241209162323-e6fa225c2576 // indirect
221+
google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576 // indirect
222+
google.golang.org/grpc v1.68.1 // indirect
223+
google.golang.org/protobuf v1.35.2 // indirect
224224
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
225225
gopkg.in/inf.v0 v0.9.1 // indirect
226226
gopkg.in/ini.v1 v1.67.0 // indirect

0 commit comments

Comments
 (0)