Add ability to provide config values via mounted file

Add ability to provide config values like ZFSSA_TARGET via a mounted file.

If ZFSSA_TARGET is not provided via environment variable , the code will default to the value found in /mnt/config/config.yaml file else will be empty.
This commit is contained in:
Arun Boovaragamoorthy
2023-05-11 00:59:24 -06:00
parent 4576b6ff54
commit 5e6c907abd
2 changed files with 44 additions and 2 deletions

39
pkg/utils/parser_utils.go Normal file
View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
*/
package utils
import (
"io/ioutil"
"gopkg.in/yaml.v2"
"errors"
"fmt"
)
func GetValueFromYAML(yamlFilePath string, key string) (string, error) {
yamlData, err := ioutil.ReadFile(yamlFilePath)
if err != nil {
return "", errors.New(fmt.Sprintf("the file <%s> could not be read from: <%s>",
yamlFilePath, err))
}
// Unmarshal YAML into a map[string]interface{}
yamlMap := make(map[string]interface{})
err = yaml.Unmarshal(yamlData, &yamlMap)
if err != nil {
return "", errors.New(fmt.Sprintf("the file <%s> could not be parsed: <%s>",
yamlFilePath, err))
}
// Get value from map using key
value, ok := yamlMap[key]
if !ok {
return "", errors.New(fmt.Sprintf("key: <%s> could not be retrieved from <%s> : <%s>",
key, yamlFilePath, err))
}
// Convert value to string and return
return fmt.Sprintf("%v", value), nil
}