forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.go
43 lines (38 loc) · 1.33 KB
/
node.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
package node
import (
"fmt"
"regexp"
"strconv"
"strings"
configapi "github.com/openshift/origin/pkg/cmd/server/apis/config"
nodeoptions "github.com/openshift/origin/pkg/cmd/server/kubernetes/node/options"
)
// safeArgRegexp matches only characters that are known safe. DO NOT add to this list
// without fully considering whether that new character can be used to break shell escaping
// rules.
var safeArgRegexp = regexp.MustCompile(`^[\da-zA-Z\-=_\.,/\:]+$`)
// shellEscapeArg quotes an argument if it contains characters that my cause a shell
// interpreter to split the single argument into multiple.
func shellEscapeArg(s string) string {
if safeArgRegexp.MatchString(s) {
return s
}
return strconv.Quote(s)
}
// WriteKubeletFlags writes the correct set of flags to start a Kubelet from the provided node config to
// stdout, instead of launching anything.
func WriteKubeletFlags(nodeConfig configapi.NodeConfig) error {
kubeletArgs, err := nodeoptions.ComputeKubeletFlags(nodeConfig.KubeletArguments, nodeConfig)
if err != nil {
return fmt.Errorf("cannot create kubelet args: %v", err)
}
if err := nodeoptions.CheckFlags(kubeletArgs); err != nil {
return err
}
var outputArgs []string
for _, s := range kubeletArgs {
outputArgs = append(outputArgs, shellEscapeArg(s))
}
fmt.Println(strings.Join(outputArgs, " "))
return nil
}