Skip to content

Commit

Permalink
Avoid panic caused by MultiPublisher (#216)
Browse files Browse the repository at this point in the history
MultiPublisher now returns an error when it's configured with no
publishers, and resolver.go now appends a nop publisher when it's
configured not to publish, that simulates a publish without actually
pushing any images.

.
  • Loading branch information
imjasonh authored Oct 6, 2020
1 parent dc30efc commit e780390
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
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")
}
}

0 comments on commit e780390

Please sign in to comment.