forked from runfinch/finch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
224 lines (193 loc) · 8.35 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// 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/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"`
}
// Finch represents the configuration file for Finch CLI.
type Finch struct {
CPUs *int `yaml:"cpus,omitempty"`
Memory *string `yaml:"memory,omitempty"`
// Snapshotters: the snapshotters that will be installed and configured automatically on vm init or on vm start.
// Values: `soci` for SOCI snapshotter; `overlayfs` for default overlay snapshotter.
Snapshotters []string `yaml:"snapshotters,omitempty"`
// CredsHelper: the list of credential helpers that will be installed and configured automatically on vm init or on vm start
CredsHelpers []string `yaml:"creds_helpers,omitempty"`
// AdditionalDirectories are the work directories that are not supported by default. In macOS, only home directory is supported by default.
// For example, if you want to mount a directory into a container, and that directory is not under your home directory,
// then you'll need to specify this field to add that directory or any ascendant of it as a work directory.
AdditionalDirectories []AdditionalDirectory `yaml:"additional_directories,omitempty"`
// VMType sets which technology to use for Finch's VM.
// Currently supports `qemu` and `vz` (Virtualization.framework).
// Also sets mountType to "virtiofs", instead of the default "reverse-sshfs"
// Requires macOS 13.0 or later.
// This setting will only be applied on vm init.
VMType *limayaml.VMType `yaml:"vmType,omitempty"`
// Use Rosetta 2 when available. Forces vmType to "vz" (Virtualization.framework) if set to `true`.
// Requires macOS 13.0 or later and an Apple Silicon (ARM64) mac.
// Has no effect on systems where Rosetta 2 is not available (Intel/AMD64 macs, or macOS < 13.0).
// This setting will only be applied on vm init.
Rosetta *bool `yaml:"rosetta,omitempty"`
}
// Nerdctl is a copy from github.com/containerd/nerdctl/cmd/nerdctl/main.go
// TODO: make PR to nerdctl repo to move this config out of the main package
// so it can be imported on macOS.
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) (*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)
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 {
return nil, 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)
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
}
// loadFinchConfig Load Finch's configuration from a YAML file.
func loadFinchConfig(fs afero.Fs, finchConfigPath string, logger flog.Logger, systemDeps LoadSystemDeps, mem fmemory.Memory) (*Finch, error) {
b, err := afero.ReadFile(fs, finchConfigPath)
if err != nil {
return nil, fmt.Errorf("failed to read 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)
}
if err := validate(&cfg, logger, systemDeps, mem); err != nil {
return nil, fmt.Errorf("failed to validate config file: %w", err)
}
return &cfg, nil
}
// ModifyFinchConfig Modify Finch's configuration from user inputs.
func ModifyFinchConfig(fs afero.Fs, logger flog.Logger, finchConfigPath string, opts VMConfigOpts) (bool, error) {
var isConfigUpdated bool
systemDeps := system.NewStdLib()
mem := fmemory.NewMemory()
finchCfg, err := loadFinchConfig(fs, finchConfigPath, logger, systemDeps, mem)
if err != nil {
return isConfigUpdated, err
}
cpus, memory := opts.CPUs, opts.Memory
if cpus != DefaultCPUs && cpus != *finchCfg.CPUs {
*finchCfg.CPUs = cpus
isConfigUpdated = true
}
if memory != DefaultMemory && memory != *finchCfg.Memory {
*finchCfg.Memory = memory
isConfigUpdated = true
}
if !isConfigUpdated {
return isConfigUpdated, fmt.Errorf("the number of CPUs or the amount of memory should be at least one valid value")
}
if err := validate(finchCfg, logger, systemDeps, mem); err != nil {
return false, fmt.Errorf("failed to validate config file: %w", err)
}
if err := writeConfig(finchCfg, fs, finchConfigPath); err != nil {
return false, err
}
return isConfigUpdated, nil
}