Skip to content

Commit

Permalink
podman handler: Work in progress.
Browse files Browse the repository at this point in the history
Signed-off-by: Paweł Szulik <pawel.szulik@intel.com>
  • Loading branch information
Paweł Szulik committed Dec 1, 2021
1 parent 34bbefa commit 341a840
Show file tree
Hide file tree
Showing 13 changed files with 1,300 additions and 53 deletions.
1 change: 1 addition & 0 deletions cmd/internal/container/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ import (
_ "github.com/google/cadvisor/container/containerd/install"
_ "github.com/google/cadvisor/container/crio/install"
_ "github.com/google/cadvisor/container/docker/install"
_ "github.com/google/cadvisor/container/podman/install"
_ "github.com/google/cadvisor/container/systemd/install"
)
3 changes: 2 additions & 1 deletion container/docker/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"strings"
"time"

"github.com/opencontainers/runc/libcontainer/cgroups"

"github.com/google/cadvisor/container"
"github.com/google/cadvisor/container/common"
dockerutil "github.com/google/cadvisor/container/docker/utils"
Expand All @@ -31,7 +33,6 @@ import (
"github.com/google/cadvisor/fs"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/zfs"
"github.com/opencontainers/runc/libcontainer/cgroups"

dockercontainer "github.com/docker/docker/api/types/container"
docker "github.com/docker/docker/client"
Expand Down
28 changes: 28 additions & 0 deletions container/podman/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2021 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package podman

import (
"context"
"os"

"github.com/containers/podman/v3/pkg/bindings"
)

func Client() (context.Context, error) {
sockDir := os.Getenv("XDG_RUNTIME_DIR")
socket := "unix:" + sockDir + "/podman/podman.sock"
return bindings.NewConnection(context.Background(), socket)
}
71 changes: 71 additions & 0 deletions container/podman/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2021 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package podman

import (
"flag"
"sync"
"time"

"github.com/google/cadvisor/container"
)

const (
rootDirRetries = 5
rootDirRetryPeriod = time.Second
)

var (
podmanRootDir string
podmanRootDirFlag = flag.String("podman_root", "/var/lib/podman")
podmanRootDirOnce sync.Once
)

func RootDir() string {
podmanRootDirOnce.Do(func() {
for i := 0; i < rootDirRetries; i++ {
status, err := Status()
if err != nil && status.RootDir != "" {
podmanRootDir = status.RootDir
break
} else {
time.Sleep(rootDirRetryPeriod)
}
}
if podmanRootDir == "" {
podmanRootDir = *podmanRootDirFlag
}
})
return podmanRootDir
}

type podmanFactory struct {
}

func (f *podmanFactory) CanHandleAndAccept(name string) (handle bool, accept bool, err error) {
return false, false, nil
}

func (f *podmanFactory) DebugInfo() map[string][]string {
return map[string][]string{}
}

func (f *podmanFactory) String() string {
return "podman"
}

func (f *podmanFactory) NewContainerHandler(name string, metadataEnvAllowList []string, inHostNamespace bool) (container.ContainerHandler, error) {
return newPodmanContainerHandler()
}
75 changes: 75 additions & 0 deletions container/podman/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2021 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package podman

import (
"fmt"

"github.com/google/cadvisor/container"
info "github.com/google/cadvisor/info/v1"
)

type podmanContainerHandler struct {
}

func (p podmanContainerHandler) ContainerReference() (info.ContainerReference, error) {
return info.ContainerReference{}, fmt.Errorf("implement me")
}

func (p podmanContainerHandler) GetSpec() (info.ContainerSpec, error) {
return info.ContainerSpec{}, fmt.Errorf("implement me")
}

func (p podmanContainerHandler) GetStats() (*info.ContainerStats, error) {
return nil, fmt.Errorf("implement me")
}

func (p podmanContainerHandler) ListContainers(listType container.ListType) ([]info.ContainerReference, error) {
return nil, fmt.Errorf("implement me")
}

func (p podmanContainerHandler) ListProcesses(listType container.ListType) ([]int, error) {
return nil, fmt.Errorf("implement me")
}

func (p podmanContainerHandler) GetCgroupPath(resource string) (string, error) {
return "", fmt.Errorf("implement me")
}

func (p podmanContainerHandler) GetContainerLabels() map[string]string {
return map[string]string{}
}

func (p podmanContainerHandler) GetContainerIPAddress() string {
return ""
}

func (p podmanContainerHandler) Exists() bool {
return false
}

func (p podmanContainerHandler) Cleanup() {
}

func (p podmanContainerHandler) Start() {
}

func (p podmanContainerHandler) Type() container.ContainerType {
return -1
}

func newPodmanContainerHandler() (container.ContainerHandler, error) {
return &podmanContainerHandler{}, nil
}
29 changes: 29 additions & 0 deletions container/podman/install/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2021 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package install

import (
"k8s.io/klog/v2"

"github.com/google/cadvisor/container"
"github.com/google/cadvisor/container/podman"
)

func init() {
err := container.RegisterPlugin("podman", podman.NewPlugin())
if err != nil {
klog.Fatalf("Failed to register podman plugin: %v", err)
}
}
53 changes: 53 additions & 0 deletions container/podman/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2021 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package podman

import (
"k8s.io/klog/v2"

"github.com/google/cadvisor/container"
"github.com/google/cadvisor/fs"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/watcher"
)

func NewPlugin() container.Plugin {
return &plugin{}
}

type plugin struct{}

func (p *plugin) InitializeFSContext(context *fs.Context) error {
context.Podman = fs.PodmanContext{
Root: "",
Driver: "",
DriverStatus: map[string]string{},
}

return nil
}

func (p *plugin) Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) {
return Register(factory, fsInfo, includedMetrics)
}

func Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, metrics container.MetricSet) (watcher.ContainerWatcher, error) {
// Register Podman container handler factory.
klog.V(1).Info("Registering Podman factory")
f := &podmanFactory{}

container.RegisterContainerHandlerFactory(f, []watcher.ContainerWatchSource{watcher.Raw})
return nil, nil
}
50 changes: 50 additions & 0 deletions container/podman/podman.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2021 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package podman

import (
"fmt"

v1 "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/machine"

"github.com/containers/podman/v3/pkg/bindings/system"
)

func Status() (v1.PodmanStatus, error) {
client, err := Client()
if err != nil {
return v1.PodmanStatus{}, fmt.Errorf("unable to commmunicate with podman daemon: %v", err)
}

podmanInfo, err := system.Info(client, nil)
if err != nil {
return v1.PodmanStatus{}, err
}

status := v1.PodmanStatus{}
status.KernelVersion = machine.KernelVersion() // TODO (Creatone): Check if it should came from Podman.
status.OS = podmanInfo.Version.OsArch
status.Hostname = podmanInfo.Host.Hostname
status.RootDir = ""
status.Driver = podmanInfo.Host.OCIRuntime.Version // TODO (Creatone): Check!
status.NumImages = podmanInfo.Store.ImageStore.Number
status.NumContainers = podmanInfo.Store.ContainerStore.Number
status.DriverStatus = nil // TODO (Creatone): Investigate!
status.Version = podmanInfo.Version.Version
status.APIVersion = podmanInfo.Version.APIVersion

return status, nil
}
3 changes: 2 additions & 1 deletion container/raw/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ package raw
import (
"fmt"

"github.com/opencontainers/runc/libcontainer/cgroups"

"github.com/google/cadvisor/container"
"github.com/google/cadvisor/container/common"
"github.com/google/cadvisor/container/libcontainer"
"github.com/google/cadvisor/fs"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/machine"
"github.com/opencontainers/runc/libcontainer/cgroups"

"k8s.io/klog/v2"
)
Expand Down
7 changes: 7 additions & 0 deletions fs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Context struct {
// docker root directory.
Docker DockerContext
Crio CrioContext
Podman PodmanContext
}

type DockerContext struct {
Expand All @@ -30,6 +31,12 @@ type DockerContext struct {
DriverStatus map[string]string
}

type PodmanContext struct {
Root string
Driver string
DriverStatus map[string]string
}

type CrioContext struct {
Root string
}
Expand Down
Loading

0 comments on commit 341a840

Please sign in to comment.