NodeSpec: transform network string to list of strings to allow checking out node networks

pull/471/head
iwilltry42 4 years ago
parent 683a92792e
commit 7a3edd9d7e
No known key found for this signature in database
GPG Key ID: 7BA57AD1CFF16110
  1. 1
      CHANGELOG.md
  2. 10
      pkg/client/cluster.go
  3. 2
      pkg/client/node.go
  4. 10
      pkg/client/registry.go
  5. 33
      pkg/runtimes/docker/translate.go
  6. 7
      pkg/runtimes/docker/translate_test.go
  7. 16
      pkg/tools/tools.go
  8. 2
      pkg/types/types.go

@ -15,6 +15,7 @@
- `ClusterCreate` and `NodeCreate` don't start the entities (containers) anymore - `ClusterCreate` and `NodeCreate` don't start the entities (containers) anymore
- `ClusterRun` and `NodeRun` orchestrate the new Create and Start functionality - `ClusterRun` and `NodeRun` orchestrate the new Create and Start functionality
- `NodeDelete` now takes an additional `NodeDeleteOpts` struct to toggle specific steps - `NodeDelete` now takes an additional `NodeDeleteOpts` struct to toggle specific steps
- NodeSpec now features a list of networks (required for registries)
- New config flow: CLIConfig (SimpleConfig) -> ClusterConfig -> Cluster + Opts - New config flow: CLIConfig (SimpleConfig) -> ClusterConfig -> Cluster + Opts
#### CLI #### CLI

@ -399,7 +399,7 @@ ClusterCreatOpts:
} }
node.Name = generateNodeName(cluster.Name, node.Role, suffix) node.Name = generateNodeName(cluster.Name, node.Role, suffix)
node.Network = cluster.Network.Name node.Networks = []string{cluster.Network.Name}
node.Restart = true node.Restart = true
node.GPURequest = clusterCreateOpts.GPURequest node.GPURequest = clusterCreateOpts.GPURequest
@ -507,10 +507,10 @@ ClusterCreatOpts:
fmt.Sprintf("PORTS=%s", ports), fmt.Sprintf("PORTS=%s", ports),
fmt.Sprintf("WORKER_PROCESSES=%d", len(strings.Split(ports, ","))), fmt.Sprintf("WORKER_PROCESSES=%d", len(strings.Split(ports, ","))),
}, },
Role: k3d.LoadBalancerRole, Role: k3d.LoadBalancerRole,
Labels: clusterCreateOpts.GlobalLabels, // TODO: createLoadBalancer: add more expressive labels Labels: clusterCreateOpts.GlobalLabels, // TODO: createLoadBalancer: add more expressive labels
Network: cluster.Network.Name, Networks: []string{cluster.Network.Name},
Restart: true, Restart: true,
} }
cluster.Nodes = append(cluster.Nodes, lbNode) // append lbNode to list of cluster nodes, so it will be considered during rollback cluster.Nodes = append(cluster.Nodes, lbNode) // append lbNode to list of cluster nodes, so it will be considered during rollback
log.Infof("Creating LoadBalancer '%s'", lbNode.Name) log.Infof("Creating LoadBalancer '%s'", lbNode.Name)

