github.com/google/cadvisor@v0.49.1/container/podman/factory.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  	"flag"
    19  	"fmt"
    20  	"path"
    21  	"sync"
    22  	"time"
    23  
    24  	"github.com/google/cadvisor/container"
    25  	"github.com/google/cadvisor/container/docker"
    26  	dockerutil "github.com/google/cadvisor/container/docker/utils"
    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/zfs"
    31  )
    32  
    33  const (
    34  	rootDirRetries     = 5
    35  	rootDirRetryPeriod = time.Second
    36  	containerBaseName  = "container"
    37  )
    38  
    39  var (
    40  	endpointFlag = flag.String("podman", "unix:///var/run/podman/podman.sock", "podman endpoint")
    41  )
    42  
    43  var (
    44  	rootDir     string
    45  	rootDirOnce sync.Once
    46  )
    47  
    48  func RootDir() string {
    49  	rootDirOnce.Do(func() {
    50  		for i := 0; i < rootDirRetries; i++ {
    51  			status, err := Status()
    52  			if err == nil && status.RootDir != "" {
    53  				rootDir = status.RootDir
    54  				break
    55  			} else {
    56  				time.Sleep(rootDirRetryPeriod)
    57  			}
    58  		}
    59  	})
    60  	return rootDir
    61  }
    62  
    63  type podmanFactory struct {
    64  	// Information about the mounted cgroup subsystems.
    65  	machineInfoFactory info.MachineInfoFactory
    66  
    67  	storageDriver docker.StorageDriver
    68  	storageDir    string
    69  
    70  	cgroupSubsystem map[string]string
    71  
    72  	fsInfo fs.FsInfo
    73  
    74  	metrics container.MetricSet
    75  
    76  	thinPoolName    string
    77  	thinPoolWatcher *devicemapper.ThinPoolWatcher
    78  
    79  	zfsWatcher *zfs.ZfsWatcher
    80  }
    81  
    82  func (f *podmanFactory) CanHandleAndAccept(name string) (handle bool, accept bool, err error) {
    83  	// Rootless
    84  	if path.Base(name) == containerBaseName {
    85  		name, _ = path.Split(name)
    86  	}
    87  	if !dockerutil.IsContainerName(name) {
    88  		return false, false, nil
    89  	}
    90  
    91  	id := dockerutil.ContainerNameToId(name)
    92  
    93  	ctnr, err := InspectContainer(id)
    94  	if err != nil || !ctnr.State.Running {
    95  		return false, true, fmt.Errorf("error inspecting container: %v", err)
    96  	}
    97  
    98  	return true, true, nil
    99  }
   100  
   101  func (f *podmanFactory) DebugInfo() map[string][]string {
   102  	return map[string][]string{}
   103  }
   104  
   105  func (f *podmanFactory) String() string {
   106  	return "podman"
   107  }
   108  
   109  func (f *podmanFactory) NewContainerHandler(name string, metadataEnvAllowList []string, inHostNamespace bool) (handler container.ContainerHandler, err error) {
   110  	return newPodmanContainerHandler(name, f.machineInfoFactory, f.fsInfo,
   111  		f.storageDriver, f.storageDir, f.cgroupSubsystem, inHostNamespace,
   112  		metadataEnvAllowList, f.metrics, f.thinPoolName, f.thinPoolWatcher, f.zfsWatcher)
   113  }