commit
6e3dfe72cb
@ -1,32 +0,0 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/containerd/containerd/platforms"
|
||||
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
)
|
||||
|
||||
func ParsePlatformSpecs(platformsStr []string) ([]specs.Platform, error) {
|
||||
if len(platformsStr) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
out := make([]specs.Platform, 0, len(platformsStr))
|
||||
for _, s := range platformsStr {
|
||||
parts := strings.Split(s, ",")
|
||||
if len(parts) > 1 {
|
||||
p, err := ParsePlatformSpecs(parts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, p...)
|
||||
continue
|
||||
}
|
||||
p, err := platforms.Parse(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, platforms.Normalize(p))
|
||||
}
|
||||
return out, nil
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tonistiigi/buildx/util/platformutil"
|
||||
)
|
||||
|
||||
func TestNodeGroupUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ng := &NodeGroup{}
|
||||
err := ng.Update("foo", "foo0", []string{"linux/amd64"}, true, false)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = ng.Update("foo1", "foo1", []string{"linux/arm64", "linux/arm/v7"}, true, true)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, len(ng.Nodes), 2)
|
||||
|
||||
// update
|
||||
err = ng.Update("foo", "foo2", []string{"linux/amd64", "linux/arm"}, true, false)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, len(ng.Nodes), 2)
|
||||
require.Equal(t, []string{"linux/amd64", "linux/arm/v7"}, platformutil.Format(ng.Nodes[0].Platforms))
|
||||
require.Equal(t, []string{"linux/arm64"}, platformutil.Format(ng.Nodes[1].Platforms))
|
||||
|
||||
require.Equal(t, "foo2", ng.Nodes[0].Endpoint)
|
||||
|
||||
// duplicate endpoint
|
||||
err = ng.Update("foo1", "foo2", nil, true, false)
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "duplicate endpoint")
|
||||
|
||||
err = ng.Leave("foo")
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, len(ng.Nodes), 1)
|
||||
require.Equal(t, []string{"linux/arm64"}, platformutil.Format(ng.Nodes[0].Platforms))
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package platformutil
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/containerd/containerd/platforms"
|
||||
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
)
|
||||
|
||||
func Parse(platformsStr []string) ([]specs.Platform, error) {
|
||||
if len(platformsStr) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
out := make([]specs.Platform, 0, len(platformsStr))
|
||||
for _, s := range platformsStr {
|
||||
parts := strings.Split(s, ",")
|
||||
if len(parts) > 1 {
|
||||
p, err := Parse(parts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, p...)
|
||||
continue
|
||||
}
|
||||
p, err := platforms.Parse(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, platforms.Normalize(p))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func Dedupe(in []specs.Platform) []specs.Platform {
|
||||
m := map[string]struct{}{}
|
||||
out := make([]specs.Platform, 0, len(in))
|
||||
for _, p := range in {
|
||||
p := platforms.Normalize(p)
|
||||
key := platforms.Format(p)
|
||||
if _, ok := m[key]; ok {
|
||||
continue
|
||||
}
|
||||
m[key] = struct{}{}
|
||||
out = append(out, p)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func Format(in []specs.Platform) []string {
|
||||
if len(in) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]string, 0, len(in))
|
||||
for _, p := range in {
|
||||
out = append(out, platforms.Format(p))
|
||||
}
|
||||
return out
|
||||
}
|
Loading…
Reference in New Issue