mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-04 15:05:20 +00:00
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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user