namespaces: Add NamespaceFromEnv

Signed-off-by: Samuel Karp <skarp@amazon.com>
This commit is contained in:
Samuel Karp
2017-06-21 17:06:25 -07:00
parent e7a06511aa
commit 879f092925
2 changed files with 50 additions and 0 deletions

View File

@@ -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
@@ -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 {
@@ -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 == "" {