namespace.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 transformers
  14. import (
  15. "sigs.k8s.io/kustomize/pkg/gvk"
  16. "sigs.k8s.io/kustomize/pkg/resmap"
  17. "sigs.k8s.io/kustomize/pkg/transformers/config"
  18. )
  19. type namespaceTransformer struct {
  20. namespace string
  21. fieldSpecsToUse []config.FieldSpec
  22. fieldSpecsToSkip []config.FieldSpec
  23. }
  24. var _ Transformer = &namespaceTransformer{}
  25. // NewNamespaceTransformer construct a namespaceTransformer.
  26. func NewNamespaceTransformer(ns string, cf []config.FieldSpec) Transformer {
  27. if len(ns) == 0 {
  28. return NewNoOpTransformer()
  29. }
  30. var skip []config.FieldSpec
  31. for _, g := range gvk.ClusterLevelGvks() {
  32. skip = append(skip, config.FieldSpec{Gvk: g})
  33. }
  34. return &namespaceTransformer{
  35. namespace: ns,
  36. fieldSpecsToUse: cf,
  37. fieldSpecsToSkip: skip,
  38. }
  39. }
  40. // Transform adds the namespace.
  41. func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
  42. mf := resmap.ResMap{}
  43. for id := range m {
  44. found := false
  45. for _, path := range o.fieldSpecsToSkip {
  46. if id.Gvk().IsSelected(&path.Gvk) {
  47. found = true
  48. break
  49. }
  50. }
  51. if !found {
  52. mf[id] = m[id]
  53. delete(m, id)
  54. }
  55. }
  56. for id := range mf {
  57. objMap := mf[id].Map()
  58. for _, path := range o.fieldSpecsToUse {
  59. if !id.Gvk().IsSelected(&path.Gvk) {
  60. continue
  61. }
  62. err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent, func(_ interface{}) (interface{}, error) {
  63. return o.namespace, nil
  64. })
  65. if err != nil {
  66. return err
  67. }
  68. newid := id.CopyWithNewNamespace(o.namespace)
  69. m[newid] = mf[id]
  70. }
  71. }
  72. o.updateClusterRoleBinding(m)
  73. return nil
  74. }
  75. func (o *namespaceTransformer) updateClusterRoleBinding(m resmap.ResMap) {
  76. saMap := map[string]bool{}
  77. for id := range m {
  78. if id.Gvk().Equals(gvk.Gvk{Version: "v1", Kind: "ServiceAccount"}) {
  79. saMap[id.Name()] = true
  80. }
  81. }
  82. for id := range m {
  83. if id.Gvk().Kind != "ClusterRoleBinding" && id.Gvk().Kind != "RoleBinding" {
  84. continue
  85. }
  86. objMap := m[id].Map()
  87. subjects := objMap["subjects"].([]interface{})
  88. for i := range subjects {
  89. subject := subjects[i].(map[string]interface{})
  90. kind, foundk := subject["kind"]
  91. name, foundn := subject["name"]
  92. if !foundk || !foundn || kind.(string) != "ServiceAccount" {
  93. continue
  94. }
  95. // a ServiceAccount named “default” exists in every active namespace
  96. if name.(string) == "default" || saMap[name.(string)] {
  97. subject := subjects[i].(map[string]interface{})
  98. mutateField(subject, []string{"namespace"}, true, func(_ interface{}) (interface{}, error) {
  99. return o.namespace, nil
  100. })
  101. subjects[i] = subject
  102. }
  103. }
  104. objMap["subjects"] = subjects
  105. }
  106. }