-
Notifications
You must be signed in to change notification settings - Fork 407
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
Include tags when --push=false is set #689
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,99 @@ | ||
// Copyright 2022 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 publish | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/google/go-containerregistry/pkg/name" | ||
"github.com/google/ko/pkg/build" | ||
) | ||
|
||
// Original spelling was preserved when this was refactored out of pkg/commands | ||
type nopPublisher struct { | ||
repoName string | ||
namer Namer | ||
tag string | ||
tagOnly bool | ||
} | ||
|
||
type noOpOpener struct { | ||
repoName string | ||
namer Namer | ||
tags []string | ||
tagOnly bool | ||
} | ||
|
||
// NoOpOption provides functional options to the NoOp publisher. | ||
type NoOpOption func(*noOpOpener) | ||
imjasonh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
func (o *noOpOpener) Open() (Interface, error) { | ||
tag := defaultTags[0] | ||
if o.tagOnly { | ||
// Replicate the tag-only validations in the default publisher | ||
if len(o.tags) != 1 { | ||
return nil, errors.New("must specify exactly one tag to resolve images into tag-only references") | ||
} | ||
if o.tags[0] == defaultTags[0] { | ||
return nil, errors.New("latest tag cannot be used in tag-only references") | ||
} | ||
} | ||
// If one or more tags are specified, use the first tag in the list | ||
if len(o.tags) >= 1 { | ||
tag = o.tags[0] | ||
} | ||
return &nopPublisher{ | ||
repoName: o.repoName, | ||
namer: o.namer, | ||
tag: tag, | ||
tagOnly: o.tagOnly, | ||
}, nil | ||
} | ||
|
||
// NewNoOp returns a publisher.Interface that simulates publishing without actually publishing | ||
// anything, to provide fallback behavior when the user configures no push destinations. | ||
func NewNoOp(baseName string, options ...NoOpOption) (Interface, error) { | ||
nop := &noOpOpener{ | ||
repoName: baseName, | ||
namer: identity, | ||
} | ||
for _, option := range options { | ||
option(nop) | ||
} | ||
return nop.Open() | ||
} | ||
|
||
// Publish implements publish.Interface | ||
func (n *nopPublisher) Publish(_ context.Context, br build.Result, s string) (name.Reference, error) { | ||
s = strings.TrimPrefix(s, build.StrictScheme) | ||
h, err := br.Digest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
// If the tag is not empty or is not "latest", use the :tag@sha suffix | ||
if n.tag != "" || n.tag != defaultTags[0] { | ||
// If tag only, just return the tag | ||
if n.tagOnly { | ||
return name.NewTag(fmt.Sprintf("%s:%s", n.namer(n.repoName, s), n.tag)) | ||
} | ||
return name.NewDigest(fmt.Sprintf("%s:%s@%s", n.namer(n.repoName, s), n.tag, h)) | ||
} | ||
return name.NewDigest(fmt.Sprintf("%s@%s", n.namer(n.repoName, s), h)) | ||
} | ||
|
||
func (n *nopPublisher) Close() error { return nil } |
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,187 @@ | ||
// Copyright 2022 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 publish_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/google/go-containerregistry/pkg/v1/random" | ||
"github.com/google/ko/pkg/build" | ||
"github.com/google/ko/pkg/publish" | ||
) | ||
|
||
func TestNoOp(t *testing.T) { | ||
repoName := "quarkus.io/charm" | ||
importPath := "crane" | ||
noop, err := publish.NewNoOp(repoName) | ||
if err != nil { | ||
t.Fatalf("NewNoOp() = %v", err) | ||
} | ||
img, err := random.Image(1024, 1) | ||
if err != nil { | ||
t.Fatalf("random.Image() = %v", err) | ||
} | ||
ref, err := noop.Publish(context.TODO(), img, build.StrictScheme+importPath) | ||
if err != nil { | ||
t.Fatalf("Publish() = %v", err) | ||
} | ||
if !strings.HasPrefix(ref.String(), repoName) { | ||
t.Errorf("Publish() = %v, wanted preifx %s", ref, repoName) | ||
} | ||
} | ||
|
||
func TestNoOpWithCustomNamer(t *testing.T) { | ||
repoName := "quarkus.io/charm" | ||
importPath := "crane" | ||
noop, err := publish.NewNoOp(repoName, publish.NoOpWithNamer(md5Hash)) | ||
if err != nil { | ||
t.Fatalf("NewNoOp() = %v", err) | ||
} | ||
img, err := random.Image(1024, 1) | ||
if err != nil { | ||
t.Fatalf("random.Image() = %v", err) | ||
} | ||
ref, err := noop.Publish(context.TODO(), img, build.StrictScheme+importPath) | ||
if err != nil { | ||
t.Fatalf("Publish() = %v", err) | ||
} | ||
if !strings.HasPrefix(ref.String(), repoName) { | ||
t.Errorf("Publish() = %v, wanted preifx %s", ref, repoName) | ||
} | ||
if !strings.HasSuffix(ref.Context().String(), md5Hash("", strings.ToLower(importPath))) { | ||
t.Errorf("Publish() = %v, wanted suffix %v", ref.Context(), md5Hash("", importPath)) | ||
} | ||
} | ||
|
||
func TestNoOpWithTags(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
tags []string | ||
expectedTag string | ||
expectOpenError bool | ||
}{ | ||
{ | ||
name: "no tags", | ||
}, | ||
{ | ||
name: "latest tag", | ||
tags: []string{"latest"}, | ||
}, | ||
{ | ||
name: "multiple tags", | ||
tags: []string{"v0.1", "v0.1.1"}, | ||
expectedTag: "v0.1", | ||
}, | ||
{ | ||
name: "single tag", | ||
tags: []string{"v0.1"}, | ||
expectedTag: "v0.1", | ||
}, | ||
} | ||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
repoName := "quarkus.io/charm" | ||
importPath := "crane" | ||
noop, err := publish.NewNoOp(repoName, publish.NoOpWithTags(tc.tags)) | ||
if tc.expectOpenError { | ||
if err == nil { | ||
t.Error("NewNoOp() - expected error, got none") | ||
} | ||
return | ||
} | ||
if err != nil { | ||
t.Fatalf("NewNoOp() = %v", err) | ||
} | ||
img, err := random.Image(1024, 1) | ||
if err != nil { | ||
t.Fatalf("random.Image() = %v", err) | ||
} | ||
ref, err := noop.Publish(context.TODO(), img, build.StrictScheme+importPath) | ||
if err != nil { | ||
t.Fatalf("Publish() = %v", err) | ||
} | ||
if !strings.HasPrefix(ref.String(), repoName) { | ||
t.Errorf("Publish() = %v, wanted preifx %s", ref, repoName) | ||
} | ||
if tc.expectedTag != "" && !strings.Contains(ref.String(), fmt.Sprintf(":%s@", tc.expectedTag)) { | ||
t.Errorf("Publish() = %v, expected tag %s", ref.String(), tc.expectedTag) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNoOpWithTagOnly(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
tags []string | ||
expectedTag string | ||
expectOpenError bool | ||
}{ | ||
{ | ||
name: "no tags", | ||
expectOpenError: true, | ||
}, | ||
{ | ||
name: "latest tag", | ||
tags: []string{"latest"}, | ||
expectOpenError: true, | ||
}, | ||
{ | ||
name: "multiple tags", | ||
tags: []string{"v0.1", "v0.1.1"}, | ||
expectOpenError: true, | ||
}, | ||
{ | ||
name: "single tag", | ||
tags: []string{"v0.1"}, | ||
expectedTag: "v0.1", | ||
}, | ||
} | ||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
repoName := "quarkus.io/charm" | ||
importPath := "crane" | ||
noop, err := publish.NewNoOp(repoName, | ||
publish.NoOpWithTags(tc.tags), | ||
publish.NoOpWithTagOnly(true)) | ||
if tc.expectOpenError { | ||
if err == nil { | ||
t.Error("NewNoOp() - expected error, got none") | ||
} | ||
return | ||
} | ||
if err != nil { | ||
t.Fatalf("NewNoOp() = %v", err) | ||
} | ||
img, err := random.Image(1024, 1) | ||
if err != nil { | ||
t.Fatalf("random.Image() = %v", err) | ||
} | ||
ref, err := noop.Publish(context.TODO(), img, build.StrictScheme+importPath) | ||
if err != nil { | ||
t.Fatalf("Publish() = %v", err) | ||
} | ||
if !strings.HasPrefix(ref.String(), repoName) { | ||
t.Errorf("Publish() = %v, wanted preifx %s", ref, repoName) | ||
} | ||
if tc.expectedTag != "" && !strings.HasSuffix(ref.String(), fmt.Sprintf(":%s", tc.expectedTag)) { | ||
t.Errorf("Publish() = %v, expected only tag %s", ref.String(), tc.expectedTag) | ||
} | ||
}) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless the intention here is to export more functionality around no-op publishing, I'd kind of like to avoid increasing the public surface area of this change. We can do that just by passing more fields to
nopPublisher
and not exporting anything new.If the intention is to export more no-op publishing functionality, I'd like to understand more about the use case so we can make sure this change helps the issue.
In any case I think we can get this in before v0.12 and the repo move, if you're interested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there is any further no-op publishing functionality to export.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I'm saying is, this change exports new methods in
pkg/publish
to enable no-op publishing. But that's not necessary just to get the CLI behavior you're proposing -- we could do that just by passing more options into the (unexported)nopPublisher
struct.I think we should try to get this CLI behavior change done without exporting new methods, since once we export it, we're married to it forever. AFAIK there aren't a lot of use cases for no-op publishing, vs local publishing or tarball publishing, which are better supported don't-push-to-a-registry options, except in this very narrow use case.