Skip to content

Commit

Permalink
exec target command, but still pipe it to tee
Browse files Browse the repository at this point in the history
  • Loading branch information
justinsb committed Jan 20, 2018
1 parent 59440bb commit a559cfe
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 25 deletions.
18 changes: 15 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,15 @@ 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 in between eating signals
execCmd := "exec " + cmd + " " + strings.Join(args, " ")

// Redirect to tee
shCmd := "exec &> >(/usr/bin/tee -a " + logfile + "); " + execCmd

// 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
4 changes: 3 additions & 1 deletion protokube/pkg/protokube/etcd_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ 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",
"exec &> >(/usr/bin/tee -a /var/log/etcd.log); " + // Redirect to tee, but still exec real command
"exec /usr/local/bin/etcd",
},
}
// build the environment variables for etcd service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ spec:
- command:
- /bin/sh
- -c
- /usr/local/bin/etcd 2>&1 | /bin/tee -a /var/log/etcd.log
- exec &> >(/usr/bin/tee -a /var/log/etcd.log); exec /usr/local/bin/etcd
env:
- name: ETCD_NAME
value: node0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ spec:
- command:
- /bin/sh
- -c
- /usr/local/bin/etcd 2>&1 | /bin/tee -a /var/log/etcd.log
- exec &> >(/usr/bin/tee -a /var/log/etcd.log); exec /usr/local/bin/etcd
env:
- name: ETCD_NAME
value: node0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ spec:
- command:
- /bin/sh
- -c
- /usr/local/bin/etcd 2>&1 | /bin/tee -a /var/log/etcd.log
- exec &> >(/usr/bin/tee -a /var/log/etcd.log); exec /usr/local/bin/etcd
env:
- name: ETCD_NAME
value: node0
Expand Down

0 comments on commit a559cfe

Please sign in to comment.