-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include tags when --push=false is set
- Refactor noop into its own publisher. - Use :tag@sha format in digest when --push=false is set. - Allow --tag-only to be used with --push=false. This includes validations that prevent --tag-only to be used with no tags or the `latest` tag. Signed-off-by: Adam Kaplan <adam@adambkaplan.com>
- Loading branch information
1 parent
84356b8
commit f508b06
Showing
5 changed files
with
317 additions
and
22 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
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) | ||
|
||
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