Skip to content

Commit

Permalink
Merge pull request containerd#1056 from samuelkarp/namespace-from-env
Browse files Browse the repository at this point in the history
namespaces: Add NamespaceFromEnv
  • Loading branch information
estesp authored Jun 22, 2017
2 parents ab8311b + 879f092 commit 753f1a6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
21 changes: 21 additions & 0 deletions namespaces/context.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package namespaces

import (
"os"

"github.com/pkg/errors"
"golang.org/x/net/context"
)

const (
namespaceEnvVar = "CONTAINERD_NAMESPACE"
defaultNamespace = "default"
)

var (
errNamespaceRequired = errors.New("namespace is required")
)

type namespaceKey struct{}

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

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

// NamespaceFromEnv uses the namespace defined in CONTAINERD_NAMESPACE or
// default
func NamespaceFromEnv(ctx context.Context) context.Context {
namespace := os.Getenv(namespaceEnvVar)
if namespace == "" {
namespace = defaultNamespace
}
return WithNamespace(ctx, namespace)
}

// Namespace returns the namespace from the context
func Namespace(ctx context.Context) (string, bool) {
namespace, ok := ctx.Value(namespaceKey{}).(string)
if !ok {
Expand All @@ -28,10 +47,12 @@ func Namespace(ctx context.Context) (string, bool) {
return namespace, ok
}

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

// NamespaceRequired returns the namespace or an error
func NamespaceRequired(ctx context.Context) (string, error) {
namespace, ok := Namespace(ctx)
if !ok || namespace == "" {
Expand Down
29 changes: 29 additions & 0 deletions namespaces/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package namespaces

import (
"context"
"os"
"testing"
)

Expand All @@ -28,3 +29,31 @@ func TestContext(t *testing.T) {
t.Fatalf("unexpected namespace: %q != %q", namespace, expected)
}
}

func TestNamespaceFromEnv(t *testing.T) {
oldenv := os.Getenv(namespaceEnvVar)
defer os.Setenv(namespaceEnvVar, oldenv) // restore old env var

ctx := context.Background()
namespace, ok := Namespace(ctx)
if ok {
t.Fatal("namespace should not be present")
}

if namespace != "" {
t.Fatalf("namespace should not be defined: got %q", namespace)
}

expected := "test-namespace"
os.Setenv(namespaceEnvVar, expected)
nctx := NamespaceFromEnv(ctx)

namespace, ok = Namespace(nctx)
if !ok {
t.Fatal("expected to find a namespace")
}

if namespace != expected {
t.Fatalf("unexpected namespace: %q != %q", namespace, expected)
}
}

0 comments on commit 753f1a6

Please sign in to comment.