mirror of
https://github.com/bingohuang/docker-labs.git
synced 2025-10-04 09:23:21 +08:00
Return swarm info to be displayed in the FE
This requires that PWD is working as a container in the same network as all dinds Add icons to swarmInfo
This commit is contained in:
@@ -4,6 +4,9 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
@@ -40,6 +43,22 @@ func GetContainerInfo(id string) (types.ContainerJSON, error) {
|
||||
return c.ContainerInspect(context.Background(), id)
|
||||
}
|
||||
|
||||
func GetDaemonInfo(host string) (types.Info, error) {
|
||||
transport := &http.Transport{
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 1 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).DialContext}
|
||||
cli := &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
c, err := client.NewClient(host, client.DefaultVersion, cli, nil)
|
||||
if err != nil {
|
||||
return types.Info{}, err
|
||||
}
|
||||
return c.Info(context.Background())
|
||||
}
|
||||
|
||||
func CreateNetwork(name string) error {
|
||||
opts := types.NetworkCreate{Driver: "overlay", Attachable: true}
|
||||
_, err := c.NetworkCreate(context.Background(), name, opts)
|
||||
|
@@ -12,6 +12,7 @@ import (
|
||||
"golang.org/x/text/encoding"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
units "github.com/docker/go-units"
|
||||
)
|
||||
|
||||
@@ -90,6 +91,7 @@ func (s *sessionWriter) Write(p []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
func (o *Instance) CollectStats() {
|
||||
|
||||
reader, err := GetContainerStats(o.Name)
|
||||
if err != nil {
|
||||
log.Println("Error while trying to collect instance stats", err)
|
||||
@@ -115,6 +117,15 @@ func (o *Instance) CollectStats() {
|
||||
break
|
||||
}
|
||||
|
||||
var isManager *bool
|
||||
if info, err := GetDaemonInfo(fmt.Sprintf("http://%s:2375", o.IP)); err == nil {
|
||||
if info.Swarm.LocalNodeState != swarm.LocalNodeStateInactive && info.Swarm.LocalNodeState != swarm.LocalNodeStateLocked {
|
||||
isManager = &info.Swarm.ControlAvailable
|
||||
}
|
||||
} else {
|
||||
fmt.Println(info, err)
|
||||
}
|
||||
|
||||
// Memory
|
||||
if v.MemoryStats.Limit != 0 {
|
||||
memPercent = float64(v.MemoryStats.Usage) / float64(v.MemoryStats.Limit) * 100.0
|
||||
@@ -130,7 +141,7 @@ func (o *Instance) CollectStats() {
|
||||
cpuPercent = calculateCPUPercentUnix(previousCPU, previousSystem, v)
|
||||
cpuFormatted = fmt.Sprintf("%.2f%%", cpuPercent)
|
||||
|
||||
wsServer.BroadcastTo(o.session.Id, "instance stats", o.Name, memFormatted, cpuFormatted)
|
||||
wsServer.BroadcastTo(o.session.Id, "instance stats", o.Name, memFormatted, cpuFormatted, isManager)
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user