Skip to content

Commit b082525

Browse files
committed
using mapstructure for flexible unmarshalling
1 parent 293ad3d commit b082525

File tree

3 files changed

+45
-95
lines changed

3 files changed

+45
-95
lines changed

cmd/otel-allocator/config/config.go

+39-86
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ import (
2121
"fmt"
2222
"io/fs"
2323
"os"
24-
"strings"
24+
"reflect"
2525
"time"
2626

27-
"dario.cat/mergo"
2827
"github.com/go-logr/logr"
29-
yaml "github.com/goccy/go-yaml"
28+
"github.com/mitchellh/mapstructure"
3029
"github.com/prometheus/common/model"
3130
promconfig "github.com/prometheus/prometheus/config"
3231
_ "github.com/prometheus/prometheus/discovery/install"
3332
"github.com/spf13/pflag"
33+
"gopkg.in/yaml.v2"
3434
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3535
"k8s.io/client-go/rest"
3636
"k8s.io/client-go/tools/clientcmd"
@@ -41,7 +41,7 @@ import (
4141

4242
const (
4343
DefaultResyncTime = 5 * time.Minute
44-
DefaultConfigFilePath string = "/conf/targetallocator.yaml"
44+
DefaultConfigFilePath string = "../../conf/targetallocator.yaml"
4545
DefaultCRScrapeInterval model.Duration = model.Duration(time.Second * 30)
4646
DefaultAllocationStrategy = "consistent-hashing"
4747
DefaultFilterStrategy = "relabel-config"
@@ -77,11 +77,6 @@ type HTTPSServerConfig struct {
7777
TLSKeyFilePath string `yaml:"tls_key_file_path,omitempty"`
7878
}
7979

80-
type LabeLSelectorPaths struct {
81-
MatchLabelsPath string
82-
MatchExpressionsPath string
83-
}
84-
8580
func LoadFromFile(file string, target *Config) error {
8681
return unmarshal(target, file)
8782
}
@@ -155,92 +150,55 @@ func LoadFromCLI(target *Config, flagSet *pflag.FlagSet) error {
155150
return nil
156151
}
157152

158-
// readPath extracts value from specific YAMLPath.
159-
func readPath(p string, yamlFile []byte, f interface{}) error {
160-
path, err := yaml.PathString(p)
161-
if err != nil {
162-
return err
163-
}
164-
if err := path.Read(strings.NewReader(string(yamlFile)), f); err != nil {
165-
return err
166-
}
167-
return nil
168-
}
169-
170-
func readLabelSelectorPaths(ps LabeLSelectorPaths, yamlFile []byte, f *metav1.LabelSelector) (bool, error) {
171-
founderrs := 0
172-
173-
if err := readPath(ps.MatchLabelsPath, yamlFile, &f.MatchLabels); err != nil {
174-
if !errors.Is(err, yaml.ErrNotFoundNode) {
175-
return false, err
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
176161
}
177-
founderrs += 1
178-
}
179-
if err := readPath(ps.MatchExpressionsPath, yamlFile, &f.MatchExpressions); err != nil {
180-
if !errors.Is(err, yaml.ErrNotFoundNode) {
181-
return false, err
182-
}
183-
founderrs += 1
184-
}
185-
186-
return founderrs < 2, nil
187-
}
188-
189-
func flexibleLabelSelector(yamlFile []byte, fieldPathsMap map[*metav1.LabelSelector]LabeLSelectorPaths) error {
190-
for f, ps := range fieldPathsMap {
191-
tmpls := metav1.LabelSelector{}
192-
193-
found, err := readLabelSelectorPaths(ps, yamlFile, &tmpls)
194-
if err != nil {
195-
return err
162+
if t != reflect.TypeOf(model.Duration(5)) {
163+
return data, nil
196164
}
197165

198-
if found {
199-
if err := mergo.Merge(f, tmpls, mergo.WithOverride); err != nil {
200-
return err
201-
}
202-
}
166+
// Convert it by parsing
167+
return time.ParseDuration(data.(string))
203168
}
204-
return nil
205169
}
206170

207-
func flexibleCollectorSelector(yamlFile []byte, cfg *Config) error {
208-
collectorSelectorFieldPathsMap := map[*metav1.LabelSelector]LabeLSelectorPaths{
209-
cfg.CollectorSelector: {
210-
MatchLabelsPath: "$.collector_selector.matchlabels",
211-
MatchExpressionsPath: "$.collector_selector.matchexpressions",
212-
},
171+
func decodeSubConfig(t interface{}, dc mapstructure.DecoderConfig) error {
172+
dec, decError := mapstructure.NewDecoder(&dc)
173+
if decError != nil {
174+
return decError
213175
}
214-
215-
if err := flexibleLabelSelector(yamlFile, collectorSelectorFieldPathsMap); err != nil {
176+
if err := dec.Decode(t); err != nil {
216177
return err
217178
}
218179
return nil
219180
}
220181

221-
func flexiblePrometheusCR(yamlFile []byte, cfg *Config) error {
222-
prometheusCRFieldPathsMap := map[*metav1.LabelSelector]LabeLSelectorPaths{
223-
cfg.PrometheusCR.PodMonitorSelector: {
224-
MatchLabelsPath: "$.prometheus_cr.pod_monitor_selector.matchlabels",
225-
MatchExpressionsPath: "$.prometheus_cr.pod_monitor_selector.matchexpressions",
226-
},
227-
cfg.PrometheusCR.ServiceMonitorSelector: {
228-
MatchLabelsPath: "$.prometheus_cr.service_monitor_selector.matchlabels",
229-
MatchExpressionsPath: "$.prometheus_cr.service_monitor_selector.matchexpressions",
230-
},
231-
cfg.PrometheusCR.ServiceMonitorNamespaceSelector: {
232-
MatchLabelsPath: "$.prometheus_cr.service_monitor_namespace_selector.matchlabels",
233-
MatchExpressionsPath: "$.prometheus_cr.service_monitor_namespace_selector.matchexpressions",
234-
},
235-
cfg.PrometheusCR.PodMonitorNamespaceSelector: {
236-
MatchLabelsPath: "$.prometheus_cr.pod_monitor_namespace_selector.matchlabels",
237-
MatchExpressionsPath: "$.prometheus_cr.pod_monitor_namespace_selector.matchexpressions",
238-
},
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)
239186
}
240187

241-
if err := flexibleLabelSelector(yamlFile, prometheusCRFieldPathsMap); err != nil {
242-
return err
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+
}
243200
}
201+
244202
return nil
245203
}
246204

@@ -249,16 +207,11 @@ func unmarshal(cfg *Config, configFile string) error {
249207
if err != nil {
250208
return err
251209
}
252-
253210
if err = yaml.Unmarshal(yamlFile, cfg); err != nil {
254211
return fmt.Errorf("error unmarshaling YAML: %w", err)
255212
}
256213

257-
if err := flexibleCollectorSelector(yamlFile, cfg); err != nil {
258-
return err
259-
}
260-
261-
if err := flexiblePrometheusCR(yamlFile, cfg); err != nil {
214+
if err := flexibleUnmarshal(yamlFile, cfg); err != nil {
262215
return err
263216
}
264217

go.mod

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ require (
1414
github.com/gin-gonic/gin v1.10.0
1515
github.com/go-kit/log v0.2.1
1616
github.com/go-logr/logr v1.4.2
17-
github.com/goccy/go-yaml v1.13.4
1817
github.com/google/uuid v1.6.0
1918
github.com/json-iterator/go v1.1.12
2019
github.com/mitchellh/mapstructure v1.5.0
@@ -94,7 +93,7 @@ require (
9493
github.com/envoyproxy/protoc-gen-validate v1.1.0 // indirect
9594
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
9695
github.com/facette/natsort v0.0.0-20181210072756-2cd4dd1e2dcb // indirect
97-
github.com/fatih/color v1.18.0 // indirect
96+
github.com/fatih/color v1.16.0 // indirect
9897
github.com/felixge/httpsnoop v1.0.4 // indirect
9998
github.com/fsnotify/fsnotify v1.7.0 // indirect
10099
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
@@ -116,7 +115,7 @@ require (
116115
github.com/go-openapi/validate v0.24.0 // indirect
117116
github.com/go-playground/locales v0.14.1 // indirect
118117
github.com/go-playground/universal-translator v0.18.1 // indirect
119-
github.com/go-playground/validator/v10 v10.22.1 // indirect
118+
github.com/go-playground/validator/v10 v10.20.0 // indirect
120119
github.com/go-resty/resty/v2 v2.13.1 // indirect
121120
github.com/go-zookeeper/zk v1.0.3 // indirect
122121
github.com/goccy/go-json v0.10.2 // indirect

go.sum

+4-6
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ github.com/facette/natsort v0.0.0-20181210072756-2cd4dd1e2dcb/go.mod h1:bH6Xx7IW
165165
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
166166
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
167167
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
168-
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
169-
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
168+
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
169+
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
170170
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
171171
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
172172
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
@@ -229,8 +229,8 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o
229229
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
230230
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
231231
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
232-
github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA=
233-
github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
232+
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
233+
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
234234
github.com/go-resty/resty/v2 v2.13.1 h1:x+LHXBI2nMB1vqndymf26quycC4aggYJ7DECYbiz03g=
235235
github.com/go-resty/resty/v2 v2.13.1/go.mod h1:GznXlLxkq6Nh4sU59rPmUw3VtgpO3aS96ORAI6Q7d+0=
236236
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
@@ -240,8 +240,6 @@ github.com/go-zookeeper/zk v1.0.3 h1:7M2kwOsc//9VeeFiPtf+uSJlVpU66x9Ba5+8XK7/TDg
240240
github.com/go-zookeeper/zk v1.0.3/go.mod h1:nOB03cncLtlp4t+UAkGSV+9beXP/akpekBwL+UX1Qcw=
241241
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
242242
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
243-
github.com/goccy/go-yaml v1.13.4 h1:XOnLX9GqT+kH/gB7YzCMUiDBFU9B7pm3HZz6kyeDPkk=
244-
github.com/goccy/go-yaml v1.13.4/go.mod h1:IjYwxUiJDoqpx2RmbdjMUceGHZwYLon3sfOGl5Hi9lc=
245243
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
246244
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
247245
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=

0 commit comments

Comments
 (0)