Skip to content

Fix config parsing for structs and pointers to structs #345

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

Merged
merged 2 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions internal/receiver/smartagentreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,7 @@ func (cfg *Config) Unmarshal(componentParser *config.Parser) error {
// which is a problem when unmarshalling custom agent monitor configs. Here we use a map of lowercase to supported
// case tag key names and update the keys where applicable.
yamlTags := yamlTagsFromStruct(monitorConfigType)
for key, val := range allSettings {
updatedKey := yamlTags[key]
if updatedKey != "" {
delete(allSettings, key)
allSettings[updatedKey] = val
}
}
recursivelyCapitalizeConfigKeys(allSettings, yamlTags)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should fix the issue reported in #322, not being able to set authType in kubelet.APIConfig.


asBytes, err := yaml.Marshal(allSettings)
if err != nil {
Expand All @@ -131,6 +125,19 @@ func (cfg *Config) Unmarshal(componentParser *config.Parser) error {
return nil
}

func recursivelyCapitalizeConfigKeys(settings map[string]interface{}, yamlTags map[string]string) {
Copy link
Contributor

@rmfitzpatrick rmfitzpatrick Apr 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a viper-centric issue I wonder if yamlTagsFromStruct and recursivelyCapitalizeConfigKeys should be in a parent function (or receiver method)* like respectYamlTagsInAllSettings()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rmfitzpatrick - I've moved these methods to a helper and exposed RespectYamlTagsInAllSettings which can be used by both the receiver and the extension. Let me know if you think it's better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do indeed!

for key, val := range settings {
updatedKey := yamlTags[key]
if updatedKey != "" {
delete(settings, key)
settings[updatedKey] = val
if m, ok := val.(map[string]interface{}); ok {
recursivelyCapitalizeConfigKeys(m, yamlTags)
}
}
}
}

func getStringSliceFromAllSettings(allSettings map[string]interface{}, key string, errToReturn error) ([]string, error) {
var items []string
if value, ok := allSettings[key]; ok {
Expand Down Expand Up @@ -165,11 +172,20 @@ func yamlTagsFromStruct(s reflect.Type) map[string]string {
}

fieldType := field.Type
if fieldType.Kind() == reflect.Struct {
switch fieldType.Kind() {
case reflect.Struct:
otherFields := yamlTagsFromStruct(fieldType)
for k, v := range otherFields {
yamlTags[k] = v
}
case reflect.Ptr:
fieldTypeElem := fieldType.Elem()
if fieldTypeElem.Kind() == reflect.Struct {
otherFields := yamlTagsFromStruct(fieldTypeElem)
for k, v := range otherFields {
yamlTags[k] = v
}
}
}
}

Expand Down
67 changes: 67 additions & 0 deletions internal/receiver/smartagentreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ import (
"time"

"github.com/signalfx/signalfx-agent/pkg/core/common/httpclient"
"github.com/signalfx/signalfx-agent/pkg/core/common/kubelet"
"github.com/signalfx/signalfx-agent/pkg/core/common/kubernetes"
saconfig "github.com/signalfx/signalfx-agent/pkg/core/config"
"github.com/signalfx/signalfx-agent/pkg/monitors/collectd/consul"
"github.com/signalfx/signalfx-agent/pkg/monitors/collectd/hadoop"
"github.com/signalfx/signalfx-agent/pkg/monitors/collectd/python"
"github.com/signalfx/signalfx-agent/pkg/monitors/collectd/redis"
"github.com/signalfx/signalfx-agent/pkg/monitors/filesystems"
"github.com/signalfx/signalfx-agent/pkg/monitors/haproxy"
"github.com/signalfx/signalfx-agent/pkg/monitors/kubernetes/volumes"
"github.com/signalfx/signalfx-agent/pkg/monitors/prometheusexporter"
"github.com/signalfx/signalfx-agent/pkg/monitors/telegraf/common/parser"
"github.com/signalfx/signalfx-agent/pkg/monitors/telegraf/monitors/exec"
"github.com/signalfx/signalfx-agent/pkg/monitors/telegraf/monitors/ntpq"
"github.com/signalfx/signalfx-agent/pkg/utils/timeutil"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -488,3 +493,65 @@ func TestInvalidFilteringConfig(t *testing.T) {
require.Error(t, err)
require.EqualError(t, err, "unexpected end of input")
}

func TestLoadConfigWithNestedMonitorConfig(t *testing.T) {
factories, err := componenttest.NopFactories()
assert.Nil(t, err)

factory := NewFactory()
factories.Receivers[config.Type(typeStr)] = factory
cfg, err := configtest.LoadConfigFile(
t, path.Join(".", "testdata", "nested_monitor_config.yaml"), factories,
)

require.NoError(t, err)
require.NotNil(t, cfg)

assert.Equal(t, len(cfg.Receivers), 2)

telegrafExecCfg := cfg.Receivers["smartagent/exec"].(*Config)
require.Equal(t, &Config{
ReceiverSettings: config.ReceiverSettings{
TypeVal: typeStr,
NameVal: typeStr + "/exec",
},
monitorConfig: &exec.Config{
MonitorConfig: saconfig.MonitorConfig{
Type: "telegraf/exec",
DatapointsToExclude: []saconfig.MetricFilter{},
},
Commands: []string{
`powershell.exe -Command "\Monitoring\Get_Directory.ps1"`,
},
TelegrafParser: &parser.Config{
DataFormat: "influx",
},
},
}, telegrafExecCfg)
require.NoError(t, telegrafExecCfg.validate())

k8sVolumesCfg := cfg.Receivers["smartagent/kubernetes_volumes"].(*Config)
tru := true
require.Equal(t, &Config{
ReceiverSettings: config.ReceiverSettings{
TypeVal: typeStr,
NameVal: typeStr + "/kubernetes_volumes",
},
monitorConfig: &volumes.Config{
MonitorConfig: saconfig.MonitorConfig{
Type: "kubernetes-volumes",
DatapointsToExclude: []saconfig.MetricFilter{},
},
KubeletAPI: kubelet.APIConfig{
URL: "https://192.168.99.103:10250",
AuthType: "serviceAccount",
SkipVerify: &tru,
},
KubernetesAPI: &kubernetes.APIConfig{
AuthType: "serviceAccount",
SkipVerify: false,
},
},
}, k8sVolumesCfg)
require.NoError(t, k8sVolumesCfg.validate())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
receivers:
smartagent/exec:
type: telegraf/exec
commands: [ 'powershell.exe -Command "\Monitoring\Get_Directory.ps1"' ]
telegrafParser:
dataFormat: "influx"
smartagent/kubernetes_volumes:
type: kubernetes-volumes
kubeletAPI:
authType: serviceAccount
url: https://192.168.99.103:10250

processors:
nop:

exporters:
nop:

service:
pipelines:
metrics:
receivers:
- smartagent/exec
- smartagent/kubernetes_volumes
processors: [nop]
exporters: [nop]