Skip to content

Commit 879f092

Browse files
committed
namespaces: Add NamespaceFromEnv
Signed-off-by: Samuel Karp <skarp@amazon.com>
1 parent e7a0651 commit 879f092

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

namespaces/context.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
package namespaces
22

33
import (
4+
"os"
5+
46
"github.com/pkg/errors"
57
"golang.org/x/net/context"
68
)
79

10+
const (
11+
namespaceEnvVar = "CONTAINERD_NAMESPACE"
12+
defaultNamespace = "default"
13+
)
14+
815
var (
916
errNamespaceRequired = errors.New("namespace is required")
1017
)
1118

1219
type namespaceKey struct{}
1320

21+
// WithNamespace sets a given namespace on the context
1422
func WithNamespace(ctx context.Context, namespace string) context.Context {
1523
ctx = context.WithValue(ctx, namespaceKey{}, namespace) // set our key for namespace
1624

@@ -19,6 +27,17 @@ func WithNamespace(ctx context.Context, namespace string) context.Context {
1927
return withGRPCNamespaceHeader(ctx, namespace)
2028
}
2129

30+
// NamespaceFromEnv uses the namespace defined in CONTAINERD_NAMESPACE or
31+
// default
32+
func NamespaceFromEnv(ctx context.Context) context.Context {
33+
namespace := os.Getenv(namespaceEnvVar)
34+
if namespace == "" {
35+
namespace = defaultNamespace
36+
}
37+
return WithNamespace(ctx, namespace)
38+
}
39+
40+
// Namespace returns the namespace from the context
2241
func Namespace(ctx context.Context) (string, bool) {
2342
namespace, ok := ctx.Value(namespaceKey{}).(string)
2443
if !ok {
@@ -28,10 +47,12 @@ func Namespace(ctx context.Context) (string, bool) {
2847
return namespace, ok
2948
}
3049

50+
// IsNamespaceRequired returns whether an error is caused by a missing namespace
3151
func IsNamespaceRequired(err error) bool {
3252
return errors.Cause(err) == errNamespaceRequired
3353
}
3454

55+
// NamespaceRequired returns the namespace or an error
3556
func NamespaceRequired(ctx context.Context) (string, error) {
3657
namespace, ok := Namespace(ctx)
3758
if !ok || namespace == "" {

namespaces/context_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package namespaces
22

33
import (
44
"context"
5+
"os"
56
"testing"
67
)
78

@@ -28,3 +29,31 @@ func TestContext(t *testing.T) {
2829
t.Fatalf("unexpected namespace: %q != %q", namespace, expected)
2930
}
3031
}
32+
33+
func TestNamespaceFromEnv(t *testing.T) {
34+
oldenv := os.Getenv(namespaceEnvVar)
35+
defer os.Setenv(namespaceEnvVar, oldenv) // restore old env var
36+
37+
ctx := context.Background()
38+
namespace, ok := Namespace(ctx)
39+
if ok {
40+
t.Fatal("namespace should not be present")
41+
}
42+
43+
if namespace != "" {
44+
t.Fatalf("namespace should not be defined: got %q", namespace)
45+
}
46+
47+
expected := "test-namespace"
48+
os.Setenv(namespaceEnvVar, expected)
49+
nctx := NamespaceFromEnv(ctx)
50+
51+
namespace, ok = Namespace(nctx)
52+
if !ok {
53+
t.Fatal("expected to find a namespace")
54+
}
55+
56+
if namespace != expected {
57+
t.Fatalf("unexpected namespace: %q != %q", namespace, expected)
58+
}
59+
}

0 commit comments

Comments
 (0)