Skip to content

Commit

Permalink
New schema for params operator file (#1020)
Browse files Browse the repository at this point in the history
Co-authored-by: alenkacz <[email protected]>
  • Loading branch information
kensipe and alenkacz authored Nov 1, 2019
1 parent f7b0902 commit 061b1eb
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 66 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/kudo/v1beta1/operatorversion_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type Parameter struct {
Description string `json:"description,omitempty"`

// Required specifies if the parameter is required to be provided by all instances, or whether a default can suffice.
Required bool `json:"required,omitempty"`
Required *bool `json:"required,omitempty"`

// Default is a default value if no parameter is provided by the instance.
Default *string `json:"default,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/clientset/versioned/clientset.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/kudoctl/cmd/testdata/index.yaml.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v1
entries:
zookeeper:
- appVersion: 3.4.10
digest: 958eeb121727a8df63a37781af4cc9168c812d3f121657fabef9a14e486df106
digest: cbacc2e23ef1accc9308acbeff92f1aa8d2ddf712c76f50c114e817fb1a2a166
maintainers:
- email: [email protected]
name: Alena Varkockova
Expand Down
50 changes: 21 additions & 29 deletions pkg/kudoctl/packages/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/ioutil"
"log"
"regexp"
"strconv"
"strings"

"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1"
Expand Down Expand Up @@ -44,6 +43,10 @@ type PackageFiles struct {
Params []v1beta1.Parameter
}

type ParametersFile struct {
Params []v1beta1.Parameter `json:"parameters"`
}

// Operator is a representation of the KEP-9 Operator YAML
type Operator struct {
APIVersion string `json:"apiVersion,omitempty"`
Expand Down Expand Up @@ -98,44 +101,33 @@ func parsePackageFile(filePath string, fileBytes []byte, currentPackage *Package
name := pathParts[len(pathParts)-1]
currentPackage.Templates[name] = string(fileBytes)
case isParametersFile(filePath):
var params map[string]map[string]string
if err := yaml.Unmarshal(fileBytes, &params); err != nil {
paramsFile, err := readParametersFile(fileBytes)
if err != nil {
return errors.Wrapf(err, "failed to unmarshal parameters file: %s", filePath)
}
paramsStruct := make([]v1beta1.Parameter, 0)
for paramName, param := range params {
required := true // defaults to true
if _, ok := param["required"]; ok {
parsed, err := strconv.ParseBool(param["required"])
if err != nil {
// ideally this should never happen and be already caught by some kind of linter
return errors.Wrapf(err, "failed parsing required field from parameter %s. cannot convert %s to bool", paramName, param["required"])
}

required = parsed
}
var defaultValue *string
if val, ok := param["default"]; ok {
defaultValue = kudo.String(val)
currentPackage.Params = make([]v1beta1.Parameter, 0)
defaultRequired := true
for _, param := range paramsFile.Params {
if param.Required == nil {
// applying default value of required for all params where not specified
param.Required = &defaultRequired
}

r := v1beta1.Parameter{
Name: paramName,
Description: param["description"],
Default: defaultValue,
Trigger: param["trigger"],
Required: required,
DisplayName: param["displayName"],
}
paramsStruct = append(paramsStruct, r)
currentPackage.Params = append(currentPackage.Params, param)
}
currentPackage.Params = paramsStruct
default:
return fmt.Errorf("unexpected file when reading package from filesystem: %s", filePath)
}
return nil
}

func readParametersFile(fileBytes []byte) (ParametersFile, error) {
paramsFile := ParametersFile{}
if err := yaml.Unmarshal(fileBytes, &paramsFile); err != nil {
return paramsFile, err
}
return paramsFile, nil
}

func newPackageFiles() PackageFiles {
return PackageFiles{
Templates: make(map[string]string),
Expand Down
37 changes: 37 additions & 0 deletions pkg/kudoctl/packages/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/go-test/deep"
"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1"
"github.com/pkg/errors"
Expand Down Expand Up @@ -115,3 +117,38 @@ func loadResourcesFromPath(goldenPath string) (*Resources, error) {
}
return result, nil
}

func Test_readParametersFile(t *testing.T) {
noParams := `
parameters:
`
param := `
parameters:
- name: example
`
example := make([]v1beta1.Parameter, 1)
example[0] = v1beta1.Parameter{Name: "example"}

bad := `
parameters:
- oops:
`
tests := []struct {
name string
fileBytes []byte
want ParametersFile
wantErr bool
}{
{"no data", []byte{}, ParametersFile{}, false},
{"no parameters", []byte(noParams), ParametersFile{}, false},
{"parameters", []byte(param), ParametersFile{example}, false},
{"bad data", []byte(bad), ParametersFile{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := readParametersFile(tt.fileBytes)
assert.Equal(t, tt.wantErr, err != nil, "readParametersFile() error = %v, wantErr %v", err, tt.wantErr)
assert.Equal(t, tt.want, got)
})
}
}
Binary file modified pkg/kudoctl/packages/testdata/zk.tgz
Binary file not shown.
13 changes: 7 additions & 6 deletions pkg/kudoctl/packages/testdata/zk/params.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
memory:
description: Amount of memory to provide to Zookeeper pods
default: "1Gi"
cpus:
description: Amount of cpu to provide to Zookeeper pods
default: "0.25"
parameters:
- name: memory
description: Amount of memory to provide to Zookeeper pods
default: "1Gi"
- name: cpus
description: Amount of cpu to provide to Zookeeper pods
default: "0.25"
2 changes: 1 addition & 1 deletion pkg/kudoctl/util/kudo/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func validate(resources *packages.Resources, skipInstance bool) error {
parameters := resources.OperatorVersion.Spec.Parameters
missingParameters := []string{}
for _, p := range parameters {
if p.Required && p.Default == nil {
if *p.Required && p.Default == nil {
_, ok := resources.Instance.Spec.Parameters[p.Name]
if !ok {
missingParameters = append(missingParameters, p.Name)
Expand Down
12 changes: 6 additions & 6 deletions pkg/kudoctl/util/kudo/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ func Test_InstallPackage(t *testing.T) {
},
},
}

tv := true
tests := []struct {
name string
parameters []v1beta1.Parameter
installParameters map[string]string
skipInstance bool
err string
}{
{"all parameters with defaults", []v1beta1.Parameter{{Name: "param", Required: true, Default: util.String("aaa")}}, map[string]string{}, false, ""},
{"missing parameter provided", []v1beta1.Parameter{{Name: "param", Required: true}}, map[string]string{"param": "value"}, false, ""},
{"missing parameter", []v1beta1.Parameter{{Name: "param", Required: true, Default: nil}}, map[string]string{}, false, "missing required parameters during installation: param"},
{"multiple missing parameter", []v1beta1.Parameter{{Name: "param", Required: true}, {Name: "param2", Required: true}}, map[string]string{}, false, "missing required parameters during installation: param,param2"},
{"skip instance ignores missing parameter", []v1beta1.Parameter{{Name: "param", Required: true}}, map[string]string{}, true, ""},
{"all parameters with defaults", []v1beta1.Parameter{{Name: "param", Required: &tv, Default: util.String("aaa")}}, map[string]string{}, false, ""},
{"missing parameter provided", []v1beta1.Parameter{{Name: "param", Required: &tv}}, map[string]string{"param": "value"}, false, ""},
{"missing parameter", []v1beta1.Parameter{{Name: "param", Required: &tv, Default: nil}}, map[string]string{}, false, "missing required parameters during installation: param"},
{"multiple missing parameter", []v1beta1.Parameter{{Name: "param", Required: &tv}, {Name: "param2", Required: &tv}}, map[string]string{}, false, "missing required parameters during installation: param,param2"},
{"skip instance ignores missing parameter", []v1beta1.Parameter{{Name: "param", Required: &tv}}, map[string]string{}, true, ""},
}

for _, tt := range tests {
Expand Down
7 changes: 4 additions & 3 deletions test/e2e/cli-install-uninstall/first-operator/params.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
replicas:
description: Number of replicas that should be run as part of the deployment
default: 2
parameters:
- name: replicas
description: Number of replicas that should be run as part of the deployment
default: 2
13 changes: 7 additions & 6 deletions test/integration/cli-install/cli-install-operator/params.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
memory:
description: Amount of memory to provide to Zookeeper pods
default: "1Gi"
cpus:
description: Amount of cpu to provide to Zookeeper pods
default: "0.25"
parameters:
- name: memory
description: Amount of memory to provide to Zookeeper pods
default: "1Gi"
- name: cpus
description: Amount of cpu to provide to Zookeeper pods
default: "0.25"
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
replicas:
description: Number of replicas that should be run as part of the deployment
default: 2
parameters:
- name: replicas
description: Number of replicas that should be run as part of the deployment
default: 2
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version:
description: Version of image
default: 1.7.9
parameters:
- name: version
description: Version of image
default: 1.7.9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
parameters:
7 changes: 4 additions & 3 deletions test/integration/upgrade-command/first-operator/params.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version:
description: Version of image
default: 1.7.9
parameters:
- name: version
description: Version of image
default: 1.7.9
7 changes: 4 additions & 3 deletions test/integration/upgrade-command/second-operator/params.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version:
description: Version of image
default: 1.7.9
parameters:
- name: version
description: Version of image
default: 1.7.9

0 comments on commit 061b1eb

Please sign in to comment.