Skip to content

Commit 9cb4860

Browse files
Instead of launching kubelet directly, exec
If the only component running on the node is the kubelet, exec with the necessary args instead of running the code in the current process.
1 parent 13f54d5 commit 9cb4860

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

pkg/cmd/server/kubernetes/node/options/options.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,7 @@ func buildKubeProxyConfig(options configapi.NodeConfig) (*componentconfig.KubePr
209209

210210
return proxyconfig, nil
211211
}
212+
213+
func ToFlags(config *kubeletoptions.KubeletServer) []string {
214+
return cmdflags.AsArgs(config.AddFlags, kubeletoptions.NewKubeletServer().AddFlags)
215+
}

pkg/cmd/server/start/start_node.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ import (
55
"fmt"
66
"io"
77
"os"
8+
"os/exec"
89
"path/filepath"
910
"strings"
11+
"syscall"
1012

1113
"github.com/coreos/go-systemd/daemon"
1214
"github.com/golang/glog"
1315
"github.com/spf13/cobra"
1416

1517
kerrors "k8s.io/apimachinery/pkg/api/errors"
18+
"k8s.io/apimachinery/pkg/util/sets"
1619
"k8s.io/apimachinery/pkg/util/wait"
1720
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
1821
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
@@ -334,6 +337,15 @@ func StartNode(nodeConfig configapi.NodeConfig, components *utilflags.ComponentF
334337
return err
335338
}
336339

340+
if components.Calculated().Equal(sets.NewString(ComponentKubelet)) {
341+
args := nodeoptions.ToFlags(server)
342+
if path, err := exec.LookPath("kubelet"); err == nil {
343+
glog.V(3).Infof("Exec %s %s", path, strings.Join(args, " "))
344+
args = append([]string{path}, args...)
345+
return syscall.Exec(path, args, os.Environ())
346+
}
347+
}
348+
337349
networkConfig, err := network.New(nodeConfig, server.ClusterDomain, proxyConfig, components.Enabled(ComponentProxy), components.Enabled(ComponentDNS) && len(nodeConfig.DNSBindAddress) > 0)
338350
if err != nil {
339351
return err

pkg/cmd/util/flags/flags.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,33 @@ func Resolve(args map[string][]string, fn func(*pflag.FlagSet)) []error {
3636
return Apply(args, fs)
3737
}
3838

39+
func AsArgs(fn, defaultFn func(*pflag.FlagSet)) []string {
40+
fs := pflag.NewFlagSet("extended", pflag.ContinueOnError)
41+
fn(fs)
42+
defaults := pflag.NewFlagSet("defaults", pflag.ContinueOnError)
43+
defaultFn(defaults)
44+
var args []string
45+
fs.VisitAll(func(flag *pflag.Flag) {
46+
defaultFlag := defaults.Lookup(flag.Name)
47+
s := flag.Value.String()
48+
defaultValue := defaultFlag.Value.String()
49+
if s == defaultValue {
50+
return
51+
}
52+
if values, err := fs.GetStringSlice(flag.Name); err == nil {
53+
for _, s := range values {
54+
args = append(args, fmt.Sprintf("--%s=%s", flag.Name, s))
55+
}
56+
} else {
57+
if len(s) == 0 {
58+
s = defaultValue
59+
}
60+
args = append(args, fmt.Sprintf("--%s=%s", flag.Name, s))
61+
}
62+
})
63+
return args
64+
}
65+
3966
// ComponentFlag represents a set of enabled components used in a command line.
4067
type ComponentFlag struct {
4168
enabled string

0 commit comments

Comments
 (0)