Skip to content

Commit

Permalink
Merge pull request #52 from k0sproject/drop-ignite-backend
Browse files Browse the repository at this point in the history
Remove ignite backend support
  • Loading branch information
kke authored Oct 5, 2023
2 parents c22aa85 + 0a567a1 commit 3692ddd
Show file tree
Hide file tree
Showing 18 changed files with 54 additions and 500 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ cluster:
privateKey: cluster-key
machines:
- count: 3
backend: docker
spec:
image: quay.io/k0sproject/bootloose-debian12
name: node%d
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/k0sproject/bootloose
go 1.21.0

require (
github.com/blang/semver v3.5.1+incompatible
github.com/carlmjohnson/versioninfo v0.22.5
github.com/docker/docker v24.0.6+incompatible
github.com/ghodss/yaml v1.0.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/carlmjohnson/versioninfo v0.22.5 h1:O00sjOLUAFxYQjlN/bzYTuZiS0y6fWDQjMRvwtKgwwc=
github.com/carlmjohnson/versioninfo v0.22.5/go.mod h1:QT9mph3wcVfISUKd0i9sZfVrPviHuSF+cUtLjm2WSf8=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down
85 changes: 28 additions & 57 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
Expand All @@ -20,7 +19,6 @@ import (
"github.com/k0sproject/bootloose/pkg/config"
"github.com/k0sproject/bootloose/pkg/docker"
"github.com/k0sproject/bootloose/pkg/exec"
"github.com/k0sproject/bootloose/pkg/ignite"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -235,55 +233,40 @@ func (c *Cluster) CreateMachine(machine *Machine, i int) error {
cmd = machine.spec.Cmd
}

if machine.IsIgnite() {
pubKeyPath := c.spec.Cluster.PrivateKey + ".pub"
if !filepath.IsAbs(pubKeyPath) {
wd, err := os.Getwd()
if err != nil {
return err
}
pubKeyPath = filepath.Join(wd, pubKeyPath)
}

if _, err := ignite.Create(machine.name, machine.spec, pubKeyPath); err != nil {
return err
}
} else {
runArgs := c.createMachineRunArgs(machine, name, i)
_, err := docker.Create(machine.spec.Image,
runArgs,
[]string{cmd},
)
if err != nil {
return err
}
runArgs := c.createMachineRunArgs(machine, name, i)
_, err = docker.Create(machine.spec.Image,
runArgs,
[]string{cmd},
)
if err != nil {
return err
}

if len(machine.spec.Networks) > 1 {
for _, network := range machine.spec.Networks[1:] {
log.Infof("Connecting %s to the %s network...", name, network)
if network == "bridge" {
if err := docker.ConnectNetwork(name, network); err != nil {
return err
}
} else {
if err := docker.ConnectNetworkWithAlias(name, network, machine.Hostname()); err != nil {
return err
}
if len(machine.spec.Networks) > 1 {
for _, network := range machine.spec.Networks[1:] {
log.Infof("Connecting %s to the %s network...", name, network)
if network == "bridge" {
if err := docker.ConnectNetwork(name, network); err != nil {
return err
}
} else {
if err := docker.ConnectNetworkWithAlias(name, network, machine.Hostname()); err != nil {
return err
}
}
}
}

if err := docker.Start(name); err != nil {
return err
}
if err := docker.Start(name); err != nil {
return err
}

// Initial provisioning.
if err := containerRunShell(name, initScript); err != nil {
return err
}
if err := copy(name, publicKey, "/root/.ssh/authorized_keys"); err != nil {
return err
}
// Initial provisioning.
if err := containerRunShell(name, initScript); err != nil {
return err
}
if err := copy(name, publicKey, "/root/.ssh/authorized_keys"); err != nil {
return err
}

return nil
Expand Down Expand Up @@ -375,11 +358,6 @@ func (c *Cluster) DeleteMachine(machine *Machine, i int) error {
return nil
}

if machine.IsIgnite() {
log.Infof("Deleting machine: %s ...", name)
return ignite.Remove(machine.name)
}

if machine.IsStarted() {
log.Infof("Machine %s is started, stopping and deleting machine...", name)
err := docker.Kill("KILL", name)
Expand Down Expand Up @@ -453,9 +431,6 @@ func (c *Cluster) gatherMachines() (machines []*Machine, err error) {
if !m.IsCreated() {
continue
}
if m.IsIgnite() {
continue
}

var inspect types.ContainerJSON
if err := docker.InspectObject(m.name, ".", &inspect); err != nil {
Expand Down Expand Up @@ -519,10 +494,6 @@ func (c *Cluster) startMachine(machine *Machine, i int) error {
}
log.Infof("Starting machine: %s ...", name)

if machine.IsIgnite() {
return ignite.Start(name)
}

// Run command while sigs.k8s.io/kind/pkg/container/docker doesn't
// have a start command
cmd := exec.Command(
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (TableFormatter) Format(w io.Writer, machines []*Machine) error {
}

table := tabwriter.NewWriter(w, 0, 0, padding, ' ', 0)
wr.writeColumns(table, []string{"NAME", "HOSTNAME", "PORTS", "IP", "IMAGE", "CMD", "STATE", "BACKEND"})
wr.writeColumns(table, []string{"NAME", "HOSTNAME", "PORTS", "IP", "IMAGE", "CMD", "STATE"})
// we bail early here if there was an error so we don't process the below loop
if wr.err != nil {
return wr.err
Expand All @@ -128,7 +128,7 @@ func (TableFormatter) Format(w io.Writer, machines []*Machine) error {
}
}
ps := strings.Join(ports, ",")
wr.writeColumns(table, []string{s.Container, s.Hostname, ps, s.IP, s.Image, s.Command, s.State, s.Spec.Backend})
wr.writeColumns(table, []string{s.Container, s.Hostname, ps, s.IP, s.Image, s.Command, s.State})
}

if wr.err != nil {
Expand Down
122 changes: 20 additions & 102 deletions pkg/cluster/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ import (
"fmt"
"strconv"
"strings"
"syscall"

"github.com/docker/docker/api/types/network"
"github.com/k0sproject/bootloose/pkg/config"
"github.com/k0sproject/bootloose/pkg/docker"
"github.com/k0sproject/bootloose/pkg/exec"
"github.com/k0sproject/bootloose/pkg/ignite"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

// Machine is a single machine.
Expand All @@ -40,14 +36,6 @@ type Machine struct {
// ContainerName is the name of the running container corresponding to this
// Machine.
func (m *Machine) ContainerName() string {
if m.IsIgnite() {
filter := fmt.Sprintf(`label=ignite.name=%s`, m.name)
cid, err := exec.ExecuteCommand("docker", "ps", "-q", "-f", filter)
if err != nil || len(cid) == 0 {
return m.name
}
return cid
}
return m.name
}

Expand All @@ -59,10 +47,6 @@ func (m *Machine) Hostname() string {
// IsCreated returns if a machine is has been created. A created machine could
// either be running or stopped.
func (m *Machine) IsCreated() bool {
if m.IsIgnite() {
return ignite.IsCreated(m.name)
}

res, _ := docker.Inspect(m.name, "{{.Name}}")
if len(res) > 0 && len(res[0]) > 0 {
return true
Expand All @@ -72,59 +56,40 @@ func (m *Machine) IsCreated() bool {

// IsStarted returns if a machine is currently started or not.
func (m *Machine) IsStarted() bool {
if m.IsIgnite() {
return ignite.IsStarted(m.name)
}

res, _ := docker.Inspect(m.name, "{{.State.Running}}")
parsed, _ := strconv.ParseBool(strings.Trim(res[0], `'`))
return parsed
}

// HostPort returns the host port corresponding to the given container port.
func (m *Machine) HostPort(containerPort int) (int, error) {
if !m.IsCreated() {
return -1, errors.Errorf("hostport: container %s is not created", m.name)
}

if !m.IsStarted() {
return -1, errors.Errorf("hostport: container %s is not started", m.name)
}

// Use the cached version first
if hostPort, ok := m.ports[containerPort]; ok {
return hostPort, nil
}

var hostPort int

// Handle Ignite VMs
if m.IsIgnite() {
// Retrieve the machine details
vm, err := ignite.PopulateMachineDetails(m.name)
if err != nil {
return -1, errors.Wrap(err, "failed to populate VM details")
}

// Find the host port for the given VM port
var found = false
for _, p := range vm.Spec.Network.Ports {
if int(p.VMPort) == containerPort {
hostPort = int(p.HostPort)
found = true
break
}
}

if !found {
return -1, fmt.Errorf("invalid VM port queried: %d", containerPort)
}
} else {
// retrieve the specific port mapping using docker inspect
lines, err := docker.Inspect(m.ContainerName(), fmt.Sprintf("{{(index (index .NetworkSettings.Ports \"%d/tcp\") 0).HostPort}}", containerPort))
if err != nil {
return -1, errors.Wrapf(err, "hostport: failed to inspect container: %v", lines)
}
if len(lines) != 1 {
return -1, errors.Errorf("hostport: should only be one line, got %d lines", len(lines))
}
// retrieve the specific port mapping using docker inspect
lines, err := docker.Inspect(m.ContainerName(), fmt.Sprintf("{{(index (index .NetworkSettings.Ports \"%d/tcp\") 0).HostPort}}", containerPort))
if err != nil {
return -1, errors.Wrapf(err, "hostport: failed to inspect container: %v", lines)
}
if len(lines) != 1 {
return -1, errors.Errorf("hostport: should only be one line, got %d lines", len(lines))
}

port := strings.Replace(lines[0], "'", "", -1)
if hostPort, err = strconv.Atoi(port); err != nil {
return -1, errors.Wrap(err, "hostport: failed to parse string to int")
}
port := strings.Replace(lines[0], "'", "", -1)
if hostPort, err = strconv.Atoi(port); err != nil {
return -1, errors.Wrap(err, "hostport: failed to parse string to int")
}

if m.ports == nil {
Expand All @@ -149,30 +114,6 @@ func (m *Machine) networks() ([]*RuntimeNetwork, error) {
return m.runtimeNetworks, nil
}

func (m *Machine) igniteStatus(s *MachineStatus) error {
vm, err := ignite.PopulateMachineDetails(m.name)
if err != nil {
return err
}

// Set Ports
var ports []port
for _, p := range vm.Spec.Network.Ports {
ports = append(ports, port{
Host: int(p.HostPort),
Guest: int(p.VMPort),
})
}
s.Ports = ports
if vm.Status.IpAddresses != nil && len(vm.Status.IpAddresses) > 0 {
m.ip = vm.Status.IpAddresses[0]
}

s.RuntimeNetworks = NewIgniteRuntimeNetwork(&vm.Status)

return nil
}

func (m *Machine) dockerStatus(s *MachineStatus) error {
var ports []port
if m.IsCreated() {
Expand Down Expand Up @@ -219,30 +160,7 @@ func (m *Machine) Status() *MachineStatus {
}
s.State = state

if m.IsIgnite() {
_ = m.igniteStatus(&s)
} else {
_ = m.dockerStatus(&s)
}
_ = m.dockerStatus(&s)

return &s
}

// Only check for Ignite prerequisites once
var igniteChecked bool

// IsIgnite returns if the backend is Ignite
func (m *Machine) IsIgnite() (b bool) {
b = m.spec.Backend == ignite.BackendName

if !igniteChecked && b {
if syscall.Getuid() != 0 {
log.Fatalf("bootloose needs to run as root to use the %q backend", ignite.BackendName)
}

ignite.CheckVersion()
igniteChecked = true
}

return
}
13 changes: 0 additions & 13 deletions pkg/cluster/runtime_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net"

"github.com/docker/docker/api/types/network"
"github.com/k0sproject/bootloose/pkg/ignite"
)

const (
Expand All @@ -32,18 +31,6 @@ func NewRuntimeNetworks(networks map[string]*network.EndpointSettings) []*Runtim
return rnList
}

// NewIgniteRuntimeNetwork creates reports network status for the ignite backend.
func NewIgniteRuntimeNetwork(status *ignite.Status) []*RuntimeNetwork {
networks := make([]*RuntimeNetwork, 0, len(status.IpAddresses))
for _, ip := range status.IpAddresses {
networks = append(networks, &RuntimeNetwork{
IP: ip,
})
}

return networks
}

// RuntimeNetwork contains information about the network
type RuntimeNetwork struct {
// Name of the network
Expand Down
1 change: 0 additions & 1 deletion pkg/config/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ func DefaultConfig() Config {
PortMappings: []PortMapping{
{ContainerPort: 22},
},
Backend: "docker",
},
},
},
Expand Down
Loading

0 comments on commit 3692ddd

Please sign in to comment.