Unverified Commit 8884f20b authored by Derek Wilson's avatar Derek Wilson Committed by GitHub
Browse files

enable env set to change server (#612)



adds --server flag to `ks env set` options

related to #345 - also also fixes a couple bugs and adds version to INFO log on init
Signed-off-by: default avatarDerek Wilson <derek@heptio.com>
parent 74f5d14c
......@@ -54,6 +54,7 @@ type EnvSet struct {
envName string
newName string
newNsName string
newServer string
envRenameFn func(a app.App, from, to string, override bool) error
updateEnvFn func(a app.App, envName string, spec *app.EnvironmentSpec, override bool) error
......@@ -68,6 +69,7 @@ func NewEnvSet(m map[string]interface{}) (*EnvSet, error) {
envName: ol.LoadString(OptionEnvName),
newName: ol.LoadOptionalString(OptionNewEnvName),
newNsName: ol.LoadOptionalString(OptionNamespace),
newServer: ol.LoadOptionalString(OptionServer),
envRenameFn: env.Rename,
updateEnvFn: updateEnv,
......@@ -91,7 +93,11 @@ func (es *EnvSet) Run() error {
return err
}
return es.updateNamespace(env)
if err := es.updateEnvSpec(env); err != nil {
return err
}
return nil
}
func (es *EnvSet) updateName(isOverride bool) error {
......@@ -106,13 +112,20 @@ func (es *EnvSet) updateName(isOverride bool) error {
return nil
}
func (es *EnvSet) updateNamespace(env *app.EnvironmentSpec) error {
func (es *EnvSet) updateEnvSpec(env *app.EnvironmentSpec) error {
if es.newNsName == "" && es.newServer == "" {
return nil
}
if es.newNsName != "" {
env.Destination.Namespace = es.newNsName
return updateEnv(es.app, es.envName, env, env.IsOverride())
}
return nil
if es.newServer != "" {
env.Destination.Server = es.newServer
}
return updateEnv(es.app, es.envName, env, env.IsOverride())
}
func updateEnv(a app.App, envName string, spec *app.EnvironmentSpec, override bool) error {
......
......@@ -26,8 +26,8 @@ import (
func TestEnvSet_name(t *testing.T) {
withApp(t, func(appMock *amocks.App) {
envName := "default"
newName := "dev"
envName := "old_env_name"
newName := "new_env_name"
in := map[string]interface{}{
OptionApp: appMock,
......@@ -46,41 +46,74 @@ func TestEnvSet_name(t *testing.T) {
return nil
}
spec := &app.EnvironmentSpec{
Destination: &app.EnvironmentDestinationSpec{},
}
appMock.On("Environment", envName).Return(spec, nil)
err = a.Run()
require.NoError(t, err)
})
}
func TestEnvSet_namespace(t *testing.T) {
withApp(t, func(appMock *amocks.App) {
envName := "env_name"
oldNamespace := "old_namespace"
namespace := "new_name_sapce"
in := map[string]interface{}{
OptionApp: appMock,
OptionEnvName: envName,
OptionNamespace: namespace,
}
a, err := NewEnvSet(in)
require.NoError(t, err)
spec := &app.EnvironmentSpec{
Destination: &app.EnvironmentDestinationSpec{
Namespace: "default",
Namespace: oldNamespace,
},
}
updatedSpec := &app.EnvironmentSpec{
Destination: &app.EnvironmentDestinationSpec{
Namespace: namespace,
},
}
appMock.On("Environment", envName).Return(spec, nil)
appMock.On("AddEnvironment", envName, "", updatedSpec, false).Return(nil)
err = a.Run()
require.NoError(t, err)
})
}
func TestEnvSet_namespace(t *testing.T) {
func TestEnvSet_server(t *testing.T) {
withApp(t, func(appMock *amocks.App) {
envName := "default"
namespace := "ns2"
envName := "env_name"
oldServer := "old_server"
server := "new_server"
in := map[string]interface{}{
OptionApp: appMock,
OptionEnvName: envName,
OptionModule: namespace,
OptionServer: server,
}
a, err := NewEnvSet(in)
require.NoError(t, err)
spec := &app.EnvironmentSpec{
Destination: &app.EnvironmentDestinationSpec{
Namespace: "default",
Server: oldServer,
},
}
updatedSpec := &app.EnvironmentSpec{
Destination: &app.EnvironmentDestinationSpec{
Namespace: namespace,
Server: server,
},
}
......@@ -91,18 +124,21 @@ func TestEnvSet_namespace(t *testing.T) {
require.NoError(t, err)
})
}
func TestEnvSet_name_and_namespace(t *testing.T) {
func TestEnvSet_all(t *testing.T) {
withApp(t, func(appMock *amocks.App) {
envName := "default"
newName := "dev"
namespace := "ns2"
envName := "old_env_name"
newName := "new_env_name"
oldNamespace := "old_namespace"
namespace := "new_name_sapce"
oldServer := "old_server"
server := "new_server"
in := map[string]interface{}{
OptionApp: appMock,
OptionEnvName: envName,
OptionNewEnvName: newName,
OptionModule: namespace,
OptionNamespace: namespace,
OptionServer: server,
}
a, err := NewEnvSet(in)
......@@ -116,27 +152,21 @@ func TestEnvSet_name_and_namespace(t *testing.T) {
return nil
}
a.updateEnvFn = func(a app.App, name string, spec *app.EnvironmentSpec, override bool) error {
assert.Equal(t, envName, name)
assert.Equal(t, namespace, spec.Destination.Namespace)
assert.False(t, override)
return nil
}
spec := &app.EnvironmentSpec{
Destination: &app.EnvironmentDestinationSpec{
Namespace: "default",
Namespace: oldNamespace,
Server: oldServer,
},
}
updatedSpec := &app.EnvironmentSpec{
Destination: &app.EnvironmentDestinationSpec{
Namespace: namespace,
Server: server,
},
}
appMock.On("Environment", "default").Return(spec, nil)
appMock.On("Environment", envName).Return(spec, nil)
appMock.On("AddEnvironment", newName, "", updatedSpec, false).Return(nil)
err = a.Run()
......
......@@ -127,7 +127,7 @@ func updateLibData(fs afero.Fs, k8sSpecFlag, libPath string, useVersionPath bool
return "", err
}
if lm.GenerateLibData(useVersionPath); err != nil {
if err := lm.GenerateLibData(useVersionPath); err != nil {
return "", err
}
......
......@@ -26,6 +26,7 @@ import (
const (
vEnvSetName = "env-set-name"
vEnvSetNamespace = "env-set-namespace"
vEnvSetServer = "env-set-server"
)
var envSetCmd = &cobra.Command{
......@@ -41,6 +42,7 @@ var envSetCmd = &cobra.Command{
actions.OptionEnvName: args[0],
actions.OptionNewEnvName: viper.GetString(vEnvSetName),
actions.OptionNamespace: viper.GetString(vEnvSetNamespace),
actions.OptionServer: viper.GetString(vEnvSetServer),
}
return runAction(actionEnvSet, m)
......@@ -71,6 +73,10 @@ func init() {
viper.BindPFlag(vEnvSetName, envSetCmd.Flags().Lookup(flagName))
envSetCmd.Flags().String(flagNamespace, "",
"Namespace for environment.")
"Namespace for environment")
viper.BindPFlag(vEnvSetNamespace, envSetCmd.Flags().Lookup(flagNamespace))
envSetCmd.Flags().String(flagServer, "",
"Cluster server for environment")
viper.BindPFlag(vEnvSetServer, envSetCmd.Flags().Lookup(flagServer))
}
......@@ -25,13 +25,14 @@ func Test_envSetCmd(t *testing.T) {
cases := []cmdTestCase{
{
name: "in general",
args: []string{"env", "set", "default", "--namespace", "new-name"},
args: []string{"env", "set", "default", "--name", "new-name", "--namespace", "new-namespace", "--server", "new-server"},
action: actionEnvSet,
expected: map[string]interface{}{
actions.OptionApp: ka,
actions.OptionEnvName: "default",
actions.OptionNamespace: "new-name",
actions.OptionNewEnvName: "",
actions.OptionNewEnvName: "new-name",
actions.OptionNamespace: "new-namespace",
actions.OptionServer: "new-server",
},
},
}
......
......@@ -34,6 +34,7 @@ const (
flagJpath = "jpath"
flagModule = "module"
flagNamespace = "namespace"
flagServer = "server"
flagSet = "set"
flagSkipDefaultRegistries = "skip-default-registries"
flagSkipGc = "skip-gc"
......
......@@ -74,8 +74,8 @@ func (c *creator) Create() error {
return fmt.Errorf("environment name %q is not valid; must not contain punctuation, spaces, or begin or end with a slash", c.name)
}
log.Infof("Creating environment %q with namespace %q, pointing to cluster at address %q",
c.name, c.d.Namespace(), c.d.Server())
log.Infof("Creating environment %q with namespace %q, pointing to %q cluster at address %q",
c.name, c.d.Namespace(), c.k8sSpecFlag, c.d.Server())
envPath := filepath.Join(c.app.Root(), app.EnvironmentDirName, c.name)
err := c.app.Fs().MkdirAll(envPath, app.DefaultFolderPermissions)
......
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