Unverified Commit 1ae91b58 authored by bryanl's avatar bryanl
Browse files

Rework env parameters to work with global format


Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent dc80ac49
......@@ -20,6 +20,7 @@ import (
"github.com/ksonnet/ksonnet/metadata/app"
param "github.com/ksonnet/ksonnet/metadata/params"
"github.com/ksonnet/ksonnet/pkg/params"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
......@@ -104,7 +105,9 @@ func collectEnvParams(a app.App, env *app.EnvironmentSpec, componentName, envNam
if err != nil {
return "", err
}
return param.DeleteEnvironmentComponent(componentName, string(envParamsFile))
ecr := params.NewEnvComponentRemover()
return ecr.Remove(componentName, string(envParamsFile))
}
/// updateEnvParam removes the component references in each environment's
......
......@@ -146,8 +146,6 @@ func (j *Jsonnet) Objects(paramsStr, envName string) ([]*unstructured.Unstructur
return nil, err
}
fmt.Println("lib path is", libPath)
vm := jsonnet.NewVM()
if j.useJsonnetMemoryImporter {
vm.Fs = j.app.Fs()
......@@ -204,7 +202,7 @@ func (j *Jsonnet) SetParam(path []string, value interface{}, options ParamOption
return err
}
updatedParams, err := params.Set(path, paramsData, j.Name(false), value, paramsComponentRoot)
updatedParams, err := params.SetInObject(path, paramsData, j.Name(false), value, paramsComponentRoot)
if err != nil {
return err
}
......@@ -224,7 +222,7 @@ func (j *Jsonnet) DeleteParam(path []string, options ParamOptions) error {
return err
}
updatedParams, err := params.Delete(path, paramsData, j.Name(false), paramsComponentRoot)
updatedParams, err := params.DeleteFromObject(path, paramsData, j.Name(false), paramsComponentRoot)
if err != nil {
return err
}
......
......@@ -108,7 +108,7 @@ func (m *FilesystemModule) SetParam(path []string, value interface{}) error {
return err
}
updated, err := params.Set(path, paramsData, "", value, "global")
updated, err := params.SetInObject(path, paramsData, "", value, "global")
if err != nil {
return err
}
......@@ -123,7 +123,7 @@ func (m *FilesystemModule) DeleteParam(path []string) error {
return err
}
updated, err := params.Delete(path, paramsData, "", "global")
updated, err := params.DeleteFromObject(path, paramsData, "", "global")
if err != nil {
return err
}
......
local params = import "../../components/params.libsonnet";
params {
components +: {
components+: {
},
}
}
\ No newline at end of file
......@@ -305,7 +305,7 @@ func (y *YAML) SetParam(path []string, value interface{}, options ParamOptions)
return err
}
updatedParams, err := params.Set(path, paramsData, entry, value, paramsComponentRoot)
updatedParams, err := params.SetInObject(path, paramsData, entry, value, paramsComponentRoot)
if err != nil {
return err
}
......@@ -326,7 +326,7 @@ func (y *YAML) DeleteParam(path []string, options ParamOptions) error {
return err
}
updatedParams, err := params.Delete(path, paramsData, entry, paramsComponentRoot)
updatedParams, err := params.DeleteFromObject(path, paramsData, entry, paramsComponentRoot)
if err != nil {
return err
}
......
......@@ -69,8 +69,8 @@ var _ = Describe("ks param", func() {
assertExitStatus(o, 0)
})
XIt("removes a parameter's environment value", func() {
o := a.paramList("--env=" + envName)
It("removes a parameter's environment value", func() {
o := a.paramList("--env", envName)
assertOutput("param/delete/env.txt", o.stdout)
})
})
......
Updating services guestbook-ui
Creating non-existent services guestbook-ui
Updating deployments guestbook-ui
Creating non-existent deployments guestbook-ui
level=info msg="Updating services guestbook-ui"
level=info msg=" Creating non-existent services guestbook-ui"
level=info msg="Updating deployments guestbook-ui"
level=info msg=" Creating non-existent deployments guestbook-ui"
Deleting services guestbook-ui
Deleting deployments guestbook-ui
level=info msg="Deleting services guestbook-ui"
level=info msg="Deleting deployments guestbook-ui"
environment "invalid" was not found
level=error msg="environment \"invalid\" was not found"
unable to find module "bad"
level=error msg="unable to find module \"bad\""
module "/" already exists
level=error msg="module \"/\" already exists"
environment "invalid" was not found
level=error msg="environment \"invalid\" was not found"
......@@ -171,16 +171,3 @@ func deleteFromEnv(l *ast.Local, components *astext.Object, componentName, param
return buf.String(), nil
}
// DeleteEnvironmentComponent takes
//
// component: the name of the component to be deleted.
// snippet: a jsonnet snippet resembling the current environment parameters (not expanded).
//
// and returns the jsonnet snippet with the removed component.
func DeleteEnvironmentComponent(component, snippet string) (string, error) {
// The implementation happens to be the same as DeleteComponent, but we're
// keeping the two interfaces separate since we're fundamentally operating
// on two different jsonnet schemas.
return deleteComponent(component, snippet)
}
......@@ -18,11 +18,13 @@ package params
import (
"bytes"
"fmt"
"runtime/debug"
"sort"
"strconv"
"strings"
str "github.com/ksonnet/ksonnet/strings"
"github.com/pkg/errors"
"github.com/google/go-jsonnet/ast"
"github.com/google/go-jsonnet/parser"
......@@ -35,12 +37,12 @@ const (
func componentsObj(component, snippet string) (*ast.Object, error) {
tokens, err := parser.Lex(component, snippet)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "lex node")
}
root, err := parser.Parse(tokens)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "parse node")
}
return findComponentsObj(root)
......@@ -57,7 +59,8 @@ func findComponentsObj(node ast.Node) (*ast.Object, error) {
if *f.Id == componentsID {
c, isObj := f.Expr2.(*ast.Object)
if !isObj {
return nil, fmt.Errorf("Expected components node type to be object")
debug.PrintStack()
return nil, errors.Errorf("expected components node type to be object, it was a a %T", f.Expr2)
}
return c, nil
}
......@@ -199,7 +202,7 @@ func writeParams(indent int, params Params) string {
func deleteComponent(component, snippet string) (string, error) {
componentsNode, err := componentsObj(component, snippet)
if err != nil {
return "", err
return "", errors.Wrap(err, "retrieve component node")
}
for _, field := range componentsNode.Fields {
......
......@@ -331,125 +331,6 @@ func TestDeleteComponent(t *testing.T) {
}
}
func TestDeleteEnvironmentComponent(t *testing.T) {
tests := []struct {
componentName string
jsonnet string
expected string
}{
// Test case with existing component
{
"bar",
`
local params = import "/fake/path";
params + {
components +: {
bar +: {
name: "bar",
"replica-count": 1,
},
foo +: {
name: "foo",
},
},
}`,
`
local params = import "/fake/path";
params + {
components +: {
foo +: {
name: "foo",
},
},
}`,
},
// Test another case with existing component
{
"foo",
`
local params = import "/fake/path";
params + {
components +: {
bar +: {
name: "bar",
"replica-count": 1,
},
foo +: {
name: "foo",
},
},
}`,
`
local params = import "/fake/path";
params + {
components +: {
bar +: {
name: "bar",
"replica-count": 1,
},
},
}`,
},
// Test case where component doesn't exist
{
"baz",
`
local params = import "/fake/path";
params + {
components +: {
bar +: {
name: "bar",
"replica-count": 1,
},
foo +: {
name: "foo",
},
},
}`,
`
local params = import "/fake/path";
params + {
components +: {
bar +: {
name: "bar",
"replica-count": 1,
},
foo +: {
name: "foo",
},
},
}`,
},
// Test case where there are no components
{
"baz",
`
local params = import "/fake/path";
params + {
components +: {
},
}`,
`
local params = import "/fake/path";
params + {
components +: {
},
}`,
},
}
for _, s := range tests {
parsed, err := DeleteEnvironmentComponent(s.componentName, s.jsonnet)
if err != nil {
t.Errorf("Unexpected error\n input: %v\n error: %v", s.jsonnet, err)
}
if parsed != s.expected {
t.Errorf("Wrong conversion\n expected: %v\n got: %v", s.expected, parsed)
}
}
}
func TestGetComponentParams(t *testing.T) {
tests := []struct {
componentName string
......
......@@ -115,6 +115,7 @@ func checkApp(t *testing.T, fs afero.Fs, rootPath, version, namespace string) {
filepath.Join("environments", "base.libsonnet"),
filepath.Join("environments", namespace, "main.jsonnet"),
filepath.Join("environments", namespace, "params.libsonnet"),
filepath.Join("environments", namespace, "globals.libsonnet"),
}
for _, d := range expectedDirs {
......
......@@ -97,6 +97,11 @@ func (c *creator) Create() error {
filepath.Join(envPath, paramsFileName),
c.paramsData,
},
{
// globals file
filepath.Join(envPath, globalsFileName),
DefaultGlobalsData,
},
}
for _, a := range metadata {
......
......@@ -34,8 +34,8 @@ base + {
// DefaultParamsData generates the contents for an environment's `params.libsonnet`
var DefaultParamsData = []byte(`local params = std.extVar("__ksonnet/params");
params + {
local globals = import "globals.libsonnet";
local envParams = params + {
components +: {
// Insert component parameter overrides here. Ex:
// guestbook +: {
......@@ -43,6 +43,12 @@ params + {
// replicas: params.global.replicas,
// },
},
};
{
components: {
[x]: envParams.components[x] + globals, for x in std.objectFields(envParams.components)
},
}
`)
......@@ -52,3 +58,7 @@ components + {
// Insert user-specified overrides here.
}
`)
// DefaultGlobalsData generates the contents for an environment's `globals.libsonnet`
var DefaultGlobalsData = []byte(`{
}`)
......@@ -17,8 +17,9 @@ package env
const (
// primary environment files.
envFileName = "main.jsonnet"
paramsFileName = "params.libsonnet"
envFileName = "main.jsonnet"
paramsFileName = "params.libsonnet"
globalsFileName = "globals.libsonnet"
// envRoot is the name for the environment root.
envRoot = "environments"
......
......@@ -19,6 +19,7 @@ import (
"github.com/ksonnet/ksonnet/component"
"github.com/ksonnet/ksonnet/metadata/app"
param "github.com/ksonnet/ksonnet/metadata/params"
"github.com/ksonnet/ksonnet/pkg/params"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
)
......@@ -29,7 +30,7 @@ type SetParamsConfig struct {
}
// SetParams sets params for an environment.
func SetParams(envName, component string, params param.Params, config SetParamsConfig) error {
func SetParams(envName, component string, p param.Params, config SetParamsConfig) error {
if err := ensureEnvExists(config.App, envName); err != nil {
return err
}
......@@ -41,12 +42,13 @@ func SetParams(envName, component string, params param.Params, config SetParamsC
return err
}
appended, err := param.SetEnvironmentParams(component, string(text), params)
eps := params.NewEnvParamSet()
updated, err := eps.Set(component, string(text), p)
if err != nil {
return err
}
err = afero.WriteFile(config.App.Fs(), path, []byte(appended), app.DefaultFilePermissions)
err = afero.WriteFile(config.App.Fs(), path, []byte(updated), app.DefaultFilePermissions)
if err != nil {
return err
}
......@@ -56,7 +58,7 @@ func SetParams(envName, component string, params param.Params, config SetParamsC
}
// DeleteParam deletes a param in an environment.
func DeleteParam(a app.App, envName, component, name string) error {
func DeleteParam(a app.App, envName, componentName, paramName string) error {
if err := ensureEnvExists(a, envName); err != nil {
return err
}
......@@ -68,7 +70,8 @@ func DeleteParam(a app.App, envName, component, name string) error {
return err
}
updated, err := param.DeleteEnvironmentParam(component, name, string(text))
epu := params.NewEnvParamUnset()
updated, err := epu.Unset(componentName, paramName, string(text))
if err != nil {
return err
}
......@@ -79,7 +82,7 @@ func DeleteParam(a app.App, envName, component, name string) error {
}
log.Debugf("deleted parameter %q for component %q at environment %q",
name, component, envName)
paramName, componentName, envName)
return nil
}
......
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