Turn 409 into 500 Try Again Later when using generateName

If a client says they want the name to be generated, a 409 is
not appropriate (since they didn't specify a name). Instead, we
should return the next most appropriate error, which is a 5xx
error indicating the request failed but the client *should* try
again.  Since there is no 5xx error that exactly fits this purpose,
use 500 with StatusReasonTryAgainLater set.

This commit does not implement client retry on TryAgainLater, but
clients should retry up to a certain number of times.
This commit is contained in:
Clayton Coleman
2015-01-28 23:11:29 -05:00
parent e485dc93ca
commit 1588970ec4
21 changed files with 237 additions and 18 deletions

View File

@@ -45,13 +45,9 @@ type RESTCreateStrategy interface {
// 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 {
_, kind, err := strategy.ObjectVersionAndKind(obj)
if err != nil {
return errors.NewInternalError(err)
}
objectMeta, err := api.ObjectMetaFor(obj)
if err != nil {
return errors.NewInternalError(err)
objectMeta, kind, kerr := objectMetaAndKind(strategy, obj)
if kerr != nil {
return kerr
}
if strategy.NamespaceScoped() {
@@ -70,3 +66,35 @@ func BeforeCreate(strategy RESTCreateStrategy, ctx api.Context, obj runtime.Obje
}
return nil
}
// CheckGeneratedNameError checks whether an error that occured creating a resource is due
// 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
}
return errors.NewTryAgainLater(kind, "POST")
}
// objectMetaAndKind retrieves kind and ObjectMeta from a runtime object, or returns an error.
func objectMetaAndKind(strategy RESTCreateStrategy, obj runtime.Object) (*api.ObjectMeta, string, error) {
objectMeta, err := api.ObjectMetaFor(obj)
if err != nil {
return nil, "", errors.NewInternalError(err)
}
_, kind, err := strategy.ObjectVersionAndKind(obj)
if err != nil {
return nil, "", errors.NewInternalError(err)
}
return objectMeta, kind, nil
}

View File

@@ -0,0 +1,41 @@
/*
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 (
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
)
func TestCheckGeneratedNameError(t *testing.T) {
expect := errors.NewNotFound("foo", "bar")
if err := CheckGeneratedNameError(Pods, expect, &api.Pod{}); err != expect {
t.Errorf("NotFoundError should be ignored: %v", err)
}
expect = errors.NewAlreadyExists("foo", "bar")
if err := CheckGeneratedNameError(Pods, expect, &api.Pod{}); err != expect {
t.Errorf("AlreadyExists should be returned when no GenerateName field: %v", err)
}
expect = errors.NewAlreadyExists("foo", "bar")
if err := CheckGeneratedNameError(Pods, expect, &api.Pod{ObjectMeta: api.ObjectMeta{GenerateName: "foo"}}); err == nil || !errors.IsTryAgainLater(err) {
t.Errorf("expected try again later error: %v", err)
}
}

View File

@@ -30,16 +30,26 @@ import (
type Tester struct {
*testing.T
storage apiserver.RESTStorage
storageError injectErrorFunc
clusterScope bool
}
func New(t *testing.T, storage apiserver.RESTStorage) *Tester {
type injectErrorFunc func(err error)
func New(t *testing.T, storage apiserver.RESTStorage, storageError injectErrorFunc) *Tester {
return &Tester{
T: t,
storage: storage,
T: t,
storage: storage,
storageError: storageError,
}
}
func (t *Tester) withStorageError(err error, fn func()) {
t.storageError(err)
defer t.storageError(nil)
fn()
}
func (t *Tester) ClusterScope() *Tester {
t.clusterScope = true
return t
@@ -56,6 +66,7 @@ func copyOrDie(obj runtime.Object) runtime.Object {
func (t *Tester) TestCreate(valid runtime.Object, invalid ...runtime.Object) {
t.TestCreateHasMetadata(copyOrDie(valid))
t.TestCreateGeneratesName(copyOrDie(valid))
t.TestCreateGeneratesNameReturnsTryAgain(copyOrDie(valid))
if t.clusterScope {
t.TestCreateRejectsNamespace(copyOrDie(valid))
} else {
@@ -129,6 +140,25 @@ func (t *Tester) TestCreateGeneratesName(valid runtime.Object) {
}
}
func (t *Tester) TestCreateGeneratesNameReturnsTryAgain(valid runtime.Object) {
objectMeta, err := api.ObjectMetaFor(valid)
if err != nil {
t.Fatalf("object does not have ObjectMeta: %v\n%#v", err, valid)
}
objectMeta.GenerateName = "test-"
t.withStorageError(errors.NewAlreadyExists("kind", "thing"), func() {
ch, err := t.storage.(apiserver.RESTCreater).Create(api.NewDefaultContext(), valid)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
res := <-ch
if err := errors.FromObject(res.Object); err == nil || !errors.IsTryAgainLater(err) {
t.Fatalf("Unexpected error: %v", err)
}
})
}
func (t *Tester) TestCreateInvokesValidation(invalid ...runtime.Object) {
for i, obj := range invalid {
ctx := api.NewDefaultContext()