1/* 2Copyright 2015 The Kubernetes Authors. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package parsers 18 19import ( 20 "fmt" 21 // Import the crypto sha256 algorithm for the docker image parser to work 22 _ "crypto/sha256" 23 // Import the crypto/sha512 algorithm for the docker image parser to work with 384 and 512 sha hashes 24 _ "crypto/sha512" 25 26 dockerref "github.com/docker/distribution/reference" 27) 28 29// ParseImageName parses a docker image string into three parts: repo, tag and digest. 30// If both tag and digest are empty, a default image tag will be returned. 31func ParseImageName(image string) (string, string, string, error) { 32 named, err := dockerref.ParseNormalizedNamed(image) 33 if err != nil { 34 return "", "", "", fmt.Errorf("couldn't parse image name: %v", err) 35 } 36 37 repoToPull := named.Name() 38 var tag, digest string 39 40 tagged, ok := named.(dockerref.Tagged) 41 if ok { 42 tag = tagged.Tag() 43 } 44 45 digested, ok := named.(dockerref.Digested) 46 if ok { 47 digest = digested.Digest().String() 48 } 49 // If no tag was specified, use the default "latest". 50 if len(tag) == 0 && len(digest) == 0 { 51 tag = "latest" 52 } 53 return repoToPull, tag, digest, nil 54} 55