clusterCreate: use tempfile with expanded env vars as viper input file
This commit is contained in:
parent
d0deee3b18
commit
7db9275a5b
@ -36,6 +36,7 @@
|
|||||||
- pkg: new `Config` interface type to support new generic `FromViper` config file parsing
|
- pkg: new `Config` interface type to support new generic `FromViper` config file parsing
|
||||||
- changed flags `--k3s-server-arg` & `--k3s-agent-arg` into `--k3s-arg` with nodefilter support (#605)
|
- changed flags `--k3s-server-arg` & `--k3s-agent-arg` into `--k3s-arg` with nodefilter support (#605)
|
||||||
- new config path `options.k3s.extraArgs`
|
- new config path `options.k3s.extraArgs`
|
||||||
|
- config file: environment variables (`$VAR`, `${VAR}` will be expanded unconditionally) (#643)
|
||||||
|
|
||||||
### Misc
|
### Misc
|
||||||
|
|
||||||
|
@ -24,7 +24,9 @@ package cluster
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -72,12 +74,31 @@ func initConfig() {
|
|||||||
|
|
||||||
// Set config file, if specified
|
// Set config file, if specified
|
||||||
if configFile != "" {
|
if configFile != "" {
|
||||||
cfgViper.SetConfigFile(configFile)
|
|
||||||
|
|
||||||
if _, err := os.Stat(configFile); err != nil {
|
if _, err := os.Stat(configFile); err != nil {
|
||||||
log.Fatalf("Failed to stat config file %s: %+v", configFile, err)
|
log.Fatalf("Failed to stat config file %s: %+v", configFile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create temporary file to expand environment variables in the config without writing that back to the original file
|
||||||
|
// we're doing it here, because this happens just before absolutely all other processing
|
||||||
|
tmpfile, err := os.CreateTemp(os.TempDir(), fmt.Sprintf("k3d-config-tmp-%s", filepath.Base(configFile)))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error creating temp copy of configfile %s for variable expansion: %v", configFile, err)
|
||||||
|
}
|
||||||
|
defer tmpfile.Close()
|
||||||
|
|
||||||
|
originalcontent, err := ioutil.ReadFile(configFile)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error reading config file %s: %v", configFile, err)
|
||||||
|
}
|
||||||
|
expandedcontent := os.ExpandEnv(string(originalcontent))
|
||||||
|
if _, err := tmpfile.WriteString(expandedcontent); err != nil {
|
||||||
|
log.Fatalf("error writing expanded config file contents to temp file %s: %v", tmpfile.Name(), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// use temp file with expanded variables
|
||||||
|
cfgViper.SetConfigFile(tmpfile.Name())
|
||||||
|
|
||||||
// try to read config into memory (viper map structure)
|
// try to read config into memory (viper map structure)
|
||||||
if err := cfgViper.ReadInConfig(); err != nil {
|
if err := cfgViper.ReadInConfig(); err != nil {
|
||||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||||
@ -96,7 +117,7 @@ func initConfig() {
|
|||||||
log.Fatalf("Schema Validation failed for config file %s: %+v", configFile, err)
|
log.Fatalf("Schema Validation failed for config file %s: %+v", configFile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Using config file %s (%s#%s)", cfgViper.ConfigFileUsed(), strings.ToLower(cfgViper.GetString("apiVersion")), strings.ToLower(cfgViper.GetString("kind")))
|
log.Infof("Using config file %s (%s#%s)", configFile, strings.ToLower(cfgViper.GetString("apiVersion")), strings.ToLower(cfgViper.GetString("kind")))
|
||||||
}
|
}
|
||||||
if log.GetLevel() >= log.DebugLevel {
|
if log.GetLevel() >= log.DebugLevel {
|
||||||
c, _ := yaml.Marshal(cfgViper.AllSettings())
|
c, _ := yaml.Marshal(cfgViper.AllSettings())
|
||||||
|
@ -8,7 +8,7 @@ kubeAPI:
|
|||||||
hostPort: "6446"
|
hostPort: "6446"
|
||||||
image: rancher/k3s:latest
|
image: rancher/k3s:latest
|
||||||
volumes:
|
volumes:
|
||||||
- volume: /my/path:/some/path
|
- volume: $HOME:/some/path
|
||||||
nodeFilters:
|
nodeFilters:
|
||||||
- all
|
- all
|
||||||
ports:
|
ports:
|
||||||
|
Loading…
Reference in New Issue
Block a user