2015-01-27 23:56:38 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2015-03-22 21:40:47 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
|
2015-01-27 23:56:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RESTCreateStrategy defines the minimum validation, accepted input, and
|
|
|
|
// name generation behavior to create an object that follows Kubernetes
|
|
|
|
// API conventions.
|
|
|
|
type RESTCreateStrategy interface {
|
|
|
|
runtime.ObjectTyper
|
|
|
|
// The name generate is used when the standard GenerateName field is set.
|
|
|
|
// The NameGenerator will be invoked prior to validation.
|
|
|
|
api.NameGenerator
|
|
|
|
|
2015-01-29 02:56:57 +00:00
|
|
|
// NamespaceScoped returns true if the object must be within a namespace.
|
|
|
|
NamespaceScoped() bool
|
2015-01-27 23:56:38 +00:00
|
|
|
// ResetBeforeCreate is invoked on create before validation to remove any fields
|
|
|
|
// that may not be persisted.
|
|
|
|
ResetBeforeCreate(obj runtime.Object)
|
|
|
|
// Validate is invoked after default fields in the object have been filled in before
|
|
|
|
// the object is persisted.
|
2015-03-22 21:40:47 +00:00
|
|
|
Validate(obj runtime.Object) fielderrors.ValidationErrorList
|
2015-01-27 23:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BeforeCreate ensures that common operations for all resources are performed on creation. It only returns
|
|
|
|
// errors that can be converted to api.Status. It invokes ResetBeforeCreate, then GenerateName, then Validate.
|
|
|
|
// It returns nil if the object should be created.
|
|
|
|
func BeforeCreate(strategy RESTCreateStrategy, ctx api.Context, obj runtime.Object) error {
|
2015-01-29 04:11:29 +00:00
|
|
|
objectMeta, kind, kerr := objectMetaAndKind(strategy, obj)
|
|
|
|
if kerr != nil {
|
|
|
|
return kerr
|
2015-01-27 23:56:38 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 02:56:57 +00:00
|
|
|
if strategy.NamespaceScoped() {
|
|
|
|
if !api.ValidNamespace(ctx, objectMeta) {
|
|
|
|
return errors.NewBadRequest("the namespace of the provided object does not match the namespace sent on the request")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
objectMeta.Namespace = api.NamespaceNone
|
2015-01-27 23:56:38 +00:00
|
|
|
}
|
|
|
|
strategy.ResetBeforeCreate(obj)
|
|
|
|
api.FillObjectMetaSystemFields(ctx, objectMeta)
|
|
|
|
api.GenerateName(strategy, objectMeta)
|
|
|
|
|
|
|
|
if errs := strategy.Validate(obj); len(errs) > 0 {
|
|
|
|
return errors.NewInvalid(kind, objectMeta.Name, errs)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-01-29 04:11:29 +00:00
|
|
|
|
2015-03-23 18:18:11 +00:00
|
|
|
// CheckGeneratedNameError checks whether an error that occurred creating a resource is due
|
2015-01-29 04:11:29 +00:00
|
|
|
// to generation being unable to pick a valid name.
|
|
|
|
func CheckGeneratedNameError(strategy RESTCreateStrategy, err error, obj runtime.Object) error {
|
|
|
|
if !errors.IsAlreadyExists(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
objectMeta, kind, kerr := objectMetaAndKind(strategy, obj)
|
|
|
|
if kerr != nil {
|
|
|
|
return kerr
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(objectMeta.GenerateName) == 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-12 17:24:34 +00:00
|
|
|
return errors.NewServerTimeout(kind, "POST")
|
2015-01-29 04:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// objectMetaAndKind retrieves kind and ObjectMeta from a runtime object, or returns an error.
|
2015-02-11 23:32:54 +00:00
|
|
|
func objectMetaAndKind(typer runtime.ObjectTyper, obj runtime.Object) (*api.ObjectMeta, string, error) {
|
2015-01-29 04:11:29 +00:00
|
|
|
objectMeta, err := api.ObjectMetaFor(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", errors.NewInternalError(err)
|
|
|
|
}
|
2015-02-11 23:32:54 +00:00
|
|
|
_, kind, err := typer.ObjectVersionAndKind(obj)
|
2015-01-29 04:11:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", errors.NewInternalError(err)
|
|
|
|
}
|
|
|
|
return objectMeta, kind, nil
|
|
|
|
}
|