github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/Godeps/_workspace/src/k8s.io/kubernetes/pkg/util/parsers/parsers.go (about)

     1  /*
     2  Copyright 2015 The Kubernetes Authors All rights reserved.
     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 parsers
    18  
    19  import (
    20  	"fmt"
    21  
    22  	dockerref "github.com/docker/distribution/reference"
    23  )
    24  
    25  const (
    26  	DefaultImageTag = "latest"
    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.
    31  func ParseImageName(image string) (string, string, string, error) {
    32  	named, err := dockerref.ParseNamed(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 = DefaultImageTag
    52  	}
    53  	return repoToPull, tag, digest, nil
    54  }