From 596c5ee6922ddf2a187bf0e06c2b5e79239b04e7 Mon Sep 17 00:00:00 2001 From: Victor Marmol Date: Tue, 5 May 2015 13:37:20 -0700 Subject: [PATCH] Make all links relative. This will allow cAdvisor to be reverse proxied. Fixes #513 Fixes #566 Fixes #596 Fixes #667 --- pages/containers.go | 16 +++++++++++++--- pages/containers_html.go | 20 ++++++++++---------- pages/pages.go | 1 + pages/static/containers_js.go | 14 +++++++------- 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/pages/containers.go b/pages/containers.go index ad083345a1e1c..4d766e7a6073d 100644 --- a/pages/containers.go +++ b/pages/containers.go @@ -195,12 +195,14 @@ func serveContainersPage(m manager.Manager, w http.ResponseWriter, u *url.URL) e return err } + rootDir := getRootDir(cont.Name) + // Make a list of the parent containers and their links pathParts := strings.Split(string(cont.Name), "/") parentContainers := make([]link, 0, len(pathParts)) parentContainers = append(parentContainers, link{ Text: "root", - Link: ContainersPage, + Link: path.Join(rootDir, ContainersPage), }) for i := 1; i < len(pathParts); i++ { // Skip empty parts. @@ -209,7 +211,7 @@ func serveContainersPage(m manager.Manager, w http.ResponseWriter, u *url.URL) e } parentContainers = append(parentContainers, link{ Text: pathParts[i], - Link: path.Join(ContainersPage, path.Join(pathParts[1:i+1]...)), + Link: path.Join(rootDir, ContainersPage, path.Join(pathParts[1:i+1]...)), }) } @@ -221,7 +223,7 @@ func serveContainersPage(m manager.Manager, w http.ResponseWriter, u *url.URL) e } subcontainerLinks = append(subcontainerLinks, link{ Text: getContainerDisplayName(sub), - Link: path.Join(ContainersPage, sub.Name), + Link: path.Join(rootDir, ContainersPage, sub.Name), }) } @@ -239,6 +241,7 @@ func serveContainersPage(m manager.Manager, w http.ResponseWriter, u *url.URL) e MemoryAvailable: cont.Spec.HasMemory, NetworkAvailable: cont.Spec.HasNetwork, FsAvailable: cont.Spec.HasFilesystem, + Root: rootDir, } err = pageTemplate.Execute(w, data) if err != nil { @@ -248,3 +251,10 @@ func serveContainersPage(m manager.Manager, w http.ResponseWriter, u *url.URL) e glog.V(5).Infof("Request took %s", time.Since(start)) return nil } + +// Build a relative path to the root of the container page. +func getRootDir(containerName string) string { + // The root is at: container depth + levels := (strings.Count(containerName, "/")) + return strings.Repeat("../", levels) +} diff --git a/pages/containers_html.go b/pages/containers_html.go index b3fdd3677f275..ee652eb246b33 100644 --- a/pages/containers_html.go +++ b/pages/containers_html.go @@ -19,23 +19,23 @@ const containersHtmlTemplate = ` cAdvisor - {{.DisplayName}} - + - + - + - - - + + + - +
-
{{if .IsRoot}} {{end}} {{if .Subcontainers}} @@ -179,7 +179,7 @@ const containersHtmlTemplate = ` {{end}}
diff --git a/pages/pages.go b/pages/pages.go index 89d1c113c54d1..2086f8a6c93d9 100644 --- a/pages/pages.go +++ b/pages/pages.go @@ -51,6 +51,7 @@ type pageData struct { MemoryAvailable bool NetworkAvailable bool FsAvailable bool + Root string } func init() { diff --git a/pages/static/containers_js.go b/pages/static/containers_js.go index 45c2afdbd3eba..86f72a6c95350 100644 --- a/pages/static/containers_js.go +++ b/pages/static/containers_js.go @@ -1518,21 +1518,21 @@ function drawGauges(elementId, gauges) { } // Get the machine info. -function getMachineInfo(callback) { - $.getJSON("/api/v1.0/machine", function(data) { +function getMachineInfo(rootDir, callback) { + $.getJSON(rootDir + "api/v1.0/machine", function(data) { callback(data); }); } // Get the container stats for the specified container. -function getStats(containerName, callback) { +function getStats(rootDir, containerName, callback) { // Request 60s of container history and no samples. var request = JSON.stringify({ // Update main.statsRequestedByUI while updating "num_stats" here. "num_stats": 60, "num_samples": 0 }); - $.post("/api/v1.0/containers" + containerName, request, function(data) { + $.post(rootDir + "api/v1.0/containers" + containerName, request, function(data) { callback(data); }, "json"); } @@ -1891,7 +1891,7 @@ function drawCharts(machineInfo, containerInfo) { } // Executed when the page finishes loading. -function startPage(containerName, hasCpu, hasMemory) { +function startPage(containerName, hasCpu, hasMemory, rootDir) { // Don't fetch data if we don't have any resource. if (!hasCpu && !hasMemory) { return; @@ -1902,9 +1902,9 @@ function startPage(containerName, hasCpu, hasMemory) { window.cadvisor.firstRun = true; // Get machine info, then get the stats every 1s. - getMachineInfo(function(machineInfo) { + getMachineInfo(rootDir, function(machineInfo) { setInterval(function() { - getStats(containerName, function(containerInfo){ + getStats(rootDir, containerName, function(containerInfo){ if (window.cadvisor.firstRun && containerInfo.spec.has_filesystem) { window.cadvisor.firstRun = false; startFileSystemUsage("filesystem-usage", machineInfo, containerInfo);