Skip to content

Commit ee21e61

Browse files
Conor-Quinjkatz
authored andcommitted
Allow for more flexibility in ConfigMap with config template file defaults.
Prior to this change, a single configuration file customization required updating every file in the ConfigMap. This change allows for individual configuration files to be updated, and defaults back to the default configuration files if the updated configuration file is not present. Author: Conor.Quin <[email protected]> @Conor-Quin Author: Jonathan S. Katz <[email protected]> @jkatz
1 parent 655cf2b commit ee21e61

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

config/pgoconfig.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -820,29 +820,53 @@ func getRootPath(clientset *kubernetes.Clientset, namespace string) (*v1.ConfigM
820820
// LoadTemplate will load a JSON template from a path
821821
func (c *PgoConfig) LoadTemplate(cMap *v1.ConfigMap, rootPath, path string) (*template.Template, error) {
822822
var value string
823+
var err error
823824

825+
// Determine if there exists a configmap entry for the template file.
824826
if cMap != nil {
827+
// Get the data that is stored in the configmap
825828
value = cMap.Data[path]
826-
if value == "" {
827-
errMsg := fmt.Sprintf("%s path not found in ConfigMap", path)
828-
return nil, errors.New(errMsg)
829-
}
830-
} else {
831-
fullPath := rootPath + path
832-
log.Debugf("loading path [%s]", fullPath)
833-
buf, err := ioutil.ReadFile(fullPath)
829+
}
830+
831+
// if the configmap does not exist, or there is no data in the configmap for
832+
// this particular configuration template, attempt to load the template from
833+
// the default configuration
834+
if cMap == nil || value == "" {
835+
value, err = c.DefaultTemplate(path)
836+
834837
if err != nil {
835-
log.Errorf("error: could not read %s", fullPath)
836-
log.Error(err)
837838
return nil, err
838839
}
839-
value = string(buf)
840840
}
841841

842+
// if we have a value for the templated file, return
842843
return template.Must(template.New(path).Parse(value)), nil
843844

844845
}
845846

847+
// DefaultTemplate attempts to load a default configuration template file
848+
func (c *PgoConfig) DefaultTemplate(path string) (string, error) {
849+
// set the lookup value for the file path based on the default configuration
850+
// path and the template file requested to be loaded
851+
fullPath := DefaultConfigsPath + path
852+
853+
log.Debugf("No entry in cmap loading default path [%s]", fullPath)
854+
855+
// read in the file from the default path
856+
buf, err := ioutil.ReadFile(fullPath)
857+
858+
if err != nil {
859+
log.Errorf("error: could not read %s", fullPath)
860+
log.Error(err)
861+
return "", err
862+
}
863+
864+
// extract the value of the default configuration file and return
865+
value := string(buf)
866+
867+
return value, nil
868+
}
869+
846870
func (c *PgoConfig) SetDefaultStorageClass(clientset *kubernetes.Clientset) error {
847871

848872
selector := LABEL_PGO_DEFAULT_SC + "=true"

0 commit comments

Comments
 (0)