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

Don't panic when MultiPublisher is given zero publishers #216

Merged
merged 1 commit into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions pkg/commands/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) {
}
publishers = append(publishers, dp)
}

// If not publishing, at least generate a digest to simulate
// publishing.
if len(publishers) == 0 {
publishers = append(publishers, nopPublisher{
repoName: repoName,
namer: namer,
})
}

return publish.MultiPublisher(publishers...), nil
}()
if err != nil {
Expand All @@ -170,6 +180,23 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) {
return publish.NewCaching(innerPublisher)
}

// nopPublisher simulates publishing without actually publishing anything, to
// provide fallback behavior when the user configures no push destinations.
type nopPublisher struct {
repoName string
namer publish.Namer
}

func (n nopPublisher) Publish(br build.Result, s string) (name.Reference, error) {
h, err := br.Digest()
if err != nil {
return nil, err
}
return name.NewDigest(fmt.Sprintf("%s/%s@%s", n.repoName, n.namer(s), h))
}

func (n nopPublisher) Close() error { return nil }

// resolvedFuture represents a "future" for the bytes of a resolved file.
type resolvedFuture chan []byte

Expand Down
7 changes: 6 additions & 1 deletion pkg/publish/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package publish

import (
"errors"

"github.com/google/go-containerregistry/pkg/name"
"github.com/google/ko/pkg/build"
)
Expand All @@ -34,13 +36,16 @@ type multiPublisher struct {

// Publish implements publish.Interface.
func (p *multiPublisher) Publish(br build.Result, s string) (ref name.Reference, err error) {
if len(p.publishers) == 0 {
return nil, errors.New("MultiPublisher configured with zero publishers")
}

for _, pub := range p.publishers {
ref, err = pub.Publish(br, s)
if err != nil {
return
}
}

return
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/publish/multi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,15 @@ func TestMulti(t *testing.T) {
t.Errorf("Close() = %v", err)
}
}

func TestMulti_Zero(t *testing.T) {
img, err := random.Image(1024, 1)
if err != nil {
t.Fatalf("random.Image() = %v", err)
}

p := MultiPublisher() // No publishers.
if _, err := p.Publish(img, "foo"); err == nil {
t.Errorf("Publish() got nil error")
}
}