Skip to content

Commit

Permalink
Merge pull request kubernetes#104209 from liggitt/automated-cherry-pi…
Browse files Browse the repository at this point in the history
…ck-of-#104182-upstream-release-1.20

Automated cherry pick of kubernetes#104182: Avoid spurious calls to update/delete validation
  • Loading branch information
k8s-ci-robot authored Aug 10, 2021
2 parents 58dbff6 + e9d62ba commit cf10012
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions staging/src/k8s.io/apiserver/pkg/storage/etcd3/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,22 @@ func (s *store) GuaranteedUpdate(
}

// It's possible we were working with stale data
// Remember the revision of the potentially stale data and the resulting update error
cachedRev := origState.rev
cachedUpdateErr := err

// Actually fetch
origState, err = getCurrentState()
if err != nil {
return err
}
mustCheckData = false

// it turns out our cached data was not stale, return the error
if cachedRev == origState.rev {
return cachedUpdateErr
}

// Retry
continue
}
Expand Down
29 changes: 29 additions & 0 deletions staging/src/k8s.io/apiserver/pkg/storage/etcd3/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,35 @@ func TestGuaranteedUpdateWithSuggestionAndConflict(t *testing.T) {
if updatedPod2.Name != "foo-3" {
t.Errorf("unexpected pod name: %q", updatedPod2.Name)
}

// Third, update using a current version as the suggestion.
// Return an error and make sure that SimpleUpdate is NOT called a second time,
// since the live lookup shows the suggestion was already up to date.
attempts := 0
updatedPod3 := &example.Pod{}
err = store.GuaranteedUpdate(ctx, key, updatedPod3, false, nil,
storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
pod := obj.(*example.Pod)
if pod.Name != updatedPod2.Name || pod.ResourceVersion != updatedPod2.ResourceVersion {
t.Errorf(
"unexpected live object (name=%s, rv=%s), expected name=%s, rv=%s",
pod.Name,
pod.ResourceVersion,
updatedPod2.Name,
updatedPod2.ResourceVersion,
)
}
attempts++
return nil, fmt.Errorf("validation or admission error")
}),
updatedPod2,
)
if err == nil {
t.Fatalf("expected error, got none")
}
if attempts != 1 {
t.Errorf("expected 1 attempt, got %d", attempts)
}
}

func TestTransformationFailure(t *testing.T) {
Expand Down

0 comments on commit cf10012

Please sign in to comment.