Skip to content

Commit a1f0930

Browse files
committed
Add remote runtime/image Close() API
The API allows to shutdown the internal gRPC connection to avoid leaking resources. Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
1 parent b93bcbf commit a1f0930

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

staging/src/k8s.io/cri-api/pkg/apis/services.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ type RuntimeService interface {
123123
Status(ctx context.Context, verbose bool) (*runtimeapi.StatusResponse, error)
124124
// RuntimeConfig returns the configuration information of the runtime.
125125
RuntimeConfig(ctx context.Context) (*runtimeapi.RuntimeConfigResponse, error)
126+
// Close will shutdown the internal gRPC client connection.
127+
Close() error
126128
}
127129

128130
// ImageManagerService interface should be implemented by a container image
@@ -139,4 +141,6 @@ type ImageManagerService interface {
139141
RemoveImage(ctx context.Context, image *runtimeapi.ImageSpec) error
140142
// ImageFsInfo returns information of the filesystem(s) used to store the read-only layers and the writeable layer.
141143
ImageFsInfo(ctx context.Context) (*runtimeapi.ImageFsInfoResponse, error)
144+
// Close will shutdown the internal gRPC client connection.
145+
Close() error
142146
}

staging/src/k8s.io/cri-client/pkg/remote_image.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type remoteImageService struct {
4444
timeout time.Duration
4545
imageClient runtimeapi.ImageServiceClient
4646
logger *klog.Logger
47+
conn *grpc.ClientConn
4748
}
4849

4950
// NewRemoteImageService creates a new internalapi.ImageManagerService.
@@ -94,6 +95,7 @@ func NewRemoteImageService(endpoint string, connectionTimeout time.Duration, tp
9495
service := &remoteImageService{
9596
timeout: connectionTimeout,
9697
logger: logger,
98+
conn: conn,
9799
}
98100
if err := service.validateServiceConnection(ctx, conn, endpoint); err != nil {
99101
return nil, fmt.Errorf("validate service connection: %w", err)
@@ -103,6 +105,14 @@ func NewRemoteImageService(endpoint string, connectionTimeout time.Duration, tp
103105

104106
}
105107

108+
// Close will shutdown the internal GRPC client connection.
109+
func (r *remoteImageService) Close() error {
110+
if r.conn != nil {
111+
return r.conn.Close()
112+
}
113+
return nil
114+
}
115+
106116
func (r *remoteImageService) log(level int, msg string, keyAndValues ...any) {
107117
internal.Log(r.logger, level, msg, keyAndValues...)
108118
}

staging/src/k8s.io/cri-client/pkg/remote_runtime.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type remoteRuntimeService struct {
5050
// Cache last per-container error message to reduce log spam
5151
logReduction *logreduction.LogReduction
5252
logger *klog.Logger
53+
conn *grpc.ClientConn
5354
}
5455

5556
const (
@@ -127,6 +128,7 @@ func NewRemoteRuntimeService(endpoint string, connectionTimeout time.Duration, t
127128
timeout: connectionTimeout,
128129
logReduction: logreduction.NewLogReduction(identicalErrorDelay),
129130
logger: logger,
131+
conn: conn,
130132
}
131133

132134
if err := service.validateServiceConnection(ctx, conn, endpoint); err != nil {
@@ -136,6 +138,14 @@ func NewRemoteRuntimeService(endpoint string, connectionTimeout time.Duration, t
136138
return service, nil
137139
}
138140

141+
// Close will shutdown the internal GRPC client connection.
142+
func (r *remoteRuntimeService) Close() error {
143+
if r.conn != nil {
144+
return r.conn.Close()
145+
}
146+
return nil
147+
}
148+
139149
func (r *remoteRuntimeService) log(level int, msg string, keyAndValues ...any) {
140150
internal.Log(r.logger, level, msg, keyAndValues...)
141151
}

0 commit comments

Comments
 (0)