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

Fix: Change condition for the behaviour when --no-push=true without setting --destinations #2676

Merged
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
29 changes: 23 additions & 6 deletions pkg/executor/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ type withUserAgent struct {

// for testing
var (
newRetry = transport.NewRetry
newRetry = transport.NewRetry
DummyDestinations = []string{DummyDestination}
)

const (
UpstreamClientUaKey = "UPSTREAM_CLIENT_TYPE"
DummyDestination = "docker.io/unset-repo/unset-image-name"
)

func (w *withUserAgent) RoundTrip(r *http.Request) (*http.Response, error) {
Expand Down Expand Up @@ -143,11 +145,18 @@ func writeDigestFile(path string, digestByteArray []byte) error {
return ioutil.WriteFile(path, digestByteArray, 0644)
}

// DoPush is responsible for pushing image to the destinations specified in opts
// DoPush is responsible for pushing image to the destinations specified in opts.
// A dummy destination would be set when --no-push is set to true and --tar-path
// is not empty with empty --destinations.
func DoPush(image v1.Image, opts *config.KanikoOptions) error {
t := timing.Start("Total Push Time")
var digestByteArray []byte
var builder strings.Builder

if !opts.NoPush && len(opts.Destinations) == 0 {
return errors.New("must provide at least one destination to push")
}

if opts.DigestFile != "" || opts.ImageNameDigestFile != "" || opts.ImageNameTagDigestFile != "" {
var err error
digestByteArray, err = getDigest(image)
Expand All @@ -173,6 +182,12 @@ func DoPush(image v1.Image, opts *config.KanikoOptions) error {
}
}

if opts.NoPush && len(opts.Destinations) == 0 {
if opts.TarPath != "" {
setDummyDestinations(opts)
}
}

destRefs := []name.Tag{}
for _, destination := range opts.Destinations {
destRef, err := name.NewTag(destination, name.WeakValidation)
Expand Down Expand Up @@ -208,10 +223,6 @@ func DoPush(image v1.Image, opts *config.KanikoOptions) error {
if opts.TarPath != "" {
tagToImage := map[name.Tag]v1.Image{}

if len(destRefs) == 0 {
return errors.New("must provide at least one destination when tarPath is specified")
}

for _, destRef := range destRefs {
tagToImage[destRef] = image
}
Expand Down Expand Up @@ -362,3 +373,9 @@ func pushLayerToCache(opts *config.KanikoOptions, cacheKey string, tarPath strin
}
return DoPush(empty, &cacheOpts)
}

// setDummyDestinations sets the dummy destinations required to generate new
// tag names for tarPath in DoPush.
func setDummyDestinations(opts *config.KanikoOptions) {
opts.Destinations = DummyDestinations
}
62 changes: 62 additions & 0 deletions pkg/executor/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,68 @@ func TestImageNameDigestFile(t *testing.T) {

}

func TestDoPushWithOpts(t *testing.T) {
tarPath := "image.tar"

for _, tc := range []struct {
name string
opts config.KanikoOptions
expectedErr bool
}{
{
name: "no push with tarPath without destinations",
opts: config.KanikoOptions{
NoPush: true,
TarPath: tarPath,
},
expectedErr: false,
}, {
name: "no push with tarPath with destinations",
opts: config.KanikoOptions{
NoPush: true,
TarPath: tarPath,
Destinations: []string{"image"},
},
expectedErr: false,
}, {
name: "no push with tarPath with destinations empty",
opts: config.KanikoOptions{
NoPush: true,
TarPath: tarPath,
Destinations: []string{},
},
expectedErr: false,
}, {
name: "tarPath with destinations empty",
opts: config.KanikoOptions{
NoPush: false,
TarPath: tarPath,
Destinations: []string{},
},
expectedErr: true,
}} {
t.Run(tc.name, func(t *testing.T) {
image, err := random.Image(1024, 4)
if err != nil {
t.Fatalf("could not create image: %s", err)
}
defer os.Remove("image.tar")

err = DoPush(image, &tc.opts)
if err != nil {
if !tc.expectedErr {
t.Errorf("unexpected error with opts: could not push image: %s", err)
}
} else {
if tc.expectedErr {
t.Error("expected error with opts not found")
}
}

})
}
}

func TestImageNameTagDigestFile(t *testing.T) {
image, err := random.Image(1024, 4)
if err != nil {
Expand Down
Loading