Skip to content

Commit

Permalink
Make all links relative.
Browse files Browse the repository at this point in the history
This will allow cAdvisor to be reverse proxied.

Fixes kubernetes#513
Fixes kubernetes#566
Fixes kubernetes#596
Fixes kubernetes#667
  • Loading branch information
vmarmol committed May 5, 2015
1 parent 6bcb4aa commit 596c5ee
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
16 changes: 13 additions & 3 deletions pages/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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]...)),
})
}

Expand All @@ -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),
})
}

Expand All @@ -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 {
Expand All @@ -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)
}
20 changes: 10 additions & 10 deletions pages/containers_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ const containersHtmlTemplate = `
<head>
<title>cAdvisor - {{.DisplayName}}</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="/static/bootstrap-3.1.1.min.css">
<link rel="stylesheet" href="{{.Root}}static/bootstrap-3.1.1.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="/static/bootstrap-theme-3.1.1.min.css">
<link rel="stylesheet" href="{{.Root}}static/bootstrap-theme-3.1.1.min.css">
<link rel="stylesheet" href="/static/containers.css">
<link rel="stylesheet" href="{{.Root}}static/containers.css">
<!-- Latest compiled and minified JavaScript -->
<script src="/static/jquery-1.10.2.min.js"></script>
<script src="/static/bootstrap-3.1.1.min.js"></script>
<script type="text/javascript" src="/static/google-jsapi.js"></script>
<script src="{{.Root}}static/jquery-1.10.2.min.js"></script>
<script src="{{.Root}}static/bootstrap-3.1.1.min.js"></script>
<script type="text/javascript" src="{{.Root}}static/google-jsapi.js"></script>
<script type="text/javascript" src="/static/containers.js"></script>
<script type="text/javascript" src="{{.Root}}static/containers.js"></script>
</head>
<body>
<div class="container theme-showcase" >
<a href="/" class="col-sm-12" id="logo">
<a href="{{.Root}}" class="col-sm-12" id="logo">
</a>
<div class="col-sm-12">
<div class="page-header">
Expand All @@ -49,7 +49,7 @@ const containersHtmlTemplate = `
</div>
{{if .IsRoot}}
<div class="col-sm-12">
<h4><a href="/docker">Docker Containers</a></h4>
<h4><a href="../docker">Docker Containers</a></h4>
</div>
{{end}}
{{if .Subcontainers}}
Expand Down Expand Up @@ -179,7 +179,7 @@ const containersHtmlTemplate = `
{{end}}
</div>
<script type="text/javascript">
startPage({{.ContainerName}}, {{.CpuAvailable}}, {{.MemoryAvailable}});
startPage({{.ContainerName}}, {{.CpuAvailable}}, {{.MemoryAvailable}}, {{.Root}});
</script>
</body>
</html>
Expand Down
1 change: 1 addition & 0 deletions pages/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type pageData struct {
MemoryAvailable bool
NetworkAvailable bool
FsAvailable bool
Root string
}

func init() {
Expand Down
14 changes: 7 additions & 7 deletions pages/static/containers_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 596c5ee

Please sign in to comment.