Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exec target command, but still pipe it to tee #4286

Merged
merged 1 commit into from
Jan 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions nodeup/pkg/model/convenience.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import (
"path/filepath"
"sort"
"strconv"
"strings"

"github.com/golang/glog"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/nodeup/nodetasks"

"github.com/golang/glog"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// s is a helper that builds a *string from a string value
Expand Down Expand Up @@ -187,3 +187,19 @@ func addHostPathMapping(pod *v1.Pod, container *v1.Container, name, path string)
func convEtcdSettingsToMs(dur *metav1.Duration) string {
return strconv.FormatInt(dur.Nanoseconds()/1000000, 10)
}

// execWithTee returns the command to run the command while piping output to both the log file and stdout/stderr
func execWithTee(cmd string, args []string, logfile string) []string {
// exec so we don't have a shell that doesn't pass signals to the real process
execCmd := "exec " + cmd + " " + strings.Join(args, " ")

// NOTE: tee & mkfifo is in /usr/bin in the kube-proxy image, but /bin in other images

// Bash supports something like this, but dash and other limited shells don't
//shCmd := "exec &> >(/usr/bin/tee -a " + logfile + "); " + execCmd
// Instead we create the pipe manually and wire up the tee:
shCmd := "mkfifo /tmp/pipe; (tee -a " + logfile + " < /tmp/pipe & ) ; " + execCmd + " > /tmp/pipe 2>&1"

// Execute shell command
return []string{"/bin/sh", "-c", shCmd}
}
8 changes: 4 additions & 4 deletions nodeup/pkg/model/kube_apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,10 @@ func (b *KubeAPIServerBuilder) buildPod() (*v1.Pod, error) {
container := &v1.Container{
Name: "kube-apiserver",
Image: b.Cluster.Spec.KubeAPIServer.Image,
Command: []string{
"/bin/sh", "-c",
"/usr/local/bin/kube-apiserver " + strings.Join(sortedStrings(flags), " ") + " 2>&1 | /bin/tee -a /var/log/kube-apiserver.log",
},
Command: execWithTee(
"/usr/local/bin/kube-apiserver",
sortedStrings(flags),
"/var/log/kube-apiserver.log"),
Env: getProxyEnvVars(b.Cluster.Spec.EgressProxy),
LivenessProbe: &v1.Probe{
Handler: v1.Handler{
Expand Down
8 changes: 4 additions & 4 deletions nodeup/pkg/model/kube_controller_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ func (b *KubeControllerManagerBuilder) buildPod() (*v1.Pod, error) {
container := &v1.Container{
Name: "kube-controller-manager",
Image: b.Cluster.Spec.KubeControllerManager.Image,
Command: []string{
"/bin/sh", "-c",
"/usr/local/bin/kube-controller-manager " + strings.Join(sortedStrings(flags), " ") + " 2>&1 | /bin/tee -a /var/log/kube-controller-manager.log",
},
Command: execWithTee(
"/usr/local/bin/kube-controller-manager",
sortedStrings(flags),
"/var/log/kube-controller-manager.log"),
Env: getProxyEnvVars(b.Cluster.Spec.EgressProxy),
LivenessProbe: &v1.Probe{
Handler: v1.Handler{
Expand Down
9 changes: 4 additions & 5 deletions nodeup/pkg/model/kube_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package model

import (
"fmt"
"strings"

"k8s.io/kops/pkg/dns"
"k8s.io/kops/pkg/flagbuilder"
Expand Down Expand Up @@ -144,10 +143,10 @@ func (b *KubeProxyBuilder) buildPod() (*v1.Pod, error) {
container := &v1.Container{
Name: "kube-proxy",
Image: image,
Command: []string{
"/bin/sh", "-c",
"/usr/local/bin/kube-proxy " + strings.Join(sortedStrings(flags), " ") + " 2>&1 | /usr/bin/tee -a /var/log/kube-proxy.log",
},
Command: execWithTee(
"/usr/local/bin/kube-proxy",
sortedStrings(flags),
"/var/log/kube-proxy.log"),
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
"cpu": cpuRequest,
Expand Down
9 changes: 4 additions & 5 deletions nodeup/pkg/model/kube_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package model

import (
"fmt"
"strings"

"k8s.io/kops/pkg/flagbuilder"
"k8s.io/kops/upup/pkg/fi"
Expand Down Expand Up @@ -125,10 +124,10 @@ func (b *KubeSchedulerBuilder) buildPod() (*v1.Pod, error) {
container := &v1.Container{
Name: "kube-scheduler",
Image: c.Image,
Command: []string{
"/bin/sh", "-c",
"/usr/local/bin/kube-scheduler " + strings.Join(sortedStrings(flags), " ") + " 2>&1 | /bin/tee -a /var/log/kube-scheduler.log",
},
Command: execWithTee(
"/usr/local/bin/kube-scheduler",
sortedStrings(flags),
"/var/log/kube-scheduler.log"),
Env: getProxyEnvVars(b.Cluster.Spec.EgressProxy),
LivenessProbe: &v1.Probe{
Handler: v1.Handler{
Expand Down
5 changes: 4 additions & 1 deletion protokube/pkg/protokube/etcd_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ func BuildEtcdManifest(c *EtcdCluster) *v1.Pod {
},
},
Command: []string{
"/bin/sh", "-c", "/usr/local/bin/etcd 2>&1 | /bin/tee -a /var/log/etcd.log",
"/bin/sh", "-c",
"/bin/mkfifo /tmp/pipe; " +
"(/bin/tee -a /var/log/etcd.log < /tmp/pipe & ); " +
"exec /usr/local/bin/etcd > /tmp/pipe 2>&1",
},
}
// build the environment variables for etcd service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ spec:
- command:
- /bin/sh
- -c
- /usr/local/bin/etcd 2>&1 | /bin/tee -a /var/log/etcd.log
- /bin/mkfifo /tmp/pipe; (/bin/tee -a /var/log/etcd.log < /tmp/pipe & ); exec
/usr/local/bin/etcd > /tmp/pipe 2>&1
env:
- name: ETCD_NAME
value: node0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ spec:
- command:
- /bin/sh
- -c
- /usr/local/bin/etcd 2>&1 | /bin/tee -a /var/log/etcd.log
- /bin/mkfifo /tmp/pipe; (/bin/tee -a /var/log/etcd.log < /tmp/pipe & ); exec
/usr/local/bin/etcd > /tmp/pipe 2>&1
env:
- name: ETCD_NAME
value: node0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ spec:
- command:
- /bin/sh
- -c
- /usr/local/bin/etcd 2>&1 | /bin/tee -a /var/log/etcd.log
- /bin/mkfifo /tmp/pipe; (/bin/tee -a /var/log/etcd.log < /tmp/pipe & ); exec
/usr/local/bin/etcd > /tmp/pipe 2>&1
env:
- name: ETCD_NAME
value: node0
Expand Down