config.go 1.12 KB
Newer Older
1
2
3
4
// config.go provides configuration loading for the checkpoint agent.
package main

import (
5
	"errors"
6
7
8
9
10
	"fmt"
	"os"

	"gopkg.in/yaml.v3"

11
	"github.com/ai-dynamo/dynamo/deploy/snapshot/pkg/types"
12
13
14
)

// ConfigMapPath is the default path where the ConfigMap is mounted.
15
const ConfigMapPath = "/etc/snapshot/config.yaml"
16

17
18
// LoadConfig loads the agent configuration from a YAML file.
func LoadConfig(path string) (*types.AgentConfig, error) {
19
20
21
22
23
	data, err := os.ReadFile(path)
	if err != nil {
		return nil, fmt.Errorf("failed to read config file %s: %w", path, err)
	}

24
	cfg := &types.AgentConfig{}
25
26
27
28
	if err := yaml.Unmarshal(data, cfg); err != nil {
		return nil, fmt.Errorf("failed to parse config file %s: %w", path, err)
	}

29
	cfg.LoadEnvOverrides()
30
31
32
	return cfg, nil
}

33
34
// LoadConfigOrDefault loads configuration from a file, falling back to defaults if the file doesn't exist.
func LoadConfigOrDefault(path string) (*types.AgentConfig, error) {
35
36
	cfg, err := LoadConfig(path)
	if err != nil {
37
38
39
		if errors.Is(err, os.ErrNotExist) {
			cfg = &types.AgentConfig{}
			cfg.LoadEnvOverrides()
40
41
42
43
44
45
			return cfg, nil
		}
		return nil, err
	}
	return cfg, nil
}