frontend/dockerfile: some minor refactor and optimizations#3199
frontend/dockerfile: some minor refactor and optimizations#3199jedevc merged 10 commits intomoby:masterfrom
Conversation
| if ok, _ := regexp.MatchString("^[a-z][a-z0-9-_\\.]*$", stageName); !ok { | ||
| return "", errors.Errorf("invalid name for build stage: %q, name can't start with a number or contain symbols", args[2]) | ||
| if !validStageName.MatchString(stageName) { | ||
| return "", errors.Errorf("invalid name for build stage: %q, name can't start with a number or contain symbols", stageName) |
There was a problem hiding this comment.
nit: this will include the lowercased version in the error message - is that what we want?
There was a problem hiding this comment.
Oh! Good catch; I initially made the regex itself case-insensitive (adding (?i)) but I saw that it introduces more allocations (or memory use) than the existing strings.ToLower() so I changed that back, but forgot to put back this value here.
Let me update that.
| @@ -44,8 +42,7 @@ func TestShellParserMandatoryEnvVars(t *testing.T) { | |||
| require.Equal(t, "", newWord) | |||
There was a problem hiding this comment.
This could be simplified to require.Empty
There was a problem hiding this comment.
Ah! Yes, let me update that as well. I'm not a frequent user of testify, so didn't go look for those. My eye landed on the string matching for errors, which felt "incorrect", so I assumed testify had a function for that (it did), which also produced better output on failures, so I updated them 👍
f17cd93 to
e5c635a
Compare
|
@aaronlehmann thanks for reviewing; I updated some of your suggestions, and also updated the commit message for the "types" commit (to outline what I commented above) Let me know what you think (feedback welcome as always 👍 ) |
|
@tonistiigi @crazy-max ptal |
e5c635a to
2831c87
Compare
|
Rebased, after #3305 was merged |
…rted These became obsolete in 906e345, which always enabled them. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Before:
BenchmarkParseBuildStageName-10 180793 5972 ns/op 8355 B/op 126 allocs/op
After:
BenchmarkParseBuildStageName-10 1980735 605.5 ns/op 48 B/op 3 allocs/op
Also renamed a variable that collided with an import.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Also fix some grammar issues in the GoDoc, and some nits in the tests.
Before/after:
BenchmarkToUpper-10 11979394 96.31 ns/op 10 B/op 2 allocs/op
BenchmarkEqualFold-10 40687216 29.65 ns/op 0 B/op 0 allocs/op
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This is makes it easier to discover what values are acceptable;
- Generated docs shows them nicely together.
- Intent is clearer (these are all the `MountType` options).
- Intent on fields and arguments is clearer (`func(t MountType)`, `struct Foo{Field: MountType}`),
but still allows for using / passing a regular string (without casting); validation
is already handled for these, so invalid strings would (should) already be handled.
- With these types, both docs at pkg.go.dev and IDEs allow for easier navigating
(click the `MountType` in the examples above to navigate to the `MountType`
definition, and the options (`const`s) below them.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
similar to df9781b Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2831c87 to
97cba15
Compare
The `m.Type == MountTypeSecret` has a more specific check for this option, which would not be hit because this check was before it. Move the check until after the validation for secret mounts, so that the specific error can be returned, instead of the generic one. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
…leanup - use strings.Cut() which simplifies handling of key/value pairs - remove some redundant intermediate variables - combined "fileInfoAllowed" if-statement for easier reading Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Also some minor cleanup in the boolean value parsing; using a switch instead of if/elseif/else. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
thaJeztah
left a comment
There was a problem hiding this comment.
@jedevc updated;
- changed types from an alias to a strong type
- merged the
!okcheck
I also pushed 2 additional commits;
- frontend/dockerfile: provide suggestions for mount share mode
- frontend/dockerfile: move check for cache-sharing
| v := ShareMode(strings.ToLower(value)) | ||
| if _, ok := allowedSharingModes[v]; !ok { | ||
| return nil, suggest.WrapError(errors.Errorf("unsupported sharing value %q", value), value, allShareModes(), true) |
There was a problem hiding this comment.
I based this on df9781b, but am slightly confused, as the suggest.WrapError() uses caseSensitive=true, but the code here suggests otherwise (perhaps this was meant to be an optimisation to not have to do the strings.ToLower()? But in that case we should pass v to the function, not value.
97cba15 to
9313bd7
Compare
crazy-max
left a comment
There was a problem hiding this comment.
LGTM, ptal @jedevc @tonistiigi
| } | ||
|
|
||
| if m.CacheSharing != "" && m.Type != MountTypeCache { | ||
| return nil, errors.Errorf("invalid cache sharing set for %v mount", m.Type) |
There was a problem hiding this comment.
We can remove the above m.Type == MountTypeSecret variant of this check entirely, and replace this with:
return nil, errors.Errorf("%s mount should not define sharing set", m.Type)or similar.
There was a problem hiding this comment.
oh, I see I missed your comment; let me know if you want me to look at this
See individual commits for details;
frontend/dockerfile: remove isSSHMountsSupported, isSecretMountsSupported
These became obsolete in 906e345, which always enabled them.
frontend/dockerfile: parseBuildStageName(): pre-compile regex
Before/After:
BenchmarkParseBuildStageName-10 180793 5972 ns/op 8355 B/op 126 allocs/op
BenchmarkParseBuildStageName-10 1980735 605.5 ns/op 48 B/op 3 allocs/op
Also renamed a variable that collided with an import.
frontend/dockerfile/parser: remove redundant concat
frontend/dockerfile/shell: use strings.Equalfold
Also fix some grammar issues in the GoDoc, and some nits in the tests.
Before/after:
frontend/dockerfile: define types for enums
This is makes it easier to discover what values are acceptable;
MountTypeoptions).func(t MountType),struct Foo{Field: MountType}),but still allows for using / passing a regular string (without casting); validation
is already handled for these, so invalid strings would (should) already be handled.
(click the
MountTypein the examples above to navigate to theMountTypedefinition, and the options (
consts) below them.frontend/dockerfile: provide suggestions for mount share mode
similar to df9781b
frontend/dockerfile: move check for cache-sharing
The
m.Type == MountTypeSecrethas a more specific check for this option,which would not be hit because this check was before it.
Move the check until after the validation for secret mounts, so that the
specific error can be returned, instead of the generic one.
frontend/dockerfile: parseMount() use strings.Cut(), and some minor cleanup
frontend/dockerfile: parseExtraHosts(): use strings.Cut()
frontend/dockerfile: BFlags.Parse(): use strings.Cut()
Also some minor cleanup in the boolean value parsing; using a switch
instead of if/elseif/else.