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() {
envSetCmd.PersistentFlags().String(flagEnvName, "",
"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{
......@@ -317,7 +320,17 @@ var envSetCmd = &cobra.Command{
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 {
return err
}
......
......@@ -20,6 +20,7 @@ import (
"fmt"
"github.com/ksonnet/ksonnet/env"
"github.com/ksonnet/ksonnet/metadata/app"
"github.com/ksonnet/ksonnet/metadata/lib"
str "github.com/ksonnet/ksonnet/strings"
......@@ -41,6 +42,13 @@ var (
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 {
a, err := m.App()
if err != nil {
......@@ -97,19 +105,52 @@ func (m *manager) GetEnvironment(name string) (*env.Env, error) {
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()
if err != nil {
return err
}
e, err := m.GetEnvironment(from)
if err != nil {
return nil
}
e.Name = from
if desired.Name != "" && desired.Name != e.Name {
config := env.RenameConfig{
App: a,
AppRoot: m.rootPath,
Fs: m.appFS,
}
return env.Rename(from, to, config)
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 nil
}
func (m *manager) GetEnvironmentParams(name, nsName string) (map[string]param.Params, error) {
......
......@@ -186,20 +186,22 @@ func TestSetEnvironment(t *testing.T) {
setName := "new-env"
// Test updating an environment that doesn't exist
err := m.SetEnvironment("notexists", setName)
if err == nil {
t.Fatal("Expected error when setting an environment that does not exist")
desired := Environment{
Name: mockEnvName2,
}
// Test updating an environment to an environment that already exists
err = m.SetEnvironment(mockEnvName, mockEnvName2)
err := m.SetEnvironment(mockEnvName, desired)
if err == nil {
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.
err = m.SetEnvironment(mockEnvName, setName)
err = m.SetEnvironment(mockEnvName, desired)
if err != nil {
t.Fatalf("Could not set \"%s\", got:\n %s", mockEnvName, err)
}
......@@ -247,7 +249,8 @@ func TestSetEnvironment(t *testing.T) {
for _, v := range tests {
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 {
t.Fatalf("Could not set '%s', got:\n %s", v.nameOld, err)
}
......
......@@ -63,7 +63,7 @@ type Manager interface {
DeleteEnvironment(name string) error
GetEnvironments() (map[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)
// Dependency/registry API.
......
......@@ -121,14 +121,26 @@ func (c *EnvListCmd) Run(out io.Writer) error {
type EnvSetCmd struct {
name string
desiredName string
server string
namespace string
manager metadata.Manager
}
func NewEnvSetCmd(name, desiredName string, manager metadata.Manager) (*EnvSetCmd, error) {
return &EnvSetCmd{name: name, desiredName: desiredName, manager: manager}, nil
func NewEnvSetCmd(name, desiredName, server, namespace string, manager metadata.Manager) (*EnvSetCmd, error) {
return &EnvSetCmd{
name: name,
desiredName: desiredName,
server: server,
namespace: namespace,
manager: manager,
}, nil
}
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