in cmd/internal/pages/containers.go [166:246]
func serveContainersPage(m manager.Manager, w http.ResponseWriter, u *url.URL) {
start := time.Now()
// The container name is the path after the handler
containerName := u.Path[len(ContainersPage)-1:]
// Get the container.
reqParams := info.ContainerInfoRequest{
NumStats: 60,
}
cont, err := m.GetContainerInfo(containerName, &reqParams)
if err != nil {
http.Error(w, fmt.Sprintf("failed to get container %q with error: %v", containerName, err), http.StatusNotFound)
return
}
displayName := getContainerDisplayName(cont.ContainerReference)
// Get the MachineInfo
machineInfo, err := m.GetMachineInfo()
if err != nil {
http.Error(w, fmt.Sprintf("failed to get machine info: %v", err), http.StatusInternalServerError)
return
}
rootDir := getRootDir(containerName)
// 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: path.Join(rootDir, ContainersPage),
})
for i := 1; i < len(pathParts); i++ {
// Skip empty parts.
if pathParts[i] == "" {
continue
}
parentContainers = append(parentContainers, link{
Text: pathParts[i],
Link: path.Join(rootDir, ContainersPage, path.Join(pathParts[1:i+1]...)),
})
}
// Build the links for the subcontainers.
subcontainerLinks := make([]link, 0, len(cont.Subcontainers))
for _, sub := range cont.Subcontainers {
if !m.Exists(sub.Name) {
continue
}
subcontainerLinks = append(subcontainerLinks, link{
Text: getContainerDisplayName(sub),
Link: path.Join(rootDir, ContainersPage, sub.Name),
})
}
data := &pageData{
DisplayName: displayName,
ContainerName: escapeContainerName(cont.Name),
ParentContainers: parentContainers,
Subcontainers: subcontainerLinks,
Spec: cont.Spec,
Stats: cont.Stats,
MachineInfo: machineInfo,
IsRoot: cont.Name == "/",
ResourcesAvailable: cont.Spec.HasCpu || cont.Spec.HasMemory || cont.Spec.HasNetwork || cont.Spec.HasFilesystem,
CpuAvailable: cont.Spec.HasCpu,
MemoryAvailable: cont.Spec.HasMemory,
NetworkAvailable: cont.Spec.HasNetwork,
FsAvailable: cont.Spec.HasFilesystem,
CustomMetricsAvailable: cont.Spec.HasCustomMetrics,
SubcontainersAvailable: len(subcontainerLinks) > 0,
Root: rootDir,
}
err = pageTemplate.Execute(w, data)
if err != nil {
klog.Errorf("Failed to apply template: %s", err)
}
klog.V(5).Infof("Request took %s", time.Since(start))
}