forked from containers/common
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libimage: image lookup: check platform
Check the platform when looking up images locally. When the user requested a custom platform and a local image doesn't match, the image will be discarded. Otherwise a warning will be emitted. Also refactor the code to make it more maintainable in the future. Fixes: containers/podman/issues/12682 Signed-off-by: Valentin Rothberg <[email protected]>
- Loading branch information
Showing
5 changed files
with
106 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package libimage | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"runtime" | ||
) | ||
|
||
func toPlatformString(architecture, os, variant string) string { | ||
if variant == "" { | ||
return fmt.Sprintf("%s/%s", os, architecture) | ||
} | ||
return fmt.Sprintf("%s/%s/%s", os, architecture, variant) | ||
} | ||
|
||
// Checks whether the image matches the specified platform. | ||
// Returns | ||
// * 1) a matching error that can be used for logging (or returning) what does not match | ||
// * 2) a bool indicating whether architecture, os or variant were set (some callers need that to decide whether they need to throw an error) | ||
// * 3) a fatal error that occured prior to check for matches (e.g., storage errors etc.) | ||
func (i *Image) matchesPlatform(ctx context.Context, architecture, os, variant string) (error, bool, error) { | ||
customPlatform := len(architecture)+len(os)+len(variant) != 0 | ||
|
||
if len(architecture) == 0 { | ||
architecture = runtime.GOARCH | ||
} | ||
if len(os) == 0 { | ||
os = runtime.GOOS | ||
} | ||
|
||
inspectInfo, err := i.inspectInfo(ctx) | ||
if err != nil { | ||
return nil, customPlatform, fmt.Errorf("inspecting image: %w", err) | ||
} | ||
|
||
matches := true | ||
switch { | ||
case architecture != inspectInfo.Architecture: | ||
matches = false | ||
case os != inspectInfo.Os: | ||
matches = false | ||
case variant != "" && variant != inspectInfo.Variant: | ||
matches = false | ||
} | ||
|
||
if matches { | ||
return nil, customPlatform, nil | ||
} | ||
|
||
imagePlatform := toPlatformString(inspectInfo.Architecture, inspectInfo.Os, inspectInfo.Variant) | ||
expectedPlatform := toPlatformString(architecture, os, variant) | ||
return fmt.Errorf("image platform (%s) does not match the expected platform (%s)", imagePlatform, expectedPlatform), customPlatform, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package libimage | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestToPlatformString(t *testing.T) { | ||
for _, test := range []struct { | ||
arch, os, variant, expected string | ||
}{ | ||
{"a", "b", "", "b/a"}, | ||
{"a", "b", "c", "b/a/c"}, | ||
{"", "", "c", "//c"}, // callers are responsible for the input | ||
} { | ||
platform := toPlatformString(test.arch, test.os, test.variant) | ||
require.Equal(t, platform, test.expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters