Debian Codegraph @

api/internal/builtins/NamespaceTransformer.go

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