1
2
3
4 package generators
5
6 import (
7 "fmt"
8 "path"
9 "strings"
10
11 "github.com/go-errors/errors"
12 "sigs.k8s.io/kustomize/api/ifc"
13 "sigs.k8s.io/kustomize/api/types"
14 "sigs.k8s.io/kustomize/kyaml/yaml"
15 )
16
17 func makeBaseNode(kind, name, namespace string) (*yaml.RNode, error) {
18 rn, err := yaml.Parse(fmt.Sprintf(`
19 apiVersion: v1
20 kind: %s
21 `, kind))
22 if err != nil {
23 return nil, err
24 }
25 if name == "" {
26 return nil, errors.Errorf("a configmap must have a name")
27 }
28 if _, err := rn.Pipe(yaml.SetK8sName(name)); err != nil {
29 return nil, err
30 }
31 if namespace != "" {
32 if _, err := rn.Pipe(yaml.SetK8sNamespace(namespace)); err != nil {
33 return nil, err
34 }
35 }
36 return rn, nil
37 }
38
39 func makeValidatedDataMap(
40 ldr ifc.KvLoader, name string, sources types.KvPairSources) (map[string]string, error) {
41 pairs, err := ldr.Load(sources)
42 if err != nil {
43 return nil, errors.WrapPrefix(err, "loading KV pairs", 0)
44 }
45 knownKeys := make(map[string]string)
46 for _, p := range pairs {
47
48 if err := ldr.Validator().ErrIfInvalidKey(p.Key); err != nil {
49 return nil, err
50 }
51 if _, ok := knownKeys[p.Key]; ok {
52 return nil, errors.Errorf(
53 "configmap %s illegally repeats the key `%s`", name, p.Key)
54 }
55 knownKeys[p.Key] = p.Value
56 }
57 return knownKeys, nil
58 }
59
60
61
62 func copyLabelsAndAnnotations(
63 rn *yaml.RNode, opts *types.GeneratorOptions) error {
64 if opts == nil {
65 return nil
66 }
67 for _, k := range yaml.SortedMapKeys(opts.Labels) {
68 v := opts.Labels[k]
69 if _, err := rn.Pipe(yaml.SetLabel(k, v)); err != nil {
70 return err
71 }
72 }
73 for _, k := range yaml.SortedMapKeys(opts.Annotations) {
74 v := opts.Annotations[k]
75 if _, err := rn.Pipe(yaml.SetAnnotation(k, v)); err != nil {
76 return err
77 }
78 }
79 return nil
80 }
81
82 func setImmutable(
83 rn *yaml.RNode, opts *types.GeneratorOptions) error {
84 if opts == nil {
85 return nil
86 }
87 if opts.Immutable {
88 n := &yaml.Node{
89 Kind: yaml.ScalarNode,
90 Value: "true",
91 Tag: yaml.NodeTagBool,
92 }
93 if _, err := rn.Pipe(yaml.FieldSetter{Name: "immutable", Value: yaml.NewRNode(n)}); err != nil {
94 return err
95 }
96 }
97
98 return nil
99 }
100
101
102
103
104
105
106
107
108
109 func ParseFileSource(source string) (keyName, filePath string, err error) {
110 numSeparators := strings.Count(source, "=")
111 switch {
112 case numSeparators == 0:
113 return path.Base(source), source, nil
114 case numSeparators == 1 && strings.HasPrefix(source, "="):
115 return "", "", errors.Errorf("missing key name for file path %q in source %q", strings.TrimPrefix(source, "="), source)
116 case numSeparators == 1 && strings.HasSuffix(source, "="):
117 return "", "", errors.Errorf("missing file path for key name %q in source %q", strings.TrimSuffix(source, "="), source)
118 case numSeparators > 1:
119 return "", "", errors.Errorf("source %q key name or file path contains '='", source)
120 default:
121 components := strings.Split(source, "=")
122 return components[0], components[1], nil
123 }
124 }
125
View as plain text