Skip to content
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
9 changes: 1 addition & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
type StaticConfig struct {
DeniedResources []GroupVersionKind `toml:"denied_resources"`

LogLevel int `toml:"log_level,omitempty"`
LogLevel int `toml:"log_level,omitzero"`
Port string `toml:"port,omitempty"`
SSEBaseURL string `toml:"sse_base_url,omitempty"`
KubeConfig string `toml:"kubeconfig,omitempty"`
Expand Down Expand Up @@ -70,13 +70,6 @@ type StaticConfig struct {
parsedClusterProviderConfigs map[string]ProviderConfig
}

func Default() *StaticConfig {
return &StaticConfig{
ListOutput: "table",
Toolsets: []string{"core", "config", "helm"},
}
}

type GroupVersionKind struct {
Group string `toml:"group"`
Version string `toml:"version"`
Expand Down
43 changes: 43 additions & 0 deletions pkg/config/config_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package config

import (
"bytes"

"github.com/BurntSushi/toml"
)

func Default() *StaticConfig {
defaultConfig := StaticConfig{
ListOutput: "table",
Toolsets: []string{"core", "config", "helm"},
}
overrides := defaultOverrides()
mergedConfig := mergeConfig(defaultConfig, overrides)
Comment on lines +14 to +15
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like this approach

return &mergedConfig
}

// HasDefaultOverrides indicates whether the internal defaultOverrides function
// provides any overrides or an empty StaticConfig.
func HasDefaultOverrides() bool {
overrides := defaultOverrides()
var buf bytes.Buffer
if err := toml.NewEncoder(&buf).Encode(overrides); err != nil {
// If marshaling fails, assume no overrides
return false
}
return len(bytes.TrimSpace(buf.Bytes())) > 0
}

// mergeConfig applies non-zero values from override to base using TOML serialization
// and returns the merged StaticConfig.
// In case of any error during marshalling or unmarshalling, it returns the base config unchanged.
func mergeConfig(base, override StaticConfig) StaticConfig {
var overrideBuffer bytes.Buffer
if err := toml.NewEncoder(&overrideBuffer).Encode(override); err != nil {
// If marshaling fails, return base unchanged
return base
}

_, _ = toml.NewDecoder(&overrideBuffer).Decode(&base)
return base
}
8 changes: 8 additions & 0 deletions pkg/config/config_default_overrides.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package config

func defaultOverrides() StaticConfig {
return StaticConfig{
// IMPORTANT: this file is used to override default config values in downstream builds.
// This is intentionally left blank.
}
Comment on lines +4 to +7
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is great @manusa !

}
43 changes: 43 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,49 @@ func (s *ConfigSuite) TestReadConfigValidPreservesDefaultsForMissingFields() {
})
}

func (s *ConfigSuite) TestMergeConfig() {
base := StaticConfig{
ListOutput: "table",
Toolsets: []string{"core", "config", "helm"},
Port: "8080",
}
s.Run("merges override values on top of base", func() {
override := StaticConfig{
ListOutput: "json",
Port: "9090",
}

result := mergeConfig(base, override)

s.Equal("json", result.ListOutput, "ListOutput should be overridden")
s.Equal("9090", result.Port, "Port should be overridden")
})

s.Run("preserves base values when override is empty", func() {
override := StaticConfig{}

result := mergeConfig(base, override)

s.Equal("table", result.ListOutput, "ListOutput should be preserved from base")
s.Equal([]string{"core", "config", "helm"}, result.Toolsets, "Toolsets should be preserved from base")
s.Equal("8080", result.Port, "Port should be preserved from base")
})

s.Run("handles partial overrides", func() {
override := StaticConfig{
Toolsets: []string{"custom"},
ReadOnly: true,
}

result := mergeConfig(base, override)

s.Equal("table", result.ListOutput, "ListOutput should be preserved from base")
s.Equal([]string{"custom"}, result.Toolsets, "Toolsets should be overridden")
s.Equal("8080", result.Port, "Port should be preserved from base since override doesn't specify it")
s.True(result.ReadOnly, "ReadOnly should be overridden to true")
})
}

func TestConfig(t *testing.T) {
suite.Run(t, new(ConfigSuite))
}
12 changes: 12 additions & 0 deletions pkg/mcp/toolsets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func (s *ToolsetsSuite) TestNoToolsets() {
}

func (s *ToolsetsSuite) TestDefaultToolsetsTools() {
if configuration.HasDefaultOverrides() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess this is a good hook, for downstream.

s.T().Skip("Skipping test because default configuration overrides are present (this is a downstream fork)")
}
s.Run("Default configuration toolsets", func() {
s.InitMcpClient()
tools, err := s.ListTools(s.T().Context(), mcp.ListToolsRequest{})
Expand All @@ -82,6 +85,9 @@ func (s *ToolsetsSuite) TestDefaultToolsetsTools() {
}

func (s *ToolsetsSuite) TestDefaultToolsetsToolsInOpenShift() {
if configuration.HasDefaultOverrides() {
s.T().Skip("Skipping test because default configuration overrides are present (this is a downstream fork)")
}
s.Run("Default configuration toolsets in OpenShift", func() {
s.Handle(&test.InOpenShiftHandler{})
s.InitMcpClient()
Expand All @@ -100,6 +106,9 @@ func (s *ToolsetsSuite) TestDefaultToolsetsToolsInOpenShift() {
}

func (s *ToolsetsSuite) TestDefaultToolsetsToolsInMultiCluster() {
if configuration.HasDefaultOverrides() {
s.T().Skip("Skipping test because default configuration overrides are present (this is a downstream fork)")
}
s.Run("Default configuration toolsets in multi-cluster (with 11 clusters)", func() {
kubeconfig := s.Kubeconfig()
for i := 0; i < 10; i++ {
Expand All @@ -123,6 +132,9 @@ func (s *ToolsetsSuite) TestDefaultToolsetsToolsInMultiCluster() {
}

func (s *ToolsetsSuite) TestDefaultToolsetsToolsInMultiClusterEnum() {
if configuration.HasDefaultOverrides() {
s.T().Skip("Skipping test because default configuration overrides are present (this is a downstream fork)")
}
s.Run("Default configuration toolsets in multi-cluster (with 2 clusters)", func() {
kubeconfig := s.Kubeconfig()
// Add additional cluster to force multi-cluster behavior with enum parameter
Expand Down
Loading