Skip to content

Commit 60d48ff

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 74a0667 commit 60d48ff

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

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/info"
@@ -118,6 +119,7 @@ containerd CLI
118119
ociCmd.Command,
119120
sandboxes.Command,
120121
info.Command,
122+
deprecations.Command,
121123
}, extraCmds...)
122124
app.Before = func(context *cli.Context) error {
123125
if context.GlobalBool("debug") {
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
"github.com/urfave/cli"
26+
27+
api "github.com/containerd/containerd/api/services/introspection/v1"
28+
"github.com/containerd/containerd/cmd/ctr/commands"
29+
"github.com/containerd/containerd/protobuf"
30+
ptypes "github.com/containerd/containerd/protobuf/types"
31+
)
32+
33+
// Command is the parent for all commands under "deprecations"
34+
var Command = cli.Command{
35+
Name: "deprecations",
36+
Subcommands: []cli.Command{
37+
listCommand,
38+
},
39+
}
40+
var listCommand = cli.Command{
41+
Name: "list",
42+
Usage: "Print warnings for deprecations",
43+
Flags: []cli.Flag{
44+
cli.StringFlag{
45+
Name: "format",
46+
Usage: "output format to use (Examples: 'default', 'json')",
47+
},
48+
},
49+
Action: func(context *cli.Context) error {
50+
client, ctx, cancel, err := commands.NewClient(context)
51+
if err != nil {
52+
return err
53+
}
54+
defer cancel()
55+
56+
resp, err := client.IntrospectionService().Server(ctx, &ptypes.Empty{})
57+
if err != nil {
58+
return err
59+
}
60+
wrn := warnings(resp)
61+
if len(wrn) > 0 {
62+
switch context.String("format") {
63+
case "json":
64+
commands.PrintAsJSON(warnings(resp))
65+
return nil
66+
default:
67+
w := tabwriter.NewWriter(os.Stdout, 4, 8, 4, ' ', 0)
68+
fmt.Fprintln(w, "ID\tLAST OCCURRENCE\tMESSAGE\t")
69+
for _, dw := range wrn {
70+
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\n",
71+
dw.ID,
72+
dw.LastOccurrence.Format(time.RFC3339Nano),
73+
dw.Message,
74+
); err != nil {
75+
return err
76+
}
77+
}
78+
return w.Flush()
79+
}
80+
81+
}
82+
return nil
83+
},
84+
}
85+
86+
type deprecationWarning struct {
87+
ID string `json:"id"`
88+
Message string `json:"message"`
89+
LastOccurrence time.Time `json:"lastOccurrence"`
90+
}
91+
92+
func warnings(in *api.ServerResponse) []deprecationWarning {
93+
var warnings []deprecationWarning
94+
for _, dw := range in.Deprecations {
95+
wrn := deprecationWarningFromPB(dw)
96+
if wrn == nil {
97+
continue
98+
}
99+
warnings = append(warnings, *wrn)
100+
}
101+
return warnings
102+
}
103+
func deprecationWarningFromPB(in *api.DeprecationWarning) *deprecationWarning {
104+
if in == nil {
105+
return nil
106+
}
107+
lo := protobuf.FromTimestamp(in.LastOccurrence)
108+
return &deprecationWarning{
109+
ID: in.ID,
110+
Message: in.Message,
111+
LastOccurrence: lo,
112+
}
113+
}

0 commit comments

Comments
 (0)