|
1 | 1 | package config |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
4 | 6 | "os" |
5 | 7 |
|
6 | 8 | "github.com/BurntSushi/toml" |
@@ -59,8 +61,13 @@ type StaticConfig struct { |
59 | 61 | // If set to "kubeconfig", the clusters will be loaded from those in the kubeconfig. |
60 | 62 | // If set to "in-cluster", the server will use the in cluster config |
61 | 63 | 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 |
64 | 71 | } |
65 | 72 |
|
66 | 73 | func Default() *StaticConfig { |
@@ -88,8 +95,46 @@ func Read(configPath string) (*StaticConfig, error) { |
88 | 95 | // ReadToml reads the toml data and returns the StaticConfig. |
89 | 96 | func ReadToml(configData []byte) (*StaticConfig, error) { |
90 | 97 | 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 { |
92 | 104 | return nil, err |
93 | 105 | } |
| 106 | + |
94 | 107 | return config, nil |
95 | 108 | } |
| 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 | +} |
0 commit comments