diff --git a/internal/app/api/wwapic/wwapic.go b/internal/app/api/wwapic/wwapic.go index fc34659c..8eca0814 100644 --- a/internal/app/api/wwapic/wwapic.go +++ b/internal/app/api/wwapic/wwapic.go @@ -24,7 +24,7 @@ import ( func main() { log.Println("Client running") - conf := warewulfconf.New() + conf := warewulfconf.Get() // Read the config file. config, err := apiconfig.NewClient(path.Join(conf.Paths.Sysconfdir, "warewulf/wwapic.conf")) diff --git a/internal/app/api/wwapid/wwapid.go b/internal/app/api/wwapid/wwapid.go index 33de9db6..5042b8c7 100644 --- a/internal/app/api/wwapid/wwapid.go +++ b/internal/app/api/wwapid/wwapid.go @@ -16,7 +16,7 @@ import ( apinode "github.com/hpcng/warewulf/internal/pkg/api/node" "github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1" "github.com/hpcng/warewulf/internal/pkg/version" - "github.com/hpcng/warewulf/internal/pkg/config" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/credentials" @@ -34,7 +34,7 @@ var apiVersion string func main() { log.Println("Server running") - conf := config.New() + conf := warewulfconf.Get() // Read the config file. config, err := apiconfig.NewServer(path.Join(conf.Paths.Sysconfdir, "warewulf/wwapid.conf")) if err != nil { diff --git a/internal/app/api/wwapird/wwapird.go b/internal/app/api/wwapird/wwapird.go index c71f56e8..354b82dd 100644 --- a/internal/app/api/wwapird/wwapird.go +++ b/internal/app/api/wwapird/wwapird.go @@ -29,7 +29,7 @@ func run() error { log.Println("test0") - conf := warewulfconf.New() + conf := warewulfconf.Get() // Read the config file. config, err := apiconfig.NewClientServer(path.Join(conf.Paths.Sysconfdir, "warewulf/wwapird.conf")) if err != nil { diff --git a/internal/app/wwclient/root.go b/internal/app/wwclient/root.go index 70167b2f..5af79840 100644 --- a/internal/app/wwclient/root.go +++ b/internal/app/wwclient/root.go @@ -49,7 +49,7 @@ func GetRootCommand() *cobra.Command { } func CobraRunE(cmd *cobra.Command, args []string) error { - conf := warewulfconf.New() + conf := warewulfconf.Get() pid, err := pidfile.Write(PIDFile) if err != nil && pid == -1 { diff --git a/internal/app/wwctl/container/exec/child/main.go b/internal/app/wwctl/container/exec/child/main.go index 3693e03f..ee2b97b9 100644 --- a/internal/app/wwctl/container/exec/child/main.go +++ b/internal/app/wwctl/container/exec/child/main.go @@ -33,7 +33,7 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) { wwlog.Error("Unknown Warewulf container: %s", containerName) os.Exit(1) } - conf := warewulfconf.New() + conf := warewulfconf.Get() mountPts := conf.MountsContainer mountPts = append(container.InitMountPnts(binds), mountPts...) // check for valid mount points diff --git a/internal/app/wwctl/container/list/main_test.go b/internal/app/wwctl/container/list/main_test.go index 160a48dc..2d5afc3a 100644 --- a/internal/app/wwctl/container/list/main_test.go +++ b/internal/app/wwctl/container/list/main_test.go @@ -55,7 +55,7 @@ nodes: WW_INTERNAL: 0 ` - conf := warewulfconf.New() + conf := warewulfconf.Get() err := conf.Read([]byte(conf_yml)) assert.NoError(t, err) warewulfd.SetNoDaemon() diff --git a/internal/app/wwctl/genconf/warewulfconf/print/main.go b/internal/app/wwctl/genconf/warewulfconf/print/main.go index 26cab821..e18ea611 100644 --- a/internal/app/wwctl/genconf/warewulfconf/print/main.go +++ b/internal/app/wwctl/genconf/warewulfconf/print/main.go @@ -9,7 +9,7 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) (err error) { - conf := warewulfconf.New() + conf := warewulfconf.Get() buffer, err := yaml.Marshal(&conf) if err != nil { return diff --git a/internal/app/wwctl/node/status/main.go b/internal/app/wwctl/node/status/main.go index 1d0dc6f9..d1675cb9 100644 --- a/internal/app/wwctl/node/status/main.go +++ b/internal/app/wwctl/node/status/main.go @@ -18,7 +18,7 @@ import ( func CobraRunE(cmd *cobra.Command, args []string) (err error) { - controller := warewulfconf.New() + controller := warewulfconf.Get() if controller.Ipaddr == "" { return fmt.Errorf("warewulf Server IP Address is not properly configured") diff --git a/internal/app/wwctl/overlay/build/main.go b/internal/app/wwctl/overlay/build/main.go index bb61b131..75c7cf82 100644 --- a/internal/app/wwctl/overlay/build/main.go +++ b/internal/app/wwctl/overlay/build/main.go @@ -14,7 +14,7 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) error { - controller := warewulfconf.New() + controller := warewulfconf.Get() nodeDB, err := node.New() if err != nil { wwlog.Error("Could not open node configuration: %s", err) diff --git a/internal/app/wwctl/root.go b/internal/app/wwctl/root.go index 2717c72e..c9d56480 100644 --- a/internal/app/wwctl/root.go +++ b/internal/app/wwctl/root.go @@ -76,7 +76,7 @@ func rootPersistentPreRunE(cmd *cobra.Command, args []string) (err error) { if LogLevel != wwlog.INFO { wwlog.SetLogLevel(LogLevel) } - conf := warewulfconf.New() + conf := warewulfconf.Get() if !AllowEmptyConf && !conf.Initialized() { if WarewulfConfArg != "" { err = conf.ReadConf(WarewulfConfArg) diff --git a/internal/app/wwctl/server/start/main.go b/internal/app/wwctl/server/start/main.go index 076e1695..01c9a19f 100644 --- a/internal/app/wwctl/server/start/main.go +++ b/internal/app/wwctl/server/start/main.go @@ -10,7 +10,7 @@ import ( func CobraRunE(cmd *cobra.Command, args []string) error { if SetForeground { - conf := warewulfconf.New() + conf := warewulfconf.Get() conf.Warewulf.Syslog = false return errors.Wrap(warewulfd.RunServer(), "failed to start Warewulf server") } else { diff --git a/internal/pkg/api/node/node.go b/internal/pkg/api/node/node.go index 8c5581ab..13a10c15 100644 --- a/internal/pkg/api/node/node.go +++ b/internal/pkg/api/node/node.go @@ -317,7 +317,7 @@ func NodeStatus(nodeNames []string) (nodeStatusResponse *wwapiv1.NodeStatusRespo Nodes map[string]*nodeStatusInternal `json:"nodes"` } - controller := warewulfconf.New() + controller := warewulfconf.Get() if controller.Ipaddr == "" { err = fmt.Errorf("the Warewulf Server IP Address is not properly configured") diff --git a/internal/pkg/config/datastructure.go b/internal/pkg/config/datastructure.go index dedc762f..92ada6ee 100644 --- a/internal/pkg/config/datastructure.go +++ b/internal/pkg/config/datastructure.go @@ -63,6 +63,6 @@ func (s *NfsConf) Unmarshal(unmarshal func(interface{}) error) error { // Waste processor cycles to make code more readable func DataStore() string { - _ = New() + _ = Get() return cachedConf.Warewulf.DataStore } diff --git a/internal/pkg/config/root.go b/internal/pkg/config/root.go index d56dbe27..e654f291 100644 --- a/internal/pkg/config/root.go +++ b/internal/pkg/config/root.go @@ -11,6 +11,7 @@ import ( "fmt" "net" "os" + "reflect" "github.com/pkg/errors" @@ -44,35 +45,37 @@ type RootConf struct { Nfs *NfsConf `yaml:"nfs"` MountsContainer []*MountEntry `yaml:"container mounts" default:"[{\"source\": \"/etc/resolv.conf\", \"dest\": \"/etc/resolv.conf\"}]"` Paths *BuildConfig `yaml:"paths"` - current bool readConf bool } -// New returns a [RootConf] which may have been cached from a previous -// call. +// New returns a [RootConf] initialized with empty values. func New() (conf RootConf) { - // NOTE: This function can be called before any log level is set - // so using wwlog.Verbose or wwlog.Debug won't work - if !cachedConf.current { - conf.Warewulf = new(WarewulfConf) - conf.Dhcp = new(DhcpConf) - conf.Tftp = new(TftpConf) - conf.Nfs = new(NfsConf) - conf.Paths = new(BuildConfig) - _ = defaults.Set(&conf) - cachedConf = conf - cachedConf.readConf = false - cachedConf.current = true - } else { - // If cached struct isn't empty, use it as the return value - conf = cachedConf - } - return conf + conf.Warewulf = new(WarewulfConf) + conf.Dhcp = new(DhcpConf) + conf.Tftp = new(TftpConf) + conf.Nfs = new(NfsConf) + conf.Paths = new(BuildConfig) + _ = defaults.Set(&conf) + return } -// ReadConf populates the configuration with the values from a -// configuration file. + +// Get returns a [RootConf] which may have been cached from a previous +// call. +func Get() (RootConf) { + // NOTE: This function can be called before any log level is set + // so using wwlog.Verbose or wwlog.Debug won't work + if reflect.ValueOf(cachedConf).IsZero() { + cachedConf = New() + cachedConf.readConf = false + } + return cachedConf +} + + +// ReadConf populates [RootConf] with the values from a configuration +// file. func (conf *RootConf) ReadConf(confFileName string) (err error) { wwlog.Debug("Reading warewulf.conf from: %s", confFileName) fileHandle, err := os.ReadFile(confFileName) @@ -82,8 +85,8 @@ func (conf *RootConf) ReadConf(confFileName string) (err error) { return conf.Read(fileHandle) } -// Read populates the configuration with the values from a yaml -// document. + +// Read populates [RootConf] with the values from a yaml document. func (conf *RootConf) Read(data []byte) (err error) { // ipxe binaries are merged not overwritten, store defaults separate defIpxe := make(map[string]string) @@ -103,13 +106,13 @@ func (conf *RootConf) Read(data []byte) (err error) { conf.Tftp.IpxeBinaries = defIpxe } cachedConf = *conf - cachedConf.current = true cachedConf.readConf = true return } -// SetDynamicDefaults populates the configuration with plausible -// defaults for the runtime environment. + +// SetDynamicDefaults populates [RootConf] with plausible defaults for +// the runtime environment. func (conf *RootConf) SetDynamicDefaults() (err error) { if conf.Ipaddr == "" || conf.Netmask == "" || conf.Network == "" { var mask net.IPMask @@ -172,18 +175,18 @@ func (conf *RootConf) SetDynamicDefaults() (err error) { } } cachedConf = *conf - cachedConf.current = true return } -// Initialized returns true if the configuration in memory was read -// from disk, or false otherwise. + +// Initialized returns true if [RootConf] memory was read from disk, +// or false otherwise. func (conf *RootConf) Initialized() bool { return conf.readConf } -// Persist writes the configuration to a file as a yaml document. +// Persist writes [RootConf] to a file as a yaml document. func (controller *RootConf) Persist() error { out, err := yaml.Marshal(controller) diff --git a/internal/pkg/configure/dhcp.go b/internal/pkg/configure/dhcp.go index c5b1c207..d7ef2e69 100644 --- a/internal/pkg/configure/dhcp.go +++ b/internal/pkg/configure/dhcp.go @@ -17,7 +17,7 @@ dhcp configuration is checked. */ func Dhcp() (err error) { - controller := warewulfconf.New() + controller := warewulfconf.Get() if !controller.Dhcp.Enabled { wwlog.Info("This system is not configured as a Warewulf DHCP controller") diff --git a/internal/pkg/configure/nfs.go b/internal/pkg/configure/nfs.go index 14f13e36..343d59c8 100644 --- a/internal/pkg/configure/nfs.go +++ b/internal/pkg/configure/nfs.go @@ -16,7 +16,7 @@ nfs server. */ func NFS() error { - controller := warewulfconf.New() + controller := warewulfconf.Get() if controller.Nfs.Enabled { if controller.Warewulf.EnableHostOverlay { diff --git a/internal/pkg/configure/ssh.go b/internal/pkg/configure/ssh.go index 48d3818a..1c24a3b2 100644 --- a/internal/pkg/configure/ssh.go +++ b/internal/pkg/configure/ssh.go @@ -14,7 +14,7 @@ import ( func SSH() error { if os.Getuid() == 0 { fmt.Printf("Updating system keys\n") - conf := warewulfconf.New() + conf := warewulfconf.Get() wwkeydir := path.Join(conf.Paths.Sysconfdir, "warewulf/keys") + "/" err := os.MkdirAll(path.Join(conf.Paths.Sysconfdir, "warewulf/keys"), 0755) diff --git a/internal/pkg/configure/tftp.go b/internal/pkg/configure/tftp.go index 4988ddfc..16a28bbb 100644 --- a/internal/pkg/configure/tftp.go +++ b/internal/pkg/configure/tftp.go @@ -11,7 +11,7 @@ import ( ) func TFTP() error { - controller := warewulfconf.New() + controller := warewulfconf.Get() var tftpdir string = path.Join(controller.Paths.Tftpdir, "warewulf") err := os.MkdirAll(tftpdir, 0755) diff --git a/internal/pkg/container/config.go b/internal/pkg/container/config.go index f0babc66..f166e643 100644 --- a/internal/pkg/container/config.go +++ b/internal/pkg/container/config.go @@ -7,7 +7,7 @@ import ( ) func SourceParentDir() string { - conf := warewulfconf.New() + conf := warewulfconf.Get() return conf.Paths.WWChrootdir } @@ -20,7 +20,7 @@ func RootFsDir(name string) string { } func ImageParentDir() string { - conf := warewulfconf.New() + conf := warewulfconf.Get() return path.Join(conf.Paths.WWProvisiondir, "container/") } diff --git a/internal/pkg/kernel/kernel.go b/internal/pkg/kernel/kernel.go index 4611eb3a..a735f384 100644 --- a/internal/pkg/kernel/kernel.go +++ b/internal/pkg/kernel/kernel.go @@ -28,7 +28,7 @@ var ( ) func KernelImageTopDir() string { - conf := warewulfconf.New() + conf := warewulfconf.Get() return path.Join(conf.Paths.WWProvisiondir, "kernel") } diff --git a/internal/pkg/node/constructors.go b/internal/pkg/node/constructors.go index 86c19b49..c77ba038 100644 --- a/internal/pkg/node/constructors.go +++ b/internal/pkg/node/constructors.go @@ -40,7 +40,7 @@ defaultnode: netmask: 255.255.255.0` func init() { - conf := warewulfconf.New() + conf := warewulfconf.Get() if ConfigFile == "" { ConfigFile = path.Join(conf.Paths.Sysconfdir, "warewulf/nodes.conf") } diff --git a/internal/pkg/overlay/config.go b/internal/pkg/overlay/config.go index 59f268fc..d2a9e924 100644 --- a/internal/pkg/overlay/config.go +++ b/internal/pkg/overlay/config.go @@ -9,7 +9,7 @@ import ( ) func OverlaySourceTopDir() string { - conf := warewulfconf.New() + conf := warewulfconf.Get() return conf.Paths.WWOverlaydir } @@ -31,6 +31,6 @@ func OverlaySourceDir(overlayName string) string { Returns the overlay name of the image for a given node */ func OverlayImage(nodeName string, overlayName []string) string { - conf := warewulfconf.New() + conf := warewulfconf.Get() return path.Join(conf.Paths.WWProvisiondir, "overlays/", nodeName, strings.Join(overlayName, "-")+".img") } diff --git a/internal/pkg/overlay/datastructure.go b/internal/pkg/overlay/datastructure.go index 2cd50b01..760d3f42 100644 --- a/internal/pkg/overlay/datastructure.go +++ b/internal/pkg/overlay/datastructure.go @@ -44,7 +44,7 @@ Initialize an TemplateStruct with the given node.NodeInfo */ func InitStruct(nodeInfo node.NodeInfo) TemplateStruct { var tstruct TemplateStruct - controller := warewulfconf.New() + controller := warewulfconf.Get() nodeDB, err := node.New() if err != nil { wwlog.Error("%s", err) diff --git a/internal/pkg/overlay/funcmap.go b/internal/pkg/overlay/funcmap.go index 793f8d9c..4acf6705 100644 --- a/internal/pkg/overlay/funcmap.go +++ b/internal/pkg/overlay/funcmap.go @@ -17,7 +17,7 @@ Reads a file file from the host fs. If the file has nor '/' prefix the path is relative to Paths.SysconfdirTemplates in the file are no evaluated. */ func templateFileInclude(inc string) string { - conf := warewulfconf.New() + conf := warewulfconf.Get() if !strings.HasPrefix(inc, "/") { inc = path.Join(conf.Paths.Sysconfdir, "warewulf", inc) } @@ -35,7 +35,7 @@ is the file to read, the second the abort string Templates in the file are no evaluated. */ func templateFileBlock(inc string, abortStr string) (string, error) { - conf := warewulfconf.New() + conf := warewulfconf.Get() if !strings.HasPrefix(inc, "/") { inc = path.Join(conf.Paths.Sysconfdir, "warewulf", inc) } diff --git a/internal/pkg/version/version.go b/internal/pkg/version/version.go index 0632e2e6..d4b5fb85 100644 --- a/internal/pkg/version/version.go +++ b/internal/pkg/version/version.go @@ -11,7 +11,7 @@ import ( Return the version of wwctl */ func GetVersion() string { - conf := warewulfconf.New() + conf := warewulfconf.Get() return fmt.Sprintf("%s-%s", conf.Paths.Version, conf.Paths.Release) } diff --git a/internal/pkg/warewulfd/daemon.go b/internal/pkg/warewulfd/daemon.go index 5d3610c2..bfa16f9d 100644 --- a/internal/pkg/warewulfd/daemon.go +++ b/internal/pkg/warewulfd/daemon.go @@ -61,7 +61,7 @@ func DaemonInitLogging() error { wwlog.SetLogLevel(wwlog.SERV) } - conf := warewulfconf.New() + conf := warewulfconf.Get() if conf.Warewulf.Syslog { diff --git a/internal/pkg/warewulfd/provision.go b/internal/pkg/warewulfd/provision.go index 230f7eb3..60457208 100644 --- a/internal/pkg/warewulfd/provision.go +++ b/internal/pkg/warewulfd/provision.go @@ -30,7 +30,7 @@ type iPxeTemplate struct { } func ProvisionSend(w http.ResponseWriter, req *http.Request) { - conf := warewulfconf.New() + conf := warewulfconf.Get() rinfo, err := parseReq(req) if err != nil { diff --git a/internal/pkg/warewulfd/warewulfd.go b/internal/pkg/warewulfd/warewulfd.go index 43c24965..796054b0 100644 --- a/internal/pkg/warewulfd/warewulfd.go +++ b/internal/pkg/warewulfd/warewulfd.go @@ -58,7 +58,7 @@ func RunServer() error { http.HandleFunc("/overlay-runtime/", ProvisionSend) http.HandleFunc("/status", StatusSend) - conf := warewulfconf.New() + conf := warewulfconf.Get() daemonPort := conf.Warewulf.Port wwlog.Serv("Starting HTTPD REST service on port %d", daemonPort)