Skip to content

Commit

Permalink
Separate container data in struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-osman committed May 8, 2022
1 parent bc7210a commit 5eb32cb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
19 changes: 3 additions & 16 deletions conman/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,15 @@ import (
"github.com/nathan-osman/i5/container"
)

// Info stores information for a specific container.
type Info struct {
ID string `json:"id"`
Name string `json:"name"`
Domains []string `json:"domains"`
Disabled bool `json:"disabled"`
}

// Info returns information about running containers.
func (c *Conman) Info() []*Info {
func (c *Conman) Info() []container.ContainerData {
c.mutex.RLock()
defer c.mutex.RUnlock()
ret := []*Info{}
ret := []container.ContainerData{}
for _, v := range c.idMap {
con := v.(*container.Container)
if con.ID != "" {
ret = append(ret, &Info{
ID: con.ID,
Name: con.Name,
Domains: con.Domains,
Disabled: con.Disabled,
})
ret = append(ret, con.ContainerData)
}
}
return ret
Expand Down
29 changes: 19 additions & 10 deletions container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"strconv"
"strings"
"time"

"github.com/nathan-osman/i5/proxy"
)
Expand Down Expand Up @@ -34,16 +35,22 @@ type Database struct {
Password string
}

// ContainerData provides a base type for container data.
type ContainerData struct {
ID string `json:"id"`
Name string `json:"name"`
Domains []string `json:"domains"`
Insecure bool `json:"insecure"`
Disabled bool `json:"disabled"`
Uptime time.Time `json:"uptime"`
}

// Container represents configuration for a Docker container with i5 metadata.
type Container struct {
ID string `json:"id"`
Name string `json:"name"`
Domains []string `json:"domains"`
Insecure bool `json:"insecure"`
Disabled bool `json:"disabled"`
Database *Database `json:"-"`
Handler http.Handler `json:"-"`
Proxy *proxy.Proxy `json:"-"`
ContainerData
Database *Database
Handler http.Handler
Proxy *proxy.Proxy
}

func getWithDefault(m map[string]string, key, def string) string {
Expand All @@ -58,8 +65,10 @@ func New(id, name string, labels map[string]string) (*Container, error) {
var (
cfg = &proxy.Config{}
c = &Container{
ID: id,
Name: name,
ContainerData: ContainerData{
ID: id,
Name: name,
},
}
)
if addr, ok := labels[labelAddr]; ok {
Expand Down
13 changes: 9 additions & 4 deletions dockmon/dockmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func (d *Dockmon) newContainerFromClient(ctx context.Context, client *client.Cli
containerJSON.Name[1:],
containerJSON.Config.Labels,
)
if t, err := time.Parse(time.RFC3339, containerJSON.Created); err == nil {
c.Uptime = t
}
if err != nil {
return nil, err
}
Expand All @@ -78,8 +81,8 @@ func (d *Dockmon) newContainerFromClient(ctx context.Context, client *client.Cli
}

type messageContainer struct {
Action string `json:"action"`
Container *container.Container `json:"container"`
Action string `json:"action"`
Container container.ContainerData `json:"container"`
}

func (d *Dockmon) sendEvent(action string, c *container.Container) {
Expand All @@ -89,7 +92,7 @@ func (d *Dockmon) sendEvent(action string, c *container.Container) {
}
d.logger.Log(messageTypeContainer, &messageContainer{
Action: action,
Container: c,
Container: c.ContainerData,
})
}

Expand Down Expand Up @@ -132,7 +135,9 @@ func (d *Dockmon) sync(ctx context.Context) error {
newConMap := util.StringMap{}
for _, c := range containers {
newConMap.Insert(c.ID, &container.Container{
Disabled: c.State != "running",
ContainerData: container.ContainerData{
Disabled: c.State != "running",
},
})
}

Expand Down
4 changes: 2 additions & 2 deletions status/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sort"

"github.com/gin-gonic/gin"
"github.com/nathan-osman/i5/conman"
"github.com/nathan-osman/i5/container"
)

const (
Expand All @@ -26,7 +26,7 @@ func (s *Status) apiStatus(c *gin.Context) {
})
}

type byName []*conman.Info
type byName []container.ContainerData

func (n byName) Len() int { return len(n) }
func (n byName) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
Expand Down
8 changes: 5 additions & 3 deletions status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ func New(cfg *Config) *Status {
c.Abort()
})
s.Container = &container.Container{
Domains: []string{cfg.Domain},
Insecure: cfg.Insecure,
Handler: r,
ContainerData: container.ContainerData{
Domains: []string{cfg.Domain},
Insecure: cfg.Insecure,
},
Handler: r,
}
return s
}
Expand Down

0 comments on commit 5eb32cb

Please sign in to comment.