[#266] Fix namespace config initialization

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 committed by Alexey Vanin
parent 42862fd69e
commit 6c5f9b2764

View file

@ -487,7 +487,7 @@ func fetchNamespacesConfig(l *zap.Logger, v *viper.Viper) NamespacesConfig {
nsConfig, err := readNamespacesConfig(v.GetString(cfgNamespacesConfig))
if err != nil {
l.Warn(logs.FailedToParseNamespacesConfig)
l.Warn(logs.FailedToParseNamespacesConfig, zap.Error(err))
}
defaultNamespacesNames := fetchDefaultNamespaces(l, v)
@ -521,18 +521,21 @@ func fetchNamespacesConfig(l *zap.Logger, v *viper.Viper) NamespacesConfig {
}
func readNamespacesConfig(filepath string) (NamespacesConfig, error) {
nsConfig := NamespacesConfig{
Namespaces: make(Namespaces),
}
if filepath == "" {
return NamespacesConfig{}, nil
return nsConfig, nil
}
data, err := os.ReadFile(filepath)
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 {
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