Skip to content

Commit 8c62ed1

Browse files
author
Paweł Szulik
committed
podman handler: Work in progress.
Signed-off-by: Paweł Szulik <[email protected]>
1 parent 5cfac3f commit 8c62ed1

File tree

21 files changed

+2509
-121
lines changed

21 files changed

+2509
-121
lines changed

cmd/go.mod

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ require (
1919
github.com/garyburd/redigo v0.0.0-20150301180006-535138d7bcd7
2020
github.com/influxdb/influxdb v0.9.6-0.20151125225445-9eab56311373
2121
github.com/mesos/mesos-go v0.0.7-0.20180413204204-29de6ff97b48
22-
github.com/pquerna/ffjson v0.0.0-20171002144729-d49c2bc1aa13 // indirect
2322
github.com/prometheus/client_golang v1.8.0
2423
github.com/stretchr/testify v1.7.0
25-
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43
26-
google.golang.org/api v0.34.0
24+
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602
25+
google.golang.org/api v0.44.0
2726
gopkg.in/olivere/elastic.v2 v2.0.12
28-
k8s.io/klog/v2 v2.4.0
27+
k8s.io/klog/v2 v2.9.0
2928
k8s.io/utils v0.0.0-20201110183641-67b214c5f920
3029
)

cmd/go.sum

Lines changed: 829 additions & 48 deletions
Large diffs are not rendered by default.

cmd/internal/container/install/install.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ import (
2121
_ "github.com/google/cadvisor/container/containerd/install"
2222
_ "github.com/google/cadvisor/container/crio/install"
2323
_ "github.com/google/cadvisor/container/docker/install"
24+
_ "github.com/google/cadvisor/container/podman/install"
2425
_ "github.com/google/cadvisor/container/systemd/install"
2526
)

cmd/internal/pages/assets/html/containers.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ <h1>{{.DisplayName}}</h1>
5050
<div class="col-sm-12">
5151
<h4><a href="../docker">Docker Containers</a></h4>
5252
</div>
53+
<div class="col-sm-12">
54+
<h4><a href="../podman">Podman Containers</a></h4>
55+
</div>
5356
{{end}}
5457
{{if .Subcontainers}}
5558
<div class="col-sm-12">

cmd/internal/pages/docker.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ func toStatusKV(status info.DockerStatus) ([]keyVal, []keyVal) {
3939
ds = append(ds, keyVal{Key: k, Value: v})
4040
}
4141
return []keyVal{
42-
{Key: "Docker Version", Value: status.Version},
43-
{Key: "Docker API Version", Value: status.APIVersion},
42+
{Key: "Version", Value: status.Version},
43+
{Key: "API Version", Value: status.APIVersion},
4444
{Key: "Kernel Version", Value: status.KernelVersion},
4545
{Key: "OS Version", Value: status.OS},
4646
{Key: "Host Name", Value: status.Hostname},
47-
{Key: "Docker Root Directory", Value: status.RootDir},
47+
{Key: "Root Directory", Value: status.RootDir},
4848
{Key: "Execution Driver", Value: status.ExecDriver},
4949
{Key: "Number of Images", Value: strconv.Itoa(status.NumImages)},
5050
{Key: "Number of Containers", Value: strconv.Itoa(status.NumContainers)},

cmd/internal/pages/pages.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ func dockerHandler(containerManager manager.Manager) auth.AuthenticatedHandlerFu
9999
}
100100
}
101101

