github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/api/generate.go (about)

     1  /*
     2  Copyright 2014 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 api
    18  
    19  import (
    20  	"fmt"
    21  
    22  	utilrand "k8s.io/kubernetes/pkg/util/rand"
    23  )
    24  
    25  // NameGenerator generates names for objects. Some backends may have more information
    26  // available to guide selection of new names and this interface hides those details.
    27  type NameGenerator interface {
    28  	// GenerateName generates a valid name from the base name, adding a random suffix to the
    29  	// the base. If base is valid, the returned name must also be valid. The generator is
    30  	// responsible for knowing the maximum valid name length.
    31  	GenerateName(base string) string
    32  }
    33  
    34  // GenerateName will resolve the object name of the provided ObjectMeta to a generated version if
    35  // necessary. It expects that validation for ObjectMeta has already completed (that Base is a
    36  // valid name) and that the NameGenerator generates a name that is also valid.
    37  func GenerateName(u NameGenerator, meta *ObjectMeta) {
    38  	if len(meta.GenerateName) == 0 || len(meta.Name) != 0 {
    39  		return
    40  	}
    41  	meta.Name = u.GenerateName(meta.GenerateName)
    42  }
    43  
    44  // simpleNameGenerator generates random names.
    45  type simpleNameGenerator struct{}
    46  
    47  // SimpleNameGenerator is a generator that returns the name plus a random suffix of five alphanumerics
    48  // when a name is requested. The string is guaranteed to not exceed the length of a standard Kubernetes
    49  // name (63 characters)
    50  var SimpleNameGenerator NameGenerator = simpleNameGenerator{}
    51  
    52  const (
    53  	// TODO: make this flexible for non-core resources with alternate naming rules.
    54  	maxNameLength          = 63
    55  	randomLength           = 5
    56  	maxGeneratedNameLength = maxNameLength - randomLength
    57  )
    58  
    59  func (simpleNameGenerator) GenerateName(base string) string {
    60  	if len(base) > maxGeneratedNameLength {
    61  		base = base[:maxGeneratedNameLength]
    62  	}
    63  	return fmt.Sprintf("%s%s", base, utilrand.String(randomLength))
    64  }