forked from projectatomic/atomic-enterprise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
422 lines (377 loc) · 12.7 KB
/
env.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
Copyright 2014 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"bufio"
"fmt"
"io"
"os"
"strings"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/spf13/cobra"
//ocutil "github.com/openshift/origin/pkg/cmd/util"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
const (
envLong = `Update the environment on a pod
Each container in a pod may have its own environment variable definitions. This command can
add, update, or remove environment variables from containers in pods or any object that has
a pod template (replication controllers or deployment configurations). You can specify a
single object or multiple, and alter environment on all containers or just those that match
a wildcard.
If "--env -" is passed, environment variables can be read from STDIN using the standard env
syntax.`
envExample = ` // Update deployment 'registry' with a new environment variable
$ %[1]s env dc/registry STORAGE_DIR=/local
// List the environment variables defined on a deployment 'registry'
$ %[1]s env dc/registry --list
// List the environment variables defined on all pods
$ %[1]s env pods --all --list
// Output a YAML object with updated enviroment for deployment 'registry'
// Does not alter the object on the server
$ %[1]s env dc/registry STORAGE_DIR=/local -o yaml
// Update all replication controllers in the project to have ENV=prod
$ %[1]s env replicationControllers --all ENV=prod
// Remove the enviroment variable ENV from all deployment configs
$ %[1]s env deploymentConfigs --all ENV-
// Remove the enviroment variable ENV from a pod definition on disk and update the pod on the server
$ %[1]s env -f pod.json ENV-
// Set some of the local shell environment into a deployment on the server
$ env | grep RAILS_ | %[1]s env -e - dc/registry`
)
func NewCmdEnv(fullName string, f *clientcmd.Factory, in io.Reader, out io.Writer) *cobra.Command {
var filenames util.StringList
var env util.StringList
cmd := &cobra.Command{
Use: "env RESOURCE/NAME KEY_1=VAL_1 ... KEY_N=VAL_N [options]",
Short: "Update the environment on a resource with a pod template",
Long: envLong,
Example: fmt.Sprintf(envExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
err := RunEnv(f, in, out, cmd, args, env, filenames)
if err == errExit {
os.Exit(1)
}
cmdutil.CheckErr(err)
},
}
cmd.Flags().StringP("containers", "c", "*", "The names of containers in the selected pod templates to change - may use wildcards")
cmd.Flags().VarP(&env, "env", "e", "Specify key value pairs of environment variables to set into each container.")
cmd.Flags().Bool("list", false, "Display the environment and any changes in the standard format")
cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on")
cmd.Flags().Bool("all", false, "select all resources in the namespace of the specified resource types")
cmd.Flags().VarP(&filenames, "filename", "f", "Filename, directory, or URL to file to use to edit the resource.")
cmd.Flags().Bool("overwrite", true, "If true, allow environment to be overwritten, otherwise reject updates that overwrite existing environment.")
cmd.Flags().String("resource-version", "", "If non-empty, the labels update will only succeed if this is the current resource-version for the object. Only valid when specifying a single resource.")
cmd.Flags().StringP("output", "o", "", "Display the changed objects instead of updating them. One of: json|yaml.")
cmd.Flags().String("output-version", "", "Output the changed objects with the given version (default api-version).")
return cmd
}
func updateObject(info *resource.Info, updateFn func(runtime.Object) (runtime.Object, error)) (runtime.Object, error) {
helper := resource.NewHelper(info.Client, info.Mapping)
obj, err := updateFn(info.Object)
if err != nil {
return nil, err
}
data, err := helper.Codec.Encode(obj)
if err != nil {
return nil, err
}
_, err = helper.Update(info.Namespace, info.Name, true, data)
if err != nil {
return nil, err
}
return obj, nil
}
func validateNoOverwrites(meta *kapi.ObjectMeta, labels map[string]string) error {
for key := range labels {
if value, found := meta.Labels[key]; found {
return fmt.Errorf("'%s' already has a value (%s), and --overwrite is false", key, value)
}
}
return nil
}
func parseEnv(spec []string, defaultReader io.Reader) ([]kapi.EnvVar, []string, error) {
env := []kapi.EnvVar{}
exists := util.NewStringSet()
var remove []string
for _, envSpec := range spec {
switch {
case envSpec == "-":
if defaultReader == nil {
return nil, nil, fmt.Errorf("when '-' is used, STDIN must be open")
}
fileEnv, err := readEnv(defaultReader)
if err != nil {
return nil, nil, err
}
env = append(env, fileEnv...)
case strings.Index(envSpec, "=") != -1:
parts := strings.SplitN(envSpec, "=", 2)
if len(parts) != 2 {
return nil, nil, fmt.Errorf("invalid environment variable: %v", envSpec)
}
exists.Insert(parts[0])
env = append(env, kapi.EnvVar{
Name: parts[0],
Value: parts[1],
})
case strings.HasSuffix(envSpec, "-"):
remove = append(remove, envSpec[:len(envSpec)-1])
default:
return nil, nil, fmt.Errorf("unknown environment variable: %v", envSpec)
}
}
for _, removeLabel := range remove {
if _, found := exists[removeLabel]; found {
return nil, nil, fmt.Errorf("can not both modify and remove an environment variable in the same command")
}
}
return env, remove, nil
}
func readEnv(r io.Reader) ([]kapi.EnvVar, error) {
env := []kapi.EnvVar{}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
envSpec := scanner.Text()
if pos := strings.Index(envSpec, "#"); pos != -1 {
envSpec = envSpec[:pos]
}
if strings.Index(envSpec, "=") != -1 {
parts := strings.SplitN(envSpec, "=", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid environment variable: %v", envSpec)
}
env = append(env, kapi.EnvVar{
Name: parts[0],
Value: parts[1],
})
}
}
if err := scanner.Err(); err != nil && err != io.EOF {
return nil, err
}
return env, nil
}
func RunEnv(f *clientcmd.Factory, in io.Reader, out io.Writer, cmd *cobra.Command, args []string, envParams, filenames util.StringList) error {
resources, envArgs := []string{}, []string{}
first := true
for _, s := range args {
isEnv := strings.Contains(s, "=") || strings.HasSuffix(s, "-")
switch {
case first && isEnv:
first = false
fallthrough
case !first && isEnv:
envArgs = append(envArgs, s)
case first && !isEnv:
resources = append(resources, s)
case !first && !isEnv:
return cmdutil.UsageError(cmd, "all resources must be specified before environment changes: %s", s)
}
}
if len(resources) < 1 {
return cmdutil.UsageError(cmd, "one or more resources must be specified as <resource> <name> or <resource>/<name>")
}
containerMatch := cmdutil.GetFlagString(cmd, "containers")
list := cmdutil.GetFlagBool(cmd, "list")
selector := cmdutil.GetFlagString(cmd, "selector")
all := cmdutil.GetFlagBool(cmd, "all")
//overwrite := cmdutil.GetFlagBool(cmd, "overwrite")
resourceVersion := cmdutil.GetFlagString(cmd, "resource-version")
outputFormat := cmdutil.GetFlagString(cmd, "output")
if list && len(outputFormat) > 0 {
return cmdutil.UsageError(cmd, "--list and --output may not be specified together")
}
clientConfig, err := f.ClientConfig()
if err != nil {
return err
}
outputVersion := cmdutil.OutputVersion(cmd, clientConfig.Version)
cmdNamespace, err := f.DefaultNamespace()
if err != nil {
return err
}
env, remove, err := parseEnv(append(envParams, envArgs...), in)
if err != nil {
return err
}
mapper, typer := f.Object()
b := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(filenames...).
SelectorParam(selector).
ResourceTypeOrNameArgs(all, resources...).
Flatten()
one := false
infos, err := b.Do().IntoSingular(&one).Infos()
if err != nil {
return err
}
// only apply resource version locking on a single resource
if !one && len(resourceVersion) > 0 {
return cmdutil.UsageError(cmd, "--resource-version may only be used with a single resource")
}
skipped := 0
for _, info := range infos {
ok, err := f.UpdatePodSpecForObject(info.Object, func(spec *kapi.PodSpec) error {
containers := selectContainers(spec.Containers, containerMatch)
if len(containers) == 0 {
fmt.Fprintf(cmd.Out(), "warning: %s/%s does not have any containers matching %q\n", info.Mapping.Resource, info.Name, containerMatch)
return nil
}
for _, c := range containers {
c.Env = updateEnv(c.Env, env, remove)
if list {
fmt.Fprintf(out, "# %s %s, container %s\n", info.Mapping.Resource, info.Name, c.Name)
for _, env := range c.Env {
// if env.ValueFrom != nil && env.ValueFrom.FieldRef != nil {
// fmt.Fprintf(cmd.Out(), "%s= # calculated from pod %s %s\n", env.Name, env.ValueFrom.FieldRef.FieldPath, env.ValueFrom.FieldRef.APIVersion)
// continue
// }
fmt.Fprintf(out, "%s=%s\n", env.Name, env.Value)
}
}
}
return nil
})
if !ok {
skipped++
continue
}
if err != nil {
fmt.Fprintf(cmd.Out(), "error: %s/%s %v\n", info.Mapping.Resource, info.Name, err)
continue
}
}
if one && skipped == len(infos) {
return fmt.Errorf("the %s %s is not a pod or does not have a pod template", infos[0].Mapping.Resource, infos[0].Name)
}
if list {
return nil
}
objects, err := resource.AsVersionedObject(infos, false, outputVersion)
if err != nil {
return err
}
if len(outputFormat) != 0 {
p, _, err := kubectl.GetPrinter(outputFormat, "")
if err != nil {
return err
}
return p.PrintObj(objects, out)
}
failed := false
for _, info := range infos {
data, err := info.Mapping.Codec.Encode(info.Object)
if err != nil {
fmt.Fprintf(cmd.Out(), "Error: %v\n", err)
failed = true
continue
}
obj, err := resource.NewHelper(info.Client, info.Mapping).Update(info.Namespace, info.Name, true, data)
if err != nil {
fmt.Fprintf(cmd.Out(), "Error: %v\n", err)
failed = true
continue
}
info.Refresh(obj, true)
fmt.Fprintf(out, "%s/%s\n", info.Mapping.Resource, info.Name)
}
if failed {
return errExit
}
return nil
}
func updateEnv(existing []kapi.EnvVar, env []kapi.EnvVar, remove []string) []kapi.EnvVar {
out := []kapi.EnvVar{}
covered := util.NewStringSet(remove...)
for _, e := range existing {
if covered.Has(e.Name) {
continue
}
newer, ok := findEnv(env, e.Name)
if ok {
covered.Insert(e.Name)
out = append(out, newer)
continue
}
out = append(out, e)
}
for _, e := range env {
if covered.Has(e.Name) {
continue
}
covered.Insert(e.Name)
out = append(out, e)
}
return out
}
func findEnv(env []kapi.EnvVar, name string) (kapi.EnvVar, bool) {
for _, e := range env {
if e.Name == name {
return e, true
}
}
return kapi.EnvVar{}, false
}
func selectContainers(containers []kapi.Container, spec string) []*kapi.Container {
out := []*kapi.Container{}
for i, c := range containers {
if selectString(c.Name, spec) {
out = append(out, &containers[i])
}
}
return out
}
// selectString returns true if the provided string matches spec, where spec is a string with
// a non-greedy '*' wildcard operator.
// TODO: turn into a regex and handle greedy matches and backtracking.
func selectString(s, spec string) bool {
if spec == "*" {
return true
}
if !strings.Contains(spec, "*") {
return s == spec
}
pos := 0
match := true
parts := strings.Split(spec, "*")
for i, part := range parts {
if len(part) == 0 {
continue
}
next := strings.Index(s[pos:], part)
switch {
// next part not in string
case next < pos:
fallthrough
// first part does not match start of string
case i == 0 && pos != 0:
fallthrough
// last part does not exactly match remaining part of string
case i == (len(parts)-1) && len(s) != (len(part)+next):
match = false
break
default:
pos = next
}
}
return match
}