github.com/containerd/nerdctl@v1.7.7/pkg/imageinspector/imageinspector.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package imageinspector
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/containerd/containerd"
    23  	"github.com/containerd/containerd/images"
    24  	"github.com/containerd/log"
    25  	imgutil "github.com/containerd/nerdctl/pkg/imgutil"
    26  	"github.com/containerd/nerdctl/pkg/inspecttypes/native"
    27  )
    28  
    29  // Inspect inspects the image, for the platform specified in image.platform.
    30  func Inspect(ctx context.Context, client *containerd.Client, image images.Image, snapshotter string) (*native.Image, error) {
    31  
    32  	n := &native.Image{}
    33  
    34  	img := containerd.NewImage(client, image)
    35  	idx, idxDesc, err := imgutil.ReadIndex(ctx, img)
    36  	if err != nil {
    37  		log.G(ctx).WithError(err).WithField("id", image.Name).Warnf("failed to inspect index")
    38  	} else {
    39  		n.IndexDesc = idxDesc
    40  		n.Index = idx
    41  	}
    42  
    43  	mani, maniDesc, err := imgutil.ReadManifest(ctx, img)
    44  	if err != nil {
    45  		log.G(ctx).WithError(err).WithField("id", image.Name).Warnf("failed to inspect manifest")
    46  	} else {
    47  		n.ManifestDesc = maniDesc
    48  		n.Manifest = mani
    49  	}
    50  
    51  	imageConfig, imageConfigDesc, err := imgutil.ReadImageConfig(ctx, img)
    52  	if err != nil {
    53  		log.G(ctx).WithError(err).WithField("id", image.Name).Warnf("failed to inspect image config")
    54  	} else {
    55  		n.ImageConfigDesc = imageConfigDesc
    56  		n.ImageConfig = imageConfig
    57  	}
    58  	snapSvc := client.SnapshotService(snapshotter)
    59  	n.Size, err = imgutil.UnpackedImageSize(ctx, snapSvc, img)
    60  	if err != nil {
    61  		log.G(ctx).WithError(err).WithField("id", image.Name).Warnf("failed to inspect calculate size")
    62  	}
    63  	n.Image = image
    64  
    65  	return n, nil
    66  }