factory.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Copyright 2018 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 resource
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "log"
  18. "strings"
  19. "sigs.k8s.io/kustomize/pkg/ifc"
  20. internal "sigs.k8s.io/kustomize/pkg/internal/error"
  21. "sigs.k8s.io/kustomize/pkg/patch"
  22. "sigs.k8s.io/kustomize/pkg/types"
  23. )
  24. // Factory makes instances of Resource.
  25. type Factory struct {
  26. kf ifc.KunstructuredFactory
  27. }
  28. // NewFactory makes an instance of Factory.
  29. func NewFactory(kf ifc.KunstructuredFactory) *Factory {
  30. return &Factory{kf: kf}
  31. }
  32. // FromMap returns a new instance of Resource.
  33. func (rf *Factory) FromMap(m map[string]interface{}) *Resource {
  34. return &Resource{
  35. Kunstructured: rf.kf.FromMap(m),
  36. options: types.NewGenArgs(nil, nil),
  37. }
  38. }
  39. // FromMapAndOption returns a new instance of Resource with given options.
  40. func (rf *Factory) FromMapAndOption(m map[string]interface{}, args *types.GeneratorArgs, option *types.GeneratorOptions) *Resource {
  41. return &Resource{
  42. Kunstructured: rf.kf.FromMap(m),
  43. options: types.NewGenArgs(args, option),
  44. }
  45. }
  46. // FromKunstructured returns a new instance of Resource.
  47. func (rf *Factory) FromKunstructured(
  48. u ifc.Kunstructured) *Resource {
  49. if u == nil {
  50. log.Fatal("unstruct ifc must not be null")
  51. }
  52. return &Resource{
  53. Kunstructured: u,
  54. options: types.NewGenArgs(nil, nil),
  55. }
  56. }
  57. // SliceFromPatches returns a slice of resources given a patch path
  58. // slice from a kustomization file.
  59. func (rf *Factory) SliceFromPatches(
  60. ldr ifc.Loader, paths []patch.StrategicMerge) ([]*Resource, error) {
  61. var result []*Resource
  62. for _, path := range paths {
  63. content, err := ldr.Load(string(path))
  64. if err != nil {
  65. return nil, err
  66. }
  67. res, err := rf.SliceFromBytes(content)
  68. if err != nil {
  69. return nil, internal.Handler(err, string(path))
  70. }
  71. result = append(result, res...)
  72. }
  73. return result, nil
  74. }
  75. // SliceFromBytes unmarshalls bytes into a Resource slice.
  76. func (rf *Factory) SliceFromBytes(in []byte) ([]*Resource, error) {
  77. kunStructs, err := rf.kf.SliceFromBytes(in)
  78. if err != nil {
  79. return nil, err
  80. }
  81. var result []*Resource
  82. for len(kunStructs) > 0 {
  83. u := kunStructs[0]
  84. kunStructs = kunStructs[1:]
  85. if strings.HasSuffix(u.GetKind(), "List") {
  86. items := u.Map()["items"]
  87. itemsSlice, ok := items.([]interface{})
  88. if !ok {
  89. if items == nil {
  90. // an empty list
  91. continue
  92. }
  93. return nil, fmt.Errorf("items in List is type %T, expected array", items)
  94. }
  95. for _, item := range itemsSlice {
  96. itemJSON, err := json.Marshal(item)
  97. if err != nil {
  98. return nil, err
  99. }
  100. innerU, err := rf.kf.SliceFromBytes(itemJSON)
  101. if err != nil {
  102. return nil, err
  103. }
  104. // append innerU to kunStructs so nested Lists can be handled
  105. kunStructs = append(kunStructs, innerU...)
  106. }
  107. } else {
  108. result = append(result, rf.FromKunstructured(u))
  109. }
  110. }
  111. return result, nil
  112. }
  113. // Set sets the loader for the underlying factory
  114. func (rf *Factory) Set(ldr ifc.Loader) {
  115. rf.kf.Set(ldr)
  116. }
  117. // MakeConfigMap makes an instance of Resource for ConfigMap
  118. func (rf *Factory) MakeConfigMap(args *types.ConfigMapArgs, options *types.GeneratorOptions) (*Resource, error) {
  119. u, err := rf.kf.MakeConfigMap(args, options)
  120. if err != nil {
  121. return nil, err
  122. }
  123. return &Resource{Kunstructured: u, options: types.NewGenArgs(&types.GeneratorArgs{Behavior: args.Behavior}, options)}, nil
  124. }
  125. // MakeSecret makes an instance of Resource for Secret
  126. func (rf *Factory) MakeSecret(args *types.SecretArgs, options *types.GeneratorOptions) (*Resource, error) {
  127. u, err := rf.kf.MakeSecret(args, options)
  128. if err != nil {
  129. return nil, err
  130. }
  131. return &Resource{Kunstructured: u, options: types.NewGenArgs(&types.GeneratorArgs{Behavior: args.Behavior}, options)}, nil
  132. }