patch_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package apiserver
  14. import (
  15. "context"
  16. "fmt"
  17. "sync"
  18. "sync/atomic"
  19. "testing"
  20. "github.com/google/uuid"
  21. v1 "k8s.io/api/core/v1"
  22. apierrors "k8s.io/apimachinery/pkg/api/errors"
  23. "k8s.io/apimachinery/pkg/api/meta"
  24. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. "k8s.io/apimachinery/pkg/types"
  26. "k8s.io/kubernetes/test/integration/framework"
  27. )
  28. // Tests that the apiserver retries patches
  29. func TestPatchConflicts(t *testing.T) {
  30. s, clientSet, closeFn := setup(t)
  31. defer closeFn()
  32. ns := framework.CreateTestingNamespace("status-code", s, t)
  33. defer framework.DeleteTestingNamespace(ns, s, t)
  34. numOfConcurrentPatches := 100
  35. UIDs := make([]types.UID, numOfConcurrentPatches)
  36. ownerRefs := []metav1.OwnerReference{}
  37. for i := 0; i < numOfConcurrentPatches; i++ {
  38. uid := types.UID(uuid.New().String())
  39. ownerName := fmt.Sprintf("owner-%d", i)
  40. UIDs[i] = uid
  41. ownerRefs = append(ownerRefs, metav1.OwnerReference{
  42. APIVersion: "example.com/v1",
  43. Kind: "Foo",
  44. Name: ownerName,
  45. UID: uid,
  46. })
  47. }
  48. secret := &v1.Secret{
  49. ObjectMeta: metav1.ObjectMeta{
  50. Name: "test",
  51. OwnerReferences: ownerRefs,
  52. },
  53. }
  54. // Create the object we're going to conflict on
  55. clientSet.CoreV1().Secrets(ns.Name).Create(context.TODO(), secret, metav1.CreateOptions{})
  56. client := clientSet.CoreV1().RESTClient()
  57. successes := int32(0)
  58. // Run a lot of simultaneous patch operations to exercise internal API server retry of application of patches that do not specify resourceVersion.
  59. // They should all succeed.
  60. wg := sync.WaitGroup{}
  61. for i := 0; i < numOfConcurrentPatches; i++ {
  62. wg.Add(1)
  63. go func(i int) {
  64. defer wg.Done()
  65. labelName := fmt.Sprintf("label-%d", i)
  66. value := uuid.New().String()
  67. obj, err := client.Patch(types.StrategicMergePatchType).
  68. Namespace(ns.Name).
  69. Resource("secrets").
  70. Name("test").
  71. Body([]byte(fmt.Sprintf(`{"metadata":{"labels":{"%s":"%s"}, "ownerReferences":[{"$patch":"delete","uid":"%s"}]}}`, labelName, value, UIDs[i]))).
  72. Do(context.TODO()).
  73. Get()
  74. if apierrors.IsConflict(err) {
  75. t.Logf("tolerated conflict error patching %s: %v", "secrets", err)
  76. return
  77. }
  78. if err != nil {
  79. t.Errorf("error patching %s: %v", "secrets", err)
  80. return
  81. }
  82. accessor, err := meta.Accessor(obj)
  83. if err != nil {
  84. t.Errorf("error getting object from %s: %v", "secrets", err)
  85. return
  86. }
  87. // make sure the label we wanted was effective
  88. if accessor.GetLabels()[labelName] != value {
  89. t.Errorf("patch of %s was ineffective, expected %s=%s, got labels %#v", "secrets", labelName, value, accessor.GetLabels())
  90. return
  91. }
  92. // make sure the patch directive didn't get lost, and that an entry in the ownerReference list was deleted.
  93. found := findOwnerRefByUID(accessor.GetOwnerReferences(), UIDs[i])
  94. if found {
  95. t.Errorf("patch of %s with $patch directive was ineffective, didn't delete the entry in the ownerReference slice: %#v", "secrets", UIDs[i])
  96. }
  97. atomic.AddInt32(&successes, 1)
  98. }(i)
  99. }
  100. wg.Wait()
  101. if successes < int32(numOfConcurrentPatches) {
  102. t.Errorf("Expected at least %d successful patches for %s, got %d", numOfConcurrentPatches, "secrets", successes)
  103. } else {
  104. t.Logf("Got %d successful patches for %s", successes, "secrets")
  105. }
  106. }
  107. func findOwnerRefByUID(ownerRefs []metav1.OwnerReference, uid types.UID) bool {
  108. for _, of := range ownerRefs {
  109. if of.UID == uid {
  110. return true
  111. }
  112. }
  113. return false
  114. }