istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/util/image/registry.go (about) 1 // Copyright Istio Authors 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 image 16 17 import ( 18 "fmt" 19 "strings" 20 21 "github.com/google/go-containerregistry/pkg/authn" 22 "github.com/google/go-containerregistry/pkg/name" 23 "github.com/google/go-containerregistry/pkg/v1/remote" 24 "github.com/google/go-containerregistry/pkg/v1/remote/transport" 25 ) 26 27 // Exists returns true if the image in the argument exists in a container registry. 28 // The argument must be a complete image name, e.g. "gcr.io/istio-release/pilot:1.20.0". 29 // If the image does not exist, it returns false and an optional error message, for debug purposes. 30 func Exists(image string) (bool, error) { 31 ref, err := name.ParseReference(image) 32 if err != nil { 33 return false, fmt.Errorf("parsing reference %q: %w", image, err) 34 } 35 _, err = remote.Get(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain)) 36 if err == nil { 37 // image exists 38 return true, nil 39 } 40 isUnknown := strings.Contains(err.Error(), string(transport.ManifestUnknownErrorCode)) 41 if isUnknown { 42 // image does not exist 43 return false, nil 44 } 45 // Some other error 46 return false, err 47 }