Skip to content

Commit

Permalink
Add matching based on platform.features and platform.compatibilities
Browse files Browse the repository at this point in the history
Signed-off-by: Marcin Franczyk <[email protected]>
  • Loading branch information
mfranczy committed Jul 8, 2024
1 parent db76a43 commit 79aa68f
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 19 deletions.
36 changes: 21 additions & 15 deletions compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ func platformVector(platform specs.Platform) []specs.Platform {
if amd64Version, err := strconv.Atoi(strings.TrimPrefix(platform.Variant, "v")); err == nil && amd64Version > 1 {
for amd64Version--; amd64Version >= 1; amd64Version-- {
vector = append(vector, specs.Platform{
Architecture: platform.Architecture,
OS: platform.OS,
OSVersion: platform.OSVersion,
OSFeatures: platform.OSFeatures,
Variant: "v" + strconv.Itoa(amd64Version),
Architecture: platform.Architecture,
OS: platform.OS,
OSVersion: platform.OSVersion,
OSFeatures: platform.OSFeatures,
Variant: "v" + strconv.Itoa(amd64Version),
Features: platform.Features,
Compatibilities: platform.Compatibilities,
})
}
}
Expand All @@ -59,11 +61,13 @@ func platformVector(platform specs.Platform) []specs.Platform {
if armVersion, err := strconv.Atoi(strings.TrimPrefix(platform.Variant, "v")); err == nil && armVersion > 5 {
for armVersion--; armVersion >= 5; armVersion-- {
vector = append(vector, specs.Platform{
Architecture: platform.Architecture,
OS: platform.OS,
OSVersion: platform.OSVersion,
OSFeatures: platform.OSFeatures,
Variant: "v" + strconv.Itoa(armVersion),
Architecture: platform.Architecture,
OS: platform.OS,
OSVersion: platform.OSVersion,
OSFeatures: platform.OSFeatures,
Variant: "v" + strconv.Itoa(armVersion),
Features: platform.Features,
Compatibilities: platform.Compatibilities,
})
}
}
Expand All @@ -73,11 +77,13 @@ func platformVector(platform specs.Platform) []specs.Platform {
variant = "v8"
}
vector = append(vector, platformVector(specs.Platform{
Architecture: "arm",
OS: platform.OS,
OSVersion: platform.OSVersion,
OSFeatures: platform.OSFeatures,
Variant: variant,
Architecture: "arm",
OS: platform.OS,
OSVersion: platform.OSVersion,
OSFeatures: platform.OSFeatures,
Variant: variant,
Features: platform.Features,
Compatibilities: platform.Compatibilities,
})...)
}

Expand Down
12 changes: 12 additions & 0 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,15 @@ func normalizeArch(arch, variant string) (string, string) {

return arch, variant
}

func normalizeFeatures(features []string) {
for i, f := range features {
features[i] = strings.ToLower(f)
}
}

func normalizeCompatibilities(compat map[string]string) {
for k, v := range compat {
compat[k] = strings.ToLower(v)
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ require (
github.com/Microsoft/hcsshim v0.10.0
github.com/containerd/log v0.1.0
github.com/opencontainers/image-spec v1.1.0-rc5
github.com/stretchr/testify v1.8.4
github.com/pelletier/go-toml/v2 v2.2.2
github.com/stretchr/testify v1.9.0
golang.org/x/sys v0.10.0
)

Expand Down
10 changes: 9 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0-rc5 h1:Ygwkfw9bpDvs+c9E34SdgGOj41dX/cbdlwvlWt0pnFI=
github.com/opencontainers/image-spec v1.1.0-rc5/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
28 changes: 27 additions & 1 deletion platforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,37 @@ func NewMatcher(platform specs.Platform) Matcher {

type matcher struct {
specs.Platform

// featuresSet contains normalized set of features built from platform.features
featuresSet map[string]bool
}

func (m *matcher) Match(platform specs.Platform) bool {
normalized := Normalize(platform)
return m.OS == normalized.OS &&

mo := m.OS == normalized.OS &&
m.Architecture == normalized.Architecture &&
m.Variant == normalized.Variant

mf := true
for _, f := range normalized.Features {
if !m.featuresSet[f] {
mf = false
break
}
}

mc := true
for k, nv := range normalized.Compatibilities {
if v, ok := m.Compatibilities[k]; ok && v == nv {
continue
}

mc = false
break
}

return mo && mf && mc
}

func (m *matcher) String() string {
Expand Down Expand Up @@ -285,6 +309,8 @@ func Format(platform specs.Platform) string {
func Normalize(platform specs.Platform) specs.Platform {
platform.OS = normalizeOS(platform.OS)
platform.Architecture, platform.Variant = normalizeArch(platform.Architecture, platform.Variant)
normalizeFeatures(platform.Features)
normalizeCompatibilities(platform.Compatibilities)

return platform
}
55 changes: 55 additions & 0 deletions platforms_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright The containerd 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 platforms

import (
"os"

toml "github.com/pelletier/go-toml/v2"
)

const platformConfigPath = "/etc/containerd/platform-config.toml"

var config *platformConfig

type platformConfig struct {
Features []string `json:"features,omitempty"`
Compatibilities map[string]string `json:"compatibilities,omitempty"`
}

func readConfig() (*platformConfig, error) {
if config == nil {
c := &platformConfig{}
b, err := os.ReadFile(platformConfigPath)
if err != nil {
return nil, err
}
if err := toml.Unmarshal(b, c); err != nil {
return nil, err
}
config = c
}
return config, nil
}

func mustReadConfig() *platformConfig {
p, err := readConfig()
if err != nil {
panic(err)
}
return p
}
15 changes: 14 additions & 1 deletion platforms_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,22 @@ import (

// NewMatcher returns the default Matcher for containerd
func newDefaultMatcher(platform specs.Platform) Matcher {
return &matcher{
m := &matcher{
Platform: Normalize(platform),
}

p := mustReadConfig()
m.Platform.Features = p.Features
m.Platform.Compatibilities = p.Compatibilities

if fs := m.Platform.Features; len(fs) > 0 {
m.featuresSet = make(map[string]bool, len(fs))
for _, f := range fs {
m.featuresSet[f] = true
}
}

return m
}

func GetWindowsOsVersion() string {
Expand Down

0 comments on commit 79aa68f

Please sign in to comment.