crdregistration_controller_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 crdregistration
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
  18. crdlisters "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/internalversion"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/runtime/schema"
  21. "k8s.io/client-go/tools/cache"
  22. "k8s.io/kube-aggregator/pkg/apis/apiregistration"
  23. )
  24. func TestHandleVersionUpdate(t *testing.T) {
  25. tests := []struct {
  26. name string
  27. startingCRDs []*apiextensions.CustomResourceDefinition
  28. version schema.GroupVersion
  29. expectedAdded []*apiregistration.APIService
  30. expectedRemoved []string
  31. }{
  32. {
  33. name: "simple add crd",
  34. startingCRDs: []*apiextensions.CustomResourceDefinition{
  35. {
  36. Spec: apiextensions.CustomResourceDefinitionSpec{
  37. Group: "group.com",
  38. // Version field is deprecated and crd registration won't rely on it at all.
  39. // defaulting route will fill up Versions field if user only provided version field.
  40. Versions: []apiextensions.CustomResourceDefinitionVersion{
  41. {
  42. Name: "v1",
  43. Served: true,
  44. Storage: true,
  45. },
  46. },
  47. },
  48. },
  49. },
  50. version: schema.GroupVersion{Group: "group.com", Version: "v1"},
  51. expectedAdded: []*apiregistration.APIService{
  52. {
  53. ObjectMeta: metav1.ObjectMeta{Name: "v1.group.com"},
  54. Spec: apiregistration.APIServiceSpec{
  55. Group: "group.com",
  56. Version: "v1",
  57. GroupPriorityMinimum: 1000,
  58. VersionPriority: 100,
  59. },
  60. },
  61. },
  62. },
  63. {
  64. name: "simple remove crd",
  65. startingCRDs: []*apiextensions.CustomResourceDefinition{
  66. {
  67. Spec: apiextensions.CustomResourceDefinitionSpec{
  68. Group: "group.com",
  69. Versions: []apiextensions.CustomResourceDefinitionVersion{
  70. {
  71. Name: "v1",
  72. Served: true,
  73. Storage: true,
  74. },
  75. },
  76. },
  77. },
  78. },
  79. version: schema.GroupVersion{Group: "group.com", Version: "v2"},
  80. expectedRemoved: []string{"v2.group.com"},
  81. },
  82. }
  83. for _, test := range tests {
  84. t.Run(test.name, func(t *testing.T) {
  85. registration := &fakeAPIServiceRegistration{}
  86. crdCache := cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
  87. crdLister := crdlisters.NewCustomResourceDefinitionLister(crdCache)
  88. c := crdRegistrationController{
  89. crdLister: crdLister,
  90. apiServiceRegistration: registration,
  91. }
  92. for i := range test.startingCRDs {
  93. crdCache.Add(test.startingCRDs[i])
  94. }
  95. c.handleVersionUpdate(test.version)
  96. if !reflect.DeepEqual(test.expectedAdded, registration.added) {
  97. t.Errorf("%s expected %v, got %v", test.name, test.expectedAdded, registration.added)
  98. }
  99. if !reflect.DeepEqual(test.expectedRemoved, registration.removed) {
  100. t.Errorf("%s expected %v, got %v", test.name, test.expectedRemoved, registration.removed)
  101. }
  102. })
  103. }
  104. }
  105. type fakeAPIServiceRegistration struct {
  106. added []*apiregistration.APIService
  107. removed []string
  108. }
  109. func (a *fakeAPIServiceRegistration) AddAPIServiceToSync(in *apiregistration.APIService) {
  110. a.added = append(a.added, in)
  111. }
  112. func (a *fakeAPIServiceRegistration) RemoveAPIServiceToSync(name string) {
  113. a.removed = append(a.removed, name)
  114. }