Skip to content

Commit ae9585f

Browse files
committed
Make the location for .crc directory configurable
- Add a new config key called crc-dir - Initialise Crc base directory variables based on the saved config - New config will take effect after rerunning `crc setup`
1 parent ec6cbfb commit ae9585f

File tree

8 files changed

+83
-8
lines changed

8 files changed

+83
-8
lines changed

cmd/crc-embedder/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ when building the crc executable for release`,
2323
}
2424

2525
func init() {
26-
err := constants.EnsureBaseDirectoriesExist()
26+
err := constants.EnsureBaseDirectoriesExist("")
2727
if err != nil {
2828
fmt.Println("CRC base directories are missing: ", err)
2929
os.Exit(1)

cmd/crc/cmd/root.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@ var (
4646
)
4747

4848
func init() {
49-
if err := constants.EnsureBaseDirectoriesExist(); err != nil {
50-
logging.Fatal(err.Error())
51-
}
5249
var err error
5350
config, viper, err = newConfig()
5451
if err != nil {
5552
logging.Fatal(err.Error())
5653
}
5754

55+
if err := constants.EnsureBaseDirectoriesExist(crcConfig.GetConfigDir(config)); err != nil {
56+
logging.Fatal(err.Error())
57+
}
58+
5859
if err := setProxyDefaults(); err != nil {
5960
logging.Warn(err.Error())
6061
}

pkg/crc/config/settings.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const (
3737
EmergencyLogin = "enable-emergency-login"
3838
PersistentVolumeSize = "persistent-volume-size"
3939
EnableBundleQuayFallback = "enable-bundle-quay-fallback"
40+
CrcDir = "crc-dir"
4041
)
4142

4243
func RegisterSettings(cfg *Config) {
@@ -141,13 +142,18 @@ func RegisterSettings(cfg *Config) {
141142

142143
cfg.AddSetting(EnableBundleQuayFallback, false, ValidateBool, SuccessfullyApplied,
143144
"If bundle download from the default location fails, fallback to quay.io (true/false, default: false)")
145+
cfg.AddSetting(CrcDir, constants.GetHomeDir(), validateDirectory, RequiresCRCSetup,
146+
"Location for .crc")
144147

145-
if err := cfg.RegisterNotifier(Preset, presetChanged); err != nil {
148+
if err := cfg.RegisterNotifier(Preset, settingChanged); err != nil {
146149
logging.Debugf("Failed to register notifier for Preset: %v", err)
147150
}
151+
if err := cfg.RegisterNotifier(CrcDir, settingChanged); err != nil {
152+
logging.Debugf("Failed to register notifier for .crc directory: %v", err)
153+
}
148154
}
149155

150-
func presetChanged(cfg *Config, _ string, _ interface{}) {
156+
func settingChanged(cfg *Config, _ string, _ interface{}) {
151157
UpdateDefaults(cfg)
152158
}
153159

@@ -167,6 +173,10 @@ func GetPreset(config Storage) preset.Preset {
167173
return preset.ParsePreset(config.Get(Preset).AsString())
168174
}
169175

176+
func GetConfigDir(config Storage) string {
177+
return config.Get(CrcDir).AsString()
178+
}
179+
170180
func defaultNetworkMode() network.Mode {
171181
if runtime.GOOS != "linux" {
172182
return network.UserNetworkingMode

pkg/crc/config/settings_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"fmt"
5+
"os"
56
"path/filepath"
67
"testing"
78

@@ -182,3 +183,28 @@ func TestPath(t *testing.T) {
182183
IsSecret: false,
183184
}, cfg.Get(ProxyCAFile))
184185
}
186+
187+
func TestDirectory(t *testing.T) {
188+
cfg, err := newInMemoryConfig()
189+
require.NoError(t, err)
190+
191+
assert.Equal(t, SettingValue{
192+
Value: constants.GetHomeDir(),
193+
Invalid: false,
194+
IsDefault: true,
195+
IsSecret: false,
196+
}, cfg.Get(CrcDir))
197+
198+
tmpDir, err := os.MkdirTemp("", "tempdir")
199+
require.NoError(t, err)
200+
defer os.Remove(tmpDir)
201+
_, err = cfg.Set(CrcDir, tmpDir)
202+
require.NoError(t, err)
203+
204+
assert.Equal(t, SettingValue{
205+
Value: tmpDir,
206+
Invalid: false,
207+
IsDefault: false,
208+
IsSecret: false,
209+
}, cfg.Get(CrcDir))
210+
}

pkg/crc/config/validations.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ func validatePath(value interface{}) (bool, string) {
103103
return true, ""
104104
}
105105

106+
func validateDirectory(value interface{}) (bool, string) {
107+
if err := validation.ValidateDirectory(cast.ToString(value)); err != nil {
108+
return false, err.Error()
109+
}
110+
return true, ""
111+
}
112+
106113
// validateHTTPProxy checks if given URI is valid for a HTTP proxy
107114
func validateHTTPProxy(value interface{}) (bool, string) {
108115
if err := httpproxy.ValidateProxyURL(cast.ToString(value), false); err != nil {

pkg/crc/constants/constants.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ func GetHomeDir() string {
164164
}
165165

166166
// EnsureBaseDirectoriesExist creates ~/.crc, ~/.crc/bin and ~/.crc/cache directories if it is not present
167-
func EnsureBaseDirectoriesExist() error {
167+
func EnsureBaseDirectoriesExist(configDir string) error {
168+
initialiseAllDirectories(configDir)
168169
baseDirectories := []string{CrcBaseDir, MachineCacheDir, CrcBinDir}
169170
for _, baseDir := range baseDirectories {
170171
err := os.MkdirAll(baseDir, 0750)
@@ -175,6 +176,26 @@ func EnsureBaseDirectoriesExist() error {
175176
return nil
176177
}
177178

179+
func initialiseAllDirectories(crcDir string) {
180+
if crcDir == "" {
181+
return
182+
}
183+
CrcBaseDir = filepath.Join(crcDir, ".crc")
184+
CrcBinDir = filepath.Join(CrcBaseDir, "bin")
185+
CrcOcBinDir = filepath.Join(CrcBinDir, "oc")
186+
CrcPodmanBinDir = filepath.Join(CrcBinDir, "podman")
187+
CrcSymlinkPath = filepath.Join(CrcBinDir, "crc")
188+
ConfigPath = filepath.Join(CrcBaseDir, ConfigFile)
189+
LogFilePath = filepath.Join(CrcBaseDir, LogFile)
190+
DaemonLogFilePath = filepath.Join(CrcBaseDir, DaemonLogFile)
191+
MachineBaseDir = CrcBaseDir
192+
MachineCacheDir = filepath.Join(MachineBaseDir, "cache")
193+
MachineInstanceDir = filepath.Join(MachineBaseDir, "machines")
194+
DaemonSocketPath = filepath.Join(CrcBaseDir, "crc.sock")
195+
KubeconfigFilePath = filepath.Join(MachineInstanceDir, DefaultName, "kubeconfig")
196+
PasswdFilePath = filepath.Join(MachineInstanceDir, DefaultName, "passwd")
197+
}
198+
178199
func GetPublicKeyPath() string {
179200
return filepath.Join(MachineInstanceDir, DefaultName, "id_ecdsa.pub")
180201
}

pkg/crc/preflight/preflight_checks_common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
func bundleCheck(bundlePath string, preset crcpreset.Preset, enableBundleQuayFallback bool) Check {
2222
return Check{
2323
configKeySuffix: "check-bundle-extracted",
24-
checkDescription: "Checking if CRC bundle is extracted in '$HOME/.crc'",
24+
checkDescription: fmt.Sprintf("Checking if CRC bundle is extracted in %q", bundlePath),
2525
check: checkBundleExtracted(bundlePath),
2626
fixDescription: "Getting bundle for the CRC executable",
2727
fix: fixBundleExtracted(bundlePath, preset, enableBundleQuayFallback),

pkg/crc/validation/validation.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ func ValidatePath(path string) error {
168168
return nil
169169
}
170170

171+
// ValidateDirectory checks if provided path exists and has sufficient permissions
172+
func ValidateDirectory(path string) error {
173+
file, err := os.CreateTemp(path, "tempfile")
174+
if err != nil {
175+
return &invalidPath{path: path}
176+
}
177+
defer os.Remove(file.Name())
178+
return nil
179+
}
180+
171181
type imagePullSecret struct {
172182
Auths map[string]map[string]interface{} `json:"auths"`
173183
}

0 commit comments

Comments
 (0)