Skip to content

Commit

Permalink
added labels to the config and cli
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveromahony committed Dec 12, 2024
1 parent 426612a commit 5c0ff91
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
41 changes: 41 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -104,6 +105,7 @@ func ResolveConfig() (*Config, error) {
Common: resolveCommon(),
Watchers: resolveWatchers(),
Features: viperInstance.GetStringSlice(FeaturesKey),
Labels: resolveLabels(),
}

slog.Debug("Agent config", "config", config)
Expand Down Expand Up @@ -202,6 +204,7 @@ func registerFlags() {
"A comma-separated list of features enabled for the agent.",
)

registerCommonFlags(fs)
registerCommandFlags(fs)
registerCollectorFlags(fs)

Expand All @@ -218,6 +221,14 @@ func registerFlags() {
})
}

func registerCommonFlags(fs *flag.FlagSet) {
fs.StringToString(
LabelsRootKey,
DefaultLabels(),
"A list of labels associated with these instances",
)
}

func registerCommandFlags(fs *flag.FlagSet) {
fs.String(
CommandServerHostKey,
Expand Down Expand Up @@ -406,6 +417,36 @@ func resolveLog() *Log {
}
}

func resolveLabels() map[string]interface{} {
input := viperInstance.GetStringMapString(LabelsRootKey)
result := make(map[string]interface{})

for key, value := range input {
// Try to parse as an integer
if intValue, err := strconv.Atoi(value); err == nil {
result[key] = intValue
continue
}

// Try to parse as a float
if floatValue, err := strconv.ParseFloat(value, 64); err == nil {
result[key] = floatValue
continue
}

// Try to parse as a boolean
if boolValue, err := strconv.ParseBool(value); err == nil {
result[key] = boolValue
continue
}

// If no conversion was possible, keep it as a string
result[key] = value
}

return result
}

func resolveDataPlaneConfig() *DataPlaneConfig {
return &DataPlaneConfig{
Nginx: &NginxDataPlaneConfig{
Expand Down
1 change: 1 addition & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func TestRegisterFlags(t *testing.T) {
assert.Equal(t, "warn", viperInstance.GetString(LogLevelKey))
assert.Equal(t, "/var/log/test/agent.log", viperInstance.GetString(LogPathKey))
assert.Equal(t, 10*time.Second, viperInstance.GetDuration(ClientTimeoutKey))
assert.Equal(t, map[string]string{}, viperInstance.GetStringMapString(LabelsRootKey))

Check failure on line 110 in internal/config/config_test.go

View workflow job for this annotation

GitHub Actions / Lint

enforce-map-style: use make(map[type]type) instead of map[type]type{} (revive)
}

func TestSeekFileInPaths(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions internal/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ func DefaultAllowedDirectories() []string {
"/var/log/nginx",
}
}

func DefaultLabels() map[string]string {
return map[string]string{}

Check failure on line 96 in internal/config/defaults.go

View workflow job for this annotation

GitHub Actions / Lint

enforce-map-style: use make(map[type]type) instead of map[type]type{} (revive)
}
1 change: 1 addition & 0 deletions internal/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
ConfigPathKey = "path"
CommandRootKey = "command"
DataPlaneConfigRootKey = "data_plane_config"
LabelsRootKey = "labels"
LogLevelRootKey = "log"
CollectorRootKey = "collector"
VersionKey = "version"
Expand Down
1 change: 1 addition & 0 deletions internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type (
UUID string `yaml:"-"`
AllowedDirectories []string `yaml:"-" mapstructure:"allowed_directories"`
Features []string `yaml:"-"`
Labels map[string]any `yaml:"-" mapstructure:"labels"`
}

Log struct {
Expand Down

0 comments on commit 5c0ff91

Please sign in to comment.