Rename BaseConf.New to BaseConf.Get

Since BaseConf.New could return a cached BaseConf, rather than always
constructing a new struct, I've renamed it to Get to more accurately
reflect its use. A new New() method is called by Get and always
initializes a new struct.

Signed-off-by: Jonathon Anderson <janderson@ciq.co>
This commit is contained in:
Jonathon Anderson
2023-04-14 17:29:35 -06:00
parent f78e6b73b1
commit 70292276e2
28 changed files with 65 additions and 62 deletions

View File

@@ -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"))

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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

View File

@@ -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()

View File

@@ -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

View File

@@ -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")

View File

@@ -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)

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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")

View File

@@ -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
}

View File

@@ -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)

View File

@@ -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")

View File

@@ -16,7 +16,7 @@ nfs server.
*/
func NFS() error {
controller := warewulfconf.New()
controller := warewulfconf.Get()
if controller.Nfs.Enabled {
if controller.Warewulf.EnableHostOverlay {

View File

@@ -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)

View File

@@ -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)

View File

@@ -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/")
}

View File

@@ -28,7 +28,7 @@ var (
)
func KernelImageTopDir() string {
conf := warewulfconf.New()
conf := warewulfconf.Get()
return path.Join(conf.Paths.WWProvisiondir, "kernel")
}

View File

@@ -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")
}

View File

@@ -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")
}

View File

@@ -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)

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -61,7 +61,7 @@ func DaemonInitLogging() error {
wwlog.SetLogLevel(wwlog.SERV)
}
conf := warewulfconf.New()
conf := warewulfconf.Get()
if conf.Warewulf.Syslog {

View File

@@ -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 {

View File

@@ -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)