manifest.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. "fmt"
  16. "io/ioutil"
  17. apps "k8s.io/api/apps/v1"
  18. "k8s.io/api/core/v1"
  19. networkingv1beta1 "k8s.io/api/networking/v1beta1"
  20. rbac "k8s.io/api/rbac/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. "k8s.io/apimachinery/pkg/runtime/schema"
  24. utilyaml "k8s.io/apimachinery/pkg/util/yaml"
  25. scheme "k8s.io/client-go/kubernetes/scheme"
  26. "k8s.io/kubernetes/test/e2e/framework/testfiles"
  27. )
  28. // PodFromManifest reads a .json/yaml file and returns the pod in it.
  29. func PodFromManifest(filename string) (*v1.Pod, error) {
  30. var pod v1.Pod
  31. data, err := testfiles.Read(filename)
  32. if err != nil {
  33. return nil, err
  34. }
  35. json, err := utilyaml.ToJSON(data)
  36. if err != nil {
  37. return nil, err
  38. }
  39. if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), json, &pod); err != nil {
  40. return nil, err
  41. }
  42. return &pod, nil
  43. }
  44. // RcFromManifest reads a .json/yaml file and returns the rc in it.
  45. func RcFromManifest(fileName string) (*v1.ReplicationController, error) {
  46. var controller v1.ReplicationController
  47. data, err := testfiles.Read(fileName)
  48. if err != nil {
  49. return nil, err
  50. }
  51. json, err := utilyaml.ToJSON(data)
  52. if err != nil {
  53. return nil, err
  54. }
  55. if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), json, &controller); err != nil {
  56. return nil, err
  57. }
  58. return &controller, nil
  59. }
  60. // SvcFromManifest reads a .json/yaml file and returns the service in it.
  61. func SvcFromManifest(fileName string) (*v1.Service, error) {
  62. var svc v1.Service
  63. data, err := testfiles.Read(fileName)
  64. if err != nil {
  65. return nil, err
  66. }
  67. json, err := utilyaml.ToJSON(data)
  68. if err != nil {
  69. return nil, err
  70. }
  71. if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), json, &svc); err != nil {
  72. return nil, err
  73. }
  74. return &svc, nil
  75. }
  76. // IngressFromManifest reads a .json/yaml file and returns the ingress in it.
  77. func IngressFromManifest(fileName string) (*networkingv1beta1.Ingress, error) {
  78. var ing networkingv1beta1.Ingress
  79. data, err := testfiles.Read(fileName)
  80. if err != nil {
  81. return nil, err
  82. }
  83. json, err := utilyaml.ToJSON(data)
  84. if err != nil {
  85. return nil, err
  86. }
  87. if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), json, &ing); err != nil {
  88. return nil, err
  89. }
  90. return &ing, nil
  91. }
  92. // IngressToManifest generates a yaml file in the given path with the given ingress.
  93. // Assumes that a directory exists at the given path.
  94. func IngressToManifest(ing *networkingv1beta1.Ingress, path string) error {
  95. serialized, err := marshalToYaml(ing, networkingv1beta1.SchemeGroupVersion)
  96. if err != nil {
  97. return fmt.Errorf("failed to marshal ingress %v to YAML: %v", ing, err)
  98. }
  99. if err := ioutil.WriteFile(path, serialized, 0600); err != nil {
  100. return fmt.Errorf("error in writing ingress to file: %s", err)
  101. }
  102. return nil
  103. }
  104. // StatefulSetFromManifest returns a StatefulSet from a manifest stored in fileName in the Namespace indicated by ns.
  105. func StatefulSetFromManifest(fileName, ns string) (*apps.StatefulSet, error) {
  106. var ss apps.StatefulSet
  107. data, err := testfiles.Read(fileName)
  108. if err != nil {
  109. return nil, err
  110. }
  111. json, err := utilyaml.ToJSON(data)
  112. if err != nil {
  113. return nil, err
  114. }
  115. if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), json, &ss); err != nil {
  116. return nil, err
  117. }
  118. ss.Namespace = ns
  119. if ss.Spec.Selector == nil {
  120. ss.Spec.Selector = &metav1.LabelSelector{
  121. MatchLabels: ss.Spec.Template.Labels,
  122. }
  123. }
  124. return &ss, nil
  125. }
  126. // DaemonSetFromManifest returns a DaemonSet from a manifest stored in fileName in the Namespace indicated by ns.
  127. func DaemonSetFromManifest(fileName, ns string) (*apps.DaemonSet, error) {
  128. var ds apps.DaemonSet
  129. data, err := testfiles.Read(fileName)
  130. if err != nil {
  131. return nil, err
  132. }
  133. json, err := utilyaml.ToJSON(data)
  134. if err != nil {
  135. return nil, err
  136. }
  137. err = runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), json, &ds)
  138. if err != nil {
  139. return nil, err
  140. }
  141. ds.Namespace = ns
  142. return &ds, nil
  143. }
  144. // RoleFromManifest returns a Role from a manifest stored in fileName in the Namespace indicated by ns.
  145. func RoleFromManifest(fileName, ns string) (*rbac.Role, error) {
  146. var role rbac.Role
  147. data, err := testfiles.Read(fileName)
  148. json, err := utilyaml.ToJSON(data)
  149. if err != nil {
  150. return nil, err
  151. }
  152. err = runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), json, &role)
  153. if err != nil {
  154. return nil, err
  155. }
  156. role.Namespace = ns
  157. return &role, nil
  158. }
  159. // marshalToYaml marshals an object into YAML for a given GroupVersion.
  160. // The object must be known in SupportedMediaTypes() for the Codecs under "client-go/kubernetes/scheme".
  161. func marshalToYaml(obj runtime.Object, gv schema.GroupVersion) ([]byte, error) {
  162. mediaType := "application/yaml"
  163. info, ok := runtime.SerializerInfoForMediaType(scheme.Codecs.SupportedMediaTypes(), mediaType)
  164. if !ok {
  165. return []byte{}, fmt.Errorf("unsupported media type %q", mediaType)
  166. }
  167. encoder := scheme.Codecs.EncoderForVersion(info.Serializer, gv)
  168. return runtime.Encode(encoder, obj)
  169. }