@ -47,7 +47,7 @@ func NodeAddToCluster(ctx context.Context, runtime runtimes.Runtime, node *k3d.N
} }
// network // network
node.Network = cluster.Network.Name node.Networks = []string{cluster.Network.Name}
// skeleton // skeleton
if node.Labels == nil { if node.Labels == nil {

@ -63,11 +63,11 @@ func RegistryCreate(ctx context.Context, runtime runtimes.Runtime, reg *k3d.Regi
// } // }
registryNode := &k3d.Node{ registryNode := &k3d.Node{
Name: reg.Host, Name: reg.Host,
Image: reg.Image, Image: reg.Image,
Role: k3d.RegistryRole, Role: k3d.RegistryRole,
Network: "bridge", // Default network: TODO: change to const from types Networks: []string{"bridge"}, // Default network: TODO: change to const from types
Restart: true, Restart: true,
} }
// error out if that registry exists already // error out if that registry exists already

@ -103,15 +103,21 @@ func TranslateNodeToContainer(node *k3d.Node) (*NodeInDocker, error) {
containerConfig.ExposedPorts = exposedPorts containerConfig.ExposedPorts = exposedPorts
hostConfig.PortBindings = node.Ports hostConfig.PortBindings = node.Ports
/* Network */ /* Network */
networkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{ endpointsConfig := map[string]*network.EndpointSettings{}
node.Network: {}, for _, net := range node.Networks {
endpointsConfig[net] = &network.EndpointSettings{}
} }
netInfo, err := GetNetwork(context.Background(), node.Network)
if err != nil { networkingConfig.EndpointsConfig = endpointsConfig
log.Warnln("Failed to get network information")
log.Warnln(err) if len(node.Networks) > 0 {
} else if netInfo.Driver == "host" { netInfo, err := GetNetwork(context.Background(), node.Networks[0]) // FIXME: only considering first network here, as that's the one k3d creates for a cluster
hostConfig.NetworkMode = "host" if err != nil {
log.Warnln("Failed to get network information")
log.Warnln(err)
} else if netInfo.Driver == "host" {
hostConfig.NetworkMode = "host"
}
} }
return &NodeInDocker{ return &NodeInDocker{
@ -158,13 +164,16 @@ func TranslateContainerDetailsToNode(containerDetails types.ContainerJSON) (*k3d
restart = true restart = true
} }
// get the clusterNetwork // get networks and ensure that the cluster network is first in list
clusterNetwork := "" orderedNetworks := []string{}
otherNetworks := []string{}
for networkName := range containerDetails.NetworkSettings.Networks { for networkName := range containerDetails.NetworkSettings.Networks {
if strings.HasPrefix(networkName, fmt.Sprintf("%s-%s", k3d.DefaultObjectNamePrefix, containerDetails.Config.Labels[k3d.LabelClusterName])) { // FIXME: catch error if label 'k3d.cluster' does not exist, but this should also never be the case if strings.HasPrefix(networkName, fmt.Sprintf("%s-%s", k3d.DefaultObjectNamePrefix, containerDetails.Config.Labels[k3d.LabelClusterName])) { // FIXME: catch error if label 'k3d.cluster' does not exist, but this should also never be the case
clusterNetwork = networkName orderedNetworks = append(orderedNetworks, networkName)
} }
otherNetworks = append(otherNetworks, networkName)
} }
orderedNetworks = append(orderedNetworks, otherNetworks...)
// serverOpts // serverOpts
serverOpts := k3d.ServerOpts{IsInit: false} serverOpts := k3d.ServerOpts{IsInit: false}
@ -213,7 +222,7 @@ func TranslateContainerDetailsToNode(containerDetails types.ContainerJSON) (*k3d
Restart: restart, Restart: restart,
Created: containerDetails.Created, Created: containerDetails.Created,
Labels: labels, Labels: labels,
Network: clusterNetwork, Networks: orderedNetworks,
ServerOpts: serverOpts, ServerOpts: serverOpts,
AgentOpts: k3d.AgentOpts{}, AgentOpts: k3d.AgentOpts{},
State: nodeState, State: nodeState,

@ -51,8 +51,9 @@ func TestTranslateNodeToContainer(t *testing.T) {
}, },
}, },
}, },
Restart: true, Restart: true,
Labels: map[string]string{k3d.LabelRole: string(k3d.ServerRole), "test_key_1": "test_val_1"}, Labels: map[string]string{k3d.LabelRole: string(k3d.ServerRole), "test_key_1": "test_val_1"},
Networks: []string{"mynet"},
} }
init := true init := true
@ -87,7 +88,7 @@ func TestTranslateNodeToContainer(t *testing.T) {
}, },
NetworkingConfig: network.NetworkingConfig{ NetworkingConfig: network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{ EndpointsConfig: map[string]*network.EndpointSettings{
"": {}, "mynet": {},
}, },
}, },
} }

@ -202,14 +202,14 @@ func ImageImportIntoClusterMulti(ctx context.Context, runtime runtimes.Runtime,
// startToolsNode will start a new k3d tools container and connect it to the network of the chosen cluster // startToolsNode will start a new k3d tools container and connect it to the network of the chosen cluster
func startToolsNode(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.Cluster, network string, volumes []string) (*k3d.Node, error) { func startToolsNode(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.Cluster, network string, volumes []string) (*k3d.Node, error) {
node := &k3d.Node{ node := &k3d.Node{
Name: fmt.Sprintf("%s-%s-tools", k3d.DefaultObjectNamePrefix, cluster.Name), Name: fmt.Sprintf("%s-%s-tools", k3d.DefaultObjectNamePrefix, cluster.Name),
Image: fmt.Sprintf("%s:%s", k3d.DefaultToolsImageRepo, version.GetHelperImageVersion()), Image: fmt.Sprintf("%s:%s", k3d.DefaultToolsImageRepo, version.GetHelperImageVersion()),
Role: k3d.NoRole, Role: k3d.NoRole,
Volumes: volumes, Volumes: volumes,
Network: network, Networks: []string{network},
Cmd: []string{}, Cmd: []string{},
Args: []string{"noop"}, Args: []string{"noop"},
Labels: k3d.DefaultObjectLabels, Labels: k3d.DefaultObjectLabels,
} }
node.Labels[k3d.LabelClusterName] = cluster.Name node.Labels[k3d.LabelClusterName] = cluster.Name
if err := k3dc.NodeRun(ctx, runtime, node, k3d.NodeCreateOpts{}); err != nil { if err := k3dc.NodeRun(ctx, runtime, node, k3d.NodeCreateOpts{}); err != nil {

@ -310,7 +310,7 @@ type Node struct {
Restart bool `yaml:"restart" json:"restart,omitempty"` Restart bool `yaml:"restart" json:"restart,omitempty"`
Created string `yaml:"created" json:"created,omitempty"` Created string `yaml:"created" json:"created,omitempty"`
Labels map[string]string // filled automatically Labels map[string]string // filled automatically
Network string // filled automatically Networks []string // filled automatically
ExtraHosts []string // filled automatically ExtraHosts []string // filled automatically
ServerOpts ServerOpts `yaml:"serverOpts" json:"serverOpts,omitempty"` ServerOpts ServerOpts `yaml:"serverOpts" json:"serverOpts,omitempty"`
AgentOpts AgentOpts `yaml:"agentOpts" json:"agentOpts,omitempty"` AgentOpts AgentOpts `yaml:"agentOpts" json:"agentOpts,omitempty"`

Loading…
Cancel
Save