Skip to content

Commit 477b7d6

Browse files
committed
ctr: new deprecations command
Signed-off-by: Samuel Karp <[email protected]> (cherry picked from commit 3fff8b4) Signed-off-by: Samuel Karp <[email protected]>
1 parent 24068b8 commit 477b7d6

2 files changed

Lines changed: 114 additions & 0 deletions

File tree

cmd/ctr/app/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/containerd/containerd/cmd/ctr/commands/containers"
2424
"github.com/containerd/containerd/cmd/ctr/commands/content"
25+
"github.com/containerd/containerd/cmd/ctr/commands/deprecations"
2526
"github.com/containerd/containerd/cmd/ctr/commands/events"
2627
"github.com/containerd/containerd/cmd/ctr/commands/images"
2728
"github.com/containerd/containerd/cmd/ctr/commands/install"
@@ -114,6 +115,7 @@ containerd CLI
114115
tasks.Command,
115116
install.Command,
116117
ociCmd.Command,
118+
deprecations.Command,
117119
}, extraCmds...)
118120
app.Before = func(context *cli.Context) error {
119121
if context.GlobalBool("debug") {
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package deprecations
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"text/tabwriter"
23+
"time"
24+
25+
ptypes "github.com/gogo/protobuf/types"
26+
"github.com/urfave/cli"
27+
28+
api "github.com/containerd/containerd/api/services/introspection/v1"
29+
"github.com/containerd/containerd/cmd/ctr/commands"
30+
)
31+
32+
// Command is the parent for all commands under "deprecations"
33+
var Command = cli.Command{
34+
Name: "deprecations",
35+
Subcommands: []cli.Command{
36+
listCommand,
37+
},
38+
}
39+
var listCommand = cli.Command{
40+
Name: "list",
41+
Usage: "Print warnings for deprecations",
42+
Flags: []cli.Flag{
43+
cli.StringFlag{
44+
Name: "format",
45+
Usage: "output format to use (Examples: 'default', 'json')",
46+
},
47+
},
48+
Action: func(context *cli.Context) error {
49+
client, ctx, cancel, err := commands.NewClient(context)
50+
if err != nil {
51+
return err
52+
}
53+
defer cancel()
54+
55+
resp, err := client.IntrospectionService().Server(ctx, &ptypes.Empty{})
56+
if err != nil {
57+
return err
58+
}
59+
wrn := warnings(resp)
60+
if len(wrn) > 0 {
61+
switch context.String("format") {
62+
case "json":
63+
commands.PrintAsJSON(warnings(resp))
64+
return nil
65+
default:
66+
w := tabwriter.NewWriter(os.Stdout, 4, 8, 4, ' ', 0)
67+
fmt.Fprintln(w, "ID\tLAST OCCURRENCE\tMESSAGE\t")
68+
for _, dw := range wrn {
69+
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\n",
70+
dw.ID,
71+
dw.LastOccurrence.Format(time.RFC3339Nano),
72+
dw.Message,
73+
); err != nil {
74+
return err
75+
}
76+
}
77+
return w.Flush()
78+
}
79+
80+
}
81+
return nil
82+
},
83+
}
84+
85+
type deprecationWarning struct {
86+
ID string `json:"id"`
87+
Message string `json:"message"`
88+
LastOccurrence time.Time `json:"lastOccurrence"`
89+
}
90+
91+
func warnings(in *api.ServerResponse) []deprecationWarning {
92+
var warnings []deprecationWarning
93+
for _, dw := range in.Deprecations {
94+
wrn := deprecationWarningFromPB(dw)
95+
if wrn == nil {
96+
continue
97+
}
98+
warnings = append(warnings, *wrn)
99+
}
100+
return warnings
101+
}
102+
func deprecationWarningFromPB(in *api.DeprecationWarning) *deprecationWarning {
103+
if in == nil {
104+
return nil
105+
}
106+
lo, _ := ptypes.TimestampFromProto(in.LastOccurrence)
107+
return &deprecationWarning{
108+
ID: in.ID,
109+
Message: in.Message,
110+
LastOccurrence: lo,
111+
}
112+
}

0 commit comments

Comments
 (0)