Skip to content

Commit 4131c65

Browse files
author
Yanqiang Miao
committed
Rename 'crictl info' to 'crictl version' and add verbose for crictl status
Signed-off-by: Yanqiang Miao <[email protected]>
1 parent dfc2e77 commit 4131c65

File tree

2 files changed

+69
-40
lines changed

2 files changed

+69
-40
lines changed

cmd/crictl/info.go

+50-19
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,75 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"bytes"
21+
"encoding/json"
2022
"fmt"
2123

2224
"github.com/Sirupsen/logrus"
25+
"github.com/ghodss/yaml"
2326
"github.com/urfave/cli"
2427
"golang.org/x/net/context"
2528
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
2629
)
2730

28-
const (
29-
criClientVersion = "v1alpha1"
30-
)
31-
32-
var runtimeVersionCommand = cli.Command{
33-
Name: "info",
34-
Usage: "Display runtime version information",
31+
var runtimeStatusCommand = cli.Command{
32+
Name: "info",
33+
Usage: "Display status and extra information of the container runtime",
34+
ArgsUsage: "",
35+
Flags: []cli.Flag{
36+
cli.StringFlag{
37+
Name: "output, o",
38+
Value: "json",
39+
Usage: "Output format, One of: json|yaml",
40+
},
41+
},
3542
Action: func(context *cli.Context) error {
36-
err := Version(runtimeClient, criClientVersion)
43+
err := Info(context, runtimeClient)
3744
if err != nil {
38-
return fmt.Errorf("Getting the runtime version failed: %v", err)
45+
return fmt.Errorf("getting status of runtime failed: %v", err)
3946
}
4047
return nil
4148
},
4249
Before: getRuntimeClient,
4350
After: closeConnection,
4451
}
4552

46-
// Version sends a VersionRequest to the server, and parses the returned VersionResponse.
47-
func Version(client pb.RuntimeServiceClient, version string) error {
48-
request := &pb.VersionRequest{Version: version}
49-
logrus.Debugf("VersionRequest: %v", request)
50-
r, err := client.Version(context.Background(), request)
51-
logrus.Debugf("VersionResponse: %v", r)
53+
// Info sends a StatusRequest to the server, and parses the returned StatusResponse.
54+
func Info(cliContext *cli.Context, client pb.RuntimeServiceClient) error {
55+
request := &pb.StatusRequest{}
56+
logrus.Debugf("StatusRequest: %v", request)
57+
r, err := client.Status(context.Background(), request)
58+
logrus.Debugf("StatusResponse: %v", r)
59+
if err != nil {
60+
return err
61+
}
62+
63+
statusByte, err := json.Marshal(r.Status)
5264
if err != nil {
5365
return err
5466
}
55-
fmt.Println("Version: ", r.Version)
56-
fmt.Println("RuntimeName: ", r.RuntimeName)
57-
fmt.Println("RuntimeVersion: ", r.RuntimeVersion)
58-
fmt.Println("RuntimeApiVersion: ", r.RuntimeApiVersion)
67+
jsonInfo := "{" + "\"status\":" + string(statusByte) + ","
68+
for k, v := range r.Info {
69+
jsonInfo += "\"" + k + "\"" + v + ","
70+
}
71+
jsonInfo = jsonInfo[:len(jsonInfo)-1]
72+
jsonInfo += "}"
73+
74+
switch cliContext.String("output") {
75+
case "yaml":
76+
yamlInfo, err := yaml.JSONToYAML([]byte(jsonInfo))
77+
if err != nil {
78+
return err
79+
}
80+
fmt.Println(string(yamlInfo))
81+
case "json":
82+
var output bytes.Buffer
83+
if err := json.Indent(&output, []byte(jsonInfo), "", " "); err != nil {
84+
return err
85+
}
86+
fmt.Println(output.String())
87+
default:
88+
fmt.Printf("Don't support %q format\n", cliContext.String("output"))
89+
}
5990
return nil
6091
}

cmd/crictl/status.go cmd/crictl/version.go

+19-21
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,43 @@ package main
1818

1919
import (
2020
"fmt"
21-
"os"
22-
"text/tabwriter"
2321

2422
"github.com/Sirupsen/logrus"
2523
"github.com/urfave/cli"
2624
"golang.org/x/net/context"
2725
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
2826
)
2927

30-
var runtimeStatusCommand = cli.Command{
31-
Name: "status",
32-
Usage: "Display status of the container runtime",
33-
ArgsUsage: "",
28+
const (
29+
criClientVersion = "v1alpha1"
30+
)
31+
32+
var runtimeVersionCommand = cli.Command{
33+
Name: "version",
34+
Usage: "Display runtime version information",
3435
Action: func(context *cli.Context) error {
35-
err := Status(runtimeClient)
36+
err := Version(runtimeClient, criClientVersion)
3637
if err != nil {
37-
return fmt.Errorf("Getting status of runtime failed: %v", err)
38+
return fmt.Errorf("Getting the runtime version failed: %v", err)
3839
}
3940
return nil
4041
},
4142
Before: getRuntimeClient,
4243
After: closeConnection,
4344
}
4445

45-
// Status sends a StatusRequest to the server, and parses the returned StatusResponse.
46-
func Status(client pb.RuntimeServiceClient) error {
47-
request := &pb.StatusRequest{}
48-
logrus.Debugf("StatusRequest: %v", request)
49-
r, err := client.Status(context.Background(), request)
50-
logrus.Debugf("StatusResponse: %v", r)
46+
// Version sends a VersionRequest to the server, and parses the returned VersionResponse.
47+
func Version(client pb.RuntimeServiceClient, version string) error {
48+
request := &pb.VersionRequest{Version: version}
49+
logrus.Debugf("VersionRequest: %v", request)
50+
r, err := client.Version(context.Background(), request)
51+
logrus.Debugf("VersionResponse: %v", r)
5152
if err != nil {
5253
return err
5354
}
54-
55-
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
56-
fmt.Fprintln(w, "CONDITION\tSTATUS\tREASON\tMESSAGE")
57-
for _, c := range r.GetStatus().GetConditions() {
58-
fmt.Fprintf(w, "%s\t%v\t%s\t%s\n", c.Type, c.Status, c.Reason, c.Message)
59-
}
60-
w.Flush()
55+
fmt.Println("Version: ", r.Version)
56+
fmt.Println("RuntimeName: ", r.RuntimeName)
57+
fmt.Println("RuntimeVersion: ", r.RuntimeVersion)
58+
fmt.Println("RuntimeApiVersion: ", r.RuntimeApiVersion)
6159
return nil
6260
}

0 commit comments

Comments
 (0)