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

Set/unset param globals


Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent bcf0de28
......@@ -26,6 +26,7 @@ import (
type getModuleFn func(ksApp app.App, moduleName string) (component.Module, error)
type deleteEnvFn func(ksApp app.App, envName, componentName, paramName string) error
type deleteEnvGlobalFn func(a app.App, envName, paramName string) error
// RunParamDelete runs `param set`
func RunParamDelete(m map[string]interface{}) error {
......@@ -46,9 +47,10 @@ type ParamDelete struct {
global bool
envName string
deleteEnvFn deleteEnvFn
getModuleFn getModuleFn
resolvePathFn func(a app.App, path string) (component.Module, component.Component, error)
deleteEnvFn deleteEnvFn
deleteEnvGlobalFn deleteEnvGlobalFn
getModuleFn getModuleFn
resolvePathFn func(a app.App, path string) (component.Module, component.Component, error)
}
// NewParamDelete creates an instance of ParamDelete.
......@@ -57,15 +59,16 @@ func NewParamDelete(m map[string]interface{}) (*ParamDelete, error) {
pd := &ParamDelete{
app: ol.loadApp(),
name: ol.loadString(OptionName),
name: ol.loadOptionalString(OptionName),
rawPath: ol.loadString(OptionPath),
global: ol.loadOptionalBool(OptionGlobal),
envName: ol.loadOptionalString(OptionEnvName),
index: ol.loadOptionalInt(OptionIndex),
deleteEnvFn: env.DeleteParam,
resolvePathFn: component.ResolvePath,
getModuleFn: component.GetModule,
deleteEnvFn: env.DeleteParam,
deleteEnvGlobalFn: env.UnsetGlobalParams,
resolvePathFn: component.ResolvePath,
getModuleFn: component.GetModule,
}
if ol.err != nil {
......@@ -82,7 +85,10 @@ func NewParamDelete(m map[string]interface{}) (*ParamDelete, error) {
// Run runs the action.
func (pd *ParamDelete) Run() error {
if pd.envName != "" {
return pd.deleteEnvFn(pd.app, pd.envName, pd.name, pd.rawPath)
if pd.name != "" {
return pd.deleteEnvFn(pd.app, pd.envName, pd.name, pd.rawPath)
}
return pd.deleteEnvGlobalFn(pd.app, pd.envName, pd.rawPath)
}
path := strings.Split(pd.rawPath, ".")
......
......@@ -133,3 +133,28 @@ func TestParamDelete_env(t *testing.T) {
require.NoError(t, err)
})
}
func TestParamDelete_env_global(t *testing.T) {
withApp(t, func(appMock *amocks.App) {
path := "replicas"
in := map[string]interface{}{
OptionApp: appMock,
OptionPath: path,
OptionEnvName: "default",
}
a, err := NewParamDelete(in)
require.NoError(t, err)
envDelete := func(ksApp app.App, envName, pName string) error {
assert.Equal(t, "default", envName)
assert.Equal(t, "replicas", pName)
return nil
}
a.deleteEnvGlobalFn = envDelete
err = a.Run()
require.NoError(t, err)
})
}
......@@ -16,7 +16,6 @@
package actions
import (
"strconv"
"strings"
"github.com/ksonnet/ksonnet/component"
......@@ -47,9 +46,10 @@ type ParamSet struct {
global bool
envName string
getModuleFn getModuleFn
resolvePathFn func(a app.App, path string) (component.Module, component.Component, error)
setEnvFn func(ksApp app.App, envName, name, pName, value string) error
getModuleFn getModuleFn
resolvePathFn func(a app.App, path string) (component.Module, component.Component, error)
setEnvFn func(ksApp app.App, envName, name, pName, value string) error
setGlobalEnvFn func(ksApp app.App, envName, pName, value string) error
}
// NewParamSet creates an instance of ParamSet.
......@@ -58,16 +58,17 @@ func NewParamSet(m map[string]interface{}) (*ParamSet, error) {
ps := &ParamSet{
app: ol.loadApp(),
name: ol.loadString(OptionName),
name: ol.loadOptionalString(OptionName),
rawPath: ol.loadString(OptionPath),
rawValue: ol.loadString(OptionValue),
global: ol.loadOptionalBool(OptionGlobal),
envName: ol.loadOptionalString(OptionEnvName),
index: ol.loadOptionalInt(OptionIndex),
getModuleFn: component.GetModule,
resolvePathFn: component.ResolvePath,
setEnvFn: setEnv,
getModuleFn: component.GetModule,
resolvePathFn: component.ResolvePath,
setEnvFn: setEnv,
setGlobalEnvFn: setGlobalEnv,
}
if ol.err != nil {
......@@ -88,13 +89,11 @@ func (ps *ParamSet) Run() error {
return errors.Wrap(err, "value is invalid")
}
evaluatedValue := ps.rawValue
if _, ok := value.(string); ok {
evaluatedValue = strconv.Quote(ps.rawValue)
}
if ps.envName != "" {
return ps.setEnvFn(ps.app, ps.envName, ps.name, ps.rawPath, evaluatedValue)
if ps.name != "" {
return ps.setEnvFn(ps.app, ps.envName, ps.name, ps.rawPath, ps.rawValue)
}
return ps.setGlobalEnvFn(ps.app, ps.envName, ps.rawPath, ps.rawValue)
}
path := strings.Split(ps.rawPath, ".")
......@@ -147,3 +146,11 @@ func setEnv(ksApp app.App, envName, name, pName, value string) error {
return env.SetParams(envName, name, p, spc)
}
func setGlobalEnv(ksApp app.App, envName, pName, value string) error {
p := mp.Params{
pName: value,
}
return env.SetGlobalParams(ksApp, envName, p)
}
......@@ -142,3 +142,31 @@ func TestParamSet_env(t *testing.T) {
require.NoError(t, err)
})
}
func TestParamSet_envGlobal(t *testing.T) {
withApp(t, func(appMock *amocks.App) {
path := "replicas"
value := "3"
in := map[string]interface{}{
OptionApp: appMock,
OptionPath: path,
OptionValue: value,
OptionEnvName: "default",
}
a, err := NewParamSet(in)
require.NoError(t, err)
envSetter := func(ksApp app.App, envName, pName, value string) error {
assert.Equal(t, "default", envName)
assert.Equal(t, "replicas", pName)
assert.Equal(t, "3", value)
return nil
}
a.setGlobalEnvFn = envSetter
err = a.Run()
require.NoError(t, err)
})
}
......@@ -16,9 +16,8 @@
package cmd
import (
"fmt"
"github.com/ksonnet/ksonnet/actions"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
......@@ -32,14 +31,23 @@ var paramDeleteCmd = &cobra.Command{
Use: "delete <component-name> <param-key>",
Short: paramShortDesc["delete"],
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return fmt.Errorf("'param delete' takes exactly two arguments, (1) the name of the component, and the key")
var name string
var path string
switch len(args) {
default:
return errors.New("invalid arguments for 'param delete'")
case 2:
name = args[0]
path = args[1]
case 1:
path = args[0]
}
m := map[string]interface{}{
actions.OptionApp: ka,
actions.OptionName: args[0],
actions.OptionPath: args[1],
actions.OptionName: name,
actions.OptionPath: path,
actions.OptionEnvName: viper.GetString(vParamDeleteEnv),
actions.OptionIndex: viper.GetInt(vParamDeleteIndex),
}
......
......@@ -16,9 +16,8 @@
package cmd
import (
"fmt"
"github.com/ksonnet/ksonnet/actions"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
......@@ -32,15 +31,27 @@ var paramSetCmd = &cobra.Command{
Use: "set <component-name> <param-key> <param-value>",
Short: paramShortDesc["set"],
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 3 {
return fmt.Errorf("'param set' takes exactly three arguments, (1) the name of the component, in addition to (2) the key and (3) value of the parameter")
var name string
var path string
var value string
switch len(args) {
default:
return errors.New("invalid arguments for 'param set'")
case 3:
name = args[0]
path = args[1]
value = args[2]
case 2:
path = args[0]
value = args[1]
}
m := map[string]interface{}{
actions.OptionApp: ka,
actions.OptionName: args[0],
actions.OptionPath: args[1],
actions.OptionValue: args[2],
actions.OptionName: name,
actions.OptionPath: path,
actions.OptionValue: value,
actions.OptionEnvName: viper.GetString(vParamSetEnv),
actions.OptionIndex: viper.GetInt(vParamSetIndex),
}
......
......@@ -36,6 +36,25 @@ func Test_paramSetCmd(t *testing.T) {
actions.OptionIndex: 0,
},
},
{
name: "set env global",
args: []string{"param", "set", "param-name", "param-value", "--env", "default"},
action: actionParamSet,
expected: map[string]interface{}{
actions.OptionApp: ka,
actions.OptionName: "",
actions.OptionPath: "param-name",
actions.OptionValue: "param-value",
actions.OptionEnvName: "default",
actions.OptionIndex: 0,
},
},
{
name: "invalid arguments",
args: []string{"param", "set"},
action: actionParamSet,
isErr: true,
},
}
runTestCmd(t, cases)
......
......@@ -18,6 +18,7 @@ package component
import (
"testing"
"github.com/ksonnet/ksonnet/metadata/app"
"github.com/ksonnet/ksonnet/metadata/app/mocks"
"github.com/ksonnet/ksonnet/pkg/util/test"
"github.com/spf13/afero"
......@@ -80,10 +81,20 @@ func TestJsonnet_Name(t *testing.T) {
func TestJsonnet_Objects(t *testing.T) {
withAppOsFs(t, "/app", func(a *mocks.App, fs afero.Fs) {
env := &app.EnvironmentSpec{
Destination: &app.EnvironmentDestinationSpec{
Namespace: "default",
Server: "http://example.com",
},
}
a.On("Environment", "default").Return(env, nil)
files := []string{"guestbook-ui.jsonnet", "params.libsonnet"}
for _, file := range files {
test.StageFile(t, fs, "guestbook/"+file, "/app/components/"+file)
}
fs.Mkdir("/app/vendor", 0755)
files = []string{"k.libsonnet", "k8s.libsonnet"}
for _, file := range files {
......
......@@ -17,7 +17,7 @@ The `delete` command deletes component or environment parameters.
```
ks param delete <component-name> <param-key> [flags]
ks param delete [component-name] <param-key> [flags]
```
### Examples
......
......@@ -24,6 +24,84 @@ import (
"github.com/spf13/afero"
)
// SetGlobalParams sets global params for an environment.
func SetGlobalParams(a app.App, envName string, p param.Params) error {
if err := ensureEnvExists(a, envName); err != nil {
return err
}
path := envPath(a, envName, globalsFileName)
exists, err := afero.Exists(a.Fs(), path)
if err != nil {
return err
}
if !exists {
if err = afero.WriteFile(a.Fs(), path, []byte("{\n}"), app.DefaultFilePermissions); err != nil {
return err
}
}
text, err := afero.ReadFile(a.Fs(), path)
if err != nil {
return err
}
egs := params.NewEnvGlobalsSet()
updated, err := egs.Set(string(text), p)
if err != nil {
return err
}
err = afero.WriteFile(a.Fs(), path, []byte(updated), app.DefaultFilePermissions)
if err != nil {
return err
}
log.WithField("environment-name", envName).
Debug("Set global parameters")
return nil
}
// UnsetGlobalParams un-sets global param for an environment.
func UnsetGlobalParams(a app.App, envName, paramName string) error {
if err := ensureEnvExists(a, envName); err != nil {
return err
}
path := envPath(a, envName, globalsFileName)
exists, err := afero.Exists(a.Fs(), path)
if err != nil {
return err
}
if !exists {
return nil
}
text, err := afero.ReadFile(a.Fs(), path)
if err != nil {
return err
}
egu := params.NewEnvGlobalsUnset()
updated, err := egu.Unset(paramName, string(text))
if err != nil {
return err
}
err = afero.WriteFile(a.Fs(), path, []byte(updated), app.DefaultFilePermissions)
if err != nil {
return err
}
log.WithField("environment-name", envName).
Debug("Set global parameters")
return nil
}
// SetParamsConfig is config items for setting environment params.
type SetParamsConfig struct {
App app.App
......
......@@ -25,6 +25,28 @@ import (
"github.com/stretchr/testify/require"
)
func TestSetGlobalParams(t *testing.T) {
withEnv(t, func(appMock *mocks.App, fs afero.Fs) {
p := params.Params{
"foo": "bar",
}
err := SetGlobalParams(appMock, "env1", p)
require.NoError(t, err)
compareOutput(t, fs, "add-global.libsonnet", "/environments/env1/globals.libsonnet")
})
}
func TestUnsetGlobalParams(t *testing.T) {
withEnv(t, func(appMock *mocks.App, fs afero.Fs) {
err := UnsetGlobalParams(appMock, "env1", "foo")
require.NoError(t, err)
compareOutput(t, fs, "remove-global.libsonnet", "/environments/env1/globals.libsonnet")
})
}
func TestSetParams(t *testing.T) {
withEnv(t, func(appMock *mocks.App, fs afero.Fs) {
config := SetParamsConfig{
......
{
foo: "bar",
}
\ No newline at end of file
{
foo: "bar",
}
\ No newline at end of file
{
}
\ No newline at end of file
......@@ -66,6 +66,9 @@ func withEnv(t *testing.T, fn func(*mocks.App, afero.Fs)) {
paramsPath := filepath.Join(path, "params.libsonnet")
stageFile(t, fs, "params.libsonnet", paramsPath)
globalsPath := filepath.Join(path, "globals.libsonnet")
stageFile(t, fs, "globals.libsonnet", globalsPath)
}
componentParamsPath := filepath.Join("/", "components", "params.libsonnet")
......
// Copyright 2018 The ksonnet authors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package params
import (
"bytes"
"github.com/ksonnet/ksonnet-lib/ksonnet-gen/astext"
nm "github.com/ksonnet/ksonnet-lib/ksonnet-gen/nodemaker"
"github.com/ksonnet/ksonnet/metadata/params"
"github.com/ksonnet/ksonnet/pkg/util/jsonnet"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// EnvGlobalsSet sets environment globals.
type EnvGlobalsSet struct {
}
// NewEnvGlobalsSet creates an instance of EnvGlobalsSet.
func NewEnvGlobalsSet() *EnvGlobalsSet {
egs := &EnvGlobalsSet{}
return egs
}
// Set sets params in environment globals files.
func (egs *EnvGlobalsSet) Set(snippet string, p params.Params) (string, error) {
logger := logrus.WithFields(logrus.Fields{})
logger.Info("setting environment globals")
n, err := jsonnet.ParseNode("globals.libsonnet", snippet)
if err != nil {
return "", err
}
obj, err := componentParams(n, "")
if err != nil {
return "", err
}
if err = egs.setParams(obj, p); err != nil {
return "", errors.Wrap(err, "delete entry")
}
// root node should be a local if not, return an error
var buf bytes.Buffer
if err = jsonnetPrinterFn(&buf, n); err != nil {
return "", errors.Wrap(err, "unable to update snippet")
}
return buf.String(), nil
}
func (egs *EnvGlobalsSet) setParams(obj *astext.Object, p params.Params) error {
for key := range p {
decoded, err := DecodeValue(p[key])
if err != nil {
return err
}
value, err := nm.ValueToNoder(decoded)
if err != nil {
return err
}
path := []string{key}
if err = jsonnetSetFn(obj, path, value.Node()); err != nil {
return err
}
}
return nil
}
// Copyright 2018 The ksonnet authors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package params
import (
"path/filepath"
"testing"
"github.com/ksonnet/ksonnet/metadata/params"
"github.com/ksonnet/ksonnet/pkg/util/test"
"github.com/stretchr/testify/require"
)
func TestEnvGlobalsSet(t *testing.T) {
cases := []struct {
name string
input string
output string
params params.Params
}{
{
name: "in general",
input: filepath.Join("env", "globals", "set-global", "in.libsonnet"),
output: filepath.Join("env", "globals", "set-global", "out.libsonnet"),
params: params.Params{
"group": "dev",
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
snippet := test.ReadTestData(t, tc.input)