factory.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 resmap
  14. import (
  15. "fmt"
  16. "github.com/pkg/errors"
  17. "sigs.k8s.io/kustomize/pkg/ifc"
  18. internal "sigs.k8s.io/kustomize/pkg/internal/error"
  19. "sigs.k8s.io/kustomize/pkg/resource"
  20. "sigs.k8s.io/kustomize/pkg/types"
  21. )
  22. // Factory makes instances of ResMap.
  23. type Factory struct {
  24. resF *resource.Factory
  25. }
  26. // NewFactory returns a new resmap.Factory.
  27. func NewFactory(rf *resource.Factory) *Factory {
  28. return &Factory{resF: rf}
  29. }
  30. // RF returns a resource.Factory.
  31. func (rmF *Factory) RF() *resource.Factory {
  32. return rmF.resF
  33. }
  34. // FromFiles returns a ResMap given a resource path slice.
  35. func (rmF *Factory) FromFiles(
  36. loader ifc.Loader, paths []string) (ResMap, error) {
  37. var result []ResMap
  38. for _, path := range paths {
  39. content, err := loader.Load(path)
  40. if err != nil {
  41. return nil, errors.Wrap(err, "Load from path "+path+" failed")
  42. }
  43. res, err := rmF.NewResMapFromBytes(content)
  44. if err != nil {
  45. return nil, internal.Handler(err, path)
  46. }
  47. result = append(result, res)
  48. }
  49. return MergeWithErrorOnIdCollision(result...)
  50. }
  51. // newResMapFromBytes decodes a list of objects in byte array format.
  52. func (rmF *Factory) NewResMapFromBytes(b []byte) (ResMap, error) {
  53. resources, err := rmF.resF.SliceFromBytes(b)
  54. if err != nil {
  55. return nil, err
  56. }
  57. result := ResMap{}
  58. for _, res := range resources {
  59. id := res.Id()
  60. if _, found := result[id]; found {
  61. return result, fmt.Errorf("GroupVersionKindName: %#v already exists b the map", id)
  62. }
  63. result[id] = res
  64. }
  65. return result, nil
  66. }
  67. // NewResMapFromConfigMapArgs returns a Resource slice given
  68. // a configmap metadata slice from kustomization file.
  69. func (rmF *Factory) NewResMapFromConfigMapArgs(argList []types.ConfigMapArgs, options *types.GeneratorOptions) (ResMap, error) {
  70. var resources []*resource.Resource
  71. for _, args := range argList {
  72. res, err := rmF.resF.MakeConfigMap(&args, options)
  73. if err != nil {
  74. return nil, errors.Wrap(err, "NewResMapFromConfigMapArgs")
  75. }
  76. resources = append(resources, res)
  77. }
  78. return newResMapFromResourceSlice(resources)
  79. }
  80. // NewResMapFromSecretArgs takes a SecretArgs slice, generates
  81. // secrets from each entry, and accumulates them in a ResMap.
  82. func (rmF *Factory) NewResMapFromSecretArgs(argsList []types.SecretArgs, options *types.GeneratorOptions) (ResMap, error) {
  83. var resources []*resource.Resource
  84. for _, args := range argsList {
  85. res, err := rmF.resF.MakeSecret(&args, options)
  86. if err != nil {
  87. return nil, errors.Wrap(err, "NewResMapFromSecretArgs")
  88. }
  89. resources = append(resources, res)
  90. }
  91. return newResMapFromResourceSlice(resources)
  92. }
  93. // Set sets the loader for the underlying factory
  94. func (rmF *Factory) Set(ldr ifc.Loader) {
  95. rmF.resF.Set(ldr)
  96. }
  97. func newResMapFromResourceSlice(resources []*resource.Resource) (ResMap, error) {
  98. result := ResMap{}
  99. for _, res := range resources {
  100. id := res.Id()
  101. if _, found := result[id]; found {
  102. return nil, fmt.Errorf("duplicated %#v is not allowed", id)
  103. }
  104. result[id] = res
  105. }
  106. return result, nil
  107. }