-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
issue #6807: Retry failed create when using generateName #6830
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Retry failed create when using generateName | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
Copyright the Velero contributors. | ||
|
||
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 client | ||
|
||
import ( | ||
"context" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/client-go/util/retry" | ||
kbclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func CreateRetryGenerateName(client kbclient.Client, ctx context.Context, obj kbclient.Object) error { | ||
return CreateRetryGenerateNameWithFunc(obj, func() error { | ||
return client.Create(ctx, obj, &kbclient.CreateOptions{}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should CreateOptions be parameterized? FieldManager could become handy in the future. Also, this maybe what we should be using to restore managedFields during create call. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kaovilai There weren't any cases in the current code that used it, so it seemed simpler to avoid it. If we need to add CreateOptions in the future, we can add it. I can add it now (and just pass in the empty CreateOptions in all cases), but my thinking was that this was easier to read/maintain without it until we needed it. I don't have a strong opinion there, though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fine as is, just read on the options and saw ability to add managedfields which you might be interested in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kaovilai Yes, at some point we should probably open a separate issue to investigate better ways to restore managed fields. If we can avoid the extra Patch, that would be great. In any case, none of this code is used for resource restore, since we'll never restore anything that doesn't already have a name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cool. |
||
}) | ||
} | ||
|
||
func CreateRetryGenerateNameWithFunc(obj kbclient.Object, createFn func() error) error { | ||
retryCreateFn := func() error { | ||
// needed to ensure that the name from the failed create isn't left on the object between retries | ||
obj.SetName("") | ||
return createFn() | ||
} | ||
if obj.GetGenerateName() != "" && obj.GetName() == "" { | ||
return retry.OnError(retry.DefaultRetry, apierrors.IsAlreadyExists, retryCreateFn) | ||
} else { | ||
return createFn() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kaovilai This doesn't relate to restore. This is when Velero creates CRs with GenerateName. Anything we're restoring already has Name set, so GenerateName isn't involved.