github.com/google/cadvisor@v0.49.1/container/podman/plugin.go (about)

     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  	"fmt"
    19  
    20  	"github.com/opencontainers/runc/libcontainer/cgroups"
    21  	"k8s.io/klog/v2"
    22  
    23  	"github.com/google/cadvisor/container"
    24  	"github.com/google/cadvisor/container/docker"
    25  	dockerutil "github.com/google/cadvisor/container/docker/utils"
    26  	"github.com/google/cadvisor/container/libcontainer"
    27  	"github.com/google/cadvisor/devicemapper"
    28  	"github.com/google/cadvisor/fs"
    29  	info "github.com/google/cadvisor/info/v1"
    30  	"github.com/google/cadvisor/watcher"
    31  	"github.com/google/cadvisor/zfs"
    32  )
    33  
    34  func NewPlugin() container.Plugin {
    35  	return &plugin{}
    36  }
    37  
    38  type plugin struct{}
    39  
    40  func (p *plugin) InitializeFSContext(context *fs.Context) error {
    41  	context.Podman = fs.PodmanContext{
    42  		Root:         "",
    43  		Driver:       "",
    44  		DriverStatus: map[string]string{},
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  func (p *plugin) Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) {
    51  	return Register(factory, fsInfo, includedMetrics)
    52  }
    53  
    54  func Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, metrics container.MetricSet) (watcher.ContainerWatcher, error) {
    55  	cgroupSubsystem, err := libcontainer.GetCgroupSubsystems(metrics)
    56  	if err != nil {
    57  		return nil, fmt.Errorf("failed to get cgroup subsystems: %v", err)
    58  	}
    59  
    60  	validatedInfo, err := docker.ValidateInfo(GetInfo, VersionString)
    61  	if err != nil {
    62  		return nil, fmt.Errorf("failed to validate Podman info: %v", err)
    63  	}
    64  
    65  	var (
    66  		thinPoolName    string
    67  		thinPoolWatcher *devicemapper.ThinPoolWatcher
    68  		zfsWatcher      *zfs.ZfsWatcher
    69  	)
    70  	if metrics.Has(container.DiskUsageMetrics) {
    71  		switch docker.StorageDriver(validatedInfo.Driver) {
    72  		case docker.DevicemapperStorageDriver:
    73  			thinPoolWatcher, err = docker.StartThinPoolWatcher(validatedInfo)
    74  			if err != nil {
    75  				klog.Errorf("devicemapper filesystem stats will not be reported: %v", err)
    76  			}
    77  
    78  			status, _ := docker.StatusFromDockerInfo(*validatedInfo)
    79  			thinPoolName = status.DriverStatus[dockerutil.DriverStatusPoolName]
    80  		case docker.ZfsStorageDriver:
    81  			zfsWatcher, err = docker.StartZfsWatcher(validatedInfo)
    82  			if err != nil {
    83  				klog.Errorf("zfs filesystem stats will not be reported: %v", err)
    84  			}
    85  		}
    86  	}
    87  
    88  	// Register Podman container handler factory.
    89  	klog.V(1).Info("Registering Podman factory")
    90  	f := &podmanFactory{
    91  		machineInfoFactory: factory,
    92  		storageDriver:      docker.StorageDriver(validatedInfo.Driver),
    93  		storageDir:         RootDir(),
    94  		cgroupSubsystem:    cgroupSubsystem,
    95  		fsInfo:             fsInfo,
    96  		metrics:            metrics,
    97  		thinPoolName:       thinPoolName,
    98  		thinPoolWatcher:    thinPoolWatcher,
    99  		zfsWatcher:         zfsWatcher,
   100  	}
   101  
   102  	container.RegisterContainerHandlerFactory(f, []watcher.ContainerWatchSource{watcher.Raw})
   103  
   104  	if !cgroups.IsCgroup2UnifiedMode() {
   105  		klog.Warning("Podman rootless containers not working with cgroups v1!")
   106  	}
   107  
   108  	return nil, nil
   109  }