exec target command, but still pipe it to tee

Equivalent of https://github.com/kubernetes/kubernetes/pull/57756
This commit is contained in:
Justin Santa Barbara 2018-01-16 21:12:28 -06:00
parent 6bcc86bc0f
commit 7ca593b994
9 changed files with 45 additions and 25 deletions

View File

@ -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
@ -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}
}

View File

@ -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{

View File

@ -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{

View File

@ -18,7 +18,6 @@ package model
import (
"fmt"
"strings"
"k8s.io/kops/pkg/dns"
"k8s.io/kops/pkg/flagbuilder"
@ -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,

View File

@ -18,7 +18,6 @@ package model
import (
"fmt"
"strings"
"k8s.io/kops/pkg/flagbuilder"
"k8s.io/kops/upup/pkg/fi"
@ -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{

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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