mirror of
https://github.com/siderolabs/talos.git
synced 2025-08-21 22:51:13 +02:00
chore: allow re-use of docker network for local clusters
This PR will allow users to use an existing docker network for their talos cluster. Hoping this will be useful for those wanting further control and configuration of their local docker clusters, as well as possibly useful for us during CI. The docker networks can be pre-created with something like: `docker network create my-cluster --subnet 192.168.0.0/24 --label talos.owned=true --label talos.cluster.name=my-cluster`. Note that the labels are pre-reqs for our discovery and re-use of these networks. Signed-off-by: Spencer Smith <robertspencersmith@gmail.com>
This commit is contained in:
parent
f0732cafcf
commit
6722a52aba
@ -6,6 +6,7 @@ package docker
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
@ -16,7 +17,24 @@ import (
|
|||||||
"github.com/talos-systems/talos/internal/pkg/provision"
|
"github.com/talos-systems/talos/internal/pkg/provision"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// createNetwork will take a network request and check if a network with the same name + cidr exists.
|
||||||
|
// If so, it simply returns without error and assumes we will re-use that network. Otherwise it will create a new one.
|
||||||
func (p *provisioner) createNetwork(ctx context.Context, req provision.NetworkRequest) error {
|
func (p *provisioner) createNetwork(ctx context.Context, req provision.NetworkRequest) error {
|
||||||
|
existingNet, err := p.listNetworks(ctx, req.Name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// If named net already exists, see if we can reuse it
|
||||||
|
if len(existingNet) > 0 {
|
||||||
|
if existingNet[0].IPAM.Config[0].Subnet != req.CIDR.String() {
|
||||||
|
return fmt.Errorf("existing network has differing cidr: %s vs %s", existingNet[0].IPAM.Config[0].Subnet, req.CIDR.String())
|
||||||
|
}
|
||||||
|
// CIDRs match, we'll reuse
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new net
|
||||||
options := types.NetworkCreate{
|
options := types.NetworkCreate{
|
||||||
Labels: map[string]string{
|
Labels: map[string]string{
|
||||||
"talos.owned": "true",
|
"talos.owned": "true",
|
||||||
@ -34,7 +52,7 @@ func (p *provisioner) createNetwork(ctx context.Context, req provision.NetworkRe
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := p.client.NetworkCreate(ctx, req.Name, options)
|
_, err = p.client.NetworkCreate(ctx, req.Name, options)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user