Skip to content

Commit d649732

Browse files
authored
feat: Make cache TTL configurable (#40)
1 parent 5cc56de commit d649732

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

envbuilder.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ type Options struct {
123123
// to using a devcontainer that some might find simpler.
124124
DockerfilePath string `env:"DOCKERFILE_PATH"`
125125

126+
// CacheTTLDays is the number of days to use cached layers before
127+
// expiring them. Defaults to 7 days.
128+
CacheTTLDays int `env:"CACHE_TTL_DAYS"`
129+
126130
// DockerConfigBase64 is a base64 encoded Docker config
127131
// file that will be used to pull images from private
128132
// container registries.
@@ -557,6 +561,10 @@ func Run(ctx context.Context, options Options) error {
557561
logf(codersdk.LogLevelInfo, "%s", scanner.Text())
558562
}
559563
}()
564+
cacheTTL := time.Hour * 24 * 7
565+
if options.CacheTTLDays != 0 {
566+
cacheTTL = time.Hour * 24 * time.Duration(options.CacheTTLDays)
567+
}
560568

561569
endStage := startStage("🏗️ Building image...")
562570
// At this point we have all the context, we can now build!
@@ -578,7 +586,7 @@ func Run(ctx context.Context, options Options) error {
578586
CompressionLevel: 3,
579587
CacheOptions: config.CacheOptions{
580588
// Cache for a week by default!
581-
CacheTTL: time.Hour * 24 * 7,
589+
CacheTTL: cacheTTL,
582590
CacheDir: options.BaseImageCacheDir,
583591
},
584592
ForceUnpack: true,
@@ -931,20 +939,23 @@ func OptionsFromEnv(getEnv func(string) (string, bool)) Options {
931939
if env == "" {
932940
continue
933941
}
942+
e, ok := getEnv(env)
943+
if !ok {
944+
continue
945+
}
934946
switch fieldTyp.Type.Kind() {
935947
case reflect.String:
936-
v, _ := getEnv(env)
937-
field.SetString(v)
948+
field.SetString(e)
938949
case reflect.Bool:
939-
e, _ := getEnv(env)
940950
v, _ := strconv.ParseBool(e)
941951
field.SetBool(v)
952+
case reflect.Int:
953+
v, _ := strconv.ParseInt(e, 10, 64)
954+
field.SetInt(v)
942955
case reflect.Slice:
943-
v, ok := getEnv(env)
944-
if !ok {
945-
continue
946-
}
947-
field.Set(reflect.ValueOf(strings.Split(v, ",")))
956+
field.Set(reflect.ValueOf(strings.Split(e, ",")))
957+
default:
958+
panic(fmt.Sprintf("unsupported type %s in OptionsFromEnv", fieldTyp.Type.String()))
948959
}
949960
}
950961

envbuilder_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ func TestSystemOptions(t *testing.T) {
2323
opts := map[string]string{
2424
"INIT_SCRIPT": "echo hello",
2525
"CACHE_REPO": "kylecarbs/testing",
26+
"CACHE_TTL_DAYS": "30",
2627
"DEVCONTAINER_JSON_PATH": "/tmp/devcontainer.json",
2728
"DOCKERFILE_PATH": "Dockerfile",
2829
"FALLBACK_IMAGE": "ubuntu:latest",
2930
"FORCE_SAFE": "true",
3031
"INSECURE": "false",
32+
"GIT_CLONE_DEPTH": "1",
3133
"GIT_URL": "https://github.com/coder/coder",
3234
"WORKSPACE_FOLDER": "/workspaces/coder",
3335
}
@@ -37,10 +39,12 @@ func TestSystemOptions(t *testing.T) {
3739
require.Equal(t, "echo hello", env.InitScript)
3840
require.Equal(t, "kylecarbs/testing", env.CacheRepo)
3941
require.Equal(t, "/tmp/devcontainer.json", env.DevcontainerJSONPath)
42+
require.Equal(t, 30, env.CacheTTLDays)
4043
require.Equal(t, "Dockerfile", env.DockerfilePath)
4144
require.Equal(t, "ubuntu:latest", env.FallbackImage)
4245
require.True(t, env.ForceSafe)
4346
require.False(t, env.Insecure)
47+
require.Equal(t, 1, env.GitCloneDepth)
4448
require.Equal(t, "https://github.com/coder/coder", env.GitURL)
4549
require.Equal(t, "/workspaces/coder", env.WorkspaceFolder)
4650
}

0 commit comments

Comments
 (0)