Skip to content

Commit 17c0ab9

Browse files
committed
refactor(config): introduce provider-specific config registry
Signed-off-by: Calum Murray <[email protected]>
1 parent 9c04e31 commit 17c0ab9

File tree

2 files changed

+81
-14
lines changed

2 files changed

+81
-14
lines changed

pkg/config/config.go

Lines changed: 48 additions & 14 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"
@@ -61,19 +63,13 @@ type StaticConfig struct {
6163
// If set to "kubeconfig", the clusters will be loaded from those in the kubeconfig.
6264
// If set to "in-cluster", the server will use the in cluster config
6365
ClusterProviderStrategy string `toml:"cluster_provider_strategy,omitempty"`
64-
// ClusterContexts is which context should be used for each cluster
65-
ClusterContexts map[string]string `toml:"cluster_contexts"`
66-
67-
// name of the context in the kubeconfig file to look for acm access credentials in. should point to the "hub" cluster
68-
AcmContextName string `toml:"acm_context_name,omitempty"`
69-
// the host for the ACM cluster proxy addon
70-
// if using the acm-kubeconfig strategy, this should be the route for the proxy
71-
// if using the acm strategy, this should be the service for the proxy
72-
AcmClusterProxyAddonHost string `toml:"acm_cluster_proxy_addon_host,omitempty"`
73-
// whether to skip verifiying the tls certs from the cluster proxy
74-
AcmClusterProxyAddonSkipTLSVerify bool `toml:"acm_cluster_proxy_addon_skip_tls_verify"`
75-
// the CA file for the cluster proxy addon
76-
AcmClusterProxyAddonCaFile string `toml:"acm_cluster_proxy_addon_ca_file"`
66+
67+
// ClusterProvider-specific configurations
68+
// This map holds raw TOML primitives that will be parsed by registered provider parsers
69+
ClusterProviderConfigs map[string]toml.Primitive `toml:"cluster_provider_configs,omitempty"`
70+
71+
// Internal: parsed provider configs (not exposed to TOML package)
72+
parsedClusterProviderConfigs map[string]ProviderConfig
7773
}
7874

7975
func Default() *StaticConfig {
@@ -101,8 +97,46 @@ func Read(configPath string) (*StaticConfig, error) {
10197
// ReadToml reads the toml data and returns the StaticConfig.
10298
func ReadToml(configData []byte) (*StaticConfig, error) {
10399
config := Default()
104-
if err := toml.Unmarshal(configData, config); err != nil {
100+
md, err := toml.NewDecoder(bytes.NewReader(configData)).Decode(config)
101+
if err != nil {
102+
return nil, err
103+
}
104+
105+
if err := config.parseClusterProviderConfigs(md); err != nil {
105106
return nil, err
106107
}
108+
107109
return config, nil
108110
}
111+
112+
func (c *StaticConfig) GetProviderConfig(strategy string) (ProviderConfig, bool) {
113+
config, ok := c.parsedClusterProviderConfigs[strategy]
114+
115+
return config, ok
116+
}
117+
118+
func (c *StaticConfig) parseClusterProviderConfigs(md toml.MetaData) error {
119+
if c.parsedClusterProviderConfigs == nil {
120+
c.parsedClusterProviderConfigs = make(map[string]ProviderConfig, len(c.ClusterProviderConfigs))
121+
}
122+
123+
for strategy, primitive := range c.ClusterProviderConfigs {
124+
parser, ok := getProviderConfigParser(strategy)
125+
if !ok {
126+
continue
127+
}
128+
129+
providerConfig, err := parser(primitive, md)
130+
if err != nil {
131+
return fmt.Errorf("failed to parse config for ClusterProvider '%s': %w", strategy, err)
132+
}
133+
134+
if err := providerConfig.Validate(); err != nil {
135+
return fmt.Errorf("invalid config file for ClusterProvider '%s': %w", strategy, err)
136+
}
137+
138+
c.parsedClusterProviderConfigs[strategy] = providerConfig
139+
}
140+
141+
return nil
142+
}

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)