mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-09 21:17:09 +08:00
commands: add platforms dedupe
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
@ -3,7 +3,10 @@ package store
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/containerd/containerd/platforms"
|
||||
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tonistiigi/buildx/util/platformutil"
|
||||
)
|
||||
|
||||
type NodeGroup struct {
|
||||
@ -15,7 +18,7 @@ type NodeGroup struct {
|
||||
type Node struct {
|
||||
Name string
|
||||
Endpoint string
|
||||
Platforms []string
|
||||
Platforms []specs.Platform
|
||||
}
|
||||
|
||||
func (ng *NodeGroup) Leave(name string) error {
|
||||
@ -38,16 +41,22 @@ func (ng *NodeGroup) Update(name, endpoint string, platforms []string, endpoints
|
||||
}
|
||||
ng.Nodes = nil
|
||||
}
|
||||
|
||||
pp, err := platformutil.Parse(platforms)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if i != -1 {
|
||||
n := ng.Nodes[i]
|
||||
if endpointsSet {
|
||||
n.Endpoint = endpoint
|
||||
}
|
||||
if len(platforms) > 0 {
|
||||
n.Platforms = platforms
|
||||
n.Platforms = pp
|
||||
}
|
||||
ng.Nodes[i] = n
|
||||
if err := ng.validateDuplicates(endpoint); err != nil {
|
||||
if err := ng.validateDuplicates(endpoint, i); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@ -57,7 +66,7 @@ func (ng *NodeGroup) Update(name, endpoint string, platforms []string, endpoints
|
||||
name = ng.nextNodeName()
|
||||
}
|
||||
|
||||
name, err := ValidateName(name)
|
||||
name, err = ValidateName(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -65,18 +74,17 @@ func (ng *NodeGroup) Update(name, endpoint string, platforms []string, endpoints
|
||||
n := Node{
|
||||
Name: name,
|
||||
Endpoint: endpoint,
|
||||
Platforms: platforms,
|
||||
Platforms: pp,
|
||||
}
|
||||
ng.Nodes = append(ng.Nodes, n)
|
||||
|
||||
if err := ng.validateDuplicates(endpoint); err != nil {
|
||||
if err := ng.validateDuplicates(endpoint, len(ng.Nodes)-1); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ng *NodeGroup) validateDuplicates(ep string) error {
|
||||
// TODO: reset platforms
|
||||
func (ng *NodeGroup) validateDuplicates(ep string, idx int) error {
|
||||
i := 0
|
||||
for _, n := range ng.Nodes {
|
||||
if n.Endpoint == ep {
|
||||
@ -86,6 +94,19 @@ func (ng *NodeGroup) validateDuplicates(ep string) error {
|
||||
if i > 1 {
|
||||
return errors.Errorf("invalid duplicate endpoint %s", ep)
|
||||
}
|
||||
|
||||
m := map[string]struct{}{}
|
||||
for _, p := range ng.Nodes[idx].Platforms {
|
||||
m[platforms.Format(p)] = struct{}{}
|
||||
}
|
||||
|
||||
for i := range ng.Nodes {
|
||||
if i == idx {
|
||||
continue
|
||||
}
|
||||
ng.Nodes[i].Platforms = filterPlatforms(ng.Nodes[i].Platforms, m)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -109,3 +130,13 @@ func (ng *NodeGroup) nextNodeName() string {
|
||||
return name
|
||||
}
|
||||
}
|
||||
|
||||
func filterPlatforms(in []specs.Platform, m map[string]struct{}) []specs.Platform {
|
||||
out := make([]specs.Platform, 0, len(in))
|
||||
for _, p := range in {
|
||||
if _, ok := m[platforms.Format(p)]; !ok {
|
||||
out = append(out, p)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
42
store/nodegroup_test.go
Normal file
42
store/nodegroup_test.go
Normal file
@ -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))
|
||||
}
|
Reference in New Issue
Block a user