Do not re-parse the list of search registries just for an error message

... when we even only count them.

This eliminates a rare error case, and saves time re-reading and re-parsing
the input.

(We still compute registryPath redundantly, and it may get out of sync.)

Should not change behavior (but does not add unit tests).

Signed-off-by: Miloslav Trmač <mitr@redhat.com>

Closes: #1176
Approved by: rhatdan
This commit is contained in:
Miloslav Trmač
2018-07-28 06:42:43 +02:00
committed by Atomic Bot
parent 8d73e45663
commit dbe2395769
2 changed files with 17 additions and 9 deletions

View File

@ -60,6 +60,7 @@ type pullGoal struct {
refPairs []pullRefPair refPairs []pullRefPair
pullAllPairs bool // Pull all refPairs instead of stopping on first success. pullAllPairs bool // Pull all refPairs instead of stopping on first success.
usedSearchRegistries bool // refPairs construction has depended on registries.GetRegistries() usedSearchRegistries bool // refPairs construction has depended on registries.GetRegistries()
searchedRegistries []string // The list of search registries used; set only if usedSearchRegistries
} }
// pullRefName records a prepared source reference and a destination name to pull. // pullRefName records a prepared source reference and a destination name to pull.
@ -74,6 +75,7 @@ type pullGoalNames struct {
refNames []pullRefName refNames []pullRefName
pullAllPairs bool // Pull all refNames instead of stopping on first success. pullAllPairs bool // Pull all refNames instead of stopping on first success.
usedSearchRegistries bool // refPairs construction has depended on registries.GetRegistries() usedSearchRegistries bool // refPairs construction has depended on registries.GetRegistries()
searchedRegistries []string // The list of search registries used; set only if usedSearchRegistries
} }
func singlePullRefNameGoal(rn pullRefName) *pullGoalNames { func singlePullRefNameGoal(rn pullRefName) *pullGoalNames {
@ -81,6 +83,7 @@ func singlePullRefNameGoal(rn pullRefName) *pullGoalNames {
refNames: []pullRefName{rn}, refNames: []pullRefName{rn},
pullAllPairs: false, // Does not really make a difference. pullAllPairs: false, // Does not really make a difference.
usedSearchRegistries: false, usedSearchRegistries: false,
searchedRegistries: nil,
} }
} }
@ -148,6 +151,7 @@ func pullGoalNamesFromImageReference(ctx context.Context, srcRef types.ImageRefe
refNames: res, refNames: res,
pullAllPairs: true, pullAllPairs: true,
usedSearchRegistries: false, usedSearchRegistries: false,
searchedRegistries: nil,
}, nil }, nil
case OCIArchive: case OCIArchive:
@ -260,11 +264,7 @@ func (i *Image) pullImage(ctx context.Context, writer io.Writer, authfile, signa
// If no image was found, we should handle. Lets be nicer to the user and see if we can figure out why. // If no image was found, we should handle. Lets be nicer to the user and see if we can figure out why.
if len(images) == 0 { if len(images) == 0 {
registryPath := sysregistries.RegistriesConfPath(&types.SystemContext{}) registryPath := sysregistries.RegistriesConfPath(&types.SystemContext{})
searchRegistries, err := registries.GetRegistries() if goal.usedSearchRegistries && len(goal.searchedRegistries) == 0 {
if err != nil {
return nil, err
}
if goal.usedSearchRegistries && len(searchRegistries) == 0 {
return nil, errors.Errorf("image name provided is a short name and no search registries are defined in %s.", registryPath) return nil, errors.Errorf("image name provided is a short name and no search registries are defined in %s.", registryPath)
} }
return nil, errors.Errorf("unable to find image in the registries defined in %q", registryPath) return nil, errors.Errorf("unable to find image in the registries defined in %q", registryPath)
@ -334,6 +334,7 @@ func pullGoalNamesFromPossiblyUnqualifiedName(inputName string) (*pullGoalNames,
refNames: pullNames, refNames: pullNames,
pullAllPairs: false, pullAllPairs: false,
usedSearchRegistries: true, usedSearchRegistries: true,
searchedRegistries: searchRegistries,
}, nil }, nil
} }
@ -369,5 +370,6 @@ func (ir *Runtime) pullGoalFromGoalNames(goalNames *pullGoalNames) (pullGoal, er
refPairs: res, refPairs: res,
pullAllPairs: goalNames.pullAllPairs, pullAllPairs: goalNames.pullAllPairs,
usedSearchRegistries: goalNames.usedSearchRegistries, usedSearchRegistries: goalNames.usedSearchRegistries,
searchedRegistries: goalNames.searchedRegistries,
}, nil }, nil
} }

View File

@ -200,6 +200,7 @@ func TestPullGoalNamesFromImageReference(t *testing.T) {
} }
assert.Equal(t, c.expectedPullAllPairs, res.pullAllPairs, c.srcName) assert.Equal(t, c.expectedPullAllPairs, res.pullAllPairs, c.srcName)
assert.False(t, res.usedSearchRegistries, c.srcName) assert.False(t, res.usedSearchRegistries, c.srcName)
assert.Nil(t, res.searchedRegistries, c.srcName)
} }
} }
} }
@ -326,6 +327,11 @@ func TestPullGoalNamesFromPossiblyUnqualifiedName(t *testing.T) {
assert.Equal(t, c.expected, strings, c.input) assert.Equal(t, c.expected, strings, c.input)
assert.False(t, res.pullAllPairs, c.input) assert.False(t, res.pullAllPairs, c.input)
assert.Equal(t, c.expectedUsedSearchRegistries, res.usedSearchRegistries, c.input) assert.Equal(t, c.expectedUsedSearchRegistries, res.usedSearchRegistries, c.input)
if !c.expectedUsedSearchRegistries {
assert.Nil(t, res.searchedRegistries, c.input)
} else {
assert.Equal(t, []string{"example.com", "docker.io"}, res.searchedRegistries, c.input)
}
} }
} }
} }