further translation improvement and first test

pull/227/head
iwilltry42 5 years ago
parent d4060bc4be
commit 1200a2a500
  1. 48
      pkg/runtimes/docker/translate.go
  2. 68
      pkg/runtimes/docker/translate_test.go
  3. 34
      pkg/runtimes/docker/types.go
  4. 1
      pkg/types/types.go

@ -29,15 +29,29 @@ import (
k3d "github.com/rancher/k3d/pkg/types"
)
func TranslateNodeToContainer(node *k3d.Node) (docker.Config, error) {
// TranslateNodeToContainer translates a k3d node specification to a docker container representation
func TranslateNodeToContainer(node *k3d.Node) (*NodeInDocker, error) {
container := docker.Config{}
container.Hostname = node.Name
container.Image = node.Image
container.Labels = node.Labels // has to include the role
container.Env = []string{} // TODO:
container.Cmd = []string{} // TODO: dependent on role and extra args
hostConfig := docker.HostConfig{}
/* initialize everything that we need */
containerConfig := &docker.Config{}
hostConfig := &docker.HostConfig{}
networkingConfig := &network.NetworkingConfig{}
/* Name & Image */
containerConfig.Hostname = node.Name
containerConfig.Image = node.Image
/* Command & Arguments */
containerConfig.Cmd = []string{}
containerConfig.Cmd = append(containerConfig.Cmd, node.Cmd...) // contains k3s command and role-specific required flags/args
containerConfig.Cmd = append(containerConfig.Cmd, node.Args...) // extra flags/args
/* Environment Variables */
containerConfig.Env = node.Env
/* Labels */
containerConfig.Labels = node.Labels // has to include the role
/* Auto-Restart */
if node.Restart {
@ -46,30 +60,36 @@ func TranslateNodeToContainer(node *k3d.Node) (docker.Config, error) {
}
}
/* Tmpfs Mounts */
// TODO: do we need this or can the default be a map with empty values already?
hostConfig.Tmpfs = make(map[string]string)
for _, mnt := range k3d.DefaultTmpfsMounts {
hostConfig.Tmpfs[mnt] = ""
}
/* They have to run in privileged mode */
// TODO: can we replace this by a reduced set of capabilities?
hostConfig.Privileged = true
/* Volumes */
// TODO: image volume
hostConfig.Binds = []string{}
container.Volumes = map[string]struct{}{} // TODO: which one do we use?
hostConfig.Binds = node.Volumes // TODO: some validation?
// containerConfig.Volumes = map[string]struct{}{} // TODO: do we need this? We only used binds before
/* Ports */
container.ExposedPorts = nat.PortSet{} // TODO:
hostConfig.PortBindings = nat.PortMap{} // TODO: this and exposedPorts required?
containerConfig.ExposedPorts = nat.PortSet{} // TODO: translate from node.Ports to nat.PortSet
hostConfig.PortBindings = nat.PortMap{} // TODO: this and exposedPorts required?
/* Network */
networkingConfig := &network.NetworkingConfig{}
networkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{
"<network-name>": { // TODO: fill
Aliases: []string{"<container-alias>"}, // TODO: fill
},
}
return container, nil
return &NodeInDocker{
ContainerConfig: containerConfig,
HostConfig: hostConfig,
NetworkingConfig: networkingConfig,
}, nil
}

@ -0,0 +1,68 @@
/*
Copyright © 2019 Thorsten Klein <iwilltry42@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package docker
import (
"testing"
"reflect"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
k3d "github.com/rancher/k3d/pkg/types"
)
func TestTranslateNodeToContainer(t *testing.T) {
inputNode := &k3d.Node{
Name: "test",
Role: "master",
Image: "rancher/k3s:v0.9.0",
Volumes: []string{"/test:/tmp/test"},
Env: []string{"TEST_KEY_1=TEST_VAL_1"},
Cmd: []string{"server", "--https-listen-port=6443"},
Args: []string{"--some-boolflag"},
Ports: []string{"0.0.0.0:6443:6443/tcp"},
Restart: true,
Labels: map[string]string{"test_key_1": "test_val_1"},
}
expectedRepresentation := &NodeInDocker{
ContainerConfig: &container.Config{
Hostname: inputNode.Name,
Image: inputNode.Image,
},
HostConfig: &container.HostConfig{},
NetworkingConfig: &network.NetworkingConfig{},
}
actualRepresentation, err := TranslateNodeToContainer(inputNode)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(actualRepresentation, expectedRepresentation) {
t.Errorf("Actual representation\n%+v\ndoes not match expected representation\n%+v", actualRepresentation, expectedRepresentation)
}
}

@ -0,0 +1,34 @@
/*
Copyright © 2019 Thorsten Klein <iwilltry42@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package docker
import (
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
)
// NodeInDocker represents everything that we need to represent a k3d node in docker
type NodeInDocker struct {
ContainerConfig *container.Config // TODO: do we need this as pointers?
HostConfig *container.HostConfig
NetworkingConfig *network.NetworkingConfig
}

@ -67,6 +67,7 @@ type Node struct {
Image string `yaml:"image" json:"image,omitempty"`
Volumes []string `yaml:"volumes" json:"volumes,omitempty"`
Env []string `yaml:"env" json:"env,omitempty"`
Cmd []string // filled automatically based on role
Args []string `yaml:"extra_args" json:"extraArgs,omitempty"`
Ports []string `yaml:"port_mappings" json:"portMappings,omitempty"` // TODO: make a struct out of this?
Restart bool `yaml:"restart" json:"restart,omitempty"`

Loading…
Cancel
Save