[#266] Fix namespace config initialization
All checks were successful
/ DCO (pull_request) Successful in 4m34s
/ Builds (1.20) (pull_request) Successful in 4m59s
/ Builds (1.21) (pull_request) Successful in 4m49s
/ Vulncheck (pull_request) Successful in 5m22s
/ Lint (pull_request) Successful in 8m8s
/ Tests (1.20) (pull_request) Successful in 4m37s
/ Tests (1.21) (pull_request) Successful in 5m28s

Don't use nil Namespaces map in case when file isn't provided or invalid

Signed-off-by: Denis Kirillov <d.kirillov@yadro.com>
This commit is contained in:
Denis Kirillov 2023-12-01 15:42:50 +03:00
parent 28c6bb4cb8
commit 5c0a502261

View file

@ -482,7 +482,7 @@ func fetchNamespacesConfig(l *zap.Logger, v *viper.Viper) NamespacesConfig {
nsConfig, err := readNamespacesConfig(v.GetString(cfgNamespacesConfig)) nsConfig, err := readNamespacesConfig(v.GetString(cfgNamespacesConfig))
if err != nil { if err != nil {
l.Warn(logs.FailedToParseNamespacesConfig) l.Warn(logs.FailedToParseNamespacesConfig, zap.Error(err))
} }
defaultNamespacesNames := fetchDefaultNamespaces(l, v) defaultNamespacesNames := fetchDefaultNamespaces(l, v)
@ -516,18 +516,21 @@ func fetchNamespacesConfig(l *zap.Logger, v *viper.Viper) NamespacesConfig {
} }
func readNamespacesConfig(filepath string) (NamespacesConfig, error) { func readNamespacesConfig(filepath string) (NamespacesConfig, error) {
nsConfig := NamespacesConfig{
Namespaces: make(Namespaces),
}
if filepath == "" { if filepath == "" {
return NamespacesConfig{}, nil return nsConfig, nil
} }
data, err := os.ReadFile(filepath) data, err := os.ReadFile(filepath)
if err != nil { if err != nil {
return NamespacesConfig{}, fmt.Errorf("failed to read namespace config '%s': %w", filepath, err) return nsConfig, fmt.Errorf("failed to read namespace config '%s': %w", filepath, err)
} }
var nsConfig NamespacesConfig
if err = json.Unmarshal(data, &nsConfig); err != nil { if err = json.Unmarshal(data, &nsConfig); err != nil {
return NamespacesConfig{}, fmt.Errorf("failed to parse namespace config: %w", err) return nsConfig, fmt.Errorf("failed to parse namespace config: %w", err)
} }
return nsConfig, nil return nsConfig, nil