Unverified Commit 4b260d7d authored by Bryan Liles's avatar Bryan Liles Committed by GitHub
Browse files

Merge pull request #787 from Fresh-Tracks/fetch-before-retry

Get a new version of the object when retrying on conflict
parents 4ae36223 5ac8a7fc
......@@ -204,6 +204,11 @@ func (a *Apply) upsert(obj *unstructured.Unstructured) (string, error) {
if !kerrors.IsConflict(cause) {
return "", err
}
// In order for the next try to work, update the resource version on the object
updatedObj, err := a.getUpdatedObject(obj)
if err == nil {
obj.SetResourceVersion(updatedObj.GetResourceVersion())
}
time.Sleep(a.conflictTimeout)
continue
......@@ -215,6 +220,14 @@ func (a *Apply) upsert(obj *unstructured.Unstructured) (string, error) {
return "", errApplyConflict
}
func (a *Apply) getUpdatedObject(obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
rc, err := a.resourceClientFactory(*a.clientOpts, obj)
if err != nil {
return nil, err
}
return rc.Get(metav1.GetOptions{})
}
// setupGC setups ksonnet's garbage collection process for objects.
func (a *Apply) setupGC(obj *unstructured.Unstructured) {
if a.GcTag != "" {
......
......@@ -20,13 +20,16 @@ import (
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"github.com/ksonnet/ksonnet/pkg/app"
amocks "github.com/ksonnet/ksonnet/pkg/app/mocks"
"github.com/ksonnet/ksonnet/pkg/client"
"github.com/ksonnet/ksonnet/pkg/cluster/mocks"
"github.com/ksonnet/ksonnet/pkg/util/test"
"github.com/pkg/errors"
"github.com/spf13/afero"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
......@@ -130,6 +133,11 @@ func Test_Apply_retry_on_conflict(t *testing.T) {
obj := &unstructured.Unstructured{Object: genObject()}
apply.clientOpts = &clientOpts{}
apply.resourceClientFactory = func(opts clientOpts, object runtime.Object) (ResourceClient, error) {
rc := &mocks.ResourceClient{}
rc.On("Get", mock.Anything).Return(obj, nil)
return rc, nil
}
apply.findObjectsFn = func(a app.App, envName string, componentNames []string) ([]*unstructured.Unstructured, error) {
objects := []*unstructured.Unstructured{obj}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment