@@ -54,7 +54,7 @@ func main() {
54
54
switch cmd {
55
55
case "kubectl" :
56
56
env := getEnv ()
57
- curCtx := cmdStrOutput ( "kubectl" , "config" , "current-context" )
57
+ curCtx := kubeCurrentContext ( )
58
58
wantCtx := fmt .Sprintf ("gke_%s_%s_go" , env .ProjectName , env .Zone )
59
59
if curCtx != wantCtx {
60
60
log .SetFlags (0 )
@@ -73,6 +73,29 @@ func main() {
73
73
}
74
74
}
75
75
76
+ func kubeCurrentContext () string {
77
+ kubectl , err := exec .LookPath ("kubectl" )
78
+ if err != nil {
79
+ log .SetFlags (0 )
80
+ log .Fatalf ("No kubectl in path." )
81
+ }
82
+ // Get current context, but ignore errors, as kubectl returns an error
83
+ // if there's no context.
84
+ out , err := exec .Command (kubectl , "config" , "current-context" ).Output ()
85
+ if err != nil {
86
+ var stderr string
87
+ if ee , ok := err .(* exec.ExitError ); ok {
88
+ stderr = string (ee .Stderr )
89
+ }
90
+ if strings .Contains (stderr , "current-context is not set" ) {
91
+ return ""
92
+ }
93
+ log .Printf ("Failed to run 'kubectl config current-context': %v, %s" , err , stderr )
94
+ return ""
95
+ }
96
+ return strings .TrimSpace (string (out ))
97
+ }
98
+
76
99
func getEnv () * buildenv.Environment {
77
100
if * prod == * staging {
78
101
log .Fatalf ("must specify exactly one of --prod or --staging" )
@@ -83,22 +106,6 @@ func getEnv() *buildenv.Environment {
83
106
return buildenv .Staging
84
107
}
85
108
86
- func cmdStrOutput (cmd string , args ... string ) string {
87
- out , err := exec .Command (cmd , args ... ).Output ()
88
- if err != nil {
89
- var stderr []byte
90
- if ee , ok := err .(* exec.ExitError ); ok {
91
- stderr = ee .Stderr
92
- }
93
- log .Fatalf ("error running %s %v: %v, %s" , cmd , args , err , stderr )
94
- }
95
- ret := strings .TrimSpace (string (out ))
96
- if ret == "" {
97
- log .Fatalf ("expected output from %s %v; got nothing" , cmd , args )
98
- }
99
- return ret
100
- }
101
-
102
109
var expectedGoLayerVersion = map [string ]string {
103
110
"golang:1.10" : "go version go1.10.2 linux/amd64" ,
104
111
}
0 commit comments