patch_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "fmt"
  16. "sync"
  17. "sync/atomic"
  18. "testing"
  19. "github.com/pborman/uuid"
  20. "k8s.io/api/core/v1"
  21. "k8s.io/apimachinery/pkg/api/errors"
  22. "k8s.io/apimachinery/pkg/api/meta"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/types"
  25. "k8s.io/kubernetes/test/integration/framework"
  26. )
  27. // Tests that the apiserver retries patches
  28. func TestPatchConflicts(t *testing.T) {
  29. s, clientSet, closeFn := setup(t)
  30. defer closeFn()
  31. ns := framework.CreateTestingNamespace("status-code", s, t)
  32. defer framework.DeleteTestingNamespace(ns, s, t)
  33. numOfConcurrentPatches := 100
  34. UIDs := make([]types.UID, numOfConcurrentPatches)
  35. ownerRefs := []metav1.OwnerReference{}
  36. for i := 0; i < numOfConcurrentPatches; i++ {
  37. uid := types.UID(uuid.NewRandom().String())
  38. ownerName := fmt.Sprintf("owner-%d", i)
  39. UIDs[i] = uid
  40. ownerRefs = append(ownerRefs, metav1.OwnerReference{
  41. APIVersion: "example.com/v1",
  42. Kind: "Foo",
  43. Name: ownerName,
  44. UID: uid,
  45. })
  46. }
  47. secret := &v1.Secret{
  48. ObjectMeta: metav1.ObjectMeta{
  49. Name: "test",
  50. OwnerReferences: ownerRefs,
  51. },
  52. }
  53. // Create the object we're going to conflict on
  54. clientSet.CoreV1().Secrets(ns.Name).Create(secret)
  55. client := clientSet.CoreV1().RESTClient()
  56. successes := int32(0)
  57. // Run a lot of simultaneous patch operations to exercise internal API server retry of application of patches that do not specify resourceVersion.
  58. // They should all succeed.
  59. wg := sync.WaitGroup{}
  60. for i := 0; i < numOfConcurrentPatches; i++ {
  61. wg.Add(1)
  62. go func(i int) {
  63. defer wg.Done()
  64. labelName := fmt.Sprintf("label-%d", i)
  65. value := uuid.NewRandom().String()
  66. obj, err := client.Patch(types.StrategicMergePatchType).
  67. Namespace(ns.Name).
  68. Resource("secrets").
  69. Name("test").
  70. Body([]byte(fmt.Sprintf(`{"metadata":{"labels":{"%s":"%s"}, "ownerReferences":[{"$patch":"delete","uid":"%s"}]}}`, labelName, value, UIDs[i]))).
  71. Do().
  72. Get()
  73. if errors.IsConflict(err) {
  74. t.Logf("tolerated conflict error patching %s: %v", "secrets", err)
  75. return
  76. }
  77. if err != nil {
  78. t.Errorf("error patching %s: %v", "secrets", err)
  79. return
  80. }
  81. accessor, err := meta.Accessor(obj)
  82. if err != nil {
  83. t.Errorf("error getting object from %s: %v", "secrets", err)
  84. return
  85. }
  86. // make sure the label we wanted was effective
  87. if accessor.GetLabels()[labelName] != value {
  88. t.Errorf("patch of %s was ineffective, expected %s=%s, got labels %#v", "secrets", labelName, value, accessor.GetLabels())
  89. return
  90. }
  91. // make sure the patch directive didn't get lost, and that an entry in the ownerReference list was deleted.
  92. found := findOwnerRefByUID(accessor.GetOwnerReferences(), UIDs[i])
  93. if err != nil {
  94. t.Errorf("%v", err)
  95. return
  96. }
  97. if found {
  98. t.Errorf("patch of %s with $patch directive was ineffective, didn't delete the entry in the ownerReference slice: %#v", "secrets", UIDs[i])
  99. }
  100. atomic.AddInt32(&successes, 1)
  101. }(i)
  102. }
  103. wg.Wait()
  104. if successes < int32(numOfConcurrentPatches) {
  105. t.Errorf("Expected at least %d successful patches for %s, got %d", numOfConcurrentPatches, "secrets", successes)
  106. } else {
  107. t.Logf("Got %d successful patches for %s", successes, "secrets")
  108. }
  109. }
  110. func findOwnerRefByUID(ownerRefs []metav1.OwnerReference, uid types.UID) bool {
  111. for _, of := range ownerRefs {
  112. if of.UID == uid {
  113. return true
  114. }
  115. }
  116. return false
  117. }