Skip to content

Commit

Permalink
cluster: Refactor MachineStatus to be part of the Machine object
Browse files Browse the repository at this point in the history
  • Loading branch information
dlespiau committed Sep 3, 2019
1 parent 6643fcf commit 9cedadb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
39 changes: 5 additions & 34 deletions pkg/cluster/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const (
Running = "Running"
)

type status struct {
// MachineStatus is the runtime status of a Machine.
type MachineStatus struct {
Container string `json:"container"`
State string `json:"state"`
Spec *config.Machine `json:"spec,omitempty"`
Expand All @@ -52,43 +53,13 @@ type status struct {

// Format will output to stdout in JSON format.
func (JSONFormatter) Format(w io.Writer, machines []*Machine) error {
var statuses []status
var statuses []MachineStatus
for _, m := range machines {
s := status{}
s.Hostname = m.Hostname()
s.Container = m.ContainerName()
s.Image = m.spec.Image
s.Command = m.spec.Cmd
s.Spec = m.spec
state := NotCreated
if m.IsCreated() {
state = Stopped
if m.IsStarted() {
state = Running
}
}
s.State = state
var ports []port
for k, v := range m.ports {
p := port{
Host: v,
Guest: k,
}
ports = append(ports, p)
}
if len(ports) < 1 {
for _, p := range m.spec.PortMappings {
ports = append(ports, port{Host: int(p.ContainerPort), Guest: 0})
}
}
s.Ports = ports
s.RuntimeNetworks = m.runtimeNetworks

statuses = append(statuses, s)
statuses = append(statuses, *m.Status())
}

m := struct {
Machines []status `json:"machines"`
Machines []MachineStatus `json:"machines"`
}{
Machines: statuses,
}
Expand Down
35 changes: 35 additions & 0 deletions pkg/cluster/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,41 @@ func (m *Machine) HostPort(containerPort int) (hostPort int, err error) {
return m.ports[containerPort], nil
}

// Status returns the machine status.
func (m *Machine) Status() *MachineStatus {
s := MachineStatus{}

s.Container = m.ContainerName()
s.Image = m.spec.Image
s.Command = m.spec.Cmd
s.Spec = m.spec
state := NotCreated
if m.IsCreated() {
state = Stopped
if m.IsStarted() {
state = Running
}
}
s.State = state
var ports []port
for k, v := range m.ports {
p := port{
Host: v,
Guest: k,
}
ports = append(ports, p)
}
if len(ports) < 1 {
for _, p := range m.spec.PortMappings {
ports = append(ports, port{Host: int(p.ContainerPort), Guest: 0})
}
}
s.Ports = ports
s.RuntimeNetworks = m.runtimeNetworks

return &s
}

// Only check for Ignite prerequisites once
var igniteChecked bool

Expand Down

0 comments on commit 9cedadb

Please sign in to comment.