Skip to content

extract RunCacheProbe function #284

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 10 commits into from
Jul 31, 2024
Merged

extract RunCacheProbe function #284

merged 10 commits into from
Jul 31, 2024

Conversation

johnstcn
Copy link
Member

@johnstcn johnstcn commented Jul 26, 2024

Part of coder/internal#11

Builds on top of #282 and #283

Extracts the logic for --get-cached-image to a separate function

Also pulls out some other common logic shared between Run and RunCacheProbe. There is more that could be DRYed up, but leaving this for later.

@johnstcn johnstcn self-assigned this Jul 26, 2024
@johnstcn johnstcn force-pushed the cj/run-cache-probe branch from 4d002c7 to a75b84c Compare July 29, 2024 14:35
@johnstcn johnstcn changed the base branch from cj/option-defaults to cj/export-log July 29, 2024 14:53
Base automatically changed from cj/export-log to main July 29, 2024 16:56
@johnstcn johnstcn force-pushed the cj/run-cache-probe branch from a75b84c to 3c8066f Compare July 29, 2024 16:58
@johnstcn johnstcn marked this pull request as ready for review July 30, 2024 16:58
@@ -1137,3 +1407,111 @@ func copyFile(src, dst string) error {
}
return nil
}

Copy link
Member Author

Choose a reason for hiding this comment

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

review: below functions are extracted

Copy link
Member

Choose a reason for hiding this comment

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

curious if we can move them to another unit/file

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah we probably should; I'd like to simplify the envbuilder.Run / RunCacheProbe functions as much as possible for future maintenance.

Copy link
Contributor

@dannykopping dannykopping left a comment

Choose a reason for hiding this comment

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

LGTM, mostly small nits and suggestions
Quite a lot of complexity in the RunCacheProbe function which would be good to decompose later on.

envbuilder.go Outdated
Comment on lines 843 to 845
// Logger is the logf to use for all operations.
// Filesystem is the filesystem to use for all operations.
// Defaults to the host filesystem.
Copy link
Contributor

Choose a reason for hiding this comment

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

Were these arguments previous, but you later moved them to options.Options by any chance?
This comment feels potentially stale.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, this is stale 👍

stageNumber := 0
startStage := func(format string, args ...any) func(format string, args ...any) {
now := time.Now()
stageNumber++
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: pass in an int pointer instead perhaps?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a pre-existing non-pointer integer; I want to refactor this whole stage builder business later on. A little copying for now.

Comment on lines +891 to +893
data := make([]byte, 4096)
for {
read, err := reader.Read(data)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: consider using https://pkg.go.dev/bufio#NewReaderSize with ReadBytes(delim byte) where delim is '\r'?

Copy link
Member Author

Choose a reason for hiding this comment

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

pre-existing; will address in follow-up

Copy link
Member

Choose a reason for hiding this comment

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

+1

Copy link
Member

@mtojek mtojek left a comment

Choose a reason for hiding this comment

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

Please ignore comments that refer to the original code.

return nil, fmt.Errorf("--cache-repo must be set when using --get-cached-image")
}

stageNumber := 0
Copy link
Member

Choose a reason for hiding this comment

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

nit: stageIndex?

Copy link
Member Author

Choose a reason for hiding this comment

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

will rename in a follow-up

Comment on lines +891 to +893
data := make([]byte, 4096)
for {
read, err := reader.Read(data)
Copy link
Member

Choose a reason for hiding this comment

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

+1

Comment on lines +1122 to +1124
Insecure: opts.Insecure,
InsecurePull: opts.Insecure,
SkipTLSVerify: opts.Insecure,
Copy link
Member

Choose a reason for hiding this comment

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

Is it on purpose or is it something we should address in the future?

Copy link
Member Author

Choose a reason for hiding this comment

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

What is the issue here?

Copy link
Member

Choose a reason for hiding this comment

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

ignoring TLS issues

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's intentional here.

Copy link
Member

Choose a reason for hiding this comment

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

synced offline: I assumed incorrectly that it is SkipTLSVerify = true

endStage("🏗️ Found cached image!")

// Sanitize the environment of any opts!
options.UnsetEnv()
Copy link
Member

Choose a reason for hiding this comment

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

nit: should run UnsetEnv in defer?

Copy link
Member Author

Choose a reason for hiding this comment

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

added at beginning of func

@@ -1137,3 +1407,111 @@ func copyFile(src, dst string) error {
}
return nil
}

Copy link
Member

Choose a reason for hiding this comment

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

curious if we can move them to another unit/file


func initDockerConfigJSON(dockerConfigBase64 string) (func() error, error) {
var cleanupOnce sync.Once
noop := func() error { return nil }
Copy link
Member

Choose a reason for hiding this comment

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

wouldn't it be more natural to just return nil and check it before execution?

Copy link
Member Author

Choose a reason for hiding this comment

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

Returning a noop function removes the need for the caller to care whether it's nil or not. I much prefer this pattern personally.

return noop, fmt.Errorf("decode docker config: %w", err)
}
var configFile DockerConfig
decoded, err = hujson.Standardize(decoded)
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

:D

// Spawn an in-memory registry to cache built layers...
registry := handlers.NewApp(ctx, cfg)

listener, err := net.Listen("tcp", "127.0.0.1:0")
Copy link
Member

Choose a reason for hiding this comment

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

nit: no need to expose it outside? I mean, 0.0.0.0:0 for debugging purposes?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't recall ever needing to do this; you can always inspect the local dir contents

if opts.CacheRepo != "" {
destinations = append(destinations, opts.CacheRepo)
}
kOpts := &config.KanikoOptions{
Copy link
Member

Choose a reason for hiding this comment

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

At some point it would be great to treat Kaniko as strategy pattern, so we can replace it in the future.

@johnstcn johnstcn merged commit cacbcb8 into main Jul 31, 2024
4 checks passed
@johnstcn johnstcn deleted the cj/run-cache-probe branch July 31, 2024 11:59
johnstcn added a commit that referenced this pull request Aug 5, 2024
Builds on top of #282 and #283:

- Extracts the logic for --get-cached-image to a separate function
- Also pulls out some other common logic shared between Run and RunCacheProbe.

(cherry picked from commit cacbcb8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants