Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve long parameter handling #1784

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pkg/apis/kudo/v1beta1/parameter_types_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ func ValidateParameterValueForType(pType ParameterType, pValue interface{}) erro
return nil
}

func ValidateParameterType(pType ParameterType) error {
switch pType {
case "", // An empty parameter type defaults to "StringValue"
StringValueType,
IntegerValueType,
NumberValueType,
BooleanValueType,
ArrayValueType,
MapValueType:
return nil
default:
return fmt.Errorf("invalid type %s", pType)
}
}

func validateIntegerType(pValue interface{}) error {
switch v := pValue.(type) {
case int, int8, int16, int32, int64:
Expand All @@ -96,6 +111,9 @@ func validateIntegerType(pValue interface{}) error {
if err != nil {
return fmt.Errorf("type is %q but format of %q is invalid: %v", IntegerValueType, pValue, err)
}
case float32, float64:
// This happens when a user defines a big number without quotes. The YAML parser then reads the value as a float...
return fmt.Errorf("type is %q but format of %s is invalid. Try using quotes around the number", IntegerValueType, pValue)
default:
return fmt.Errorf("type is %q but format of %s is invalid", IntegerValueType, pValue)
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/apis/kudo/v1beta1/parameter_types_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ func TestValidateType(t *testing.T) {
pValue: "42",
pType: IntegerValueType,
},
{
name: "longAsString",
pValue: "432000000",
pType: IntegerValueType,
},
{
name: "longAsUnquotedNumber",
pValue: float64(432000000),
pType: IntegerValueType,
expectedErr: true,
},
{
name: "float32",
pValue: float32(3.14),
Expand Down
8 changes: 8 additions & 0 deletions pkg/engine/renderer/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ func TestRender(t *testing.T) {
},
expected: " Baz: Quux\n Foo: Bar",
},
{
name: "longRender",
template: "{{ .Params.LongParam }}",
params: map[string]interface{}{
"LongParam": int64(432000000),
},
expected: "432000000",
},
}

engine := New()
Expand Down
7 changes: 7 additions & 0 deletions pkg/kudoctl/packages/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ func (p *Parameter) ValidateDefault() error {
return nil
}

func (p *Parameter) ValidateType() error {
if err := kudoapi.ValidateParameterType(p.Type); err != nil {
return fmt.Errorf("parameter \"%s\" has an invalid type: %v", p.Name, err)
}
return nil
}

func (p *Parameter) EnumValues() []interface{} {
if p.IsEnum() {
return *p.Enum
Expand Down
11 changes: 11 additions & 0 deletions pkg/kudoctl/packages/verifier/template/verify_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (ParametersVerifier) Verify(pf *packages.Files) verifier.Result {
res.Merge(immutableParams(pf))
res.Merge(enumParams(pf))
res.Merge(paramDefaults(pf))
res.Merge(paramTypes(pf))
res.Merge(metadata(pf))
res.Merge(paramGroups(pf))

Expand Down Expand Up @@ -59,6 +60,16 @@ func immutableParams(pf *packages.Files) verifier.Result {
return res
}

func paramTypes(pf *packages.Files) verifier.Result {
res := verifier.NewResult()
for _, p := range pf.Params.Parameters {
if err := p.ValidateType(); err != nil {
res.AddErrors(err.Error())
}
}
return res
}

func paramDefaults(pf *packages.Files) verifier.Result {
res := verifier.NewResult()
for _, p := range pf.Params.Parameters {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,26 @@ func TestMetadata(t *testing.T) {
assert.Equal(t, `parameter "InvalidAdvanced" is marked as advanced, but also as required and has no default. An advanced parameter must either be optional or have a default value`, res.Errors[1])
assert.Equal(t, `parameter "InvalidGroup" has a group "some/group" that is not defined in the group section`, res.Errors[2])
}

func TestTypes(t *testing.T) {
params := []packages.Parameter{
{Name: "InvalidType", Type: "long"},
{Name: "ValidEmptyType", Type: ""},
{Name: "ValidTypeInteger", Type: kudoapi.IntegerValueType},
}
paramFile := packages.ParamsFile{Parameters: params}
templates := make(map[string]string)

operator := packages.OperatorFile{}
pf := packages.Files{
Templates: templates,
Operator: &operator,
Params: &paramFile,
}
verifier := ParametersVerifier{}
res := verifier.Verify(&pf)

assert.Equal(t, 3, len(res.Warnings)) // NotUsed Warnings
assert.Equal(t, 1, len(res.Errors))
assert.Equal(t, `parameter "InvalidType" has an invalid type: invalid type long`, res.Errors[0])
}