forked from ko-build/ko
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Export functions and a variable to enable embedding of ko's `publish` functionality to be embedded in other tools. See GoogleContainerTools/skaffold#5611 - Remove DockerRepo PublishOption and flag. This removes the `DockerRepo` config option and `--docker-repo` flag from the PR. New PR with the extracted config option: ko-build#351 - Fix copyright headers for boilerplate check. - Use DockerRepo PublishOption instead of env var. - Override defaultBaseImage using BuildOptions. Remove exported package global SetDefaultBaseImage and instead allow programmatic override of the default base image using the field `BaseImage` in `options.BuildOptions`. Also fix copyright header years. - Add BuildOptions parameter to getBaseImage This enables access to BaseImage for programmatically overriding the default base image from `.ko.yaml`. - Add UserAgent to BuildOptions and PublishOptions This enables programmatically overriding the `User-Agent` HTTP request header for both pulling the base image and pushing the built image. - Rename MakeBuilder to NewBuilder and MakePublisher to NewPublisher. For more idiomatic constructor function names.
- Loading branch information
Showing
8 changed files
with
299 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Copyright 2021 Google LLC All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package commands | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/google/ko/pkg/commands/options" | ||
) | ||
|
||
func TestOverrideDefaultBaseImageUsingBuildOption(t *testing.T) { | ||
wantDigest := "sha256:76c39a6f76890f8f8b026f89e081084bc8c64167d74e6c93da7a053cb4ccb5dd" | ||
wantImage := "gcr.io/distroless/static-debian9@" + wantDigest | ||
bo := &options.BuildOptions{ | ||
BaseImage: wantImage, | ||
} | ||
|
||
baseFn := getBaseImage("all", bo) | ||
res, err := baseFn(context.Background(), "ko://example.com/helloworld") | ||
if err != nil { | ||
t.Fatalf("getBaseImage(): %v", err) | ||
} | ||
|
||
digest, err := res.Digest() | ||
if err != nil { | ||
t.Fatalf("res.Digest(): %v", err) | ||
} | ||
gotDigest := digest.String() | ||
if gotDigest != wantDigest { | ||
t.Errorf("got digest %s, wanted %s", gotDigest, wantDigest) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
Copyright 2021 Google LLC All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package commands | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/google/ko/pkg/build" | ||
"github.com/google/ko/pkg/commands/options" | ||
) | ||
|
||
func TestPublishImages(t *testing.T) { | ||
repo := "registry.example.com/repository" | ||
sampleAppDir, err := sampleAppRelDir() | ||
if err != nil { | ||
t.Fatalf("sampleAppRelDir(): %v", err) | ||
} | ||
tests := []struct { | ||
description string | ||
publishArg string | ||
importpath string | ||
}{ | ||
{ | ||
description: "import path with ko scheme", | ||
publishArg: "ko://github.com/google/ko/test", | ||
importpath: "github.com/google/ko/test", | ||
}, | ||
{ | ||
description: "import path without ko scheme", | ||
publishArg: "github.com/google/ko/test", | ||
importpath: "github.com/google/ko/test", | ||
}, | ||
{ | ||
description: "file path", | ||
publishArg: sampleAppDir, | ||
importpath: "github.com/google/ko/test", | ||
}, | ||
} | ||
for _, test := range tests { | ||
ctx := context.Background() | ||
bo := &options.BuildOptions{ | ||
ConcurrentBuilds: 1, | ||
} | ||
builder, err := NewBuilder(ctx, bo) | ||
if err != nil { | ||
t.Fatalf("%s: MakeBuilder(): %v", test.description, err) | ||
} | ||
po := &options.PublishOptions{ | ||
DockerRepo: repo, | ||
PreserveImportPaths: true, | ||
} | ||
publisher, err := NewPublisher(po) | ||
if err != nil { | ||
t.Fatalf("%s: MakePublisher(): %v", test.description, err) | ||
} | ||
importpathWithScheme := build.StrictScheme + test.importpath | ||
refs, err := PublishImages(ctx, []string{test.publishArg}, publisher, builder) | ||
if err != nil { | ||
t.Fatalf("%s: PublishImages(): %v", test.description, err) | ||
} | ||
ref, exists := refs[importpathWithScheme] | ||
if !exists { | ||
t.Errorf("%s: could not find image for importpath %s", test.description, importpathWithScheme) | ||
} | ||
gotImageName := ref.Context().Name() | ||
wantImageName := strings.ToLower(fmt.Sprintf("%s/%s", repo, test.importpath)) | ||
if gotImageName != wantImageName { | ||
t.Errorf("%s: got %s, wanted %s", test.description, gotImageName, wantImageName) | ||
} | ||
} | ||
} | ||
|
||
func sampleAppRelDir() (string, error) { | ||
_, filename, _, ok := runtime.Caller(0) | ||
if !ok { | ||
return "", fmt.Errorf("could not get current filename") | ||
} | ||
basepath := filepath.Dir(filename) | ||
testAppDir := filepath.Join(basepath, "..", "..", "test") | ||
return filepath.Rel(basepath, testAppDir) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.