From 9dc864fd0feefd907aba16ba98cf453dd16df694 Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Thu, 19 Mar 2026 09:52:33 -0700 Subject: [PATCH] Switch to proto instead of json Signed-off-by: Maksym Pavlenko --- api/runtime/bootstrap/v1/bootstrap.proto | 5 ---- core/runtime/v2/shim.go | 32 +++++++++++++++++------- pkg/shim/shim.go | 7 +++--- pkg/shim/util.go | 3 +-- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/api/runtime/bootstrap/v1/bootstrap.proto b/api/runtime/bootstrap/v1/bootstrap.proto index 1cb27129f..c90aecc76 100644 --- a/api/runtime/bootstrap/v1/bootstrap.proto +++ b/api/runtime/bootstrap/v1/bootstrap.proto @@ -26,11 +26,6 @@ // 3. shim initializes and writes BootstrapResult as JSON to stdout // 4. containerd connects to the address provided in BootstrapResult // -// Note: JSON serialization is used to keep shim binaries small since the -// encoding/json package is already part of the Go runtime. The proto -// definition serves as the schema for detecting breaking changes and -// maintaining forward/backward compatibility. -// // This design enables: // - Forward/backward compatibility via version field // - Typed extensibility via google.protobuf.Any and Extension diff --git a/core/runtime/v2/shim.go b/core/runtime/v2/shim.go index 92dff55bd..7832444af 100644 --- a/core/runtime/v2/shim.go +++ b/core/runtime/v2/shim.go @@ -53,6 +53,7 @@ import ( "github.com/containerd/containerd/v2/pkg/dialer" "github.com/containerd/containerd/v2/pkg/identifiers" "github.com/containerd/containerd/v2/pkg/protobuf" + "github.com/containerd/containerd/v2/pkg/protobuf/proto" ptypes "github.com/containerd/containerd/v2/pkg/protobuf/types" client "github.com/containerd/containerd/v2/pkg/shim" "github.com/containerd/containerd/v2/pkg/timeout" @@ -221,20 +222,33 @@ type clientVersionDowngrader interface { } func parseStartResponse(response []byte) (*bootapi.BootstrapResult, error) { - var params bootapi.BootstrapResult + var result bootapi.BootstrapResult - if err := json.Unmarshal(response, ¶ms); err != nil || params.Version < 2 { - // Use TTRPC for legacy shims - params.Address = string(response) - params.Protocol = "ttrpc" - params.Version = 2 + if json.Valid(response) { + var params client.BootstrapParams + if err := json.Unmarshal(response, ¶ms); err != nil || params.Version < 2 { + // Use TTRPC for legacy shims + params.Address = string(response) + params.Protocol = "ttrpc" + params.Version = 2 + } + + if params.Version > CurrentShimVersion { + return nil, fmt.Errorf("unsupported shim version (%d): %w", params.Version, errdefs.ErrNotImplemented) + } + + return &bootapi.BootstrapResult{ + Version: int32(params.Version), + Address: params.Address, + Protocol: params.Protocol, + }, nil } - if params.Version > CurrentShimVersion { - return nil, fmt.Errorf("unsupported shim version (%d): %w", params.Version, errdefs.ErrNotImplemented) + if err := proto.Unmarshal(response, &result); err != nil { + return nil, fmt.Errorf("unable to read shim bootstrap response: %w", err) } - return ¶ms, nil + return &result, nil } // writeBootstrapParams writes shim's bootstrap configuration (e.g. how to connect, version, etc). diff --git a/pkg/shim/shim.go b/pkg/shim/shim.go index add38df00..3b45e8060 100644 --- a/pkg/shim/shim.go +++ b/pkg/shim/shim.go @@ -18,7 +18,6 @@ package shim import ( "context" - "encoding/json" "errors" "flag" "fmt" @@ -280,7 +279,7 @@ func run(ctx context.Context, manager Shim, config Config) error { } var params bootapi.BootstrapParams - if err := json.Unmarshal(input, ¶ms); err != nil { + if err := proto.Unmarshal(input, ¶ms); err != nil { // TODO: Return error once the new API is stable if err := readBootstrapParamsFromDeprecatedFields(input, ¶ms); err != nil { return err @@ -292,9 +291,9 @@ func run(ctx context.Context, manager Shim, config Config) error { return err } - data, err := json.Marshal(result) + data, err := proto.Marshal(result) if err != nil { - return fmt.Errorf("failed to marshal bootstrap params to json: %w", err) + return fmt.Errorf("failed to marshal bootstrap params: %w", err) } if _, err := os.Stdout.Write(data); err != nil { diff --git a/pkg/shim/util.go b/pkg/shim/util.go index 4c95859f3..c51166d9e 100644 --- a/pkg/shim/util.go +++ b/pkg/shim/util.go @@ -19,7 +19,6 @@ package shim import ( "bytes" "context" - "encoding/json" "errors" "fmt" "io" @@ -141,7 +140,7 @@ func Command(ctx context.Context, config *CommandConfig) (*exec.Cmd, error) { } } - data, err := json.Marshal(¶ms) + data, err := proto.Marshal(¶ms) if err != nil { return nil, fmt.Errorf("unable to marshal bootstrap params: %w", err) }