| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
//go:generate pluginator |
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
|
| 10 |
"sigs.k8s.io/kustomize/api/filters/namespace" |
| 11 |
"sigs.k8s.io/kustomize/api/konfig" |
| 12 |
"sigs.k8s.io/kustomize/api/resmap" |
| 13 |
"sigs.k8s.io/kustomize/api/types" |
| 14 |
"sigs.k8s.io/kustomize/kyaml/errors" |
| 15 |
"sigs.k8s.io/yaml" |
| 16 |
) |
| 17 |
|
| 18 |
// Change or set the namespace of non-cluster level resources. |
| 19 |
// |
| 20 |
//nolint:tagalign |
| 21 |
type plugin struct { |
| 22 |
types.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"` |
| 23 |
FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` |
| 24 |
UnsetOnly bool `json:"unsetOnly" yaml:"unsetOnly"` |
| 25 |
SetRoleBindingSubjects namespace.RoleBindingSubjectMode `json:"setRoleBindingSubjects" yaml:"setRoleBindingSubjects"` |
| 26 |
} |
| 27 |
|
| 28 |
var KustomizePlugin plugin //nolint:gochecknoglobals |
| 29 |
|
| 30 |
func (p *plugin) Config( |
| 31 |
_ *resmap.PluginHelpers, c []byte) (err error) { |
| 32 |
p.Namespace = "" |
| 33 |
p.FieldSpecs = nil |
| 34 |
if err := yaml.Unmarshal(c, p); err != nil { |
| 35 |
return errors.WrapPrefixf(err, "unmarshalling NamespaceTransformer config") |
| 36 |
} |
| 37 |
switch p.SetRoleBindingSubjects { |
| 38 |
case namespace.AllServiceAccountSubjects, namespace.DefaultSubjectsOnly, namespace.NoSubjects: |
| 39 |
// valid |
| 40 |
case namespace.SubjectModeUnspecified: |
| 41 |
p.SetRoleBindingSubjects = namespace.DefaultSubjectsOnly |
| 42 |
default: |
| 43 |
return errors.Errorf("invalid value %q for setRoleBindingSubjects: "+ |
| 44 |
"must be one of %q, %q or %q", p.SetRoleBindingSubjects, |
| 45 |
namespace.DefaultSubjectsOnly, namespace.NoSubjects, namespace.AllServiceAccountSubjects) |
| 46 |
} |
| 47 |
|
| 48 |
return nil |
| 49 |
} |
| 50 |
|
| 51 |
func (p *plugin) Transform(m resmap.ResMap) error { |
| 52 |
if len(p.Namespace) == 0 { |
| 53 |
return nil |
| 54 |
} |
| 55 |
for _, r := range m.Resources() { |
| 56 |
if r.IsNilOrEmpty() { |
| 57 |
// Don't mutate empty objects? |
| 58 |
continue |
| 59 |
} |
| 60 |
if annotations := r.GetAnnotations(konfig.HelmGeneratedAnnotation); annotations[konfig.HelmGeneratedAnnotation] == "true" { |
| 61 |
// Don't apply namespace on Helm generated manifest. Helm should take care of it. |
| 62 |
continue |
| 63 |
} |
| 64 |
r.StorePreviousId() |
| 65 |
if err := r.ApplyFilter(namespace.Filter{ |
| 66 |
Namespace: p.Namespace, |
| 67 |
FsSlice: p.FieldSpecs, |
| 68 |
SetRoleBindingSubjects: p.SetRoleBindingSubjects, |
| 69 |
UnsetOnly: p.UnsetOnly, |
| 70 |
}); err != nil { |
| 71 |
return err |
| 72 |
} |
| 73 |
matches := m.GetMatchingResourcesByCurrentId(r.CurId().Equals) |
| 74 |
if len(matches) != 1 { |
| 75 |
return fmt.Errorf( |
| 76 |
"namespace transformation produces ID conflict: %+v", matches) |
| 77 |
} |
| 78 |
} |
| 79 |
return nil |
| 80 |
} |