-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support platform selection on Copy
Signed-off-by: REDMOND\zoeyli <[email protected]>
- Loading branch information
Showing
4 changed files
with
285 additions
and
25 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
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,67 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package platform | ||
|
||
import ( | ||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
) | ||
|
||
// MatchPlatform checks whether the current platform matches the target platform. | ||
// MatchPlatform will return true if: | ||
// - Architecture and OS exactly match. | ||
// - Variant and OSVersion exactly match if target platform provided. | ||
// - OSFeatures of the target platform are the subsets of the OSFeatures array | ||
// of the current platform. | ||
// Note: Variant, OSVersion and OSFeatures are optional fields, will skip the | ||
// comparison if the target platform does not provide specfic value. | ||
func MatchPlatform(curr *ocispec.Platform, target *ocispec.Platform) bool { | ||
if curr.Architecture != target.Architecture || curr.OS != target.OS { | ||
return false | ||
} | ||
|
||
if target.OSVersion != "" && curr.OSVersion != target.OSVersion { | ||
return false | ||
} | ||
|
||
if target.Variant != "" && curr.Variant != target.Variant { | ||
return false | ||
} | ||
|
||
if len(target.OSFeatures) != 0 && !isSubset(curr.OSFeatures, target.OSFeatures) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
// isSubset returns true if all items in target slice are present in current slice | ||
func isSubset(curr, target []string) bool { | ||
if len(curr) < len(target) { | ||
return false | ||
} | ||
|
||
set := make(map[string]bool) | ||
for _, v := range curr { | ||
set[v] = true | ||
} | ||
for _, v := range target { | ||
if _, ok := set[v]; !ok { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
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,102 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package platform | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
) | ||
|
||
func TestMatches(t *testing.T) { | ||
tests := []struct { | ||
curr ocispec.Platform | ||
target ocispec.Platform | ||
want bool | ||
}{{ | ||
ocispec.Platform{Architecture: "amd64", OS: "linux"}, | ||
ocispec.Platform{Architecture: "amd64", OS: "linux"}, | ||
true, | ||
}, { | ||
ocispec.Platform{Architecture: "amd64", OS: "linux"}, | ||
ocispec.Platform{Architecture: "amd64", OS: "LINUX"}, | ||
false, | ||
}, { | ||
ocispec.Platform{Architecture: "amd64", OS: "linux"}, | ||
ocispec.Platform{Architecture: "arm64", OS: "linux"}, | ||
false, | ||
}, { | ||
ocispec.Platform{Architecture: "arm", OS: "linux"}, | ||
ocispec.Platform{Architecture: "arm", OS: "linux", Variant: "v7"}, | ||
false, | ||
}, { | ||
ocispec.Platform{Architecture: "arm", OS: "linux", Variant: "v7"}, | ||
ocispec.Platform{Architecture: "arm", OS: "linux"}, | ||
true, | ||
}, { | ||
ocispec.Platform{Architecture: "arm", OS: "linux", Variant: "v7"}, | ||
ocispec.Platform{Architecture: "arm", OS: "linux", Variant: "v7"}, | ||
true, | ||
}, { | ||
ocispec.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.20348.768"}, | ||
ocispec.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.20348.700"}, | ||
false, | ||
}, { | ||
ocispec.Platform{Architecture: "amd64", OS: "windows"}, | ||
ocispec.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.20348.768"}, | ||
false, | ||
}, { | ||
ocispec.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.20348.768"}, | ||
ocispec.Platform{Architecture: "amd64", OS: "windows"}, | ||
true, | ||
}, { | ||
ocispec.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.20348.768"}, | ||
ocispec.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.20348.768"}, | ||
true, | ||
}, { | ||
ocispec.Platform{Architecture: "arm", OS: "linux", OSFeatures: []string{"a", "d"}}, | ||
ocispec.Platform{Architecture: "arm", OS: "linux", OSFeatures: []string{"a", "c"}}, | ||
false, | ||
}, { | ||
ocispec.Platform{Architecture: "arm", OS: "linux"}, | ||
ocispec.Platform{Architecture: "arm", OS: "linux", OSFeatures: []string{"a"}}, | ||
false, | ||
}, { | ||
ocispec.Platform{Architecture: "arm", OS: "linux", OSFeatures: []string{"a"}}, | ||
ocispec.Platform{Architecture: "arm", OS: "linux"}, | ||
true, | ||
}, { | ||
ocispec.Platform{Architecture: "arm", OS: "linux", OSFeatures: []string{"a", "b"}}, | ||
ocispec.Platform{Architecture: "arm", OS: "linux", OSFeatures: []string{"a", "b"}}, | ||
true, | ||
}, { | ||
ocispec.Platform{Architecture: "arm", OS: "linux", OSFeatures: []string{"a", "d", "c", "b"}}, | ||
ocispec.Platform{Architecture: "arm", OS: "linux", OSFeatures: []string{"d", "c", "a", "b"}}, | ||
true, | ||
}} | ||
|
||
for _, tt := range tests { | ||
currPlatforJSON, _ := json.Marshal(tt.curr) | ||
targetPlatforJSON, _ := json.Marshal(tt.target) | ||
name := string(currPlatforJSON) + string(targetPlatforJSON) | ||
t.Run(name, func(t *testing.T) { | ||
if got := MatchPlatform(&tt.curr, &tt.target); got != tt.want { | ||
t.Errorf("Matches() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |