func()

in cmd/ip-masq-agent-v2/ip-masq-agent.go [193:249]


func (m *MasqDaemon) syncConfig(fs fakefs.FileSystem) error {
	var err error
	c := EmptyMasqConfig()
	defer func() {
		if err == nil {
			json, _ := utiljson.Marshal(c)
			klog.V(2).Infof("using config: %s", string(json))
		}
	}()

	files, err := fs.ReadDir(configPath)
	if err != nil {
		return fmt.Errorf("failed to read config directory, error: %w", err)
	}

	var configAdded bool
	for _, file := range files {
		if strings.HasPrefix(file.Name(), configFilePrefix) {
			klog.V(2).Infof("syncing config file %q at %q", file.Name(), configPath)
			yaml, err := fs.ReadFile(filepath.Join(configPath, file.Name()))
			if err != nil {
				return fmt.Errorf("failed to read config file %q, error: %w", file.Name(), err)
			}

			json, err := utilyaml.ToJSON(yaml)
			if err != nil {
				return fmt.Errorf("failed to convert config file %q to JSON, error: %w", file.Name(), err)
			}

			var newConfig MasqConfig
			err = utiljson.Unmarshal(json, &newConfig)
			if err != nil {
				return fmt.Errorf("failed to unmarshal config file %q, error: %w", file.Name(), err)
			}

			err = newConfig.validate()
			if err != nil {
				return fmt.Errorf("config file %q is invalid: %w", file.Name(), err)
			}

			c.merge(&newConfig)

			configAdded = true
		}
	}

	if !configAdded {
		// no valid config files found, use defaults
		c = DefaultMasqConfig()
		klog.V(2).Infof("no valid config files found at %q, using default values", configPath)
	}

	// apply new config
	m.config = c

	return nil
}