Skip to content

feat: Make cache TTL configurable #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions envbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ type Options struct {
// to using a devcontainer that some might find simpler.
DockerfilePath string `env:"DOCKERFILE_PATH"`

// CacheTTLDays is the number of days to use cached layers before
// expiring them. Defaults to 7 days.
CacheTTLDays int `env:"CACHE_TTL_DAYS"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make this a time.Duration instead? Go can parse pretty nice strings like "7d" or "12hr".

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about that, but since days aren't supported in the format, it's an awkward way to specify long time periods. Okay with me if you prefer that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, you're right I forgot. I think days is fine in that case. It's weird that someone would want to cache for hours anyways.


// DockerConfigBase64 is a base64 encoded Docker config
// file that will be used to pull images from private
// container registries.
Expand Down Expand Up @@ -557,6 +561,10 @@ func Run(ctx context.Context, options Options) error {
logf(codersdk.LogLevelInfo, "%s", scanner.Text())
}
}()
cacheTTL := time.Hour * 24 * 7
if options.CacheTTLDays != 0 {
cacheTTL = time.Hour * 24 * time.Duration(options.CacheTTLDays)
}

endStage := startStage("🏗️ Building image...")
// At this point we have all the context, we can now build!
Expand All @@ -578,7 +586,7 @@ func Run(ctx context.Context, options Options) error {
CompressionLevel: 3,
CacheOptions: config.CacheOptions{
// Cache for a week by default!
CacheTTL: time.Hour * 24 * 7,
CacheTTL: cacheTTL,
CacheDir: options.BaseImageCacheDir,
},
ForceUnpack: true,
Expand Down Expand Up @@ -931,20 +939,23 @@ func OptionsFromEnv(getEnv func(string) (string, bool)) Options {
if env == "" {
continue
}
e, ok := getEnv(env)
if !ok {
continue
}
switch fieldTyp.Type.Kind() {
case reflect.String:
v, _ := getEnv(env)
field.SetString(v)
field.SetString(e)
case reflect.Bool:
e, _ := getEnv(env)
v, _ := strconv.ParseBool(e)
field.SetBool(v)
case reflect.Int:
v, _ := strconv.ParseInt(e, 10, 64)
field.SetInt(v)
case reflect.Slice:
v, ok := getEnv(env)
if !ok {
continue
}
field.Set(reflect.ValueOf(strings.Split(v, ",")))
field.Set(reflect.ValueOf(strings.Split(e, ",")))
default:
panic(fmt.Sprintf("unsupported type %s in OptionsFromEnv", fieldTyp.Type.String()))
}
}

Expand Down
4 changes: 4 additions & 0 deletions envbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ func TestSystemOptions(t *testing.T) {
opts := map[string]string{
"INIT_SCRIPT": "echo hello",
"CACHE_REPO": "kylecarbs/testing",
"CACHE_TTL_DAYS": "30",
"DEVCONTAINER_JSON_PATH": "/tmp/devcontainer.json",
"DOCKERFILE_PATH": "Dockerfile",
"FALLBACK_IMAGE": "ubuntu:latest",
"FORCE_SAFE": "true",
"INSECURE": "false",
"GIT_CLONE_DEPTH": "1",
"GIT_URL": "https://github.com/coder/coder",
"WORKSPACE_FOLDER": "/workspaces/coder",
}
Expand All @@ -37,10 +39,12 @@ func TestSystemOptions(t *testing.T) {
require.Equal(t, "echo hello", env.InitScript)
require.Equal(t, "kylecarbs/testing", env.CacheRepo)
require.Equal(t, "/tmp/devcontainer.json", env.DevcontainerJSONPath)
require.Equal(t, 30, env.CacheTTLDays)
require.Equal(t, "Dockerfile", env.DockerfilePath)
require.Equal(t, "ubuntu:latest", env.FallbackImage)
require.True(t, env.ForceSafe)
require.False(t, env.Insecure)
require.Equal(t, 1, env.GitCloneDepth)
require.Equal(t, "https://github.com/coder/coder", env.GitURL)
require.Equal(t, "/workspaces/coder", env.WorkspaceFolder)
}