custom_resource_definition.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. Copyright 2016 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 apimachinery
  14. import (
  15. "context"
  16. "fmt"
  17. "time"
  18. "github.com/onsi/ginkgo"
  19. v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  20. "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
  21. "k8s.io/apiextensions-apiserver/test/integration/fixtures"
  22. "k8s.io/apimachinery/pkg/api/equality"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  25. "k8s.io/apimachinery/pkg/runtime"
  26. "k8s.io/apimachinery/pkg/runtime/schema"
  27. "k8s.io/apimachinery/pkg/types"
  28. "k8s.io/apimachinery/pkg/util/diff"
  29. "k8s.io/apimachinery/pkg/util/uuid"
  30. "k8s.io/apimachinery/pkg/util/wait"
  31. "k8s.io/apiserver/pkg/storage/names"
  32. "k8s.io/client-go/dynamic"
  33. "k8s.io/client-go/util/retry"
  34. "k8s.io/kubernetes/test/e2e/framework"
  35. )
  36. var _ = SIGDescribe("CustomResourceDefinition resources [Privileged:ClusterAdmin]", func() {
  37. f := framework.NewDefaultFramework("custom-resource-definition")
  38. ginkgo.Context("Simple CustomResourceDefinition", func() {
  39. /*
  40. Release : v1.9
  41. Testname: Custom Resource Definition, create
  42. Description: Create a API extension client and define a random custom resource definition.
  43. Create the custom resource definition and then delete it. The creation and deletion MUST
  44. be successful.
  45. */
  46. framework.ConformanceIt("creating/deleting custom resource definition objects works ", func() {
  47. config, err := framework.LoadConfig()
  48. framework.ExpectNoError(err, "loading config")
  49. apiExtensionClient, err := clientset.NewForConfig(config)
  50. framework.ExpectNoError(err, "initializing apiExtensionClient")
  51. randomDefinition := fixtures.NewRandomNameV1CustomResourceDefinition(v1.ClusterScoped)
  52. // Create CRD and waits for the resource to be recognized and available.
  53. randomDefinition, err = fixtures.CreateNewV1CustomResourceDefinitionWatchUnsafe(randomDefinition, apiExtensionClient)
  54. framework.ExpectNoError(err, "creating CustomResourceDefinition")
  55. defer func() {
  56. err = fixtures.DeleteV1CustomResourceDefinition(randomDefinition, apiExtensionClient)
  57. framework.ExpectNoError(err, "deleting CustomResourceDefinition")
  58. }()
  59. })
  60. /*
  61. Release : v1.16
  62. Testname: Custom Resource Definition, list
  63. Description: Create a API extension client, define 10 labeled custom resource definitions and list them using
  64. a label selector; the list result MUST contain only the labeled custom resource definitions. Delete the labeled
  65. custom resource definitions via delete collection; the delete MUST be successful and MUST delete only the
  66. labeled custom resource definitions.
  67. */
  68. framework.ConformanceIt("listing custom resource definition objects works ", func() {
  69. testListSize := 10
  70. config, err := framework.LoadConfig()
  71. framework.ExpectNoError(err, "loading config")
  72. apiExtensionClient, err := clientset.NewForConfig(config)
  73. framework.ExpectNoError(err, "initializing apiExtensionClient")
  74. // Label the CRDs we create so we can list only them even though they are cluster scoped
  75. testUUID := string(uuid.NewUUID())
  76. // Create CRD and wait for the resource to be recognized and available.
  77. crds := make([]*v1.CustomResourceDefinition, testListSize)
  78. for i := 0; i < testListSize; i++ {
  79. crd := fixtures.NewRandomNameV1CustomResourceDefinition(v1.ClusterScoped)
  80. crd.Labels = map[string]string{"e2e-list-test-uuid": testUUID}
  81. crd, err = fixtures.CreateNewV1CustomResourceDefinitionWatchUnsafe(crd, apiExtensionClient)
  82. framework.ExpectNoError(err, "creating CustomResourceDefinition")
  83. crds[i] = crd
  84. }
  85. // Create a crd w/o the label to ensure the label selector matching works correctly
  86. crd := fixtures.NewRandomNameV1CustomResourceDefinition(v1.ClusterScoped)
  87. crd, err = fixtures.CreateNewV1CustomResourceDefinitionWatchUnsafe(crd, apiExtensionClient)
  88. framework.ExpectNoError(err, "creating CustomResourceDefinition")
  89. defer func() {
  90. err = fixtures.DeleteV1CustomResourceDefinition(crd, apiExtensionClient)
  91. framework.ExpectNoError(err, "deleting CustomResourceDefinition")
  92. }()
  93. selectorListOpts := metav1.ListOptions{LabelSelector: "e2e-list-test-uuid=" + testUUID}
  94. list, err := apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().List(context.TODO(), selectorListOpts)
  95. framework.ExpectNoError(err, "listing CustomResourceDefinitions")
  96. framework.ExpectEqual(len(list.Items), testListSize)
  97. for _, actual := range list.Items {
  98. var expected *v1.CustomResourceDefinition
  99. for _, e := range crds {
  100. if e.Name == actual.Name && e.Namespace == actual.Namespace {
  101. expected = e
  102. }
  103. }
  104. framework.ExpectNotEqual(expected, nil)
  105. if !equality.Semantic.DeepEqual(actual.Spec, expected.Spec) {
  106. framework.Failf("Expected CustomResourceDefinition in list with name %s to match crd created with same name, but got different specs:\n%s",
  107. actual.Name, diff.ObjectReflectDiff(expected.Spec, actual.Spec))
  108. }
  109. }
  110. // Use delete collection to remove the CRDs
  111. err = fixtures.DeleteV1CustomResourceDefinitions(selectorListOpts, apiExtensionClient)
  112. framework.ExpectNoError(err, "deleting CustomResourceDefinitions")
  113. _, err = apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Get(context.TODO(), crd.Name, metav1.GetOptions{})
  114. framework.ExpectNoError(err, "getting remaining CustomResourceDefinition")
  115. })
  116. /*
  117. Release : v1.16
  118. Testname: Custom Resource Definition, status sub-resource
  119. Description: Create a custom resource definition. Attempt to read, update and patch its status sub-resource;
  120. all mutating sub-resource operations MUST be visible to subsequent reads.
  121. */
  122. framework.ConformanceIt("getting/updating/patching custom resource definition status sub-resource works ", func() {
  123. config, err := framework.LoadConfig()
  124. framework.ExpectNoError(err, "loading config")
  125. apiExtensionClient, err := clientset.NewForConfig(config)
  126. framework.ExpectNoError(err, "initializing apiExtensionClient")
  127. dynamicClient, err := dynamic.NewForConfig(config)
  128. framework.ExpectNoError(err, "initializing dynamic client")
  129. gvr := v1.SchemeGroupVersion.WithResource("customresourcedefinitions")
  130. resourceClient := dynamicClient.Resource(gvr)
  131. // Create CRD and waits for the resource to be recognized and available.
  132. crd := fixtures.NewRandomNameV1CustomResourceDefinition(v1.ClusterScoped)
  133. crd, err = fixtures.CreateNewV1CustomResourceDefinitionWatchUnsafe(crd, apiExtensionClient)
  134. framework.ExpectNoError(err, "creating CustomResourceDefinition")
  135. defer func() {
  136. err = fixtures.DeleteV1CustomResourceDefinition(crd, apiExtensionClient)
  137. framework.ExpectNoError(err, "deleting CustomResourceDefinition")
  138. }()
  139. var updated *v1.CustomResourceDefinition
  140. updateCondition := v1.CustomResourceDefinitionCondition{Message: "updated"}
  141. err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
  142. // Use dynamic client to read the status sub-resource since typed client does not expose it.
  143. u, err := resourceClient.Get(crd.GetName(), metav1.GetOptions{}, "status")
  144. framework.ExpectNoError(err, "getting CustomResourceDefinition status")
  145. status := unstructuredToCRD(u)
  146. if !equality.Semantic.DeepEqual(status.Spec, crd.Spec) {
  147. framework.Failf("Expected CustomResourceDefinition Spec to match status sub-resource Spec, but got:\n%s", diff.ObjectReflectDiff(status.Spec, crd.Spec))
  148. }
  149. status.Status.Conditions = append(status.Status.Conditions, updateCondition)
  150. updated, err = apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().UpdateStatus(context.TODO(), status, metav1.UpdateOptions{})
  151. return err
  152. })
  153. framework.ExpectNoError(err, "updating CustomResourceDefinition status")
  154. expectCondition(updated.Status.Conditions, updateCondition)
  155. patchCondition := v1.CustomResourceDefinitionCondition{Message: "patched"}
  156. patched, err := apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Patch(context.TODO(), crd.GetName(),
  157. types.JSONPatchType,
  158. []byte(`[{"op": "add", "path": "/status/conditions", "value": [{"message": "patched"}]}]`), metav1.PatchOptions{},
  159. "status")
  160. framework.ExpectNoError(err, "patching CustomResourceDefinition status")
  161. expectCondition(updated.Status.Conditions, updateCondition)
  162. expectCondition(patched.Status.Conditions, patchCondition)
  163. })
  164. })
  165. /*
  166. Release: v1.16
  167. Testname: Custom Resource Definition, discovery
  168. Description: Fetch /apis, /apis/apiextensions.k8s.io, and /apis/apiextensions.k8s.io/v1 discovery documents,
  169. and ensure they indicate CustomResourceDefinition apiextensions.k8s.io/v1 resources are available.
  170. */
  171. framework.ConformanceIt("should include custom resource definition resources in discovery documents", func() {
  172. {
  173. ginkgo.By("fetching the /apis discovery document")
  174. apiGroupList := &metav1.APIGroupList{}
  175. err := f.ClientSet.Discovery().RESTClient().Get().AbsPath("/apis").Do(context.TODO()).Into(apiGroupList)
  176. framework.ExpectNoError(err, "fetching /apis")
  177. ginkgo.By("finding the apiextensions.k8s.io API group in the /apis discovery document")
  178. var group *metav1.APIGroup
  179. for _, g := range apiGroupList.Groups {
  180. if g.Name == v1.GroupName {
  181. group = &g
  182. break
  183. }
  184. }
  185. framework.ExpectNotEqual(group, nil, "apiextensions.k8s.io API group not found in /apis discovery document")
  186. ginkgo.By("finding the apiextensions.k8s.io/v1 API group/version in the /apis discovery document")
  187. var version *metav1.GroupVersionForDiscovery
  188. for _, v := range group.Versions {
  189. if v.Version == v1.SchemeGroupVersion.Version {
  190. version = &v
  191. break
  192. }
  193. }
  194. framework.ExpectNotEqual(version, nil, "apiextensions.k8s.io/v1 API group version not found in /apis discovery document")
  195. }
  196. {
  197. ginkgo.By("fetching the /apis/apiextensions.k8s.io discovery document")
  198. group := &metav1.APIGroup{}
  199. err := f.ClientSet.Discovery().RESTClient().Get().AbsPath("/apis/apiextensions.k8s.io").Do(context.TODO()).Into(group)
  200. framework.ExpectNoError(err, "fetching /apis/apiextensions.k8s.io")
  201. framework.ExpectEqual(group.Name, v1.GroupName, "verifying API group name in /apis/apiextensions.k8s.io discovery document")
  202. ginkgo.By("finding the apiextensions.k8s.io/v1 API group/version in the /apis/apiextensions.k8s.io discovery document")
  203. var version *metav1.GroupVersionForDiscovery
  204. for _, v := range group.Versions {
  205. if v.Version == v1.SchemeGroupVersion.Version {
  206. version = &v
  207. break
  208. }
  209. }
  210. framework.ExpectNotEqual(version, nil, "apiextensions.k8s.io/v1 API group version not found in /apis/apiextensions.k8s.io discovery document")
  211. }
  212. {
  213. ginkgo.By("fetching the /apis/apiextensions.k8s.io/v1 discovery document")
  214. apiResourceList := &metav1.APIResourceList{}
  215. err := f.ClientSet.Discovery().RESTClient().Get().AbsPath("/apis/apiextensions.k8s.io/v1").Do(context.TODO()).Into(apiResourceList)
  216. framework.ExpectNoError(err, "fetching /apis/apiextensions.k8s.io/v1")
  217. framework.ExpectEqual(apiResourceList.GroupVersion, v1.SchemeGroupVersion.String(), "verifying API group/version in /apis/apiextensions.k8s.io/v1 discovery document")
  218. ginkgo.By("finding customresourcedefinitions resources in the /apis/apiextensions.k8s.io/v1 discovery document")
  219. var crdResource *metav1.APIResource
  220. for i := range apiResourceList.APIResources {
  221. if apiResourceList.APIResources[i].Name == "customresourcedefinitions" {
  222. crdResource = &apiResourceList.APIResources[i]
  223. }
  224. }
  225. framework.ExpectNotEqual(crdResource, nil, "customresourcedefinitions resource not found in /apis/apiextensions.k8s.io/v1 discovery document")
  226. }
  227. })
  228. /*
  229. Release : v1.17
  230. Testname: Custom Resource Definition, defaulting
  231. Description: Create a custom resource definition without default. Create CR. Add default and read CR until
  232. the default is applied. Create another CR. Remove default, add default for another field and read CR until
  233. new field is defaulted, but old default stays.
  234. */
  235. framework.ConformanceIt("custom resource defaulting for requests and from storage works ", func() {
  236. config, err := framework.LoadConfig()
  237. framework.ExpectNoError(err, "loading config")
  238. apiExtensionClient, err := clientset.NewForConfig(config)
  239. framework.ExpectNoError(err, "initializing apiExtensionClient")
  240. dynamicClient, err := dynamic.NewForConfig(config)
  241. framework.ExpectNoError(err, "initializing dynamic client")
  242. // Create CRD without default and waits for the resource to be recognized and available.
  243. crd := fixtures.NewRandomNameV1CustomResourceDefinition(v1.ClusterScoped)
  244. if crd.Spec.Versions[0].Schema.OpenAPIV3Schema.Properties == nil {
  245. crd.Spec.Versions[0].Schema.OpenAPIV3Schema.Properties = map[string]v1.JSONSchemaProps{}
  246. }
  247. crd.Spec.Versions[0].Schema.OpenAPIV3Schema.Properties["a"] = v1.JSONSchemaProps{Type: "string"}
  248. crd.Spec.Versions[0].Schema.OpenAPIV3Schema.Properties["b"] = v1.JSONSchemaProps{Type: "string"}
  249. crd, err = fixtures.CreateNewV1CustomResourceDefinitionWatchUnsafe(crd, apiExtensionClient)
  250. framework.ExpectNoError(err, "creating CustomResourceDefinition")
  251. defer func() {
  252. err = fixtures.DeleteV1CustomResourceDefinition(crd, apiExtensionClient)
  253. framework.ExpectNoError(err, "deleting CustomResourceDefinition")
  254. }()
  255. // create CR without default in storage
  256. name1 := names.SimpleNameGenerator.GenerateName("cr-1")
  257. gvr := schema.GroupVersionResource{
  258. Group: crd.Spec.Group,
  259. Version: crd.Spec.Versions[0].Name,
  260. Resource: crd.Spec.Names.Plural,
  261. }
  262. crClient := dynamicClient.Resource(gvr)
  263. _, err = crClient.Create(&unstructured.Unstructured{Object: map[string]interface{}{
  264. "apiVersion": gvr.Group + "/" + gvr.Version,
  265. "kind": crd.Spec.Names.Kind,
  266. "metadata": map[string]interface{}{
  267. "name": name1,
  268. },
  269. }}, metav1.CreateOptions{})
  270. framework.ExpectNoError(err, "creating CR")
  271. // Setting default for a to "A" and waiting for the CR to get defaulted on read
  272. crd, err = apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Patch(context.TODO(), crd.Name, types.JSONPatchType, []byte(`[
  273. {"op":"add","path":"/spec/versions/0/schema/openAPIV3Schema/properties/a/default", "value": "A"}
  274. ]`), metav1.PatchOptions{})
  275. framework.ExpectNoError(err, "setting default for a to \"A\" in schema")
  276. err = wait.PollImmediate(time.Millisecond*100, wait.ForeverTestTimeout, func() (bool, error) {
  277. u1, err := crClient.Get(name1, metav1.GetOptions{})
  278. if err != nil {
  279. return false, err
  280. }
  281. a, found, err := unstructured.NestedFieldNoCopy(u1.Object, "a")
  282. if err != nil {
  283. return false, err
  284. }
  285. if !found {
  286. return false, nil
  287. }
  288. if a != "A" {
  289. return false, fmt.Errorf("expected a:\"A\", but got a:%q", a)
  290. }
  291. return true, nil
  292. })
  293. framework.ExpectNoError(err, "waiting for CR to be defaulted on read")
  294. // create CR with default in storage
  295. name2 := names.SimpleNameGenerator.GenerateName("cr-2")
  296. u2, err := crClient.Create(&unstructured.Unstructured{Object: map[string]interface{}{
  297. "apiVersion": gvr.Group + "/" + gvr.Version,
  298. "kind": crd.Spec.Names.Kind,
  299. "metadata": map[string]interface{}{
  300. "name": name2,
  301. },
  302. }}, metav1.CreateOptions{})
  303. framework.ExpectNoError(err, "creating CR")
  304. v, found, err := unstructured.NestedFieldNoCopy(u2.Object, "a")
  305. framework.ExpectEqual(found, true, "\"a\" is defaulted")
  306. framework.ExpectEqual(v, "A", "\"a\" is defaulted to \"A\"")
  307. // Deleting default for a, adding default "B" for b and waiting for the CR to get defaulted on read for b
  308. crd, err = apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Patch(context.TODO(), crd.Name, types.JSONPatchType, []byte(`[
  309. {"op":"remove","path":"/spec/versions/0/schema/openAPIV3Schema/properties/a/default"},
  310. {"op":"add","path":"/spec/versions/0/schema/openAPIV3Schema/properties/b/default", "value": "B"}
  311. ]`), metav1.PatchOptions{})
  312. framework.ExpectNoError(err, "setting default for b to \"B\" and remove default for a")
  313. err = wait.PollImmediate(time.Millisecond*100, wait.ForeverTestTimeout, func() (bool, error) {
  314. u2, err := crClient.Get(name2, metav1.GetOptions{})
  315. if err != nil {
  316. return false, err
  317. }
  318. b, found, err := unstructured.NestedFieldNoCopy(u2.Object, "b")
  319. if err != nil {
  320. return false, err
  321. }
  322. if !found {
  323. return false, nil
  324. }
  325. if b != "B" {
  326. return false, fmt.Errorf("expected b:\"B\", but got b:%q", b)
  327. }
  328. a, found, err := unstructured.NestedFieldNoCopy(u2.Object, "a")
  329. if err != nil {
  330. return false, err
  331. }
  332. if !found {
  333. return false, fmt.Errorf("expected a:\"A\" to be unchanged, but it was removed")
  334. }
  335. if a != "A" {
  336. return false, fmt.Errorf("expected a:\"A\" to be unchanged, but it changed to %q", a)
  337. }
  338. return true, nil
  339. })
  340. framework.ExpectNoError(err, "waiting for CR to be defaulted on read for b and a staying the same")
  341. })
  342. })
  343. func unstructuredToCRD(obj *unstructured.Unstructured) *v1.CustomResourceDefinition {
  344. crd := new(v1.CustomResourceDefinition)
  345. err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, crd)
  346. framework.ExpectNoError(err, "converting unstructured to CustomResourceDefinition")
  347. return crd
  348. }
  349. func expectCondition(conditions []v1.CustomResourceDefinitionCondition, expected v1.CustomResourceDefinitionCondition) {
  350. for _, c := range conditions {
  351. if equality.Semantic.DeepEqual(c, expected) {
  352. return
  353. }
  354. }
  355. framework.Failf("Condition %#v not found in conditions %#v", expected, conditions)
  356. }