Skip to content
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

Prevent setting SA monitor configs with invalid endpoint fields #3187

Merged
merged 1 commit into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Evaluate `--set` properties as yaml values ([#3175](https://github.com/signalfx/splunk-otel-collector/pull/3175))
- Evaluate config converter updates to `--dry-run` content ([#3176](https://github.com/signalfx/splunk-otel-collector/pull/3176))
- Support config provider uris in `--config` option values ([#3182](https://github.com/signalfx/splunk-otel-collector/pull/3182))
- `smartagent`: Don't attempt to expand observer `endpoint` fields if host and port are unsupported ([#3187](https://github.com/signalfx/splunk-otel-collector/pull/3187))

## v0.77.0

Expand Down
23 changes: 15 additions & 8 deletions pkg/receiver/smartagentreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ func (cfg *Config) Unmarshal(componentParser *confmap.Conf) error {
}

if cfg.acceptsEndpoints {
err = setHostAndPortViaEndpoint(cfg.Endpoint, monitorConfig)
if err != nil {
if err = setHostAndPortViaEndpoint(cfg.Endpoint, monitorConfig); err != nil {
return err
}
}
Expand Down Expand Up @@ -185,16 +184,24 @@ func setHostAndPortViaEndpoint(endpoint string, monitorConfig any) error {
}

if host != "" {
_, err := setStructFieldIfZeroValue(monitorConfig, "Host", host)
if err != nil {
return fmt.Errorf("unable to set monitor Host field using Endpoint-derived value of %s: %w", host, err)
// determine if a Host field exists with expected type and ignore if not.
if field, err := getSettableStructFieldValue(monitorConfig, "Host", reflect.TypeOf(host)); err == nil && field != nil {
if _, err = setStructFieldIfZeroValue(monitorConfig, "Host", host); err != nil {
return fmt.Errorf("unable to set monitor Host field using Endpoint-derived value of %s: %w", host, err)
}
}
}

if port != 0 {
_, err := setStructFieldIfZeroValue(monitorConfig, "Port", port)
if err != nil {
return fmt.Errorf("unable to set monitor Port field using Endpoint-derived value of %d: %w", port, err)
// Determine if a Port field exists with expected type and ignore if not.
// Elasticsearch monitors have port fields that are strings so attempt this value if uint16 not found.
for _, p := range []any{port, portStr} {
if field, err := getSettableStructFieldValue(monitorConfig, "Port", reflect.TypeOf(p)); err == nil && field != nil {
if _, err = setStructFieldIfZeroValue(monitorConfig, "Port", p); err != nil {
return fmt.Errorf("unable to set monitor Port field using Endpoint-derived value of %v: %w", p, err)
}
break
}
}
}

Expand Down
55 changes: 54 additions & 1 deletion pkg/receiver/smartagentreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import (
"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/cadvisor"
"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/elasticsearch/stats"
"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"
Expand Down Expand Up @@ -246,7 +248,7 @@ func TestLoadConfigWithEndpoints(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, cfg)

assert.Equal(t, 4, len(cfg.ToStringMap()))
assert.Equal(t, 6, len(cfg.ToStringMap()))

cm, err := cfg.Sub(component.NewIDWithName(typeStr, "haproxy").String())
require.NoError(t, err)
Expand Down Expand Up @@ -333,6 +335,57 @@ func TestLoadConfigWithEndpoints(t *testing.T) {
acceptsEndpoints: true,
}, etcdCfg)
require.NoError(t, etcdCfg.validate())

cm, err = cfg.Sub(component.NewIDWithName(typeStr, "elasticsearch").String())
require.NoError(t, err)
elasticCfg := CreateDefaultConfig().(*Config)
require.NoError(t, component.UnmarshalConfig(cm, elasticCfg))
tru := true
require.Equal(t, &Config{
Endpoint: "elastic:567",
monitorConfig: &stats.Config{
MonitorConfig: saconfig.MonitorConfig{
Type: "elasticsearch",
IntervalSeconds: 567,
DatapointsToExclude: []saconfig.MetricFilter{},
},
HTTPConfig: httpclient.HTTPConfig{
HTTPTimeout: timeutil.Duration(10 * time.Second),
},
Host: "elastic",
Port: "567",
EnableIndexStats: &tru,
IndexStatsIntervalSeconds: 60,
IndexStatsMasterOnly: &tru,
EnableClusterHealth: &tru,
ClusterHealthStatsMasterOnly: &tru,
ThreadPools: []string{"search", "index"},
MetadataRefreshIntervalSeconds: 30,
},
acceptsEndpoints: true,
}, elasticCfg)
require.NoError(t, elasticCfg.validate())

cm, err = cfg.Sub(component.NewIDWithName(typeStr, "kubelet-stats").String())
require.NoError(t, err)
kubeletCfg := CreateDefaultConfig().(*Config)
require.NoError(t, component.UnmarshalConfig(cm, kubeletCfg))
require.Equal(t, &Config{
Endpoint: "disregarded:678",
monitorConfig: &cadvisor.KubeletStatsConfig{
MonitorConfig: saconfig.MonitorConfig{
Type: "kubelet-stats",
IntervalSeconds: 789,
DatapointsToExclude: []saconfig.MetricFilter{},
},
KubeletAPI: kubelet.APIConfig{
AuthType: "serviceAccount",
SkipVerify: &tru,
},
},
acceptsEndpoints: true,
}, kubeletCfg)
require.NoError(t, kubeletCfg.validate())
}

func TestLoadInvalidConfigWithInvalidEndpoint(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions pkg/receiver/smartagentreceiver/testdata/endpoints_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ smartagent/etcd:
type: etcd
host: localhost
intervalSeconds: 456
smartagent/elasticsearch:
type: elasticsearch
endpoint: elastic:567
intervalSeconds: 567
smartagent/kubelet-stats:
type: kubelet-stats
endpoint: disregarded:678
intervalSeconds: 789
kubeletAPI:
authType: serviceAccount