Skip to content
Open
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
7 changes: 7 additions & 0 deletions cmd/amazon-cloudwatch-agent-target-allocator/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ func LoadFromCLI(target *Config, flagSet *pflag.FlagSet) error {
return err
}

// OR the CLI flag into the YAML value so either source can enable the watcher.
prometheusCREnabled, err := getPrometheusCREnabled(flagSet)
if err != nil {
return err
}
target.PrometheusCR.Enabled = target.PrometheusCR.Enabled || prometheusCREnabled

target.HTTPS.Enabled, err = getHttpsEnabled(flagSet)
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions cmd/amazon-cloudwatch-agent-target-allocator/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func getFlagSet(errorHandling pflag.ErrorHandling) *pflag.FlagSet {
flagSet := pflag.NewFlagSet(targetAllocatorName, errorHandling)
flagSet.String(configFilePathFlagName, DefaultConfigFilePath, "The path to the config file.")
flagSet.String(kubeConfigPathFlagName, filepath.Join(homedir.HomeDir(), ".kube", "config"), "absolute path to the KubeconfigPath file")
flagSet.Bool(prometheusCREnabledFlagName, false, "Enable watching of Prometheus Operator custom resources (ServiceMonitor/PodMonitor) to dynamically generate scrape configs.")
flagSet.Bool(reloadConfigFlagName, false, "Enable automatic configuration reloading. This functionality is deprecated and will be removed in a future release.")
flagSet.Bool(httpsEnabledFlagName, true, "Enable HTTPS additional server")
flagSet.String(listenAddrHttpsFlagName, ":8443", "The address where this service serves over HTTPS.")
Expand All @@ -58,6 +59,10 @@ func getConfigReloadEnabled(flagSet *pflag.FlagSet) (bool, error) {
return flagSet.GetBool(reloadConfigFlagName)
}

func getPrometheusCREnabled(flagSet *pflag.FlagSet) (bool, error) {
return flagSet.GetBool(prometheusCREnabledFlagName)
}

func getHttpsListenAddr(flagSet *pflag.FlagSet) (string, error) {
return flagSet.GetString(listenAddrHttpsFlagName)
}
Expand Down
13 changes: 13 additions & 0 deletions cmd/amazon-cloudwatch-agent-target-allocator/config/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestGetFlagSet(t *testing.T) {
// Check if each flag exists
assert.NotNil(t, fs.Lookup(configFilePathFlagName), "Flag %s not found", configFilePathFlagName)
assert.NotNil(t, fs.Lookup(kubeConfigPathFlagName), "Flag %s not found", kubeConfigPathFlagName)
assert.NotNil(t, fs.Lookup(prometheusCREnabledFlagName), "Flag %s not found", prometheusCREnabledFlagName)
}

func TestFlagGetters(t *testing.T) {
Expand Down Expand Up @@ -45,6 +46,18 @@ func TestFlagGetters(t *testing.T) {
expectedValue: true,
getterFunc: func(fs *pflag.FlagSet) (interface{}, error) { return getConfigReloadEnabled(fs) },
},
{
name: "GetPrometheusCREnabled",
flagArgs: []string{"--" + prometheusCREnabledFlagName},
expectedValue: true,
getterFunc: func(fs *pflag.FlagSet) (interface{}, error) { return getPrometheusCREnabled(fs) },
},
{
name: "GetPrometheusCREnabledDefault",
flagArgs: []string{},
expectedValue: false,
getterFunc: func(fs *pflag.FlagSet) (interface{}, error) { return getPrometheusCREnabled(fs) },
},
{
name: "InvalidFlag",
flagArgs: []string{"--invalid-flag", "value"},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package config

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TestScrapeProtocolsDefaultedOnLoad guards against "scrape_protocols cannot be empty" regression.
func TestScrapeProtocolsDefaultedOnLoad(t *testing.T) {
got := CreateDefaultConfig()
err := LoadFromFile("./testdata/scrape_protocols_omitted_test.yaml", &got)
require.NoError(t, err)

require.NotNil(t, got.PromConfig)
require.NotEmpty(t, got.PromConfig.ScrapeConfigs)

for _, sc := range got.PromConfig.ScrapeConfigs {
assert.NotEmpty(t, sc.ScrapeProtocols,
"scrape_protocols must be defaulted for job %q", sc.JobName)
}

// Spot-check the specific static job from the reproduction by name.
found := false
for _, sc := range got.PromConfig.ScrapeConfigs {
if sc.JobName == "prometheus-sample-app" {
found = true
assert.Greater(t, len(sc.ScrapeProtocols), 0)
}
}
require.True(t, found, "expected the 'prometheus-sample-app' job to be present")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Regression fixture: a scrape job that omits scrape_protocols.
# The TA config-load path must default scrape_protocols so the agent does not
# reject the config with "scrape_protocols cannot be empty".
config:
scrape_configs:
- job_name: prometheus-sample-app
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_label_app]
regex: prometheus-sample-app
action: keep
- target_label: label1
replacement: value1
Loading