Skip to content

frontend/dockerfile: some minor refactor and optimizations#3199

Merged
jedevc merged 10 commits intomoby:masterfrom
thaJeztah:frontend_cleanup
Jan 12, 2023
Merged

frontend/dockerfile: some minor refactor and optimizations#3199
jedevc merged 10 commits intomoby:masterfrom
thaJeztah:frontend_cleanup

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

@thaJeztah thaJeztah commented Oct 19, 2022

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:

 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

frontend/dockerfile: define types for enums

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 (consts) below them.

Screenshot 2022-10-19 at 22 06 33

frontend/dockerfile: provide suggestions for mount share mode

similar to df9781b

frontend/dockerfile: move check for cache-sharing

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.

frontend/dockerfile: parseMount() use strings.Cut(), and some minor cleanup

  • use strings.Cut() which simplifies handling of key/value pairs
  • remove some redundant intermediate variables
  • combined "fileInfoAllowed" if-statement for easier reading

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.

@thaJeztah thaJeztah marked this pull request as ready for review October 19, 2022 21:00
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)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: this will include the lowercased version in the error message - is that what we want?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This could be simplified to require.Empty

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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 👍

@thaJeztah
Copy link
Copy Markdown
Member Author

@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 👍 )

@thaJeztah
Copy link
Copy Markdown
Member Author

@tonistiigi @crazy-max ptal

@thaJeztah
Copy link
Copy Markdown
Member Author

Rebased, after #3305 was merged

@AkihiroSuda @crazy-max @tonistiigi PTAL

…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>
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>
Copy link
Copy Markdown
Member Author

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

@jedevc updated;

  • changed types from an alias to a strong type
  • merged the !ok check

I also pushed 2 additional commits;

  • frontend/dockerfile: provide suggestions for mount share mode
  • frontend/dockerfile: move check for cache-sharing

Comment on lines +236 to +238
v := ShareMode(strings.ToLower(value))
if _, ok := allowedSharingModes[v]; !ok {
return nil, suggest.WrapError(errors.Errorf("unsupported sharing value %q", value), value, allShareModes(), true)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

@crazy-max crazy-max left a comment

Choose a reason for hiding this comment

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

LGTM, ptal @jedevc @tonistiigi

}

if m.CacheSharing != "" && m.Type != MountTypeCache {
return nil, errors.Errorf("invalid cache sharing set for %v mount", m.Type)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

oh, I see I missed your comment; let me know if you want me to look at this

@jedevc jedevc merged commit 60e82c1 into moby:master Jan 12, 2023
@thaJeztah thaJeztah deleted the frontend_cleanup branch January 12, 2023 11:53
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.

4 participants