Skip to content

Commit c3c71fb

Browse files
committed
add support for injector flags
1 parent e8f8200 commit c3c71fb

File tree

3 files changed

+73
-40
lines changed

3 files changed

+73
-40
lines changed

README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,21 @@ Usage:
2626
inject upgrade [RELEASE] [CHART] [flags]
2727
2828
Flags:
29-
--command string injection command to be used (default "inject")
30-
--debug enable verbose output
31-
--dry-run simulate an upgrade
32-
--injector string injector to use (must be pre-installed) (default "linkerd")
33-
-i, --install if a release by this name doesn't already exist, run an install
34-
--kubecontext string name of the kubeconfig context to use
35-
--namespace string namespace to install the release into (only used if --install is set). Defaults to the current kube config namespace
36-
--set stringArray set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
37-
--timeout int time in seconds to wait for any individual Kubernetes operation (like Jobs for hooks) (default 300)
38-
--tls enable TLS for request
39-
--tls-cert string path to TLS certificate file (default: $HELM_HOME/cert.pem)
40-
--tls-key string path to TLS key file (default: $HELM_HOME/key.pem)
41-
-f, --values stringArray specify values in a YAML file or a URL (can specify multiple)
29+
--command string injection command to be used (default "inject")
30+
--debug enable verbose output
31+
--dry-run simulate an upgrade
32+
-h, --help help for upgrade
33+
--inject-flags strings flags to be passed to injector, without leading "--" (can specify multiple). Example: "--inject-flags tls=optional,skip-inbound-ports=25,skip-inbound-ports=26"
34+
--injector string injector to use (must be pre-installed) (default "linkerd")
35+
-i, --install if a release by this name doesn't already exist, run an install
36+
--kubecontext string name of the kubeconfig context to use
37+
--namespace string namespace to install the release into (only used if --install is set). Defaults to the current kube config namespace
38+
--set stringArray set values on the command line (can specify multiple)
39+
--timeout int time in seconds to wait for any individual Kubernetes operation (like Jobs for hooks) (default 300)
40+
--tls enable TLS for request
41+
--tls-cert string path to TLS certificate file (default: $HELM_HOME/cert.pem)
42+
--tls-key string path to TLS key file (default: $HELM_HOME/key.pem)
43+
-f, --values stringArray specify values in a YAML file or a URL (can specify multiple)
4244
```
4345

4446

main.go

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bytes"
45
"errors"
56
"fmt"
67
"io"
@@ -43,6 +44,7 @@ func NewRootCmd(args []string) *cobra.Command {
4344
type upgradeCmd struct {
4445
injector string
4546
command string
47+
injectFlags []string
4648
release string
4749
chart string
4850
dryRun bool
@@ -84,10 +86,10 @@ func NewUpgradeCommand(out io.Writer) *cobra.Command {
8486

8587
tempDir, err := copyToTempDir(chart)
8688
if err != nil {
87-
fmt.Fprintf(os.Stderr, err.Error())
89+
fmt.Println(err)
8890
return
8991
}
90-
defer os.RemoveAll(tempDir)
92+
// defer os.RemoveAll(tempDir)
9193

9294
fileOptions := fileOptions{
9395
basePath: tempDir,
@@ -96,7 +98,7 @@ func NewUpgradeCommand(out io.Writer) *cobra.Command {
9698
}
9799
files, err := getFilesToActOn(fileOptions)
98100
if err != nil {
99-
fmt.Fprintf(os.Stderr, err.Error())
101+
fmt.Println(err)
100102
return
101103
}
102104

@@ -109,17 +111,18 @@ func NewUpgradeCommand(out io.Writer) *cobra.Command {
109111
valuesFiles: u.valueFiles,
110112
}
111113
if err := template(templateOptions); err != nil {
112-
fmt.Fprintf(os.Stderr, err.Error())
114+
fmt.Println(err)
113115
return
114116
}
115117

116118
injectOptions := injectOptions{
117-
injector: u.injector,
118-
command: u.command,
119-
files: files,
119+
injector: u.injector,
120+
command: u.command,
121+
injectFlags: u.injectFlags,
122+
files: files,
120123
}
121124
if err := inject(injectOptions); err != nil {
122-
fmt.Fprintf(os.Stderr, err.Error())
125+
fmt.Println(err)
123126
return
124127
}
125128

@@ -139,7 +142,7 @@ func NewUpgradeCommand(out io.Writer) *cobra.Command {
139142
tlsKey: u.tlsKey,
140143
}
141144
if err := upgrade(upgradeOptions); err != nil {
142-
fmt.Fprintf(os.Stderr, err.Error())
145+
fmt.Println(err)
143146
return
144147
}
145148
},
@@ -148,9 +151,10 @@ func NewUpgradeCommand(out io.Writer) *cobra.Command {
148151

149152
f.StringVar(&u.injector, "injector", "linkerd", "injector to use (must be pre-installed)")
150153
f.StringVar(&u.command, "command", "inject", "injection command to be used")
154+
f.StringSliceVar(&u.injectFlags, "inject-flags", []string{}, "flags to be passed to injector, without leading \"--\" (can specify multiple). Example: \"--inject-flags tls=optional,skip-inbound-ports=25,skip-inbound-ports=26\"")
151155

152156
f.StringArrayVarP(&u.valueFiles, "values", "f", []string{}, "specify values in a YAML file or a URL (can specify multiple)")
153-
f.StringArrayVar(&u.values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
157+
f.StringArrayVar(&u.values, "set", []string{}, "set values on the command line (can specify multiple)")
154158
f.StringVar(&u.namespace, "namespace", "", "namespace to install the release into (only used if --install is set). Defaults to the current kube config namespace")
155159
f.StringVar(&u.kubeContext, "kubecontext", "", "name of the kubeconfig context to use")
156160
f.IntVar(&u.timeout, "timeout", 300, "time in seconds to wait for any individual Kubernetes operation (like Jobs for hooks)")
@@ -176,7 +180,10 @@ func copyToTempDir(path string) (string, error) {
176180
}
177181
if !exists {
178182
command := fmt.Sprintf("helm fetch %s --untar -d %s", path, tempDir)
179-
Exec(command)
183+
_, stderr, err := Exec(command)
184+
if err != nil || len(stderr) != 0 {
185+
return "", fmt.Errorf(string(stderr))
186+
}
180187
files, err := ioutil.ReadDir(tempDir)
181188
if err != nil {
182189
return "", err
@@ -248,8 +255,11 @@ func template(o templateOptions) error {
248255

249256
for _, file := range o.files {
250257
command := fmt.Sprintf("helm template --debug=false %s --name %s -x %s%s", o.chart, o.name, file, additionalFlags)
251-
output := Exec(command)
252-
if err := ioutil.WriteFile(file, output, 0644); err != nil {
258+
stdout, stderr, err := Exec(command)
259+
if err != nil || len(stderr) != 0 {
260+
return fmt.Errorf(string(stderr))
261+
}
262+
if err := ioutil.WriteFile(file, stdout, 0644); err != nil {
253263
return err
254264
}
255265
}
@@ -258,16 +268,29 @@ func template(o templateOptions) error {
258268
}
259269

260270
type injectOptions struct {
261-
injector string
262-
command string
263-
files []string
271+
injector string
272+
command string
273+
injectFlags []string
274+
files []string
264275
}
265276

266277
func inject(o injectOptions) error {
278+
var flags string
279+
for _, flag := range o.injectFlags {
280+
flagSplit := strings.Split(flag, "=")
281+
if len(flagSplit) != 2 {
282+
return fmt.Errorf("inject-flags must be in the form of key1=value1[,key2=value2,...]")
283+
}
284+
key, val := flagSplit[0], flagSplit[1]
285+
flags += createFlagChain(key, []string{val})
286+
}
267287
for _, file := range o.files {
268-
command := fmt.Sprintf("%s %s %s", o.injector, o.command, file)
269-
output := Exec(command)
270-
if err := ioutil.WriteFile(file, output, 0644); err != nil {
288+
command := fmt.Sprintf("%s %s%s %s", o.injector, o.command, flags, file)
289+
stdout, stderr, err := Exec(command)
290+
if err != nil {
291+
return fmt.Errorf(string(stderr))
292+
}
293+
if err := ioutil.WriteFile(file, stdout, 0644); err != nil {
271294
return err
272295
}
273296
}
@@ -323,26 +346,34 @@ func upgrade(o upgradeOptions) error {
323346
}
324347

325348
command := fmt.Sprintf("helm upgrade %s %s%s", o.name, o.chart, additionalFlags)
326-
output := Exec(command)
327-
fmt.Println(string(output))
349+
stdout, stderr, err := Exec(command)
350+
if err != nil || len(stderr) != 0 {
351+
return fmt.Errorf(string(stderr))
352+
}
353+
fmt.Println(string(stdout))
328354

329355
return nil
330356
}
331357

332358
// Exec takes a command as a string and executes it
333-
func Exec(cmd string) []byte {
359+
func Exec(cmd string) ([]byte, []byte, error) {
334360
args := strings.Split(cmd, " ")
335361
binary := args[0]
336362
_, err := exec.LookPath(binary)
337363
if err != nil {
338-
log.Fatal(err)
364+
return nil, nil, err
339365
}
340366

341-
output, err := exec.Command(binary, args[1:]...).Output()
367+
command := exec.Command(binary, args[1:]...)
368+
var stdout, stderr bytes.Buffer
369+
command.Stdout = &stdout
370+
command.Stderr = &stderr
371+
err = command.Run()
342372
if err != nil {
343-
log.Fatal(string(output))
373+
log.Print(stderr.String())
374+
log.Fatal(err)
344375
}
345-
return output
376+
return stdout.Bytes(), stderr.Bytes(), err
346377
}
347378

348379
// MkRandomDir creates a new directory with a random name made of numbers

plugin.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: "inject"
2-
version: "0.1.7"
2+
version: "0.1.8"
33
usage: "inject resources during chart installation"
44
description: "inject plugin for Helm"
55
command: "$HELM_PLUGIN_DIR/bin/inj"

0 commit comments

Comments
 (0)