rename connectors

This commit is contained in:
Bradley Cicenas 2017-06-08 15:01:08 +00:00
parent b85ca680f0
commit 44379cd9fd
4 changed files with 43 additions and 40 deletions

View File

@ -7,34 +7,24 @@ import (
"sync" "sync"
"github.com/bcicen/ctop/container" "github.com/bcicen/ctop/container"
"github.com/bcicen/ctop/logging"
"github.com/bcicen/ctop/metrics" "github.com/bcicen/ctop/metrics"
"github.com/fsouza/go-dockerclient" api "github.com/fsouza/go-dockerclient"
) )
var ( type Docker struct {
log = logging.Init() client *api.Client
)
type ContainerSource interface {
All() container.Containers
Get(string) (*container.Container, bool)
}
type DockerContainerSource struct {
client *docker.Client
containers map[string]*container.Container containers map[string]*container.Container
needsRefresh chan string // container IDs requiring refresh needsRefresh chan string // container IDs requiring refresh
lock sync.RWMutex lock sync.RWMutex
} }
func NewDockerContainerSource() *DockerContainerSource { func NewDocker() *Docker {
// init docker client // init docker client
client, err := docker.NewClientFromEnv() client, err := api.NewClientFromEnv()
if err != nil { if err != nil {
panic(err) panic(err)
} }
cm := &DockerContainerSource{ cm := &Docker{
client: client, client: client,
containers: make(map[string]*container.Container), containers: make(map[string]*container.Container),
needsRefresh: make(chan string, 60), needsRefresh: make(chan string, 60),
@ -47,9 +37,9 @@ func NewDockerContainerSource() *DockerContainerSource {
} }
// Docker events watcher // Docker events watcher
func (cm *DockerContainerSource) watchEvents() { func (cm *Docker) watchEvents() {
log.Info("docker event listener starting") log.Info("docker event listener starting")
events := make(chan *docker.APIEvents) events := make(chan *api.APIEvents)
cm.client.AddEventListener(events) cm.client.AddEventListener(events)
for e := range events { for e := range events {
@ -67,7 +57,7 @@ func (cm *DockerContainerSource) watchEvents() {
} }
} }
func portsFormat(ports map[docker.Port][]docker.PortBinding) string { func portsFormat(ports map[api.Port][]api.PortBinding) string {
var exposed []string var exposed []string
var published []string var published []string
@ -85,7 +75,7 @@ func portsFormat(ports map[docker.Port][]docker.PortBinding) string {
return strings.Join(append(exposed, published...), "\n") return strings.Join(append(exposed, published...), "\n")
} }
func (cm *DockerContainerSource) refresh(c *container.Container) { func (cm *Docker) refresh(c *container.Container) {
insp := cm.inspect(c.Id) insp := cm.inspect(c.Id)
// remove container if no longer exists // remove container if no longer exists
if insp == nil { if insp == nil {
@ -99,10 +89,10 @@ func (cm *DockerContainerSource) refresh(c *container.Container) {
c.SetState(insp.State.Status) c.SetState(insp.State.Status)
} }
func (cm *DockerContainerSource) inspect(id string) *docker.Container { func (cm *Docker) inspect(id string) *api.Container {
c, err := cm.client.InspectContainer(id) c, err := cm.client.InspectContainer(id)
if err != nil { if err != nil {
if _, ok := err.(*docker.NoSuchContainer); ok == false { if _, ok := err.(*api.NoSuchContainer); ok == false {
log.Errorf(err.Error()) log.Errorf(err.Error())
} }
} }
@ -110,8 +100,8 @@ func (cm *DockerContainerSource) inspect(id string) *docker.Container {
} }
// Mark all container IDs for refresh // Mark all container IDs for refresh
func (cm *DockerContainerSource) refreshAll() { func (cm *Docker) refreshAll() {
opts := docker.ListContainersOptions{All: true} opts := api.ListContainersOptions{All: true}
allContainers, err := cm.client.ListContainers(opts) allContainers, err := cm.client.ListContainers(opts)
if err != nil { if err != nil {
panic(err) panic(err)
@ -125,7 +115,7 @@ func (cm *DockerContainerSource) refreshAll() {
} }
} }
func (cm *DockerContainerSource) Loop() { func (cm *Docker) Loop() {
for id := range cm.needsRefresh { for id := range cm.needsRefresh {
c := cm.MustGet(id) c := cm.MustGet(id)
cm.refresh(c) cm.refresh(c)
@ -133,7 +123,7 @@ func (cm *DockerContainerSource) Loop() {
} }
// Get a single container, creating one anew if not existing // Get a single container, creating one anew if not existing
func (cm *DockerContainerSource) MustGet(id string) *container.Container { func (cm *Docker) MustGet(id string) *container.Container {
c, ok := cm.Get(id) c, ok := cm.Get(id)
// append container struct for new containers // append container struct for new containers
if !ok { if !ok {
@ -149,7 +139,7 @@ func (cm *DockerContainerSource) MustGet(id string) *container.Container {
} }
// Get a single container, by ID // Get a single container, by ID
func (cm *DockerContainerSource) Get(id string) (*container.Container, bool) { func (cm *Docker) Get(id string) (*container.Container, bool) {
cm.lock.Lock() cm.lock.Lock()
c, ok := cm.containers[id] c, ok := cm.containers[id]
cm.lock.Unlock() cm.lock.Unlock()
@ -157,7 +147,7 @@ func (cm *DockerContainerSource) Get(id string) (*container.Container, bool) {
} }
// Remove containers by ID // Remove containers by ID
func (cm *DockerContainerSource) delByID(id string) { func (cm *Docker) delByID(id string) {
cm.lock.Lock() cm.lock.Lock()
delete(cm.containers, id) delete(cm.containers, id)
cm.lock.Unlock() cm.lock.Unlock()
@ -165,7 +155,7 @@ func (cm *DockerContainerSource) delByID(id string) {
} }
// Return array of all containers, sorted by field // Return array of all containers, sorted by field
func (cm *DockerContainerSource) All() (containers container.Containers) { func (cm *Docker) All() (containers container.Containers) {
cm.lock.Lock() cm.lock.Lock()
for _, c := range cm.containers { for _, c := range cm.containers {
containers = append(containers, c) containers = append(containers, c)

13
connector/main.go Normal file
View File

@ -0,0 +1,13 @@
package connector
import (
"github.com/bcicen/ctop/container"
"github.com/bcicen/ctop/logging"
)
var log = logging.Init()
type ContainerSource interface {
All() container.Containers
Get(string) (*container.Container, bool)
}

View File

@ -14,19 +14,19 @@ import (
"github.com/nu7hatch/gouuid" "github.com/nu7hatch/gouuid"
) )
type MockContainerSource struct { type Mock struct {
containers container.Containers containers container.Containers
} }
func NewMockContainerSource() *MockContainerSource { func NewMock() *Mock {
cs := &MockContainerSource{} cs := &Mock{}
go cs.Init() go cs.Init()
go cs.Loop() go cs.Loop()
return cs return cs
} }
// Create Mock containers // Create Mock containers
func (cs *MockContainerSource) Init() { func (cs *Mock) Init() {
rand.Seed(int64(time.Now().Nanosecond())) rand.Seed(int64(time.Now().Nanosecond()))
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
@ -39,7 +39,7 @@ func (cs *MockContainerSource) Init() {
} }
func (cs *MockContainerSource) makeContainer(aggression int64) { func (cs *Mock) makeContainer(aggression int64) {
collector := metrics.NewMock(aggression) collector := metrics.NewMock(aggression)
c := container.New(makeID(), collector) c := container.New(makeID(), collector)
c.SetMeta("name", makeName()) c.SetMeta("name", makeName())
@ -47,7 +47,7 @@ func (cs *MockContainerSource) makeContainer(aggression int64) {
cs.containers = append(cs.containers, c) cs.containers = append(cs.containers, c)
} }
func (cs *MockContainerSource) Loop() { func (cs *Mock) Loop() {
iter := 0 iter := 0
for { for {
// Change state for random container // Change state for random container
@ -61,7 +61,7 @@ func (cs *MockContainerSource) Loop() {
} }
// Get a single container, by ID // Get a single container, by ID
func (cs *MockContainerSource) Get(id string) (*container.Container, bool) { func (cs *Mock) Get(id string) (*container.Container, bool) {
for _, c := range cs.containers { for _, c := range cs.containers {
if c.Id == id { if c.Id == id {
return c, true return c, true
@ -71,14 +71,14 @@ func (cs *MockContainerSource) Get(id string) (*container.Container, bool) {
} }
// Return array of all containers, sorted by field // Return array of all containers, sorted by field
func (cs *MockContainerSource) All() container.Containers { func (cs *Mock) All() container.Containers {
sort.Sort(cs.containers) sort.Sort(cs.containers)
cs.containers.Filter() cs.containers.Filter()
return cs.containers return cs.containers
} }
// Remove containers by ID // Remove containers by ID
func (cs *MockContainerSource) delByID(id string) { func (cs *Mock) delByID(id string) {
for n, c := range cs.containers { for n, c := range cs.containers {
if c.Id == id { if c.Id == id {
cs.del(n) cs.del(n)
@ -88,7 +88,7 @@ func (cs *MockContainerSource) delByID(id string) {
} }
// Remove one or more containers by index // Remove one or more containers by index
func (cs *MockContainerSource) del(idx ...int) { func (cs *Mock) del(idx ...int) {
for _, i := range idx { for _, i := range idx {
cs.containers = append(cs.containers[:i], cs.containers[i+1:]...) cs.containers = append(cs.containers[:i], cs.containers[i+1:]...)
} }

View File

@ -16,7 +16,7 @@ type GridCursor struct {
func NewGridCursor() *GridCursor { func NewGridCursor() *GridCursor {
return &GridCursor{ return &GridCursor{
cSource: connector.NewDockerContainerSource(), cSource: connector.NewDocker(),
} }
} }