-
Notifications
You must be signed in to change notification settings - Fork 675
Adds retry-on-conflict during updates #725
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
Merged
k8s-ci-robot
merged 3 commits into
kubernetes-sigs:master
from
chuckha:retry-on-conflict
Apr 19, 2019
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,8 @@ import ( | |
| "github.com/aws/aws-sdk-go/service/elb" | ||
| "github.com/go-logr/logr" | ||
| "github.com/pkg/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/util/retry" | ||
| "k8s.io/klog/klogr" | ||
| "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1alpha1" | ||
| clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1" | ||
|
|
@@ -131,40 +133,46 @@ func (s *Scope) Region() string { | |
| return s.ClusterConfig.Region | ||
| } | ||
|
|
||
| func (s *Scope) storeClusterConfig(cluster *clusterv1.Cluster) (*clusterv1.Cluster, error) { | ||
| ext, err := v1alpha1.EncodeClusterSpec(s.ClusterConfig) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| cluster.Spec.ProviderSpec.Value = ext | ||
| return s.ClusterClient.Update(cluster) | ||
| } | ||
|
|
||
| func (s *Scope) storeClusterStatus(cluster *clusterv1.Cluster) (*clusterv1.Cluster, error) { | ||
| ext, err := v1alpha1.EncodeClusterStatus(s.ClusterStatus) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| cluster.Status.ProviderStatus = ext | ||
| return s.ClusterClient.UpdateStatus(cluster) | ||
| } | ||
|
|
||
| // Close closes the current scope persisting the cluster configuration and status. | ||
| func (s *Scope) Close() { | ||
| if s.ClusterClient == nil { | ||
| return | ||
| } | ||
|
|
||
| latestCluster, err := s.storeClusterConfig(s.Cluster) | ||
| ext, err := v1alpha1.EncodeClusterSpec(s.ClusterConfig) | ||
| if err != nil { | ||
| s.Error(err, "failed to store provider config") | ||
| s.Error(err, "failed encoding cluster spec") | ||
| return | ||
| } | ||
|
|
||
| _, err = s.storeClusterStatus(latestCluster) | ||
| status, err := v1alpha1.EncodeClusterStatus(s.ClusterStatus) | ||
| if err != nil { | ||
| s.Error(err, "failed to store provider status") | ||
| s.Error(err, "failed encoding cluster status") | ||
| return | ||
| } | ||
|
|
||
| // Update the resource version and try again if there is a conflict saving the object. | ||
| if err := retry.RetryOnConflict(retry.DefaultBackoff, func() error { | ||
|
chuckha marked this conversation as resolved.
Outdated
|
||
| s.V(2).Info("Updating cluster", "cluster-resource-version", s.Cluster.ResourceVersion) | ||
| s.Cluster.Spec.ProviderSpec.Value = ext | ||
| s.V(6).Info("Cluster status before update", "cluster-status", s.Cluster.Status) | ||
| latest, err := s.ClusterClient.Update(s.Cluster) | ||
| if err != nil { | ||
| s.V(3).Info("Cluster resource version is out of date") | ||
| // Fetch and update the latest resource version | ||
| newestCluster, err2 := s.ClusterClient.Get(s.Cluster.Name, metav1.GetOptions{}) | ||
| if err2 != nil { | ||
| s.Error(err2, "failed to fetch latest cluster") | ||
| return err2 | ||
| } | ||
| s.Cluster.ResourceVersion = newestCluster.ResourceVersion | ||
| return err | ||
| } | ||
| s.V(5).Info("Latest cluster status", "cluster-status", latest.Status) | ||
| s.Cluster.Status.DeepCopyInto(&latest.Status) | ||
| latest.Status.ProviderStatus = status | ||
| _, err = s.ClusterClient.UpdateStatus(latest) | ||
|
Contributor
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 we add a retry around updating the status as well? |
||
| return err | ||
| }); err != nil { | ||
| s.Error(err, "failed to retry on conflict") | ||
| } | ||
| s.V(2).Info("Successfully updated cluster") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should there also be a retry around UpdateStatus() here as well?
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.
update status doesn't need a retry. If the update status does fail then we'll run the Update again and get the new resource version and try the UpdateStatus again.
I think the biggest issue we could see is if there were multiple things updating this resource regularly, then we may have to consider breaking this Update & UpdateStatus from one operation into two with individual retry blocks.