github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/image/reference/util.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     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 reference
    16  
    17  import (
    18  	"errors"
    19  	"strings"
    20  	"unicode"
    21  )
    22  
    23  const (
    24  	defaultDomain = "registry.cn-qingdao.aliyuncs.com"
    25  	defaultRepo   = "sealer-io"
    26  	defaultTag    = "latest"
    27  	localhost     = "localhost"
    28  )
    29  
    30  func validate(name string) error {
    31  	if name == "" {
    32  		return errors.New("empty image name is not allowed")
    33  	}
    34  
    35  	for _, c := range name {
    36  		if unicode.IsSpace(c) {
    37  			return errors.New("space is not allowed in image name")
    38  		}
    39  	}
    40  
    41  	return nil
    42  }
    43  
    44  func normalizeDomainRepoTag(name string) (domain, repoTag string) {
    45  	ind := strings.IndexRune(name, '/')
    46  	if ind >= 0 && (strings.ContainsAny(name[0:ind], ".:") || name[0:ind] == localhost) {
    47  		domain = name[0:ind]
    48  		repoTag = name[ind+1:]
    49  	} else {
    50  		domain = defaultDomain
    51  		repoTag = name
    52  	}
    53  	if domain == defaultDomain && !strings.ContainsRune(repoTag, '/') {
    54  		repoTag = defaultRepo + "/" + repoTag
    55  	}
    56  	if !strings.ContainsRune(repoTag, ':') {
    57  		repoTag = repoTag + ":" + defaultTag
    58  	}
    59  	return
    60  }
    61  
    62  // input: urlImageName could be like "***.com/k8s:v1.1" or "k8s:v1.1"
    63  // output: like "k8s:v1.1"
    64  func buildRepoAndTag(repoTag string) (string, string) {
    65  	splits := strings.Split(repoTag, ":")
    66  	return splits[0], splits[1]
    67  }
    68  
    69  func buildRaw(name string) string {
    70  	i := strings.LastIndexByte(name, ':')
    71  	if i == -1 {
    72  		return name + ":" + defaultTag
    73  	}
    74  	if i > strings.LastIndexByte(name, '/') {
    75  		return name
    76  	}
    77  	return name + ":" + defaultTag
    78  }