From 2bf6c1a5298e5dfcdbdf081c515673930e71c81d Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Thu, 4 Jun 2015 16:18:01 -0700 Subject: [PATCH 1/3] improve docker infi with 1.7 Signed-off-by: Victor Vieux --- api/handlers.go | 13 ++++++++++++- cluster/cluster.go | 2 +- cluster/mesos/cluster.go | 4 ++-- cluster/swarm/cluster.go | 8 +++++--- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/api/handlers.go b/api/handlers.go index e986afba16..e4bd0d8d84 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -24,18 +24,29 @@ const APIVERSION = "1.16" // GET /info func getInfo(c *context, w http.ResponseWriter, r *http.Request) { + clusterInfo, totalMemory, totalCpus := c.cluster.Info() info := struct { Containers int Images int DriverStatus [][2]string NEventsListener int Debug bool + MemoryLimit bool + SwapLimit bool + IPv4Forwarding bool + NCPU int64 + MemTotal int64 }{ len(c.cluster.Containers()), len(c.cluster.Images()), - c.cluster.Info(), + clusterInfo, c.eventsHandler.Size(), c.debug, + true, + true, + true, + totalCpus, + totalMemory, } w.Header().Set("Content-Type", "application/json") diff --git a/cluster/cluster.go b/cluster/cluster.go index bab478055d..dd3ceaa11b 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -51,7 +51,7 @@ type Cluster interface { // Return some info about the cluster, like nb or containers / images // It is pretty open, so the implementation decides what to return. - Info() [][2]string + Info() ([][2]string, int64, int64) // Register an event handler for cluster-wide events. RegisterEventHandler(h EventHandler) error diff --git a/cluster/mesos/cluster.go b/cluster/mesos/cluster.go index d2d2e50d4f..438ecb6171 100644 --- a/cluster/mesos/cluster.go +++ b/cluster/mesos/cluster.go @@ -303,7 +303,7 @@ func (c *Cluster) listOffers() []*mesosproto.Offer { } // Info gives minimal information about containers and resources on the mesos cluster -func (c *Cluster) Info() [][2]string { +func (c *Cluster) Info() ([][2]string, int64, int64) { offers := c.listOffers() info := [][2]string{ {"\bStrategy", c.scheduler.Strategy()}, @@ -320,7 +320,7 @@ func (c *Cluster) Info() [][2]string { } } - return info + return info, 0, 0 } func (c *Cluster) addOffer(offer *mesosproto.Offer) { diff --git a/cluster/swarm/cluster.go b/cluster/swarm/cluster.go index ff6507afea..b8affcc458 100644 --- a/cluster/swarm/cluster.go +++ b/cluster/swarm/cluster.go @@ -498,8 +498,8 @@ func (c *Cluster) listEngines() []*cluster.Engine { } // Info is exported -func (c *Cluster) Info() [][2]string { - info := [][2]string{ +func (c *Cluster) Info() (info [][2]string, totalMemory int64, totalCpus int64) { + info = [][2]string{ {"\bStrategy", c.scheduler.Strategy()}, {"\bFilters", c.scheduler.Filters()}, {"\bNodes", fmt.Sprintf("%d", len(c.engines))}, @@ -519,9 +519,11 @@ func (c *Cluster) Info() [][2]string { } sort.Strings(labels) info = append(info, [2]string{" └ Labels", fmt.Sprintf("%s", strings.Join(labels, ", "))}) + totalMemory += engine.TotalMemory() + totalCpus += engine.TotalCpus() } - return info + return info, totalMemory, totalCpus } // RANDOMENGINE returns a random engine. From 4f9c9f586791dee2dff53ef21809e38f0a6d7fea Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Thu, 4 Jun 2015 16:24:57 -0700 Subject: [PATCH 2/3] use dockerclient structs Signed-off-by: Victor Vieux --- api/handlers.go | 44 ++++++++++++---------------------------- cluster/cluster.go | 2 +- cluster/mesos/cluster.go | 8 ++++---- cluster/swarm/cluster.go | 14 ++++++------- 4 files changed, 25 insertions(+), 43 deletions(-) diff --git a/api/handlers.go b/api/handlers.go index e4bd0d8d84..97c6669f2b 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -25,28 +25,17 @@ const APIVERSION = "1.16" // GET /info func getInfo(c *context, w http.ResponseWriter, r *http.Request) { clusterInfo, totalMemory, totalCpus := c.cluster.Info() - info := struct { - Containers int - Images int - DriverStatus [][2]string - NEventsListener int - Debug bool - MemoryLimit bool - SwapLimit bool - IPv4Forwarding bool - NCPU int64 - MemTotal int64 - }{ - len(c.cluster.Containers()), - len(c.cluster.Images()), - clusterInfo, - c.eventsHandler.Size(), - c.debug, - true, - true, - true, - totalCpus, - totalMemory, + info := dockerclient.Info{ + Containers: int64(len(c.cluster.Containers())), + Images: int64(len(c.cluster.Images())), + DriverStatus: clusterInfo, + NEventsListener: int64(c.eventsHandler.Size()), + Debug: c.debug, + MemoryLimit: true, + SwapLimit: true, + IPv4Forwarding: true, + NCPU: totalCpus, + MemTotal: totalMemory, } w.Header().Set("Content-Type", "application/json") @@ -55,16 +44,9 @@ func getInfo(c *context, w http.ResponseWriter, r *http.Request) { // GET /version func getVersion(c *context, w http.ResponseWriter, r *http.Request) { - version := struct { - Version string - APIVersion string `json:"ApiVersion"` - GoVersion string - GitCommit string - Os string - Arch string - }{ + version := dockerclient.Version{ Version: "swarm/" + version.VERSION, - APIVersion: APIVERSION, + ApiVersion: APIVERSION, GoVersion: runtime.Version(), GitCommit: version.GITCOMMIT, Os: runtime.GOOS, diff --git a/cluster/cluster.go b/cluster/cluster.go index dd3ceaa11b..598f4e08c4 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -51,7 +51,7 @@ type Cluster interface { // Return some info about the cluster, like nb or containers / images // It is pretty open, so the implementation decides what to return. - Info() ([][2]string, int64, int64) + Info() ([][]string, int64, int64) // Register an event handler for cluster-wide events. RegisterEventHandler(h EventHandler) error diff --git a/cluster/mesos/cluster.go b/cluster/mesos/cluster.go index 438ecb6171..01c401a9f2 100644 --- a/cluster/mesos/cluster.go +++ b/cluster/mesos/cluster.go @@ -303,9 +303,9 @@ func (c *Cluster) listOffers() []*mesosproto.Offer { } // Info gives minimal information about containers and resources on the mesos cluster -func (c *Cluster) Info() ([][2]string, int64, int64) { +func (c *Cluster) Info() ([][]string, int64, int64) { offers := c.listOffers() - info := [][2]string{ + info := [][]string{ {"\bStrategy", c.scheduler.Strategy()}, {"\bFilters", c.scheduler.Filters()}, {"\bOffers", fmt.Sprintf("%d", len(offers))}, @@ -314,9 +314,9 @@ func (c *Cluster) Info() ([][2]string, int64, int64) { sort.Sort(offerSorter(offers)) for _, offer := range offers { - info = append(info, [2]string{" Offer", offer.Id.GetValue()}) + info = append(info, []string{" Offer", offer.Id.GetValue()}) for _, resource := range offer.Resources { - info = append(info, [2]string{" └ " + *resource.Name, fmt.Sprintf("%v", resource)}) + info = append(info, []string{" └ " + *resource.Name, fmt.Sprintf("%v", resource)}) } } diff --git a/cluster/swarm/cluster.go b/cluster/swarm/cluster.go index b8affcc458..ca32cf8f17 100644 --- a/cluster/swarm/cluster.go +++ b/cluster/swarm/cluster.go @@ -498,8 +498,8 @@ func (c *Cluster) listEngines() []*cluster.Engine { } // Info is exported -func (c *Cluster) Info() (info [][2]string, totalMemory int64, totalCpus int64) { - info = [][2]string{ +func (c *Cluster) Info() (info [][]string, totalMemory int64, totalCpus int64) { + info = [][]string{ {"\bStrategy", c.scheduler.Strategy()}, {"\bFilters", c.scheduler.Filters()}, {"\bNodes", fmt.Sprintf("%d", len(c.engines))}, @@ -509,16 +509,16 @@ func (c *Cluster) Info() (info [][2]string, totalMemory int64, totalCpus int64) sort.Sort(cluster.EngineSorter(engines)) for _, engine := range engines { - info = append(info, [2]string{engine.Name, engine.Addr}) - info = append(info, [2]string{" └ Containers", fmt.Sprintf("%d", len(engine.Containers()))}) - info = append(info, [2]string{" └ Reserved CPUs", fmt.Sprintf("%d / %d", engine.UsedCpus(), engine.TotalCpus())}) - info = append(info, [2]string{" └ Reserved Memory", fmt.Sprintf("%s / %s", units.BytesSize(float64(engine.UsedMemory())), units.BytesSize(float64(engine.TotalMemory())))}) + info = append(info, []string{engine.Name, engine.Addr}) + info = append(info, []string{" └ Containers", fmt.Sprintf("%d", len(engine.Containers()))}) + info = append(info, []string{" └ Reserved CPUs", fmt.Sprintf("%d / %d", engine.UsedCpus(), engine.TotalCpus())}) + info = append(info, []string{" └ Reserved Memory", fmt.Sprintf("%s / %s", units.BytesSize(float64(engine.UsedMemory())), units.BytesSize(float64(engine.TotalMemory())))}) labels := make([]string, 0, len(engine.Labels)) for k, v := range engine.Labels { labels = append(labels, k+"="+v) } sort.Strings(labels) - info = append(info, [2]string{" └ Labels", fmt.Sprintf("%s", strings.Join(labels, ", "))}) + info = append(info, []string{" └ Labels", fmt.Sprintf("%s", strings.Join(labels, ", "))}) totalMemory += engine.TotalMemory() totalCpus += engine.TotalCpus() } From c1020da6366d3e77306ed028d90bf87bee354884 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Thu, 4 Jun 2015 18:25:32 -0700 Subject: [PATCH 3/3] nits Signed-off-by: Victor Vieux --- api/handlers.go | 7 +++---- cluster/cluster.go | 8 +++++++- cluster/mesos/cluster.go | 16 ++++++++++++++-- cluster/swarm/cluster.go | 28 ++++++++++++++++++++++------ 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/api/handlers.go b/api/handlers.go index 97c6669f2b..e7468eefa4 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -24,18 +24,17 @@ const APIVERSION = "1.16" // GET /info func getInfo(c *context, w http.ResponseWriter, r *http.Request) { - clusterInfo, totalMemory, totalCpus := c.cluster.Info() info := dockerclient.Info{ Containers: int64(len(c.cluster.Containers())), Images: int64(len(c.cluster.Images())), - DriverStatus: clusterInfo, + DriverStatus: c.cluster.Info(), NEventsListener: int64(c.eventsHandler.Size()), Debug: c.debug, MemoryLimit: true, SwapLimit: true, IPv4Forwarding: true, - NCPU: totalCpus, - MemTotal: totalMemory, + NCPU: c.cluster.TotalCpus(), + MemTotal: c.cluster.TotalMemory(), } w.Header().Set("Content-Type", "application/json") diff --git a/cluster/cluster.go b/cluster/cluster.go index 598f4e08c4..f1c5fd4120 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -51,7 +51,13 @@ type Cluster interface { // Return some info about the cluster, like nb or containers / images // It is pretty open, so the implementation decides what to return. - Info() ([][]string, int64, int64) + Info() [][]string + + // Return the total memory of the cluster + TotalMemory() int64 + + // Return the number of CPUs in the cluster + TotalCpus() int64 // Register an event handler for cluster-wide events. RegisterEventHandler(h EventHandler) error diff --git a/cluster/mesos/cluster.go b/cluster/mesos/cluster.go index 01c401a9f2..31ab34c8cc 100644 --- a/cluster/mesos/cluster.go +++ b/cluster/mesos/cluster.go @@ -302,8 +302,20 @@ func (c *Cluster) listOffers() []*mesosproto.Offer { return list } +// TotalMemory return the total memory of the cluster +func (c *Cluster) TotalMemory() int64 { + // TODO: use current offers + return 0 +} + +// TotalCpus return the total memory of the cluster +func (c *Cluster) TotalCpus() int64 { + // TODO: use current offers + return 0 +} + // Info gives minimal information about containers and resources on the mesos cluster -func (c *Cluster) Info() ([][]string, int64, int64) { +func (c *Cluster) Info() [][]string { offers := c.listOffers() info := [][]string{ {"\bStrategy", c.scheduler.Strategy()}, @@ -320,7 +332,7 @@ func (c *Cluster) Info() ([][]string, int64, int64) { } } - return info, 0, 0 + return info } func (c *Cluster) addOffer(offer *mesosproto.Offer) { diff --git a/cluster/swarm/cluster.go b/cluster/swarm/cluster.go index ca32cf8f17..86e635c6bc 100644 --- a/cluster/swarm/cluster.go +++ b/cluster/swarm/cluster.go @@ -497,9 +497,27 @@ func (c *Cluster) listEngines() []*cluster.Engine { return out } -// Info is exported -func (c *Cluster) Info() (info [][]string, totalMemory int64, totalCpus int64) { - info = [][]string{ +// TotalMemory return the total memory of the cluster +func (c *Cluster) TotalMemory() int64 { + var totalMemory int64 + for _, engine := range c.engines { + totalMemory += engine.TotalMemory() + } + return totalMemory +} + +// TotalCpus return the total memory of the cluster +func (c *Cluster) TotalCpus() int64 { + var totalCpus int64 + for _, engine := range c.engines { + totalCpus += engine.TotalCpus() + } + return totalCpus +} + +// Info returns some info about the cluster, like nb or containers / images +func (c *Cluster) Info() [][]string { + info := [][]string{ {"\bStrategy", c.scheduler.Strategy()}, {"\bFilters", c.scheduler.Filters()}, {"\bNodes", fmt.Sprintf("%d", len(c.engines))}, @@ -519,11 +537,9 @@ func (c *Cluster) Info() (info [][]string, totalMemory int64, totalCpus int64) { } sort.Strings(labels) info = append(info, []string{" └ Labels", fmt.Sprintf("%s", strings.Join(labels, ", "))}) - totalMemory += engine.TotalMemory() - totalCpus += engine.TotalCpus() } - return info, totalMemory, totalCpus + return info } // RANDOMENGINE returns a random engine.