manifest.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 manifest
  14. import (
  15. appsv1 "k8s.io/api/apps/v1"
  16. "k8s.io/api/core/v1"
  17. rbacv1 "k8s.io/api/rbac/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. utilyaml "k8s.io/apimachinery/pkg/util/yaml"
  21. scheme "k8s.io/client-go/kubernetes/scheme"
  22. "k8s.io/kubernetes/test/e2e/framework/testfiles"
  23. )
  24. // PodFromManifest reads a .json/yaml file and returns the pod in it.
  25. func PodFromManifest(filename string) (*v1.Pod, error) {
  26. var pod v1.Pod
  27. data, err := testfiles.Read(filename)
  28. if err != nil {
  29. return nil, err
  30. }
  31. json, err := utilyaml.ToJSON(data)
  32. if err != nil {
  33. return nil, err
  34. }
  35. if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), json, &pod); err != nil {
  36. return nil, err
  37. }
  38. return &pod, nil
  39. }
  40. // RcFromManifest reads a .json/yaml file and returns the rc in it.
  41. func RcFromManifest(fileName string) (*v1.ReplicationController, error) {
  42. var controller v1.ReplicationController
  43. data, err := testfiles.Read(fileName)
  44. if err != nil {
  45. return nil, err
  46. }
  47. json, err := utilyaml.ToJSON(data)
  48. if err != nil {
  49. return nil, err
  50. }
  51. if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), json, &controller); err != nil {
  52. return nil, err
  53. }
  54. return &controller, nil
  55. }
  56. // SvcFromManifest reads a .json/yaml file and returns the service in it.
  57. func SvcFromManifest(fileName string) (*v1.Service, error) {
  58. var svc v1.Service
  59. data, err := testfiles.Read(fileName)
  60. if err != nil {
  61. return nil, err
  62. }
  63. json, err := utilyaml.ToJSON(data)
  64. if err != nil {
  65. return nil, err
  66. }
  67. if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), json, &svc); err != nil {
  68. return nil, err
  69. }
  70. return &svc, nil
  71. }
  72. // StatefulSetFromManifest returns a StatefulSet from a manifest stored in fileName in the Namespace indicated by ns.
  73. func StatefulSetFromManifest(fileName, ns string) (*appsv1.StatefulSet, error) {
  74. var ss appsv1.StatefulSet
  75. data, err := testfiles.Read(fileName)
  76. if err != nil {
  77. return nil, err
  78. }
  79. json, err := utilyaml.ToJSON(data)
  80. if err != nil {
  81. return nil, err
  82. }
  83. if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), json, &ss); err != nil {
  84. return nil, err
  85. }
  86. ss.Namespace = ns
  87. if ss.Spec.Selector == nil {
  88. ss.Spec.Selector = &metav1.LabelSelector{
  89. MatchLabels: ss.Spec.Template.Labels,
  90. }
  91. }
  92. return &ss, nil
  93. }
  94. // DaemonSetFromManifest returns a DaemonSet from a manifest stored in fileName in the Namespace indicated by ns.
  95. func DaemonSetFromManifest(fileName, ns string) (*appsv1.DaemonSet, error) {
  96. var ds appsv1.DaemonSet
  97. data, err := testfiles.Read(fileName)
  98. if err != nil {
  99. return nil, err
  100. }
  101. json, err := utilyaml.ToJSON(data)
  102. if err != nil {
  103. return nil, err
  104. }
  105. err = runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), json, &ds)
  106. if err != nil {
  107. return nil, err
  108. }
  109. ds.Namespace = ns
  110. return &ds, nil
  111. }
  112. // RoleFromManifest returns a Role from a manifest stored in fileName in the Namespace indicated by ns.
  113. func RoleFromManifest(fileName, ns string) (*rbacv1.Role, error) {
  114. var role rbacv1.Role
  115. data, err := testfiles.Read(fileName)
  116. if err != nil {
  117. return nil, err
  118. }
  119. json, err := utilyaml.ToJSON(data)
  120. if err != nil {
  121. return nil, err
  122. }
  123. err = runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), json, &role)
  124. if err != nil {
  125. return nil, err
  126. }
  127. role.Namespace = ns
  128. return &role, nil
  129. }