Skip to content

Commit

Permalink
bcicen#191 Remove projects when no any containers left
Browse files Browse the repository at this point in the history
  • Loading branch information
stokito committed Nov 16, 2020
1 parent 07553e9 commit a3ddfff
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
11 changes: 10 additions & 1 deletion connector/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func (cm *Docker) initContainerProject(c *container.Container, labels map[string
cm.projects[projectName] = c.Project
}
}
c.Project.Count++
}

func (cm *Docker) Loop() {
Expand Down Expand Up @@ -222,7 +223,15 @@ func (cm *Docker) Get(id string) (*container.Container, bool) {
// Remove containers by ID
func (cm *Docker) delByID(id string) {
cm.lock.Lock()
delete(cm.containers, id)
c, hasContainer := cm.containers[id]
if hasContainer {
c.Project.Count--
// if this was the last container in project then remove project
if c.Project != cm.noneProject && c.Project.Count <= 0 {
delete(cm.projects, c.Project.Name)
}
delete(cm.containers, id)
}
cm.lock.Unlock()
log.Infof("removed dead container: %s", id)
}
Expand Down
2 changes: 2 additions & 0 deletions connector/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (cs *Mock) makeContainer(aggression int64, health bool, project *container.
manager := manager.NewMock()
c := container.New(makeID(), collector, manager)
c.Project = project
c.Project.Count++
c.SetMeta("name", makeName())
c.SetState(makeState())
if health {
Expand Down Expand Up @@ -117,6 +118,7 @@ func (cs *Mock) All() container.Containers {
func (cs *Mock) delByID(id string) {
for n, c := range cs.containers {
if c.Id == id {
c.Project.Count--
cs.del(n)
return
}
Expand Down
7 changes: 6 additions & 1 deletion connector/runc.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func (cm *Runc) MustGet(id string) *container.Container {
manager := manager.NewRunc()
c = container.New(id, collector, manager)
c.Project = cm.noneProject
c.Project.Count++

name := libc.ID()
// set initial metadata
Expand All @@ -218,7 +219,11 @@ func (cm *Runc) MustGet(id string) *container.Container {
// Remove containers by ID
func (cm *Runc) delByID(id string) {
cm.lock.Lock()
delete(cm.containers, id)
c, hasContainer := cm.containers[id]
if hasContainer {
c.Project.Count--
delete(cm.containers, id)
}
delete(cm.libContainers, id)
cm.lock.Unlock()
log.Infof("removed dead container: %s", id)
Expand Down
1 change: 1 addition & 0 deletions container/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Project struct {
Name string
WorkDir string
Config string
Count int // Containers Count
Widgets *compact.CompactRow
}

Expand Down

0 comments on commit a3ddfff

Please sign in to comment.