resource.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 implements representations of k8s API resources as "unstructured" objects.
  14. package resource
  15. import (
  16. "strings"
  17. "sigs.k8s.io/kustomize/pkg/ifc"
  18. "sigs.k8s.io/kustomize/pkg/resid"
  19. "sigs.k8s.io/kustomize/pkg/types"
  20. )
  21. // Resource is map representation of a Kubernetes API resource object
  22. // paired with a GenerationBehavior.
  23. type Resource struct {
  24. ifc.Kunstructured
  25. options *types.GenArgs
  26. }
  27. // String returns resource as JSON.
  28. func (r *Resource) String() string {
  29. bs, err := r.MarshalJSON()
  30. if err != nil {
  31. return "<" + err.Error() + ">"
  32. }
  33. return strings.TrimSpace(string(bs)) + r.options.String()
  34. }
  35. // DeepCopy returns a new copy of resource
  36. func (r *Resource) DeepCopy() *Resource {
  37. return &Resource{
  38. Kunstructured: r.Kunstructured.Copy(),
  39. options: r.options,
  40. }
  41. }
  42. // Behavior returns the behavior for the resource.
  43. func (r *Resource) Behavior() types.GenerationBehavior {
  44. return r.options.Behavior()
  45. }
  46. // NeedAppendHash checks if the resource need a hash suffix
  47. func (r *Resource) NeedHashSuffix() bool {
  48. return r.options != nil && r.options.NeedsHashSuffix()
  49. }
  50. // Id returns the ResId for the resource.
  51. func (r *Resource) Id() resid.ResId {
  52. namespace, _ := r.GetFieldValue("metadata.namespace")
  53. return resid.NewResIdWithPrefixNamespace(r.GetGvk(), r.GetName(), "", namespace)
  54. }
  55. // Merge performs merge with other resource.
  56. func (r *Resource) Merge(other *Resource) {
  57. r.Replace(other)
  58. mergeConfigmap(r.Map(), other.Map(), r.Map())
  59. }
  60. // Replace performs replace with other resource.
  61. func (r *Resource) Replace(other *Resource) {
  62. r.SetLabels(mergeStringMaps(other.GetLabels(), r.GetLabels()))
  63. r.SetAnnotations(
  64. mergeStringMaps(other.GetAnnotations(), r.GetAnnotations()))
  65. r.SetName(other.GetName())
  66. r.options = other.options
  67. }
  68. // TODO: Add BinaryData once we sync to new k8s.io/api
  69. func mergeConfigmap(
  70. mergedTo map[string]interface{},
  71. maps ...map[string]interface{}) {
  72. mergedMap := map[string]interface{}{}
  73. for _, m := range maps {
  74. datamap, ok := m["data"].(map[string]interface{})
  75. if ok {
  76. for key, value := range datamap {
  77. mergedMap[key] = value
  78. }
  79. }
  80. }
  81. mergedTo["data"] = mergedMap
  82. }
  83. func mergeStringMaps(maps ...map[string]string) map[string]string {
  84. result := map[string]string{}
  85. for _, m := range maps {
  86. for key, value := range m {
  87. result[key] = value
  88. }
  89. }
  90. return result
  91. }