apply_crd_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*
  2. Copyright 2019 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. "encoding/json"
  17. "fmt"
  18. "reflect"
  19. "testing"
  20. apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
  21. "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
  22. "k8s.io/apiextensions-apiserver/test/integration/fixtures"
  23. apierrors "k8s.io/apimachinery/pkg/api/errors"
  24. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  25. "k8s.io/apimachinery/pkg/types"
  26. genericfeatures "k8s.io/apiserver/pkg/features"
  27. utilfeature "k8s.io/apiserver/pkg/util/feature"
  28. "k8s.io/client-go/dynamic"
  29. featuregatetesting "k8s.io/component-base/featuregate/testing"
  30. apiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
  31. "k8s.io/kubernetes/test/integration/framework"
  32. )
  33. // TestApplyCRDNoSchema tests that CRDs and CRs can both be applied to with a PATCH request with the apply content type
  34. // when there is no validation field provided.
  35. func TestApplyCRDNoSchema(t *testing.T) {
  36. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, genericfeatures.ServerSideApply, true)()
  37. server, err := apiservertesting.StartTestServer(t, apiservertesting.NewDefaultTestServerOptions(), nil, framework.SharedEtcd())
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. defer server.TearDownFn()
  42. config := server.ClientConfig
  43. apiExtensionClient, err := clientset.NewForConfig(config)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. dynamicClient, err := dynamic.NewForConfig(config)
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. noxuDefinition := fixtures.NewMultipleVersionNoxuCRD(apiextensionsv1beta1.ClusterScoped)
  52. noxuDefinition, err = fixtures.CreateNewCustomResourceDefinition(noxuDefinition, apiExtensionClient, dynamicClient)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. kind := noxuDefinition.Spec.Names.Kind
  57. apiVersion := noxuDefinition.Spec.Group + "/" + noxuDefinition.Spec.Version
  58. name := "mytest"
  59. rest := apiExtensionClient.Discovery().RESTClient()
  60. yamlBody := []byte(fmt.Sprintf(`
  61. apiVersion: %s
  62. kind: %s
  63. metadata:
  64. name: %s
  65. spec:
  66. replicas: 1`, apiVersion, kind, name))
  67. result, err := rest.Patch(types.ApplyPatchType).
  68. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  69. Name(name).
  70. Param("fieldManager", "apply_test").
  71. Body(yamlBody).
  72. DoRaw(context.TODO())
  73. if err != nil {
  74. t.Fatalf("failed to create custom resource with apply: %v:\n%v", err, string(result))
  75. }
  76. verifyReplicas(t, result, 1)
  77. // Patch object to change the number of replicas
  78. result, err = rest.Patch(types.MergePatchType).
  79. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  80. Name(name).
  81. Body([]byte(`{"spec":{"replicas": 5}}`)).
  82. DoRaw(context.TODO())
  83. if err != nil {
  84. t.Fatalf("failed to update number of replicas with merge patch: %v:\n%v", err, string(result))
  85. }
  86. verifyReplicas(t, result, 5)
  87. // Re-apply, we should get conflicts now, since the number of replicas was changed.
  88. result, err = rest.Patch(types.ApplyPatchType).
  89. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  90. Name(name).
  91. Param("fieldManager", "apply_test").
  92. Body(yamlBody).
  93. DoRaw(context.TODO())
  94. if err == nil {
  95. t.Fatalf("Expecting to get conflicts when applying object after updating replicas, got no error: %s", result)
  96. }
  97. status, ok := err.(*apierrors.StatusError)
  98. if !ok {
  99. t.Fatalf("Expecting to get conflicts as API error")
  100. }
  101. if len(status.Status().Details.Causes) != 1 {
  102. t.Fatalf("Expecting to get one conflict when applying object after updating replicas, got: %v", status.Status().Details.Causes)
  103. }
  104. // Re-apply with force, should work fine.
  105. result, err = rest.Patch(types.ApplyPatchType).
  106. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  107. Name(name).
  108. Param("force", "true").
  109. Param("fieldManager", "apply_test").
  110. Body(yamlBody).
  111. DoRaw(context.TODO())
  112. if err != nil {
  113. t.Fatalf("failed to apply object with force after updating replicas: %v:\n%v", err, string(result))
  114. }
  115. verifyReplicas(t, result, 1)
  116. }
  117. // TestApplyCRDStructuralSchema tests that when a CRD has a structural schema in its validation field,
  118. // it will be used to construct the CR schema used by apply.
  119. func TestApplyCRDStructuralSchema(t *testing.T) {
  120. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, genericfeatures.ServerSideApply, true)()
  121. server, err := apiservertesting.StartTestServer(t, apiservertesting.NewDefaultTestServerOptions(), nil, framework.SharedEtcd())
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. defer server.TearDownFn()
  126. config := server.ClientConfig
  127. apiExtensionClient, err := clientset.NewForConfig(config)
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. dynamicClient, err := dynamic.NewForConfig(config)
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. noxuDefinition := fixtures.NewMultipleVersionNoxuCRD(apiextensionsv1beta1.ClusterScoped)
  136. var c apiextensionsv1beta1.CustomResourceValidation
  137. err = json.Unmarshal([]byte(`{
  138. "openAPIV3Schema": {
  139. "type": "object",
  140. "properties": {
  141. "spec": {
  142. "type": "object",
  143. "x-kubernetes-preserve-unknown-fields": true,
  144. "properties": {
  145. "cronSpec": {
  146. "type": "string",
  147. "pattern": "^(\\d+|\\*)(/\\d+)?(\\s+(\\d+|\\*)(/\\d+)?){4}$"
  148. },
  149. "ports": {
  150. "type": "array",
  151. "x-kubernetes-list-map-keys": [
  152. "containerPort",
  153. "protocol"
  154. ],
  155. "x-kubernetes-list-type": "map",
  156. "items": {
  157. "properties": {
  158. "containerPort": {
  159. "format": "int32",
  160. "type": "integer"
  161. },
  162. "hostIP": {
  163. "type": "string"
  164. },
  165. "hostPort": {
  166. "format": "int32",
  167. "type": "integer"
  168. },
  169. "name": {
  170. "type": "string"
  171. },
  172. "protocol": {
  173. "type": "string",
  174. "nullable": true
  175. }
  176. },
  177. "required": [
  178. "containerPort"
  179. ],
  180. "type": "object"
  181. }
  182. }
  183. }
  184. }
  185. }
  186. }
  187. }`), &c)
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. noxuDefinition.Spec.Validation = &c
  192. falseBool := false
  193. noxuDefinition.Spec.PreserveUnknownFields = &falseBool
  194. noxuDefinition, err = fixtures.CreateNewCustomResourceDefinition(noxuDefinition, apiExtensionClient, dynamicClient)
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. kind := noxuDefinition.Spec.Names.Kind
  199. apiVersion := noxuDefinition.Spec.Group + "/" + noxuDefinition.Spec.Version
  200. name := "mytest"
  201. rest := apiExtensionClient.Discovery().RESTClient()
  202. yamlBody := []byte(fmt.Sprintf(`
  203. apiVersion: %s
  204. kind: %s
  205. metadata:
  206. name: %s
  207. finalizers:
  208. - test-finalizer
  209. spec:
  210. cronSpec: "* * * * */5"
  211. replicas: 1
  212. ports:
  213. - name: x
  214. containerPort: 80
  215. protocol: TCP`, apiVersion, kind, name))
  216. result, err := rest.Patch(types.ApplyPatchType).
  217. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  218. Name(name).
  219. Param("fieldManager", "apply_test").
  220. Body(yamlBody).
  221. DoRaw(context.TODO())
  222. if err != nil {
  223. t.Fatalf("failed to create custom resource with apply: %v:\n%v", err, string(result))
  224. }
  225. verifyNumFinalizers(t, result, 1)
  226. verifyFinalizersIncludes(t, result, "test-finalizer")
  227. verifyReplicas(t, result, 1)
  228. verifyNumPorts(t, result, 1)
  229. // Patch object to add another finalizer to the finalizers list
  230. result, err = rest.Patch(types.MergePatchType).
  231. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  232. Name(name).
  233. Body([]byte(`{"metadata":{"finalizers":["test-finalizer","another-one"]}}`)).
  234. DoRaw(context.TODO())
  235. if err != nil {
  236. t.Fatalf("failed to add finalizer with merge patch: %v:\n%v", err, string(result))
  237. }
  238. verifyNumFinalizers(t, result, 2)
  239. verifyFinalizersIncludes(t, result, "test-finalizer")
  240. verifyFinalizersIncludes(t, result, "another-one")
  241. // Re-apply the same config, should work fine, since finalizers should have the list-type extension 'set'.
  242. result, err = rest.Patch(types.ApplyPatchType).
  243. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  244. Name(name).
  245. Param("fieldManager", "apply_test").
  246. SetHeader("Accept", "application/json").
  247. Body(yamlBody).
  248. DoRaw(context.TODO())
  249. if err != nil {
  250. t.Fatalf("failed to apply same config after adding a finalizer: %v:\n%v", err, string(result))
  251. }
  252. verifyNumFinalizers(t, result, 2)
  253. verifyFinalizersIncludes(t, result, "test-finalizer")
  254. verifyFinalizersIncludes(t, result, "another-one")
  255. // Patch object to change the number of replicas
  256. result, err = rest.Patch(types.MergePatchType).
  257. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  258. Name(name).
  259. Body([]byte(`{"spec":{"replicas": 5}}`)).
  260. DoRaw(context.TODO())
  261. if err != nil {
  262. t.Fatalf("failed to update number of replicas with merge patch: %v:\n%v", err, string(result))
  263. }
  264. verifyReplicas(t, result, 5)
  265. // Re-apply, we should get conflicts now, since the number of replicas was changed.
  266. result, err = rest.Patch(types.ApplyPatchType).
  267. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  268. Name(name).
  269. Param("fieldManager", "apply_test").
  270. Body(yamlBody).
  271. DoRaw(context.TODO())
  272. if err == nil {
  273. t.Fatalf("Expecting to get conflicts when applying object after updating replicas, got no error: %s", result)
  274. }
  275. status, ok := err.(*apierrors.StatusError)
  276. if !ok {
  277. t.Fatalf("Expecting to get conflicts as API error")
  278. }
  279. if len(status.Status().Details.Causes) != 1 {
  280. t.Fatalf("Expecting to get one conflict when applying object after updating replicas, got: %v", status.Status().Details.Causes)
  281. }
  282. // Re-apply with force, should work fine.
  283. result, err = rest.Patch(types.ApplyPatchType).
  284. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  285. Name(name).
  286. Param("force", "true").
  287. Param("fieldManager", "apply_test").
  288. Body(yamlBody).
  289. DoRaw(context.TODO())
  290. if err != nil {
  291. t.Fatalf("failed to apply object with force after updating replicas: %v:\n%v", err, string(result))
  292. }
  293. verifyReplicas(t, result, 1)
  294. // New applier tries to edit an existing list item, we should get conflicts.
  295. result, err = rest.Patch(types.ApplyPatchType).
  296. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  297. Name(name).
  298. Param("fieldManager", "apply_test_2").
  299. Body([]byte(fmt.Sprintf(`
  300. apiVersion: %s
  301. kind: %s
  302. metadata:
  303. name: %s
  304. spec:
  305. ports:
  306. - name: "y"
  307. containerPort: 80
  308. protocol: TCP`, apiVersion, kind, name))).
  309. DoRaw(context.TODO())
  310. if err == nil {
  311. t.Fatalf("Expecting to get conflicts when a different applier updates existing list item, got no error: %s", result)
  312. }
  313. status, ok = err.(*apierrors.StatusError)
  314. if !ok {
  315. t.Fatalf("Expecting to get conflicts as API error")
  316. }
  317. if len(status.Status().Details.Causes) != 1 {
  318. t.Fatalf("Expecting to get one conflict when a different applier updates existing list item, got: %v", status.Status().Details.Causes)
  319. }
  320. // New applier tries to add a new list item, should work fine.
  321. result, err = rest.Patch(types.ApplyPatchType).
  322. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  323. Name(name).
  324. Param("fieldManager", "apply_test_2").
  325. Body([]byte(fmt.Sprintf(`
  326. apiVersion: %s
  327. kind: %s
  328. metadata:
  329. name: %s
  330. spec:
  331. ports:
  332. - name: "y"
  333. containerPort: 8080
  334. protocol: TCP`, apiVersion, kind, name))).
  335. SetHeader("Accept", "application/json").
  336. DoRaw(context.TODO())
  337. if err != nil {
  338. t.Fatalf("failed to add a new list item to the object as a different applier: %v:\n%v", err, string(result))
  339. }
  340. verifyNumPorts(t, result, 2)
  341. }
  342. // TestApplyCRDNonStructuralSchema tests that when a CRD has a non-structural schema in its validation field,
  343. // it will be used to construct the CR schema used by apply, but any non-structural parts of the schema will be treated as
  344. // nested maps (same as a CRD without a schema)
  345. func TestApplyCRDNonStructuralSchema(t *testing.T) {
  346. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, genericfeatures.ServerSideApply, true)()
  347. server, err := apiservertesting.StartTestServer(t, apiservertesting.NewDefaultTestServerOptions(), nil, framework.SharedEtcd())
  348. if err != nil {
  349. t.Fatal(err)
  350. }
  351. defer server.TearDownFn()
  352. config := server.ClientConfig
  353. apiExtensionClient, err := clientset.NewForConfig(config)
  354. if err != nil {
  355. t.Fatal(err)
  356. }
  357. dynamicClient, err := dynamic.NewForConfig(config)
  358. if err != nil {
  359. t.Fatal(err)
  360. }
  361. noxuDefinition := fixtures.NewNoxuCustomResourceDefinition(apiextensionsv1beta1.ClusterScoped)
  362. var c apiextensionsv1beta1.CustomResourceValidation
  363. err = json.Unmarshal([]byte(`{
  364. "openAPIV3Schema": {
  365. "type": "object",
  366. "properties": {
  367. "spec": {
  368. "anyOf": [
  369. {
  370. "type": "object",
  371. "properties": {
  372. "cronSpec": {
  373. "type": "string",
  374. "pattern": "^(\\d+|\\*)(/\\d+)?(\\s+(\\d+|\\*)(/\\d+)?){4}$"
  375. }
  376. }
  377. }, {
  378. "type": "string"
  379. }
  380. ]
  381. }
  382. }
  383. }
  384. }`), &c)
  385. if err != nil {
  386. t.Fatal(err)
  387. }
  388. noxuDefinition.Spec.Validation = &c
  389. noxuDefinition, err = fixtures.CreateNewCustomResourceDefinition(noxuDefinition, apiExtensionClient, dynamicClient)
  390. if err != nil {
  391. t.Fatal(err)
  392. }
  393. kind := noxuDefinition.Spec.Names.Kind
  394. apiVersion := noxuDefinition.Spec.Group + "/" + noxuDefinition.Spec.Version
  395. name := "mytest"
  396. rest := apiExtensionClient.Discovery().RESTClient()
  397. yamlBody := []byte(fmt.Sprintf(`
  398. apiVersion: %s
  399. kind: %s
  400. metadata:
  401. name: %s
  402. finalizers:
  403. - test-finalizer
  404. spec:
  405. cronSpec: "* * * * */5"
  406. replicas: 1`, apiVersion, kind, name))
  407. result, err := rest.Patch(types.ApplyPatchType).
  408. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  409. Name(name).
  410. Param("fieldManager", "apply_test").
  411. Body(yamlBody).
  412. DoRaw(context.TODO())
  413. if err != nil {
  414. t.Fatalf("failed to create custom resource with apply: %v:\n%v", err, string(result))
  415. }
  416. verifyNumFinalizers(t, result, 1)
  417. verifyFinalizersIncludes(t, result, "test-finalizer")
  418. verifyReplicas(t, result, 1.0)
  419. // Patch object to add another finalizer to the finalizers list
  420. result, err = rest.Patch(types.MergePatchType).
  421. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  422. Name(name).
  423. Body([]byte(`{"metadata":{"finalizers":["test-finalizer","another-one"]}}`)).
  424. DoRaw(context.TODO())
  425. if err != nil {
  426. t.Fatalf("failed to add finalizer with merge patch: %v:\n%v", err, string(result))
  427. }
  428. verifyNumFinalizers(t, result, 2)
  429. verifyFinalizersIncludes(t, result, "test-finalizer")
  430. verifyFinalizersIncludes(t, result, "another-one")
  431. // Re-apply the same config, should work fine, since finalizers should have the list-type extension 'set'.
  432. result, err = rest.Patch(types.ApplyPatchType).
  433. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  434. Name(name).
  435. Param("fieldManager", "apply_test").
  436. SetHeader("Accept", "application/json").
  437. Body(yamlBody).
  438. DoRaw(context.TODO())
  439. if err != nil {
  440. t.Fatalf("failed to apply same config after adding a finalizer: %v:\n%v", err, string(result))
  441. }
  442. verifyNumFinalizers(t, result, 2)
  443. verifyFinalizersIncludes(t, result, "test-finalizer")
  444. verifyFinalizersIncludes(t, result, "another-one")
  445. // Patch object to change the number of replicas
  446. result, err = rest.Patch(types.MergePatchType).
  447. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  448. Name(name).
  449. Body([]byte(`{"spec":{"replicas": 5}}`)).
  450. DoRaw(context.TODO())
  451. if err != nil {
  452. t.Fatalf("failed to update number of replicas with merge patch: %v:\n%v", err, string(result))
  453. }
  454. verifyReplicas(t, result, 5.0)
  455. // Re-apply, we should get conflicts now, since the number of replicas was changed.
  456. result, err = rest.Patch(types.ApplyPatchType).
  457. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  458. Name(name).
  459. Param("fieldManager", "apply_test").
  460. Body(yamlBody).
  461. DoRaw(context.TODO())
  462. if err == nil {
  463. t.Fatalf("Expecting to get conflicts when applying object after updating replicas, got no error: %s", result)
  464. }
  465. status, ok := err.(*apierrors.StatusError)
  466. if !ok {
  467. t.Fatalf("Expecting to get conflicts as API error")
  468. }
  469. if len(status.Status().Details.Causes) != 1 {
  470. t.Fatalf("Expecting to get one conflict when applying object after updating replicas, got: %v", status.Status().Details.Causes)
  471. }
  472. // Re-apply with force, should work fine.
  473. result, err = rest.Patch(types.ApplyPatchType).
  474. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  475. Name(name).
  476. Param("force", "true").
  477. Param("fieldManager", "apply_test").
  478. Body(yamlBody).
  479. DoRaw(context.TODO())
  480. if err != nil {
  481. t.Fatalf("failed to apply object with force after updating replicas: %v:\n%v", err, string(result))
  482. }
  483. verifyReplicas(t, result, 1.0)
  484. }
  485. // verifyNumFinalizers checks that len(.metadata.finalizers) == n
  486. func verifyNumFinalizers(t *testing.T, b []byte, n int) {
  487. obj := unstructured.Unstructured{}
  488. err := obj.UnmarshalJSON(b)
  489. if err != nil {
  490. t.Fatalf("failed to unmarshal response: %v", err)
  491. }
  492. if actual, expected := len(obj.GetFinalizers()), n; actual != expected {
  493. t.Fatalf("expected %v finalizers but got %v:\n%v", expected, actual, string(b))
  494. }
  495. }
  496. // verifyFinalizersIncludes checks that .metadata.finalizers includes e
  497. func verifyFinalizersIncludes(t *testing.T, b []byte, e string) {
  498. obj := unstructured.Unstructured{}
  499. err := obj.UnmarshalJSON(b)
  500. if err != nil {
  501. t.Fatalf("failed to unmarshal response: %v", err)
  502. }
  503. for _, a := range obj.GetFinalizers() {
  504. if a == e {
  505. return
  506. }
  507. }
  508. t.Fatalf("expected finalizers to include %q but got: %v", e, obj.GetFinalizers())
  509. }
  510. // verifyReplicas checks that .spec.replicas == r
  511. func verifyReplicas(t *testing.T, b []byte, r int) {
  512. obj := unstructured.Unstructured{}
  513. err := obj.UnmarshalJSON(b)
  514. if err != nil {
  515. t.Fatalf("failed to find replicas number in response: %v:\n%v", err, string(b))
  516. }
  517. spec, ok := obj.Object["spec"]
  518. if !ok {
  519. t.Fatalf("failed to find replicas number in response:\n%v", string(b))
  520. }
  521. specMap, ok := spec.(map[string]interface{})
  522. if !ok {
  523. t.Fatalf("failed to find replicas number in response:\n%v", string(b))
  524. }
  525. replicas, ok := specMap["replicas"]
  526. if !ok {
  527. t.Fatalf("failed to find replicas number in response:\n%v", string(b))
  528. }
  529. replicasNumber, ok := replicas.(int64)
  530. if !ok {
  531. t.Fatalf("failed to find replicas number in response: expected int64 but got: %v", reflect.TypeOf(replicas))
  532. }
  533. if actual, expected := replicasNumber, int64(r); actual != expected {
  534. t.Fatalf("expected %v ports but got %v:\n%v", expected, actual, string(b))
  535. }
  536. }
  537. // verifyNumPorts checks that len(.spec.ports) == n
  538. func verifyNumPorts(t *testing.T, b []byte, n int) {
  539. obj := unstructured.Unstructured{}
  540. err := obj.UnmarshalJSON(b)
  541. if err != nil {
  542. t.Fatalf("failed to find ports list in response: %v:\n%v", err, string(b))
  543. }
  544. spec, ok := obj.Object["spec"]
  545. if !ok {
  546. t.Fatalf("failed to find ports list in response:\n%v", string(b))
  547. }
  548. specMap, ok := spec.(map[string]interface{})
  549. if !ok {
  550. t.Fatalf("failed to find ports list in response:\n%v", string(b))
  551. }
  552. ports, ok := specMap["ports"]
  553. if !ok {
  554. t.Fatalf("failed to find ports list in response:\n%v", string(b))
  555. }
  556. portsList, ok := ports.([]interface{})
  557. if !ok {
  558. t.Fatalf("failed to find ports list in response: expected array but got: %v", reflect.TypeOf(ports))
  559. }
  560. if actual, expected := len(portsList), n; actual != expected {
  561. t.Fatalf("expected %v ports but got %v:\n%v", expected, actual, string(b))
  562. }
  563. }
  564. // TestApplyCRDUnhandledSchema tests that when a CRD has a schema that kube-openapi ToProtoModels cannot handle correctly,
  565. // apply falls back to non-schema behavior
  566. func TestApplyCRDUnhandledSchema(t *testing.T) {
  567. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, genericfeatures.ServerSideApply, true)()
  568. server, err := apiservertesting.StartTestServer(t, apiservertesting.NewDefaultTestServerOptions(), nil, framework.SharedEtcd())
  569. if err != nil {
  570. t.Fatal(err)
  571. }
  572. defer server.TearDownFn()
  573. config := server.ClientConfig
  574. apiExtensionClient, err := clientset.NewForConfig(config)
  575. if err != nil {
  576. t.Fatal(err)
  577. }
  578. dynamicClient, err := dynamic.NewForConfig(config)
  579. if err != nil {
  580. t.Fatal(err)
  581. }
  582. noxuDefinition := fixtures.NewNoxuCustomResourceDefinition(apiextensionsv1beta1.ClusterScoped)
  583. // This is a schema that kube-openapi ToProtoModels does not handle correctly.
  584. // https://github.com/kubernetes/kubernetes/blob/38752f7f99869ed65fb44378360a517649dc2f83/vendor/k8s.io/kube-openapi/pkg/util/proto/document.go#L184
  585. var c apiextensionsv1beta1.CustomResourceValidation
  586. err = json.Unmarshal([]byte(`{
  587. "openAPIV3Schema": {
  588. "properties": {
  589. "TypeFooBar": {
  590. "type": "array"
  591. }
  592. }
  593. }
  594. }`), &c)
  595. if err != nil {
  596. t.Fatal(err)
  597. }
  598. noxuDefinition.Spec.Validation = &c
  599. noxuDefinition, err = fixtures.CreateNewCustomResourceDefinition(noxuDefinition, apiExtensionClient, dynamicClient)
  600. if err != nil {
  601. t.Fatal(err)
  602. }
  603. kind := noxuDefinition.Spec.Names.Kind
  604. apiVersion := noxuDefinition.Spec.Group + "/" + noxuDefinition.Spec.Version
  605. name := "mytest"
  606. rest := apiExtensionClient.Discovery().RESTClient()
  607. yamlBody := []byte(fmt.Sprintf(`
  608. apiVersion: %s
  609. kind: %s
  610. metadata:
  611. name: %s
  612. spec:
  613. replicas: 1`, apiVersion, kind, name))
  614. result, err := rest.Patch(types.ApplyPatchType).
  615. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  616. Name(name).
  617. Param("fieldManager", "apply_test").
  618. Body(yamlBody).
  619. DoRaw(context.TODO())
  620. if err != nil {
  621. t.Fatalf("failed to create custom resource with apply: %v:\n%v", err, string(result))
  622. }
  623. verifyReplicas(t, result, 1)
  624. // Patch object to change the number of replicas
  625. result, err = rest.Patch(types.MergePatchType).
  626. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  627. Name(name).
  628. Body([]byte(`{"spec":{"replicas": 5}}`)).
  629. DoRaw(context.TODO())
  630. if err != nil {
  631. t.Fatalf("failed to update number of replicas with merge patch: %v:\n%v", err, string(result))
  632. }
  633. verifyReplicas(t, result, 5)
  634. // Re-apply, we should get conflicts now, since the number of replicas was changed.
  635. result, err = rest.Patch(types.ApplyPatchType).
  636. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  637. Name(name).
  638. Param("fieldManager", "apply_test").
  639. Body(yamlBody).
  640. DoRaw(context.TODO())
  641. if err == nil {
  642. t.Fatalf("Expecting to get conflicts when applying object after updating replicas, got no error: %s", result)
  643. }
  644. status, ok := err.(*apierrors.StatusError)
  645. if !ok {
  646. t.Fatalf("Expecting to get conflicts as API error")
  647. }
  648. if len(status.Status().Details.Causes) != 1 {
  649. t.Fatalf("Expecting to get one conflict when applying object after updating replicas, got: %v", status.Status().Details.Causes)
  650. }
  651. // Re-apply with force, should work fine.
  652. result, err = rest.Patch(types.ApplyPatchType).
  653. AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Version, noxuDefinition.Spec.Names.Plural).
  654. Name(name).
  655. Param("force", "true").
  656. Param("fieldManager", "apply_test").
  657. Body(yamlBody).
  658. DoRaw(context.TODO())
  659. if err != nil {
  660. t.Fatalf("failed to apply object with force after updating replicas: %v:\n%v", err, string(result))
  661. }
  662. verifyReplicas(t, result, 1)
  663. }