Skip to content

Commit

Permalink
Support osversion when selecting base images (#536)
Browse files Browse the repository at this point in the history
* Support osversion when selecting base images

Use this in e2e tests.

Also clean up usage of bo.Platform vs a new platform var.

* use correct osversion
  • Loading branch information
imjasonh authored Dec 15, 2021
1 parent b813b95 commit 33fa766
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Basic e2e test

on:
pull_request:
pull_request:
branches: ['main']

jobs:
Expand Down Expand Up @@ -35,9 +35,8 @@ jobs:
export PLATFORM=${GOOS}/${GOARCH}
if [[ "${{ matrix.platform }}" == "windows-latest" ]]; then
# Always use the nanoserver base image, which matches the Windows
# version used by the GitHub Actions Windows runner.
rm .ko.yaml
OSVERSION="10.0.17763.2366"
PLATFORM=${PLATFORM}:${OSVERSION}
export KO_DEFAULTBASEIMAGE=mcr.microsoft.com/windows/nanoserver:1809
else
# Explicitly test multiple platform builds (a subset of what's in the base!)
Expand Down
18 changes: 12 additions & 6 deletions pkg/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
)

// getBaseImage returns a function that determines the base image for a given import path.
func getBaseImage(platform string, bo *options.BuildOptions) build.GetBase {
func getBaseImage(bo *options.BuildOptions) build.GetBase {
cache := map[string]build.Result{}
fetch := func(ctx context.Context, ref name.Reference) (build.Result, error) {
// For ko.local, look in the daemon.
Expand All @@ -61,10 +61,16 @@ func getBaseImage(platform string, bo *options.BuildOptions) build.GetBase {
//
// Platforms can be comma-separated if we only want a subset of the base
// image.
multiplatform := platform == "all" || strings.Contains(platform, ",")
var p v1.Platform
if platform != "" && !multiplatform {
parts := strings.Split(platform, "/")
multiplatform := bo.Platform == "all" || strings.Contains(bo.Platform, ",")
if bo.Platform != "" && !multiplatform {
var p v1.Platform

parts := strings.Split(bo.Platform, ":")
if len(parts) == 2 {
p.OSVersion = parts[1]
}

parts = strings.Split(parts[0], "/")
if len(parts) > 0 {
p.OS = parts[0]
}
Expand All @@ -75,7 +81,7 @@ func getBaseImage(platform string, bo *options.BuildOptions) build.GetBase {
p.Variant = parts[2]
}
if len(parts) > 3 {
return nil, fmt.Errorf("too many slashes in platform spec: %s", platform)
return nil, fmt.Errorf("too many slashes in platform spec: %s", bo.Platform)
}
ropt = append(ropt, remote.WithPlatform(p))
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/commands/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ func TestOverrideDefaultBaseImageUsingBuildOption(t *testing.T) {
wantImage := fmt.Sprintf("%s@%s", baseImage, wantDigest)
bo := &options.BuildOptions{
BaseImage: wantImage,
Platform: "all",
}

baseFn := getBaseImage("all", bo)
baseFn := getBaseImage(bo)
_, res, err := baseFn(context.Background(), "ko://example.com/helloworld")
if err != nil {
t.Fatalf("getBaseImage(): %v", err)
Expand Down
13 changes: 6 additions & 7 deletions pkg/commands/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,19 @@ func gobuildOptions(bo *options.BuildOptions) ([]build.Option, error) {
return nil, err
}

platform := bo.Platform
if platform == "" {
platform = "linux/amd64"
if bo.Platform == "" {
bo.Platform = "linux/amd64"

goos, goarch, goarm := os.Getenv("GOOS"), os.Getenv("GOARCH"), os.Getenv("GOARM")

// Default to linux/amd64 unless GOOS and GOARCH are set.
if goos != "" && goarch != "" {
platform = path.Join(goos, goarch)
bo.Platform = path.Join(goos, goarch)
}

// Use GOARM for variant if it's set and GOARCH is arm.
if strings.Contains(goarch, "arm") && goarm != "" {
platform = path.Join(platform, "v"+goarm)
bo.Platform = path.Join(bo.Platform, "v"+goarm)
}
} else {
// Make sure these are all unset
Expand All @@ -86,8 +85,8 @@ func gobuildOptions(bo *options.BuildOptions) ([]build.Option, error) {
}

opts := []build.Option{
build.WithBaseImages(getBaseImage(platform, bo)),
build.WithPlatforms(platform),
build.WithBaseImages(getBaseImage(bo)),
build.WithPlatforms(bo.Platform),
build.WithJobs(bo.ConcurrentBuilds),
}
if creationTime != nil {
Expand Down

0 comments on commit 33fa766

Please sign in to comment.