github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/api/rest/create.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 rest
    18  
    19  import (
    20  	"k8s.io/kubernetes/pkg/api"
    21  	"k8s.io/kubernetes/pkg/api/errors"
    22  	"k8s.io/kubernetes/pkg/api/validation"
    23  	"k8s.io/kubernetes/pkg/runtime"
    24  	utilvalidation "k8s.io/kubernetes/pkg/util/validation"
    25  )
    26  
    27  // RESTCreateStrategy defines the minimum validation, accepted input, and
    28  // name generation behavior to create an object that follows Kubernetes
    29  // API conventions.
    30  type RESTCreateStrategy interface {
    31  	runtime.ObjectTyper
    32  	// The name generate is used when the standard GenerateName field is set.
    33  	// The NameGenerator will be invoked prior to validation.
    34  	api.NameGenerator
    35  
    36  	// NamespaceScoped returns true if the object must be within a namespace.
    37  	NamespaceScoped() bool
    38  	// PrepareForCreate is invoked on create before validation to normalize
    39  	// the object.  For example: remove fields that are not to be persisted,
    40  	// sort order-insensitive list fields, etc.  This should not remove fields
    41  	// whose presence would be considered a validation error.
    42  	PrepareForCreate(obj runtime.Object)
    43  	// Validate is invoked after default fields in the object have been filled in before
    44  	// the object is persisted.  This method should not mutate the object.
    45  	Validate(ctx api.Context, obj runtime.Object) utilvalidation.ErrorList
    46  	// Canonicalize is invoked after validation has succeeded but before the
    47  	// object has been persisted.  This method may mutate the object.
    48  	Canonicalize(obj runtime.Object)
    49  }
    50  
    51  // BeforeCreate ensures that common operations for all resources are performed on creation. It only returns
    52  // errors that can be converted to api.Status. It invokes PrepareForCreate, then GenerateName, then Validate.
    53  // It returns nil if the object should be created.
    54  func BeforeCreate(strategy RESTCreateStrategy, ctx api.Context, obj runtime.Object) error {
    55  	objectMeta, kind, kerr := objectMetaAndKind(strategy, obj)
    56  	if kerr != nil {
    57  		return kerr
    58  	}
    59  
    60  	if strategy.NamespaceScoped() {
    61  		if !api.ValidNamespace(ctx, objectMeta) {
    62  			return errors.NewBadRequest("the namespace of the provided object does not match the namespace sent on the request")
    63  		}
    64  	} else {
    65  		objectMeta.Namespace = api.NamespaceNone
    66  	}
    67  	objectMeta.DeletionTimestamp = nil
    68  	objectMeta.DeletionGracePeriodSeconds = nil
    69  	strategy.PrepareForCreate(obj)
    70  	api.FillObjectMetaSystemFields(ctx, objectMeta)
    71  	api.GenerateName(strategy, objectMeta)
    72  
    73  	if errs := strategy.Validate(ctx, obj); len(errs) > 0 {
    74  		return errors.NewInvalid(kind, objectMeta.Name, errs)
    75  	}
    76  
    77  	// Custom validation (including name validation) passed
    78  	// Now run common validation on object meta
    79  	// Do this *after* custom validation so that specific error messages are shown whenever possible
    80  	if errs := validation.ValidateObjectMeta(objectMeta, strategy.NamespaceScoped(), validation.ValidatePathSegmentName); len(errs) > 0 {
    81  		return errors.NewInvalid(kind, objectMeta.Name, errs)
    82  	}
    83  
    84  	strategy.Canonicalize(obj)
    85  
    86  	return nil
    87  }
    88  
    89  // CheckGeneratedNameError checks whether an error that occurred creating a resource is due
    90  // to generation being unable to pick a valid name.
    91  func CheckGeneratedNameError(strategy RESTCreateStrategy, err error, obj runtime.Object) error {
    92  	if !errors.IsAlreadyExists(err) {
    93  		return err
    94  	}
    95  
    96  	objectMeta, kind, kerr := objectMetaAndKind(strategy, obj)
    97  	if kerr != nil {
    98  		return kerr
    99  	}
   100  
   101  	if len(objectMeta.GenerateName) == 0 {
   102  		return err
   103  	}
   104  
   105  	return errors.NewServerTimeout(kind, "POST", 0)
   106  }
   107  
   108  // objectMetaAndKind retrieves kind and ObjectMeta from a runtime object, or returns an error.
   109  func objectMetaAndKind(typer runtime.ObjectTyper, obj runtime.Object) (*api.ObjectMeta, string, error) {
   110  	objectMeta, err := api.ObjectMetaFor(obj)
   111  	if err != nil {
   112  		return nil, "", errors.NewInternalError(err)
   113  	}
   114  	_, kind, err := typer.ObjectVersionAndKind(obj)
   115  	if err != nil {
   116  		return nil, "", errors.NewInternalError(err)
   117  	}
   118  	return objectMeta, kind, nil
   119  }