Change condition for the behavior when --no-push=true without --destinations (#2676)
This commit changes the condition check for the behavior when no-push is set to true while destinations are needed. Prior this change, users would have to set destinations even when noPush option is set to true. More specifically, a workaround for tar files to be generated when --no-push is true and destinations is empty is provided where a dummy destination would be set.
This commit is contained in:
parent
2ca710a2a2
commit
6ee84f128d
|
|
@ -53,11 +53,13 @@ type withUserAgent struct {
|
||||||
|
|
||||||
// for testing
|
// for testing
|
||||||
var (
|
var (
|
||||||
newRetry = transport.NewRetry
|
newRetry = transport.NewRetry
|
||||||
|
DummyDestinations = []string{DummyDestination}
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
UpstreamClientUaKey = "UPSTREAM_CLIENT_TYPE"
|
UpstreamClientUaKey = "UPSTREAM_CLIENT_TYPE"
|
||||||
|
DummyDestination = "docker.io/unset-repo/unset-image-name"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (w *withUserAgent) RoundTrip(r *http.Request) (*http.Response, error) {
|
func (w *withUserAgent) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||||
|
|
@ -145,11 +147,18 @@ func writeDigestFile(path string, digestByteArray []byte) error {
|
||||||
return ioutil.WriteFile(path, digestByteArray, 0644)
|
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 {
|
func DoPush(image v1.Image, opts *config.KanikoOptions) error {
|
||||||
t := timing.Start("Total Push Time")
|
t := timing.Start("Total Push Time")
|
||||||
var digestByteArray []byte
|
var digestByteArray []byte
|
||||||
var builder strings.Builder
|
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 != "" {
|
if opts.DigestFile != "" || opts.ImageNameDigestFile != "" || opts.ImageNameTagDigestFile != "" {
|
||||||
var err error
|
var err error
|
||||||
digestByteArray, err = getDigest(image)
|
digestByteArray, err = getDigest(image)
|
||||||
|
|
@ -175,6 +184,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{}
|
destRefs := []name.Tag{}
|
||||||
for _, destination := range opts.Destinations {
|
for _, destination := range opts.Destinations {
|
||||||
destRef, err := name.NewTag(destination, name.WeakValidation)
|
destRef, err := name.NewTag(destination, name.WeakValidation)
|
||||||
|
|
@ -210,10 +225,6 @@ func DoPush(image v1.Image, opts *config.KanikoOptions) error {
|
||||||
if opts.TarPath != "" {
|
if opts.TarPath != "" {
|
||||||
tagToImage := map[name.Tag]v1.Image{}
|
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 {
|
for _, destRef := range destRefs {
|
||||||
tagToImage[destRef] = image
|
tagToImage[destRef] = image
|
||||||
}
|
}
|
||||||
|
|
@ -364,3 +375,9 @@ func pushLayerToCache(opts *config.KanikoOptions, cacheKey string, tarPath strin
|
||||||
}
|
}
|
||||||
return DoPush(empty, &cacheOpts)
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
func TestImageNameTagDigestFile(t *testing.T) {
|
||||||
image, err := random.Image(1024, 4)
|
image, err := random.Image(1024, 4)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue