Skip to content

Commit 3b51c22

Browse files
committed
refactor(config): introduce provider-specific config registry
Signed-off-by: Calum Murray <[email protected]>
1 parent 2503269 commit 3b51c22

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

pkg/config/config.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package config
22

33
import (
4+
"bytes"
5+
"fmt"
46
"os"
57

68
"github.com/BurntSushi/toml"
@@ -59,8 +61,13 @@ type StaticConfig struct {
5961
// If set to "kubeconfig", the clusters will be loaded from those in the kubeconfig.
6062
// If set to "in-cluster", the server will use the in cluster config
6163
ClusterProviderStrategy string `toml:"cluster_provider_strategy,omitempty"`
62-
// ClusterContexts is which context should be used for each cluster
63-
ClusterContexts map[string]string `toml:"cluster_contexts"`
64+
65+
// ClusterProvider-specific configurations
66+
// This map holds raw TOML primitives that will be parsed by registered provider parsers
67+
ClusterProviderConfigs map[string]toml.Primitive `toml:"cluster_provider_configs,omitempty"`
68+
69+
// Internal: parsed provider configs (not exposed to TOML package)
70+
parsedClusterProviderConfigs map[string]ProviderConfig
6471
}
6572

6673
func Default() *StaticConfig {
@@ -88,8 +95,46 @@ func Read(configPath string) (*StaticConfig, error) {
8895
// ReadToml reads the toml data and returns the StaticConfig.
8996
func ReadToml(configData []byte) (*StaticConfig, error) {
9097
config := Default()
91-
if err := toml.Unmarshal(configData, config); err != nil {
98+
md, err := toml.NewDecoder(bytes.NewReader(configData)).Decode(config)
99+
if err != nil {
100+
return nil, err
101+
}
102+
103+
if err := config.parseClusterProviderConfigs(md); err != nil {
92104
return nil, err
93105
}
106+
94107
return config, nil
95108
}
109+
110+
func (c *StaticConfig) GetProviderConfig(strategy string) (ProviderConfig, bool) {
111+
config, ok := c.parsedClusterProviderConfigs[strategy]
112+
113+
return config, ok
114+
}
115+
116+
func (c *StaticConfig) parseClusterProviderConfigs(md toml.MetaData) error {
117+
if c.parsedClusterProviderConfigs == nil {
118+
c.parsedClusterProviderConfigs = make(map[string]ProviderConfig, len(c.ClusterProviderConfigs))
119+
}
120+
121+
for strategy, primitive := range c.ClusterProviderConfigs {
122+
parser, ok := getProviderConfigParser(strategy)
123+
if !ok {
124+
continue
125+
}
126+
127+
providerConfig, err := parser(primitive, md)
128+
if err != nil {
129+
return fmt.Errorf("failed to parse config for ClusterProvider '%s': %w", strategy, err)
130+
}
131+
132+
if err := providerConfig.Validate(); err != nil {
133+
return fmt.Errorf("invalid config file for ClusterProvider '%s': %w", strategy, err)
134+
}
135+
136+
c.parsedClusterProviderConfigs[strategy] = providerConfig
137+
}
138+
139+
return nil
140+
}

pkg/config/provider_config.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/BurntSushi/toml"
7+
)
8+
9+
// ProviderConfig is the interface that all provider-specific configurations must implement.
10+
// Each provider registers a factory function to parse its config from TOML primitives
11+
type ProviderConfig interface {
12+
Validate() error
13+
}
14+
15+
type ProviderConfigParser func(primitive toml.Primitive, md toml.MetaData) (ProviderConfig, error)
16+
17+
var (
18+
providerConfigParsers = make(map[string]ProviderConfigParser)
19+
)
20+
21+
func RegisterProviderConfig(strategy string, parser ProviderConfigParser) {
22+
if _, exists := providerConfigParsers[strategy]; exists {
23+
panic(fmt.Sprintf("provider config parser already registered for strategy '%s'", strategy))
24+
}
25+
26+
providerConfigParsers[strategy] = parser
27+
}
28+
29+
func getProviderConfigParser(strategy string) (ProviderConfigParser, bool) {
30+
provider, ok := providerConfigParsers[strategy]
31+
32+
return provider, ok
33+
}

0 commit comments

Comments
 (0)