|
| 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