podtemplates.go 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Copyright 2020 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 common
  14. import (
  15. "context"
  16. "encoding/json"
  17. v1 "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/types"
  20. "k8s.io/apimachinery/pkg/util/uuid"
  21. "k8s.io/kubernetes/test/e2e/framework"
  22. "github.com/onsi/ginkgo"
  23. )
  24. var _ = ginkgo.Describe("[sig-architecture] PodTemplates", func() {
  25. f := framework.NewDefaultFramework("podtemplate")
  26. ginkgo.It("should run the lifecycle of PodTemplates", func() {
  27. testNamespaceName := f.Namespace.Name
  28. podTemplateName := "nginx-pod-template-" + string(uuid.NewUUID())
  29. // get a list of PodTemplates (in all namespaces to hit endpoint)
  30. podTemplateList, err := f.ClientSet.CoreV1().PodTemplates("").List(context.TODO(), metav1.ListOptions{
  31. LabelSelector: "podtemplate-static=true",
  32. })
  33. framework.ExpectNoError(err, "failed to list all PodTemplates")
  34. framework.ExpectEqual(len(podTemplateList.Items), 0, "unable to find templates")
  35. // create a PodTemplate
  36. _, err = f.ClientSet.CoreV1().PodTemplates(testNamespaceName).Create(context.TODO(), &v1.PodTemplate{
  37. ObjectMeta: metav1.ObjectMeta{
  38. Name: podTemplateName,
  39. Labels: map[string]string{
  40. "podtemplate-static": "true",
  41. },
  42. },
  43. Template: v1.PodTemplateSpec{
  44. Spec: v1.PodSpec{
  45. Containers: []v1.Container{
  46. {Name: "nginx", Image: "nginx"},
  47. },
  48. },
  49. },
  50. }, metav1.CreateOptions{})
  51. framework.ExpectNoError(err, "failed to create PodTemplate")
  52. // get template
  53. podTemplateRead, err := f.ClientSet.CoreV1().PodTemplates(testNamespaceName).Get(context.TODO(), podTemplateName, metav1.GetOptions{})
  54. framework.ExpectNoError(err, "failed to get created PodTemplate")
  55. framework.ExpectEqual(podTemplateRead.ObjectMeta.Name, podTemplateName)
  56. // patch template
  57. podTemplatePatch, err := json.Marshal(map[string]interface{}{
  58. "metadata": map[string]interface{}{
  59. "labels": map[string]string{
  60. "podtemplate": "patched",
  61. },
  62. },
  63. })
  64. framework.ExpectNoError(err, "failed to marshal patch data")
  65. _, err = f.ClientSet.CoreV1().PodTemplates(testNamespaceName).Patch(context.TODO(), podTemplateName, types.StrategicMergePatchType, []byte(podTemplatePatch), metav1.PatchOptions{})
  66. framework.ExpectNoError(err, "failed to patch PodTemplate")
  67. // get template (ensure label is there)
  68. podTemplateRead, err = f.ClientSet.CoreV1().PodTemplates(testNamespaceName).Get(context.TODO(), podTemplateName, metav1.GetOptions{})
  69. framework.ExpectNoError(err, "failed to get PodTemplate")
  70. framework.ExpectEqual(podTemplateRead.ObjectMeta.Labels["podtemplate"], "patched", "failed to patch template, new label not found")
  71. // delete the PodTemplate
  72. err = f.ClientSet.CoreV1().PodTemplates(testNamespaceName).Delete(context.TODO(), podTemplateName, &metav1.DeleteOptions{})
  73. framework.ExpectNoError(err, "failed to delete PodTemplate")
  74. // list the PodTemplates
  75. podTemplateList, err = f.ClientSet.CoreV1().PodTemplates("").List(context.TODO(), metav1.ListOptions{
  76. LabelSelector: "podtemplate-static=true",
  77. })
  78. framework.ExpectNoError(err, "failed to list PodTemplate")
  79. framework.ExpectEqual(len(podTemplateList.Items), 0, "PodTemplate list returned items, failed to delete PodTemplate")
  80. })
  81. })