@@ -17,7 +17,10 @@ limitations under the License.
17
17
package cmd
18
18
19
19
import (
20
+ "encoding/json"
21
+ "fmt"
20
22
"os"
23
+ "strings"
21
24
"text/template"
22
25
23
26
"github.com/docker/machine/libmachine/state"
@@ -35,13 +38,20 @@ import (
35
38
)
36
39
37
40
var statusFormat string
41
+ var output string
42
+
43
+ type KubeconfigStatus struct {
44
+ Correct bool
45
+ IP string
46
+ }
38
47
39
48
// Status represents the status
40
49
type Status struct {
41
- Host string
42
- Kubelet string
43
- APIServer string
44
- Kubeconfig string
50
+ Host string
51
+ Kubelet string
52
+ APIServer string
53
+ Kubeconfig string
54
+ KubeconfigStatus KubeconfigStatus
45
55
}
46
56
47
57
const (
@@ -63,6 +73,11 @@ var statusCmd = &cobra.Command{
63
73
Exit status contains the status of minikube's VM, cluster and kubernetes encoded on it's bits in this order from right to left.
64
74
Eg: 7 meaning: 1 (for minikube NOK) + 2 (for cluster NOK) + 4 (for kubernetes NOK)` ,
65
75
Run : func (cmd * cobra.Command , args []string ) {
76
+
77
+ if output != "text" && statusFormat != defaultStatusFormat {
78
+ exit .UsageT ("Cannot use both --output and --format options" )
79
+ }
80
+
66
81
var returnCode = 0
67
82
api , err := machine .NewAPIClient ()
68
83
if err != nil {
@@ -78,6 +93,8 @@ var statusCmd = &cobra.Command{
78
93
kubeletSt := state .None .String ()
79
94
kubeconfigSt := state .None .String ()
80
95
apiserverSt := state .None .String ()
96
+ var ks bool
97
+ var ipString = ""
81
98
82
99
if hostSt == state .Running .String () {
83
100
clusterBootstrapper , err := getClusterBootstrapper (api , viper .GetString (cmdcfg .Bootstrapper ))
@@ -110,12 +127,13 @@ var statusCmd = &cobra.Command{
110
127
returnCode |= clusterNotRunningStatusFlag
111
128
}
112
129
113
- ks , err : = kubeconfig .IsClusterInConfig (ip , config .GetMachineName ())
130
+ ks , err = kubeconfig .IsClusterInConfig (ip , config .GetMachineName ())
114
131
if err != nil {
115
132
glog .Errorln ("Error kubeconfig status:" , err )
116
133
}
117
134
if ks {
118
135
kubeconfigSt = "Correctly Configured: pointing to minikube-vm at " + ip .String ()
136
+ ipString = ip .String ()
119
137
} else {
120
138
kubeconfigSt = "Misconfigured: pointing to stale minikube-vm." +
121
139
"\n To fix the kubectl context, run minikube update-context"
@@ -130,22 +148,63 @@ var statusCmd = &cobra.Command{
130
148
Kubelet : kubeletSt ,
131
149
APIServer : apiserverSt ,
132
150
Kubeconfig : kubeconfigSt ,
151
+ KubeconfigStatus : KubeconfigStatus {
152
+ Correct : ks ,
153
+ IP : ipString ,
154
+ },
133
155
}
134
- tmpl , err := template .New ("status" ).Parse (statusFormat )
135
- if err != nil {
136
- exit .WithError ("Error creating status template" , err )
137
- }
138
- err = tmpl .Execute (os .Stdout , status )
139
- if err != nil {
140
- exit .WithError ("Error executing status template" , err )
156
+
157
+ switch strings .ToLower (output ) {
158
+ case "text" :
159
+ printStatusText (status )
160
+ case "json" :
161
+ printStatusJSON (status )
162
+ default :
163
+ exit .WithCodeT (exit .BadUsage , fmt .Sprintf ("invalid output format: %s. Valid values: 'text', 'json'" , output ))
141
164
}
142
165
143
166
os .Exit (returnCode )
144
167
},
145
168
}
146
169
147
170
func init () {
148
- statusCmd .Flags ().StringVar (& statusFormat , "format" , defaultStatusFormat ,
171
+ statusCmd .Flags ().StringVarP (& statusFormat , "format" , "f " , defaultStatusFormat ,
149
172
`Go template format string for the status output. The format for Go templates can be found here: https://golang.org/pkg/text/template/
150
173
For the list accessible variables for the template, see the struct values here: https://godoc.org/k8s.io/minikube/cmd/minikube/cmd#Status` )
174
+ statusCmd .Flags ().StringVarP (& output , "output" , "o" , "text" ,
175
+ `minikube status --output OUTPUT. json, text` )
176
+ }
177
+
178
+ var printStatusText = func (status Status ) {
179
+ tmpl , err := template .New ("status" ).Parse (statusFormat )
180
+ if err != nil {
181
+ exit .WithError ("Error creating status template" , err )
182
+ }
183
+ err = tmpl .Execute (os .Stdout , status )
184
+ if err != nil {
185
+ exit .WithError ("Error executing status template" , err )
186
+ }
187
+ }
188
+
189
+ var printStatusJSON = func (status Status ) {
190
+
191
+ var kubeConfigStatus interface {}
192
+ if status .Kubeconfig != state .None .String () {
193
+ kubeConfigStatus = status .KubeconfigStatus
194
+ } else {
195
+ kubeConfigStatus = nil
196
+ }
197
+
198
+ var outputObject = map [string ]interface {}{
199
+ "Host" : status .Host ,
200
+ "Kubelet" : status .Kubelet ,
201
+ "APIServer" : status .APIServer ,
202
+ "Kubeconfig" : kubeConfigStatus ,
203
+ }
204
+
205
+ jsonString , err := json .Marshal (outputObject )
206
+ if err != nil {
207
+ exit .WithError ("Error converting status to json" , err )
208
+ }
209
+ out .String (string (jsonString ))
151
210
}
0 commit comments