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

     1  // Copyright 2022 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  	"encoding/json"
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  
    23  	"github.com/google/cadvisor/container/docker"
    24  )
    25  
    26  const (
    27  	containersJSONFilename = "containers.json"
    28  )
    29  
    30  type containersJSON struct {
    31  	ID    string `json:"id"`
    32  	Layer string `json:"layer"`
    33  	// rest in unnecessary
    34  }
    35  
    36  func rwLayerID(storageDriver docker.StorageDriver, storageDir string, containerID string) (string, error) {
    37  	data, err := os.ReadFile(filepath.Join(storageDir, string(storageDriver)+"-containers", containersJSONFilename))
    38  	if err != nil {
    39  		return "", err
    40  	}
    41  	var containers []containersJSON
    42  	err = json.Unmarshal(data, &containers)
    43  	if err != nil {
    44  		return "", err
    45  	}
    46  
    47  	for _, c := range containers {
    48  		if c.ID == containerID {
    49  			return c.Layer, nil
    50  		}
    51  	}
    52  
    53  	return "", fmt.Errorf("unable to determine %v rw layer id", containerID)
    54  }