-
Notifications
You must be signed in to change notification settings - Fork 459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support camelcase matchLabels and matchExpressions in TA config #3418
Open
davidhaja
wants to merge
5
commits into
open-telemetry:main
Choose a base branch
from
davidhaja:3350-ta-matchlabels
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e9ab42b
unmarshal camelcase matchLabels and matchExpressions in target alloca…
davidhaja 982d739
fix readPath comment
davidhaja 293ad3d
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
davidhaja b082525
using mapstructure for flexible unmarshalling
davidhaja dbdb7db
revert deafultconfigfilepath
davidhaja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action) | ||
component: target allocator | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: "Support camelcase matchLabels and matchExpressions in target allocator config" | ||
|
||
# One or more tracking issues related to the change | ||
issues: [3350] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,9 +21,11 @@ import ( | |
"fmt" | ||
"io/fs" | ||
"os" | ||
"reflect" | ||
"time" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/mitchellh/mapstructure" | ||
"github.com/prometheus/common/model" | ||
promconfig "github.com/prometheus/prometheus/config" | ||
_ "github.com/prometheus/prometheus/discovery/install" | ||
|
@@ -148,6 +150,58 @@ func LoadFromCLI(target *Config, flagSet *pflag.FlagSet) error { | |
return nil | ||
} | ||
|
||
func StringToModelDurationHookFunc() mapstructure.DecodeHookFunc { | ||
return func( | ||
f reflect.Type, | ||
t reflect.Type, | ||
data interface{}, | ||
) (interface{}, error) { | ||
if f.Kind() != reflect.String { | ||
return data, nil | ||
} | ||
if t != reflect.TypeOf(model.Duration(5)) { | ||
return data, nil | ||
} | ||
|
||
// Convert it by parsing | ||
return time.ParseDuration(data.(string)) | ||
} | ||
} | ||
|
||
func decodeSubConfig(t interface{}, dc mapstructure.DecoderConfig) error { | ||
dec, decError := mapstructure.NewDecoder(&dc) | ||
if decError != nil { | ||
return decError | ||
} | ||
if err := dec.Decode(t); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func flexibleUnmarshal(yamlFile []byte, cfg *Config) error { | ||
t := make(map[string]interface{}) | ||
if err := yaml.Unmarshal(yamlFile, &t); err != nil { | ||
return fmt.Errorf("error unmarshaling YAML: %w", err) | ||
} | ||
|
||
if t["collector_selector"] != nil { | ||
dc := mapstructure.DecoderConfig{TagName: "yaml", Result: cfg.CollectorSelector} | ||
if err := decodeSubConfig(t["collector_selector"], dc); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if t["prometheus_cr"] != nil { | ||
dc := mapstructure.DecoderConfig{TagName: "yaml", Result: &cfg.PrometheusCR, DecodeHook: StringToModelDurationHookFunc()} | ||
if err := decodeSubConfig(t["prometheus_cr"], dc); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func unmarshal(cfg *Config, configFile string) error { | ||
yamlFile, err := os.ReadFile(configFile) | ||
if err != nil { | ||
|
@@ -156,6 +210,11 @@ func unmarshal(cfg *Config, configFile string) error { | |
if err = yaml.Unmarshal(yamlFile, cfg); err != nil { | ||
return fmt.Errorf("error unmarshaling YAML: %w", err) | ||
} | ||
|
||
if err := flexibleUnmarshal(yamlFile, cfg); err != nil { | ||
return err | ||
} | ||
|
||
Comment on lines
210
to
+217
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add a comment here explaining why both these statements are necessary. Incidentally, what we're doing here also means that if someone were to use both versions, the camel case version wins. |
||
return nil | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
cmd/otel-allocator/config/testdata/pod_service_selector_camelcase_expressions_test.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
collector_selector: | ||
matchExpressions: | ||
- key: "app.kubernetes.io/instance" | ||
operator: "In" | ||
values: | ||
- "default.test" | ||
- key: "app.kubernetes.io/managed-by" | ||
operator: "In" | ||
values: | ||
- "opentelemetry-operator" | ||
prometheus_cr: | ||
pod_monitor_selector: | ||
matchExpressions: | ||
- key: "release" | ||
operator: "In" | ||
values: | ||
- "test" | ||
service_monitor_selector: | ||
matchExpressions: | ||
- key: "release" | ||
operator: "In" | ||
values: | ||
- "test" | ||
config: | ||
scrape_configs: | ||
- job_name: prometheus | ||
static_configs: | ||
- targets: ["prom.domain:9001", "prom.domain:9002", "prom.domain:9003"] | ||
labels: | ||
my: label |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add docstrings to these functions, as well as a longer comment explaining why they exist.