github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/api/validation/name.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 validation
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  )
    23  
    24  // NameMayNotBe specifies strings that cannot be used as names specified as path segments (like the REST API or etcd store)
    25  var NameMayNotBe = []string{".", ".."}
    26  
    27  // NameMayNotContain specifies substrings that cannot be used in names specified as path segments (like the REST API or etcd store)
    28  var NameMayNotContain = []string{"/", "%"}
    29  
    30  // IsValidPathSegmentName validates the name can be safely encoded as a path segment
    31  func IsValidPathSegmentName(name string) (bool, string) {
    32  	for _, illegalName := range NameMayNotBe {
    33  		if name == illegalName {
    34  			return false, fmt.Sprintf(`name may not be %q`, illegalName)
    35  		}
    36  	}
    37  
    38  	for _, illegalContent := range NameMayNotContain {
    39  		if strings.Contains(name, illegalContent) {
    40  			return false, fmt.Sprintf(`name may not contain %q`, illegalContent)
    41  		}
    42  	}
    43  
    44  	return true, ""
    45  }
    46  
    47  // IsValidPathSegmentPrefix validates the name can be used as a prefix for a name which will be encoded as a path segment
    48  // It does not check for exact matches with disallowed names, since an arbitrary suffix might make the name valid
    49  func IsValidPathSegmentPrefix(name string) (bool, string) {
    50  	for _, illegalContent := range NameMayNotContain {
    51  		if strings.Contains(name, illegalContent) {
    52  			return false, fmt.Sprintf(`name may not contain %q`, illegalContent)
    53  		}
    54  	}
    55  
    56  	return true, ""
    57  }
    58  
    59  // ValidatePathSegmentName validates the name can be safely encoded as a path segment
    60  func ValidatePathSegmentName(name string, prefix bool) (bool, string) {
    61  	if prefix {
    62  		return IsValidPathSegmentPrefix(name)
    63  	} else {
    64  		return IsValidPathSegmentName(name)
    65  	}
    66  }