Skip to content

Commit

Permalink
ctr: new deprecations command
Browse files Browse the repository at this point in the history
Signed-off-by: Samuel Karp <samuelkarp@google.com>
  • Loading branch information
samuelkarp committed Oct 25, 2023
1 parent 079383d commit 3fff8b4
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 3 deletions.
9 changes: 6 additions & 3 deletions cmd/ctr/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ import (
"fmt"
"io"

"github.com/containerd/log"
"github.com/urfave/cli"
"google.golang.org/grpc/grpclog"

"github.com/containerd/containerd/cmd/ctr/commands/containers"
"github.com/containerd/containerd/cmd/ctr/commands/content"
"github.com/containerd/containerd/cmd/ctr/commands/deprecations"
"github.com/containerd/containerd/cmd/ctr/commands/events"
"github.com/containerd/containerd/cmd/ctr/commands/images"
"github.com/containerd/containerd/cmd/ctr/commands/info"
Expand All @@ -39,9 +44,6 @@ import (
"github.com/containerd/containerd/defaults"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/version"
"github.com/containerd/log"
"github.com/urfave/cli"
"google.golang.org/grpc/grpclog"
)

var extraCmds = []cli.Command{}
Expand Down Expand Up @@ -118,6 +120,7 @@ containerd CLI
ociCmd.Command,
sandboxes.Command,
info.Command,
deprecations.Command,
}, extraCmds...)
app.Before = func(context *cli.Context) error {
if context.GlobalBool("debug") {
Expand Down
113 changes: 113 additions & 0 deletions cmd/ctr/commands/deprecations/deprecations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package deprecations

import (
"fmt"
"os"
"text/tabwriter"
"time"

"github.com/urfave/cli"

api "github.com/containerd/containerd/api/services/introspection/v1"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/protobuf"
ptypes "github.com/containerd/containerd/protobuf/types"
)

// Command is the parent for all commands under "deprecations"
var Command = cli.Command{
Name: "deprecations",
Subcommands: []cli.Command{
listCommand,
},
}
var listCommand = cli.Command{
Name: "list",
Usage: "Print warnings for deprecations",
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "output format to use (Examples: 'default', 'json')",
},
},
Action: func(context *cli.Context) error {
client, ctx, cancel, err := commands.NewClient(context)
if err != nil {
return err
}
defer cancel()

resp, err := client.IntrospectionService().Server(ctx, &ptypes.Empty{})
if err != nil {
return err
}
wrn := warnings(resp)
if len(wrn) > 0 {
switch context.String("format") {
case "json":
commands.PrintAsJSON(warnings(resp))
return nil
default:
w := tabwriter.NewWriter(os.Stdout, 4, 8, 4, ' ', 0)
fmt.Fprintln(w, "ID\tLAST OCCURRENCE\tMESSAGE\t")
for _, dw := range wrn {
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\n",
dw.ID,
dw.LastOccurrence.Format(time.RFC3339Nano),
dw.Message,
); err != nil {
return err
}
}
return w.Flush()
}

}
return nil
},
}

type deprecationWarning struct {
ID string `json:"id"`
Message string `json:"message"`
LastOccurrence time.Time `json:"lastOccurrence"`
}

func warnings(in *api.ServerResponse) []deprecationWarning {
var warnings []deprecationWarning
for _, dw := range in.Deprecations {
wrn := deprecationWarningFromPB(dw)
if wrn == nil {
continue
}
warnings = append(warnings, *wrn)
}
return warnings
}
func deprecationWarningFromPB(in *api.DeprecationWarning) *deprecationWarning {
if in == nil {
return nil
}
lo := protobuf.FromTimestamp(in.LastOccurrence)
return &deprecationWarning{
ID: in.ID,
Message: in.Message,
LastOccurrence: lo,
}
}

0 comments on commit 3fff8b4

Please sign in to comment.