Skip to content

Commit 130a9c7

Browse files
committed
Ensure namespace is proxied to grpc/ttrpc plugins
Before this change we only ever had the grpc incoming medata set so when we make a request to a shim or a grpc plugin the namespace is not sent over the RPC. I need this for github.com/cpuguy83/systemdshim, and I am sure there are other use-cases where this would be needed. Signed-off-by: Brian Goff <[email protected]>
1 parent 10d9d1a commit 130a9c7

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

services/server/namespace.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 server
18+
19+
import (
20+
"context"
21+
22+
"github.com/containerd/containerd/namespaces"
23+
"google.golang.org/grpc"
24+
)
25+
26+
func unaryNamespaceInterceptor(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
27+
if ns, ok := namespaces.Namespace(ctx); ok {
28+
// The above call checks the *incoming* metadata, this makes sure the outgoing metadata is also set
29+
ctx = namespaces.WithNamespace(ctx, ns)
30+
}
31+
return handler(ctx, req)
32+
}
33+
34+
func streamNamespaceInterceptor(srv interface{}, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
35+
ctx := ss.Context()
36+
if ns, ok := namespaces.Namespace(ctx); ok {
37+
// The above call checks the *incoming* metadata, this makes sure the outgoing metadata is also set
38+
ctx = namespaces.WithNamespace(ctx, ns)
39+
ss = &wrappedSSWithContext{ctx: ctx, ServerStream: ss}
40+
}
41+
42+
return handler(srv, ss)
43+
}
44+
45+
type wrappedSSWithContext struct {
46+
grpc.ServerStream
47+
ctx context.Context
48+
}
49+
50+
func (w *wrappedSSWithContext) Context() context.Context {
51+
return w.ctx
52+
}

services/server/server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ func New(ctx context.Context, config *srvconfig.Config) (*Server, error) {
102102
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
103103
otelgrpc.StreamServerInterceptor(),
104104
grpc.StreamServerInterceptor(grpc_prometheus.StreamServerInterceptor),
105+
streamNamespaceInterceptor,
105106
)),
106107
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
107108
otelgrpc.UnaryServerInterceptor(),
108109
grpc.UnaryServerInterceptor(grpc_prometheus.UnaryServerInterceptor),
110+
unaryNamespaceInterceptor,
109111
)),
110112
}
111113
if config.GRPC.MaxRecvMsgSize > 0 {

0 commit comments

Comments
 (0)