Skip to content

Commit 04e7747

Browse files
authored
Merge pull request #3321 from crosbymichael/ttrpc-namespace
Update ttrpc for metadata and namespace support
2 parents 15ae6b7 + fa6a9f0 commit 04e7747

7 files changed

Lines changed: 156 additions & 12 deletions

File tree

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+
}

vendor.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ github.com/Microsoft/go-winio 84b4ab48a50763fe7b3abcef38e5205c12027fac
3737
github.com/Microsoft/hcsshim 8abdbb8205e4192c68b5f84c31197156f31be517
3838
google.golang.org/genproto d80a6e20e776b0b17a324d0ba1ab50a39c8e8944
3939
golang.org/x/text 19e51611da83d6be54ddafce4a4af510cb3e9ea4
40-
github.com/containerd/ttrpc f82148331ad2181edea8f3f649a1f7add6c3f9c2
40+
github.com/containerd/ttrpc a5bd8ce9e40bc7c065a11c6936f4d032ce6bfa2b
4141
github.com/syndtr/gocapability d98352740cb2c55f81556b63d4a1ec64c5a319c2
4242
gotest.tools v2.3.0
4343
github.com/google/go-cmp v0.2.0

vendor/github.com/containerd/ttrpc/client.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/ttrpc/metadata.go

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/ttrpc/server.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/ttrpc/types.go

Lines changed: 15 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)