102+
func podmanHandlerNoAuth(containerManager manager.Manager) http.HandlerFunc {
103+
return func(w http.ResponseWriter, r *http.Request) {
104+
servePodmanPage(containerManager, w, r.URL)
105+
}
106+
}
107+
102108
// Register http handlers
103109
func RegisterHandlersDigest(mux httpmux.Mux, containerManager manager.Manager, authenticator *auth.DigestAuth, urlBasePrefix string) error {
104110
// Register the handler for the containers page.
@@ -127,9 +133,11 @@ func RegisterHandlersBasic(mux httpmux.Mux, containerManager manager.Manager, au
127133
if authenticator != nil {
128134
mux.HandleFunc(ContainersPage, authenticator.Wrap(containerHandler(containerManager)))
129135
mux.HandleFunc(DockerPage, authenticator.Wrap(dockerHandler(containerManager)))
136+
// TODO (Creatone): Add Podman.
130137
} else {
131138
mux.HandleFunc(ContainersPage, containerHandlerNoAuth(containerManager))
132139
mux.HandleFunc(DockerPage, dockerHandlerNoAuth(containerManager))
140+
mux.HandleFunc(PodmanPage, podmanHandlerNoAuth(containerManager))
133141
}
134142

135143
if ContainersPage[len(ContainersPage)-1] == '/' {

cmd/internal/pages/podman.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Copyright 2021 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package pages
16+
17+
import (
18+
"fmt"
19+
"net/http"
20+
"net/url"
21+
"path"
22+
"time"
23+
24+
"github.com/google/cadvisor/container/podman"
25+
info "github.com/google/cadvisor/info/v1"
26+
"github.com/google/cadvisor/manager"
27+
28+
"k8s.io/klog/v2"
29+
)
30+
31+
const PodmanPage = "/podman/"
32+
33+
func servePodmanPage(m manager.Manager, w http.ResponseWriter, u *url.URL) {
34+
start := time.Now()
35+
36+
containerName := u.Path[len(PodmanPage)-1:]
37+
rootDir := getRootDir(containerName)
38+
39+
var data *pageData
40+
41+
if containerName == "/" {
42+
// Scenario for all containers.
43+
reqParams := info.ContainerInfoRequest{
44+
NumStats: 0,
45+
}
46+
conts, err := m.AllPodmanContainers(&reqParams)
47+
if err != nil {
48+
http.Error(w, fmt.Sprintf("failed to get container %q with error: %v", containerName, err), http.StatusNotFound)
49+
return
50+
}
51+
subcontainers := make([]link, 0, len(conts))
52+
for _, cont := range conts {
53+
subcontainers = append(subcontainers, link{
54+
Text: getContainerDisplayName(cont.ContainerReference),
55+
Link: path.Join(rootDir, PodmanPage, podman.ContainerNameToPodmanID(cont.ContainerReference.Name)),
56+
})
57+
}
58+
59+
// Get Podman status
60+
status, err := podman.Status()
61+
if err != nil {
62+
http.Error(w, fmt.Sprintf("failed to get docker info: %v", err), http.StatusInternalServerError)
63+
return
64+
}
65+
66+
podmanStatus, driverStatus := toStatusKV(info.DockerStatus(status))
67+
// Get Podman Images
68+
images, err := podman.Images()
69+
if err != nil {
70+
http.Error(w, fmt.Sprintf("failed to get podman images: %v", err), http.StatusInternalServerError)
71+
return
72+
}
73+
74+
podmanContainerText := "Podman Containers"
75+
data = &pageData{
76+
DisplayName: podmanContainerText,
77+
ParentContainers: []link{
78+
{
79+
Text: podmanContainerText,
80+
Link: path.Join(rootDir, PodmanPage),
81+
}},
82+
Subcontainers: subcontainers,
83+
Root: rootDir,
84+
DockerStatus: podmanStatus,
85+
DockerDriverStatus: driverStatus,
86+
DockerImages: images,
87+
}
88+
} else {
89+
// Scenario for specific container.
90+
reqParams := info.ContainerInfoRequest{
91+
NumStats: 60,
92+
}
93+
cont, err := m.PodmanContainer(containerName[1:], &reqParams)
94+
if err != nil {
95+
http.Error(w, fmt.Sprintf("failed to get container %v with error: %v", containerName, err), http.StatusNotFound)
96+
return
97+
}
98+
displayName := getContainerDisplayName(cont.ContainerReference)
99+
100+
var parentContainers []link
101+
parentContainers = append(parentContainers, link{
102+
Text: "Podman Containers",
103+
Link: path.Join(rootDir, PodmanPage),
104+
})
105+
parentContainers = append(parentContainers, link{
106+
Text: displayName,
107+
Link: path.Join(rootDir, PodmanPage, podman.ContainerNameToPodmanID(cont.Name)),
108+
})
109+
110+
machineInfo, err := m.GetMachineInfo()
111+
if err != nil {
112+
http.Error(w, fmt.Sprintf("failed to get machine info: %v", err), http.StatusInternalServerError)
113+
return
114+
}
115+
data = &pageData{
116+
DisplayName: displayName,
117+
ContainerName: escapeContainerName(cont.Name),
118+
ParentContainers: parentContainers,
119+
Spec: cont.Spec,
120+
Stats: cont.Stats,
121+
MachineInfo: machineInfo,
122+
ResourcesAvailable: cont.Spec.HasCpu || cont.Spec.HasMemory || cont.Spec.HasNetwork,
123+
CpuAvailable: cont.Spec.HasCpu,
124+
MemoryAvailable: cont.Spec.HasMemory,
125+
NetworkAvailable: cont.Spec.HasNetwork,
126+
FsAvailable: cont.Spec.HasFilesystem,
127+
CustomMetricsAvailable: cont.Spec.HasCustomMetrics,
128+
Root: rootDir,
129+
}
130+
}
131+
132+
err := pageTemplate.Execute(w, data)
133+
if err != nil {
134+
klog.Errorf("Failed to apply template: %s", err)
135+
}
136+
137+
klog.V(5).Infof("Request took %s", time.Since(start))
138+
}

cmd/internal/pages/templates.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

container/docker/handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"strings"
2424
"time"
2525

26+
"github.com/opencontainers/runc/libcontainer/cgroups"
27+
2628
"github.com/google/cadvisor/container"
2729
"github.com/google/cadvisor/container/common"
2830
dockerutil "github.com/google/cadvisor/container/docker/utils"
@@ -31,7 +33,6 @@ import (
3133
"github.com/google/cadvisor/fs"
3234
info "github.com/google/cadvisor/info/v1"
3335
"github.com/google/cadvisor/zfs"
34-
"github.com/opencontainers/runc/libcontainer/cgroups"
3536

3637
dockercontainer "github.com/docker/docker/api/types/container"
3738
docker "github.com/docker/docker/client"

container/podman/client.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2021 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package podman
16+
17+
import (
18+
"context"
19+
20+
"github.com/containers/podman/v3/pkg/bindings"
21+
)
22+
23+
func Client() (context.Context, error) {
24+
return bindings.NewConnection(context.Background(), *endpointFlag)
25+
}

0 commit comments

Comments
 (0)