Skip to content

Commit fa6a9f0

Browse files
committed
Add ttrpc namespace support
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
1 parent 42f24b5 commit fa6a9f0

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

namespaces/context.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ type namespaceKey struct{}
3636
// WithNamespace sets a given namespace on the context
3737
func WithNamespace(ctx context.Context, namespace string) context.Context {
3838
ctx = context.WithValue(ctx, namespaceKey{}, namespace) // set our key for namespace
39-
40-
// also store on the grpc headers so it gets picked up by any clients that
39+
// also store on the grpc and ttrpc headers so it gets picked up by any clients that
4140
// are using this.
42-
return withGRPCNamespaceHeader(ctx, namespace)
41+
return withTTRPCNamespaceHeader(withGRPCNamespaceHeader(ctx, namespace), namespace)
4342
}
4443

4544
// NamespaceFromEnv uses the namespace defined in CONTAINERD_NAMESPACE or
@@ -58,9 +57,10 @@ func NamespaceFromEnv(ctx context.Context) context.Context {
5857
func Namespace(ctx context.Context) (string, bool) {
5958
namespace, ok := ctx.Value(namespaceKey{}).(string)
6059
if !ok {
61-
return fromGRPCHeader(ctx)
60+
if namespace, ok = fromGRPCHeader(ctx); !ok {
61+
return fromTTRPCHeader(ctx)
62+
}
6263
}
63-
6464
return namespace, ok
6565
}
6666

@@ -70,10 +70,8 @@ func NamespaceRequired(ctx context.Context) (string, error) {
7070
if !ok || namespace == "" {
7171
return "", errors.Wrapf(errdefs.ErrFailedPrecondition, "namespace is required")
7272
}
73-
7473
if err := Validate(namespace); err != nil {
7574
return "", errors.Wrap(err, "namespace validation")
7675
}
77-
7876
return namespace, nil
7977
}

namespaces/ttrpc.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 namespaces
18+
19+
import (
20+
"context"
21+
22+
"github.com/containerd/ttrpc"
23+
)
24+
25+
const (
26+
// TTRPCHeader defines the header name for specifying a containerd namespace
27+
TTRPCHeader = "containerd-namespace-ttrpc"
28+
)
29+
30+
func withTTRPCNamespaceHeader(ctx context.Context, namespace string) context.Context {
31+
md, ok := ttrpc.GetMetadata(ctx)
32+
if !ok {
33+
md = ttrpc.Metadata{}
34+
}
35+
md.Set(TTRPCHeader, namespace)
36+
return ttrpc.WithMetadata(ctx, md)
37+
}
38+
39+
func fromTTRPCHeader(ctx context.Context) (string, bool) {
40+
return ttrpc.GetMetadataValue(ctx, TTRPCHeader)
41+
}

0 commit comments

Comments
 (0)