-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathconfig.go
163 lines (141 loc) · 5.5 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// Package config handles parsing and applying options from finch's config
// file. These options can be applied to any aspect of the project, from the VMM
// to components running inside the VM.
//
// Currently, VMM options are applied to one of Lima's configuration files and options
// within the VM are applied via running SSH commands and writing files via SFTP.
package config
import (
"errors"
"fmt"
"path/filepath"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/spf13/afero"
"gopkg.in/yaml.v3"
"github.com/runfinch/finch/pkg/command"
"github.com/runfinch/finch/pkg/flog"
"github.com/runfinch/finch/pkg/fmemory"
"github.com/runfinch/finch/pkg/system"
)
// AdditionalDirectory represents the additional directory used in Finch config.
type AdditionalDirectory struct {
Path *string `yaml:"path"`
}
// SharedSystemSettings represents all settings shared by virtualized Finch configurations.
type SharedSystemSettings struct {
VMType *limayaml.VMType `yaml:"vmType,omitempty"`
}
// SharedSettings represents settings shared by all Finch configurations.
type SharedSettings struct {
Snapshotters []string `yaml:"snapshotters,omitempty"`
CredsHelpers []string `yaml:"creds_helpers,omitempty"`
DockerCompat bool `yaml:"dockercompat,omitempty"`
}
// Nerdctl is a copy from github.com/containerd/nerdctl/cmd/nerdctl/main.go
// TODO: this should be importable on macOS once nerdctl v2 is released.
type Nerdctl struct {
Debug bool `toml:"debug,omitempty"`
DebugFull bool `toml:"debug_full1,omitempty"`
Address string `toml:"address,omitempty"`
Namespace string `toml:"namespace,omitempty"`
Snapshotter string `toml:"snapshotter,omitempty"`
CNIPath string `toml:"cni_path,omitempty"`
CNINetConfPath string `toml:"cni_netconfpath,omitempty"`
DataRoot string `toml:"data_root,omitempty"`
CgroupManager string `toml:"cgroup_manager,omitempty"`
InsecureRegistry bool `toml:"insecure_registry,omitempty"`
HostsDir []string `toml:"hosts_dir,omitempty"`
}
// VMConfigOpts represents the Options for finch vm settings command.
type VMConfigOpts struct {
CPUs int
Memory string
}
// Default values for the command line arguments --cpus and --memory.
const (
DefaultCPUs = 0
DefaultMemory = ""
)
// LimaConfigApplier applies lima configuration changes.
//
//go:generate mockgen -copyright_file=../../copyright_header -destination=../mocks/pkg_config_lima_config_applier.go -package=mocks -mock_names LimaConfigApplier=LimaConfigApplier . LimaConfigApplier
type LimaConfigApplier interface {
ConfigureOverrideLimaYaml() error
ConfigureDefaultLimaYaml() error
GetFinchConfigPath() string
}
// NerdctlConfigApplier applies nerdctl configuration changes.
//
//go:generate mockgen -copyright_file=../../copyright_header -destination=../mocks/pkg_config_nerdctl_config_applier.go -package=mocks -mock_names NerdctlConfigApplier=NerdctlConfigApplier . NerdctlConfigApplier
type NerdctlConfigApplier interface {
Apply(remoteAddr string) error
}
// LoadSystemDeps contains the system dependencies for Load.
//
//go:generate mockgen -copyright_file=../../copyright_header -destination=../mocks/pkg_config_load_system_deps.go -package=mocks -mock_names LoadSystemDeps=LoadSystemDeps . LoadSystemDeps
type LoadSystemDeps interface {
system.RuntimeCPUGetter
}
// writeConfig writes a config struct back to a YAML file at a path.
func writeConfig(cfg *Finch, fs afero.Fs, path string) error {
cfgBuf, err := yaml.Marshal(cfg)
if err != nil {
return fmt.Errorf("failed to write to marshal config: %w", err)
}
if err := afero.WriteFile(fs, path, cfgBuf, 0o600); err != nil {
return fmt.Errorf("failed to write to config file: %w", err)
}
return nil
}
func ensureConfigDir(fs afero.Fs, path string, log flog.Logger) error {
dirExists, err := afero.DirExists(fs, path)
if err != nil {
return fmt.Errorf("failed to get status of config directory: %w", err)
}
if !dirExists {
log.Infof("%q directory doesn't exist, attempting to create it", path)
if err := fs.Mkdir(path, 0o700); err != nil {
return fmt.Errorf("failed to create config directory: %w", err)
}
}
return nil
}
// Load loads Finch's configuration from a YAML file and initializes default values.
func Load(
fs afero.Fs,
cfgPath string,
log flog.Logger,
systemDeps LoadSystemDeps,
mem fmemory.Memory,
ecc command.Creator,
) (*Finch, error) {
b, err := afero.ReadFile(fs, cfgPath)
if err != nil {
if errors.Is(err, afero.ErrFileNotFound) {
log.Infof("Using default values due to missing config file at %q", cfgPath)
defCfg := applyDefaults(&Finch{}, systemDeps, mem, ecc)
if err := ensureConfigDir(fs, filepath.Dir(cfgPath), log); err != nil {
return nil, fmt.Errorf("failed to ensure %q directory: %w", cfgPath, err)
}
if err := writeConfig(defCfg, fs, cfgPath); err != nil {
log.Warnf("Could not save default values to %q: %w", cfgPath, err)
}
return defCfg, nil
}
return nil, fmt.Errorf("failed to read the config file: %w", err)
}
var cfg Finch
if err := yaml.Unmarshal(b, &cfg); err != nil {
return nil, fmt.Errorf("failed to unmarshal config file: %w", err)
}
defCfg := applyDefaults(&cfg, systemDeps, mem, ecc)
if err := writeConfig(defCfg, fs, cfgPath); err != nil {
return nil, err
}
if err := validate(defCfg, log, systemDeps, mem); err != nil {
return nil, fmt.Errorf("failed to validate config file: %w", err)
}
return defCfg, nil
}