-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
178 lines (149 loc) · 4.02 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"k8s.io/kubernetes/pkg/api/v1"
)
var (
varsFiles FlagSlice
secretFiles FlagSlice
configMapFiles FlagSlice
name string
namespace string
convertKeys bool
toYAML bool
flagSet *flag.FlagSet
)
func init() {
// workaround to avoid inheriting vendor flags
flagSet = flag.NewFlagSet("kenv", flag.ExitOnError)
flagSet.StringVar(&name, "name", "", "Name to give the ConfigMap and Secret resources")
flagSet.StringVar(&namespace, "namespace", "default", "Namespace to create the ConfigMap in")
flagSet.BoolVar(&convertKeys, "convert-keys", false, "Convert ConfigMap keys to support k8s version < 1.4")
flagSet.BoolVar(&toYAML, "yaml", false, "Output as YAML")
flagSet.Var(&varsFiles, "v", "Files containing variables to inject as environment variables (repeatable)")
flagSet.Var(&secretFiles, "s", "Files containing variables to inject as Secrets (repeatable)")
flagSet.Var(&configMapFiles, "c", "Files containing variables to inject as ConfigMaps (repeatable)")
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] file\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, `Examples:
kenv -v fixtures/vars.env fixtures/deployment.yaml
kenv -name nginx -v fixtures/vars.env -s fixtures/secrets.yml fixtures/deployment.yaml
cat fixtures/deployment.yaml | kenv -v fixtures/vars.env
Options:
`)
flagSet.PrintDefaults()
}
}
func main() {
var in *os.File
var err error
if err = flagSet.Parse(os.Args[1:]); err != nil {
log.Fatal(err)
}
switch name := flagSet.Arg(0); {
case name == "":
fi, err := os.Stdin.Stat()
if err != nil {
log.Fatal(err)
}
// Print usage unless we already have STDIN data or incoming pipe
if fi.Size() == 0 && fi.Mode()&os.ModeNamedPipe == 0 {
flagSet.Usage()
return
}
in = os.Stdin
default:
if in, err = os.Open(name); err != nil {
log.Fatal(err)
}
defer in.Close()
}
resources, err := ParseDocs(in)
if err != nil {
log.Fatal(err)
}
envVars := []v1.EnvVar{}
if len(varsFiles) > 0 {
v, err := newVarsFromFiles(varsFiles)
if err != nil {
log.Fatal(err)
}
e := v.toEnvVar()
if err != nil {
log.Fatal(err)
}
envVars = append(envVars, e...)
}
if len(secretFiles) > 0 {
if name == "" {
log.Fatal("A name must be set for the Secret resource")
}
v, err := newVarsFromFiles(secretFiles)
if err != nil {
log.Fatal(err)
}
e, secret, err := v.toSecret(name, namespace, convertKeys)
if err != nil {
log.Fatal(err)
}
if err = printResource(secret, toYAML); err != nil {
log.Fatal(err)
}
envVars = append(envVars, e...)
}
if len(configMapFiles) > 0 {
if name == "" {
log.Fatal("A name must be set for the ConfigMap resource")
}
v, err := newVarsFromFiles(configMapFiles)
if err != nil {
log.Fatal(err)
}
e, configMap, err := v.toConfigMap(name, namespace, convertKeys)
if err != nil {
log.Fatal(err)
}
if err = printResource(configMap, toYAML); err != nil {
log.Fatal(err)
}
envVars = append(envVars, e...)
}
// inject environment variables into the supplied resource doc
// and print the result to STDOUT
for _, resource := range resources {
var result interface{}
switch resource.Kind {
case "Deployment":
result, err = resource.InjectVarsDeployment(envVars)
case "DaemonSet":
result, err = resource.InjectVarsDaemonSet(envVars)
case "ReplicaSet":
result, err = resource.InjectVarsReplicaSet(envVars)
case "ReplicationController":
result, err = resource.InjectVarsRC(envVars)
default:
result, err = resource.UnmarshalGeneric()
}
if err != nil {
log.Fatal(err)
}
if err = printResource(result, toYAML); err != nil {
log.Fatal(err)
}
}
}
// FlagSlice represents a repeatable string flag
type FlagSlice []string
// String returns a string representation of FlagSlice
func (f *FlagSlice) String() string {
return strings.Join(*f, ",")
}
// Set appends a string value to FlagSlice
func (f *FlagSlice) Set(value string) error {
*f = append(*f, value)
return nil
}