Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions cmd/bootloose/config_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package bootloose
import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/k0sproject/bootloose/pkg/cluster"
"github.com/k0sproject/bootloose/pkg/config"
Expand All @@ -17,6 +19,7 @@ import (
type configCreateOptions struct {
override bool
config config.Config
volumes []string
}

func NewConfigCreateCommand() *cobra.Command {
Expand Down Expand Up @@ -50,6 +53,8 @@ func NewConfigCreateCommand() *cobra.Command {
containerCmd := &opts.config.Machines[0].Spec.Cmd
cmd.Flags().StringVarP(containerCmd, "cmd", "d", *containerCmd, "The command to execute on the container")

cmd.Flags().StringSliceVarP(&opts.volumes, "volume", "v", nil, "Volumes to mount in the container")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be extracted and added as a feature.


return cmd
}

Expand All @@ -72,6 +77,45 @@ func (opts *configCreateOptions) create(cmd *cobra.Command, args []string) error
if configExists(cfgFile) && !opts.override {
return fmt.Errorf("configuration file at %s already exists", cfgFile)
}
for _, v := range opts.volumes {
volume, err := parseVolume(v)
if err != nil {
return err
}
for _, machine := range opts.config.Machines {
machine.Spec.Volumes = append(machine.Spec.Volumes, volume)
}
}
return cluster.Save(cfgFile)
}

// volume flags can be in the form of:
// -v /host/path:/container/path (bind mount)
// -v volume:/container/path (volume mount)
// or contain the permissions field:
// -v /host/path:/container/path:ro (bind mount (read only))
// -v volume:/container/path:rw (volume mount (read write))
func parseVolume(v string) (config.Volume, error) {
if v == "" {
return config.Volume{}, fmt.Errorf("empty volume value")
}
parts := strings.Split(v, ":")
if len(parts) < 2 || len(parts) > 3 {
return config.Volume{}, fmt.Errorf("invalid volume value: %v", v)
}

vol := config.Volume{}
if filepath.IsAbs(parts[0]) {
vol.Type = "bind"
} else {
vol.Type = "volume"
}

if len(parts) == 3 {
vol.ReadOnly = parts[2] == "ro"
}

vol.Source = parts[0]
vol.Destination = parts[1]
return vol, nil
}
8 changes: 4 additions & 4 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (c *Cluster) forEachMachine(do func(*Machine, int) error) error {
for _, template := range c.spec.Machines {
for i := 0; i < template.Count; i++ {
// machine name indexed with i
machine := c.machine(&template.Spec, i)
machine := c.machine(template.Spec, i)
// but to prevent port collision, we use machineIndex for the real machine creation
if err := do(machine, machineIndex); err != nil {
return err
Expand All @@ -144,7 +144,7 @@ func (c *Cluster) forSpecificMachines(do func(*Machine, int) error, machineNames
}
for _, template := range c.spec.Machines {
for i := 0; i < template.Count; i++ {
machine := c.machine(&template.Spec, i)
machine := c.machine(template.Spec, i)
_, ok := machineToStart[machine.name]
if ok {
if err := do(machine, i); err != nil {
Expand Down Expand Up @@ -546,7 +546,7 @@ func (c *Cluster) gatherMachinesByCluster() (machines []*Machine) {
for _, template := range c.spec.Machines {
for i := 0; i < template.Count; i++ {
s := template.Spec
machine := c.machine(&s, i)
machine := c.machine(s, i)
machines = append(machines, machine)
}
}
Expand Down Expand Up @@ -673,7 +673,7 @@ func (c *Cluster) machineFromHostname(hostname string) (*Machine, error) {
for _, template := range c.spec.Machines {
for i := 0; i < template.Count; i++ {
if hostname == f(template.Spec.Name, i) {
return c.machine(&template.Spec, i), nil
return c.machine(template.Spec, i), nil
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ machines:
assert.Equal(t, uint16(22), portMapping.ContainerPort)
assert.Equal(t, uint16(2222), portMapping.HostPort)

machine0 := cluster.machine(&template.Spec, 0)
machine0 := cluster.machine(template.Spec, 0)
args0 := cluster.createMachineRunArgs(machine0, machine0.ContainerName(), 0)
i := indexOf("-p", args0)
assert.NotEqual(t, -1, i)
assert.Equal(t, "2222:22", args0[i+1])

machine1 := cluster.machine(&template.Spec, 1)
machine1 := cluster.machine(template.Spec, 1)
args1 := cluster.createMachineRunArgs(machine1, machine1.ContainerName(), 1)
i = indexOf("-p", args1)
assert.NotEqual(t, -1, i)
Expand Down Expand Up @@ -96,13 +96,12 @@ func TestCluster_EnsureSSHKeys(t *testing.T) {

privStat, err = os.Stat(keyPath)
if assert.NoError(t, err, "failed to stat private key file") {
assert.Equal(t, privStat.Mode().Perm(), os.FileMode(0600), "private key file has wrong permissions")

assert.Equal(t, privStat.Mode().Perm(), os.FileMode(0o600), "private key file has wrong permissions")
}

pubStat, err = os.Stat(keyPath + ".pub")
if assert.NoError(t, err, "failed to stat public key file") {
assert.Equal(t, pubStat.Mode().Perm(), os.FileMode(0644), "public key file has wrong permissions")
assert.Equal(t, pubStat.Mode().Perm(), os.FileMode(0o644), "public key file has wrong permissions")
}
})

Expand Down
6 changes: 3 additions & 3 deletions pkg/config/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func NewConfigFromFile(path string) (*Config, error) {

// MachineReplicas are a number of machine following the same specification.
type MachineReplicas struct {
Spec Machine `json:"spec"`
Count int `json:"count"`
Spec *Machine `json:"spec"`
Count int `json:"count"`
}

// Cluster is a set of Machines.
Expand Down Expand Up @@ -85,7 +85,7 @@ func DefaultConfig() Config {
Machines: []MachineReplicas{
{
Count: 1,
Spec: Machine{
Spec: &Machine{
Name: "node%d",
Image: "quay.io/k0sproject/bootloose-ubuntu20.04",
PortMappings: []PortMapping{
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ func TestGetValueFromConfig(t *testing.T) {
config := Config{
Cluster: Cluster{Name: "clustername", PrivateKey: "privatekey"},
Machines: []MachineReplicas{
MachineReplicas{
{
Count: 3,
Spec: Machine{
Spec: &Machine{
Image: "myImage",
Name: "myName",
Privileged: true,
Expand All @@ -32,7 +32,7 @@ func TestGetValueFromConfig(t *testing.T) {
"cluster.name",
Config{
Cluster: Cluster{Name: "clustername", PrivateKey: "privatekey"},
Machines: []MachineReplicas{MachineReplicas{Count: 3, Spec: Machine{}}},
Machines: []MachineReplicas{{Count: 3, Spec: &Machine{}}},
},
"clustername",
},
Expand Down