Unverified Commit f59c3f12 authored by bryanl's avatar bryanl
Browse files

bug: fix updating env namespace


Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent e3806408
...@@ -61,6 +61,9 @@ func init() { ...@@ -61,6 +61,9 @@ func init() {
envSetCmd.PersistentFlags().String(flagEnvName, "", envSetCmd.PersistentFlags().String(flagEnvName, "",
"Name used to uniquely identify the environment. Must not already exist within the ksonnet app") "Name used to uniquely identify the environment. Must not already exist within the ksonnet app")
envSetCmd.PersistentFlags().String(flagEnvNamespace, "",
"Name used to uniquely identify the environment. Must not already exist within the ksonnet app")
} }
var envCmd = &cobra.Command{ var envCmd = &cobra.Command{
...@@ -317,7 +320,17 @@ var envSetCmd = &cobra.Command{ ...@@ -317,7 +320,17 @@ var envSetCmd = &cobra.Command{
return err return err
} }
c, err := kubecfg.NewEnvSetCmd(originalName, name, manager) namespace, err := flags.GetString(flagEnvNamespace)
if err != nil {
return err
}
server, err := flags.GetString(flagEnvServer)
if err != nil {
return err
}
c, err := kubecfg.NewEnvSetCmd(originalName, name, server, namespace, manager)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -20,6 +20,7 @@ import ( ...@@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"github.com/ksonnet/ksonnet/env" "github.com/ksonnet/ksonnet/env"
"github.com/ksonnet/ksonnet/metadata/app"
"github.com/ksonnet/ksonnet/metadata/lib" "github.com/ksonnet/ksonnet/metadata/lib"
str "github.com/ksonnet/ksonnet/strings" str "github.com/ksonnet/ksonnet/strings"
...@@ -41,6 +42,13 @@ var ( ...@@ -41,6 +42,13 @@ var (
envCreate = env.Create envCreate = env.Create
) )
// Environment represents all fields of a ksonnet environment.
type Environment struct {
Name string
Namespace string
Server string
}
func (m *manager) CreateEnvironment(name, server, namespace, k8sSpecFlag string) error { func (m *manager) CreateEnvironment(name, server, namespace, k8sSpecFlag string) error {
a, err := m.App() a, err := m.App()
if err != nil { if err != nil {
...@@ -97,19 +105,52 @@ func (m *manager) GetEnvironment(name string) (*env.Env, error) { ...@@ -97,19 +105,52 @@ func (m *manager) GetEnvironment(name string) (*env.Env, error) {
return env.Retrieve(a, name) return env.Retrieve(a, name)
} }
func (m *manager) SetEnvironment(from, to string) error { func (m *manager) SetEnvironment(from string, desired Environment) error {
a, err := m.App() a, err := m.App()
if err != nil { if err != nil {
return err return err
} }
config := env.RenameConfig{ e, err := m.GetEnvironment(from)
App: a, if err != nil {
AppRoot: m.rootPath, return nil
Fs: m.appFS, }
e.Name = from
if desired.Name != "" && desired.Name != e.Name {
config := env.RenameConfig{
App: a,
AppRoot: m.rootPath,
Fs: m.appFS,
}
if err = env.Rename(from, desired.Name, config); err != nil {
return err
}
e.Name = desired.Name
}
if desired.Namespace != "" && desired.Namespace != e.Destination.Namespace() {
spec := &app.EnvironmentSpec{
Name: e.Name,
Destination: &app.EnvironmentDestinationSpec{
Namespace: desired.Namespace,
Server: e.Destination.Server(),
},
KubernetesVersion: e.KubernetesVersion,
Path: e.Name,
}
flagSpec := fmt.Sprintf("version:%s", spec.KubernetesVersion)
if err := a.AddEnvironment(e.Name, flagSpec, spec); err != nil {
return err
}
} }
return env.Rename(from, to, config) return nil
} }
func (m *manager) GetEnvironmentParams(name, nsName string) (map[string]param.Params, error) { func (m *manager) GetEnvironmentParams(name, nsName string) (map[string]param.Params, error) {
......
...@@ -186,20 +186,22 @@ func TestSetEnvironment(t *testing.T) { ...@@ -186,20 +186,22 @@ func TestSetEnvironment(t *testing.T) {
setName := "new-env" setName := "new-env"
// Test updating an environment that doesn't exist desired := Environment{
err := m.SetEnvironment("notexists", setName) Name: mockEnvName2,
if err == nil {
t.Fatal("Expected error when setting an environment that does not exist")
} }
// Test updating an environment to an environment that already exists // Test updating an environment to an environment that already exists
err = m.SetEnvironment(mockEnvName, mockEnvName2) err := m.SetEnvironment(mockEnvName, desired)
if err == nil { if err == nil {
t.Fatalf("Expected error when setting \"%s\" to \"%s\", because env already exists", mockEnvName, mockEnvName2) t.Fatalf("Expected error when setting \"%s\" to \"%s\", because env already exists", mockEnvName, mockEnvName2)
} }
desired = Environment{
Name: setName,
}
// Test changing the name an existing environment. // Test changing the name an existing environment.
err = m.SetEnvironment(mockEnvName, setName) err = m.SetEnvironment(mockEnvName, desired)
if err != nil { if err != nil {
t.Fatalf("Could not set \"%s\", got:\n %s", mockEnvName, err) t.Fatalf("Could not set \"%s\", got:\n %s", mockEnvName, err)
} }
...@@ -247,7 +249,8 @@ func TestSetEnvironment(t *testing.T) { ...@@ -247,7 +249,8 @@ func TestSetEnvironment(t *testing.T) {
for _, v := range tests { for _, v := range tests {
m = mockEnvironmentsWith(t, fs, v.appName, []string{v.nameOld}) m = mockEnvironmentsWith(t, fs, v.appName, []string{v.nameOld})
err = m.SetEnvironment(v.nameOld, v.nameNew) desired := Environment{Name: v.nameNew}
err = m.SetEnvironment(v.nameOld, desired)
if err != nil { if err != nil {
t.Fatalf("Could not set '%s', got:\n %s", v.nameOld, err) t.Fatalf("Could not set '%s', got:\n %s", v.nameOld, err)
} }
......
...@@ -63,7 +63,7 @@ type Manager interface { ...@@ -63,7 +63,7 @@ type Manager interface {
DeleteEnvironment(name string) error DeleteEnvironment(name string) error
GetEnvironments() (map[string]env.Env, error) GetEnvironments() (map[string]env.Env, error)
GetEnvironment(name string) (*env.Env, error) GetEnvironment(name string) (*env.Env, error)
SetEnvironment(name, desiredName string) error SetEnvironment(name string, desired Environment) error
GetDestination(envName string) (env.Destination, error) GetDestination(envName string) (env.Destination, error)
// Dependency/registry API. // Dependency/registry API.
......
...@@ -121,14 +121,26 @@ func (c *EnvListCmd) Run(out io.Writer) error { ...@@ -121,14 +121,26 @@ func (c *EnvListCmd) Run(out io.Writer) error {
type EnvSetCmd struct { type EnvSetCmd struct {
name string name string
desiredName string desiredName string
server string
namespace string
manager metadata.Manager manager metadata.Manager
} }
func NewEnvSetCmd(name, desiredName string, manager metadata.Manager) (*EnvSetCmd, error) { func NewEnvSetCmd(name, desiredName, server, namespace string, manager metadata.Manager) (*EnvSetCmd, error) {
return &EnvSetCmd{name: name, desiredName: desiredName, manager: manager}, nil return &EnvSetCmd{
name: name,
desiredName: desiredName,
server: server,
namespace: namespace,
manager: manager,
}, nil
} }
func (c *EnvSetCmd) Run() error { func (c *EnvSetCmd) Run() error {
return c.manager.SetEnvironment(c.name, c.desiredName) e := metadata.Environment{
Name: c.desiredName,
Namespace: c.namespace,
}
return c.manager.SetEnvironment(c.name, e)
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment