namespaces: Export env var and default value
Signed-off-by: Samuel Karp <skarp@amazon.com>
This commit is contained in:
parent
753f1a6c16
commit
9190f98e9c
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/containerd/containerd"
|
"github.com/containerd/containerd"
|
||||||
|
"github.com/containerd/containerd/namespaces"
|
||||||
"github.com/containerd/containerd/version"
|
"github.com/containerd/containerd/version"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
@ -48,8 +49,8 @@ containerd CLI
|
|||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "namespace, n",
|
Name: "namespace, n",
|
||||||
Usage: "namespace to use with commands",
|
Usage: "namespace to use with commands",
|
||||||
Value: "default",
|
Value: namespaces.Default,
|
||||||
EnvVar: "CONTAINERD_NAMESPACE",
|
EnvVar: namespaces.NamespaceEnvVar,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
app.Commands = append([]cli.Command{
|
app.Commands = append([]cli.Command{
|
||||||
|
5
cmd/dist/main.go
vendored
5
cmd/dist/main.go
vendored
@ -6,6 +6,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
|
namespaces2 "github.com/containerd/containerd/namespaces"
|
||||||
"github.com/containerd/containerd/version"
|
"github.com/containerd/containerd/version"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
@ -62,8 +63,8 @@ distribution tool
|
|||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "namespace, n",
|
Name: "namespace, n",
|
||||||
Usage: "namespace to use with commands",
|
Usage: "namespace to use with commands",
|
||||||
Value: "default",
|
Value: namespaces2.Default,
|
||||||
EnvVar: "CONTAINERD_NAMESPACE",
|
EnvVar: namespaces2.NamespaceEnvVar,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
app.Commands = []cli.Command{
|
app.Commands = []cli.Command{
|
||||||
|
@ -8,8 +8,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
namespaceEnvVar = "CONTAINERD_NAMESPACE"
|
NamespaceEnvVar = "CONTAINERD_NAMESPACE"
|
||||||
defaultNamespace = "default"
|
Default = "default"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -30,9 +30,9 @@ func WithNamespace(ctx context.Context, namespace string) context.Context {
|
|||||||
// NamespaceFromEnv uses the namespace defined in CONTAINERD_NAMESPACE or
|
// NamespaceFromEnv uses the namespace defined in CONTAINERD_NAMESPACE or
|
||||||
// default
|
// default
|
||||||
func NamespaceFromEnv(ctx context.Context) context.Context {
|
func NamespaceFromEnv(ctx context.Context) context.Context {
|
||||||
namespace := os.Getenv(namespaceEnvVar)
|
namespace := os.Getenv(NamespaceEnvVar)
|
||||||
if namespace == "" {
|
if namespace == "" {
|
||||||
namespace = defaultNamespace
|
namespace = Default
|
||||||
}
|
}
|
||||||
return WithNamespace(ctx, namespace)
|
return WithNamespace(ctx, namespace)
|
||||||
}
|
}
|
||||||
|
@ -31,8 +31,8 @@ func TestContext(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceFromEnv(t *testing.T) {
|
func TestNamespaceFromEnv(t *testing.T) {
|
||||||
oldenv := os.Getenv(namespaceEnvVar)
|
oldenv := os.Getenv(NamespaceEnvVar)
|
||||||
defer os.Setenv(namespaceEnvVar, oldenv) // restore old env var
|
defer os.Setenv(NamespaceEnvVar, oldenv) // restore old env var
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
namespace, ok := Namespace(ctx)
|
namespace, ok := Namespace(ctx)
|
||||||
@ -45,7 +45,7 @@ func TestNamespaceFromEnv(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
expected := "test-namespace"
|
expected := "test-namespace"
|
||||||
os.Setenv(namespaceEnvVar, expected)
|
os.Setenv(NamespaceEnvVar, expected)
|
||||||
nctx := NamespaceFromEnv(ctx)
|
nctx := NamespaceFromEnv(ctx)
|
||||||
|
|
||||||
namespace, ok = Namespace(nctx)
|
namespace, ok = Namespace(nctx)
|
||||||
|
Loading…
Reference in New Issue
Block a user