Skip to content
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

Pre-parse platform string with StringSliceVar #551

Merged
merged 5 commits into from
Jan 4, 2022
Merged

Pre-parse platform string with StringSliceVar #551

merged 5 commits into from
Jan 4, 2022

Conversation

wilsonehusin
Copy link
Contributor

Hello! First time contributing to ko here 👋🏼

  • The variable BuildOptions.Platform was renamed to BuildOptions.Platforms to reflect the actual data type, though I'm not sure how comfortable are we in breaking changes of (supposedly) public structs.
  • I took somewhat of a "persisting structure all the way down" approach which has a side effect of platformMatcher.spec and gobuildOpener.platforms are now of type []string (instead of string).

Happy to discuss and make changes as desired by maintainers!

Closes #537

This allows users to declare --platform multiple times and have the
values appended, i.e.:
  ko build --platform=linux/amd64 --platform=linux/arm64
is equivalent to
  ko build --platform=linux/amd64,linux/arm64

As a side effect, platformMatcher.spec and gobuildOpener.platforms are
now of type []string (instead of string) to maintain structure of
information from flag parsing.
Copy link
Member

@imjasonh imjasonh left a comment

Choose a reason for hiding this comment

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

Thanks for this contribution, and sorry for the delay in reviewing. This looks mostly good, just a few smallish comments/questions.

pkg/commands/resolver.go Outdated Show resolved Hide resolved
Comment on lines 64 to 67
allPlatforms := len(bo.Platforms) == 1 && bo.Platforms[0] == "all"
selectiveMultiplatform := len(bo.Platforms) > 1
multiplatform := allPlatforms || selectiveMultiplatform
if len(bo.Platforms) > 0 && !multiplatform {
Copy link
Member

Choose a reason for hiding this comment

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

I like the new descriptive variable names!

I think bo.Platforms is always non-empty at this point, because we set it to some default if unset. Can this just be if !multiplatform? That makes the bo.Platforms[0] easier to understand, since we'll know there's only one value.

pkg/commands/config.go Show resolved Hide resolved
Copy link
Contributor Author

@wilsonehusin wilsonehusin left a comment

Choose a reason for hiding this comment

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

@imjasonh less than 24 hours is not "late" 😄 — thank you for reviewing! Revision incoming in a bit, though would like confirmation on the --platform=all,linux/arm64 scenario.

pkg/commands/resolver.go Outdated Show resolved Hide resolved
pkg/commands/config.go Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Dec 17, 2021

Codecov Report

Merging #551 (c24fe5f) into main (8135bf2) will increase coverage by 0.06%.
The diff coverage is 83.33%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #551      +/-   ##
==========================================
+ Coverage   48.82%   48.88%   +0.06%     
==========================================
  Files          42       42              
  Lines        2204     2207       +3     
==========================================
+ Hits         1076     1079       +3     
  Misses        946      946              
  Partials      182      182              
Impacted Files Coverage Δ
pkg/commands/resolver.go 31.88% <66.66%> (+0.33%) ⬆️
pkg/commands/config.go 57.57% <83.33%> (+1.32%) ⬆️
pkg/build/gobuild.go 57.40% <100.00%> (ø)
pkg/build/options.go 72.72% <100.00%> (ø)
pkg/commands/options/build.go 62.85% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8135bf2...c24fe5f. Read the comment docs.

@wilsonehusin
Copy link
Contributor Author

wilsonehusin commented Dec 17, 2021

The diff in Verify Codegen seems expected:

-      --platform strings         Which platform to use when pulling a multi-platform base. Format: all | <os>[/<arch>[/<variant>]][,platform]*
+      --platform string          Which platform to use when pulling a multi-platform base. Format: all | <os>[/<arch>[/<variant>]][,platform]*

Should I run hack/update-codegen.sh?

Edit: running it seems only to update the expected lines, so I'm going to push it.

Internally cobra/pflag defines StringSliceVar as "strings" whereas
StringVar is defined as "string".

This change is updated by running hack/update-codegen.sh script.
@@ -89,9 +89,9 @@ func WithConfig(buildConfigs map[string]Config) Option {
//
// platform = <os>[/<arch>[/<variant>]]
// allowed = all | platform[,platform]*
func WithPlatforms(platforms string) Option {
func WithPlatforms(platforms []string) Option {
Copy link
Member

Choose a reason for hiding this comment

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

@jonjohnsonjr had the good idea that this change could be WithPlatforms(platforms string...) and not break existing users. (Thanks Jon!)

If you feel like doing this change in this PR that'd be great, if not I can do it in a followup before we release this.

Otherwise, lgtm!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes! I felt silly I didn't think of it in the first place. Fixing...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤔 actually, using ...string would not break compile-time for existing users, but it would break the behavior, unless we do some sort of strings.Spilt here.

Do we want to add that logic as well? Something like:

func WithPlatforms(platforms ...string) Option {
  if len(platforms) == 1 {
    platforms = strings.Split(platforms, ",")
  }
  ...
}

There is an itch on me that this code is waiting to be deleted, but happy to make changes if maintainers are ok with maintaining it 😄

Copy link
Member

Choose a reason for hiding this comment

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

Yeah let's add the behavior you describe, for folks using WithPlatforms("linux/amd64,linux/arm64"), and document that this behavior will change in a future release. This is a fairly small concession we should make to existing library users.

imjasonh
imjasonh previously approved these changes Dec 18, 2021
pkg/commands/config.go Outdated Show resolved Hide resolved
Update comments to reflect implementation as well.
@wilsonehusin
Copy link
Contributor Author

thanks for the review folks! 🎉

@imjasonh
Copy link
Member

imjasonh commented Jan 4, 2022

Thanks for working on this, and sorry for the holiday-related slowness in reviewing it again. Looks like there's a build failure, otherwise LGTM.

@wilsonehusin
Copy link
Contributor Author

That was a silly mistake, apologies! Happy new year! 🎉

@imjasonh imjasonh merged commit c67fb03 into ko-build:main Jan 4, 2022
@wilsonehusin wilsonehusin deleted the gh-537-platform-stringslicevar branch January 4, 2022 19:20
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.

--platform should be a StringSliceVar flag
4 participants