...

Source file src/github.com/google/go-containerregistry/pkg/crane/digest.go

Documentation: github.com/google/go-containerregistry/pkg/crane

     1  // Copyright 2018 Google LLC 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 crane
    16  
    17  import "github.com/google/go-containerregistry/pkg/logs"
    18  
    19  // Digest returns the sha256 hash of the remote image at ref.
    20  func Digest(ref string, opt ...Option) (string, error) {
    21  	o := makeOptions(opt...)
    22  	if o.Platform != nil {
    23  		desc, err := getManifest(ref, opt...)
    24  		if err != nil {
    25  			return "", err
    26  		}
    27  		if !desc.MediaType.IsIndex() {
    28  			return desc.Digest.String(), nil
    29  		}
    30  
    31  		// TODO: does not work for indexes which contain schema v1 manifests
    32  		img, err := desc.Image()
    33  		if err != nil {
    34  			return "", err
    35  		}
    36  		digest, err := img.Digest()
    37  		if err != nil {
    38  			return "", err
    39  		}
    40  		return digest.String(), nil
    41  	}
    42  	desc, err := Head(ref, opt...)
    43  	if err != nil {
    44  		logs.Warn.Printf("HEAD request failed, falling back on GET: %v", err)
    45  		rdesc, err := getManifest(ref, opt...)
    46  		if err != nil {
    47  			return "", err
    48  		}
    49  		return rdesc.Digest.String(), nil
    50  	}
    51  	return desc.Digest.String(), nil
    52  }
    53  

View as plain text