Skip to content

Commit

Permalink
Distinguish between empty string and not provided (#541)
Browse files Browse the repository at this point in the history
* Distinguish between empty string and not provided

* Format

* Missing file
  • Loading branch information
alenkacz authored and kensipe committed Jul 11, 2019
1 parent ee0bece commit 38a8a20
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/kudo/v1alpha1/operatorversion_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ type Parameter struct {
Required bool `json:"required,omitempty"`

// Default is a default value if no parameter is provided by the instance.
Default string `json:"default,omitempty"`
Default *string `json:"default,omitempty"`

// Trigger identifies the plan that gets executed when this parameter changes in the Instance object.
// Default is `update` if a plan with that name exists, otherwise it's `deploy`
Expand Down
9 changes: 8 additions & 1 deletion pkg/apis/kudo/v1alpha1/zz_generated.deepcopy.go

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

4 changes: 3 additions & 1 deletion pkg/controller/planexecution/planexecution_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"log"
"strconv"

"github.com/kudobuilder/kudo/pkg/util/kudo"

apijson "k8s.io/apimachinery/pkg/util/json"

kudoengine "github.com/kudobuilder/kudo/pkg/engine"
Expand Down Expand Up @@ -310,7 +312,7 @@ func (r *ReconcilePlanExecution) Reconcile(request reconcile.Request) (reconcile
r.recorder.Event(planExecution, "Warning", "MissingParameter", fmt.Sprintf("Could not find required parameter (%v)", param.Name))
return reconcile.Result{}, err
}
params[param.Name] = param.Default
params[param.Name] = kudo.StringValue(param.Default)
}
}

Expand Down
8 changes: 7 additions & 1 deletion pkg/kudoctl/bundle/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strconv"
"strings"

"github.com/kudobuilder/kudo/pkg/util/kudo"

"k8s.io/apimachinery/pkg/util/rand"

"github.com/kudobuilder/kudo/pkg/apis/kudo/v1alpha1"
Expand Down Expand Up @@ -79,11 +81,15 @@ func parsePackageFile(filePath string, fileBytes []byte, currentPackage *Package

required = parsed
}
var defaultValue *string
if val, ok := param["default"]; ok {
defaultValue = kudo.String(val)
}

r := v1alpha1.Parameter{
Name: paramName,
Description: param["description"],
Default: param["default"],
Default: defaultValue,
Trigger: param["trigger"],
Required: required,
DisplayName: param["displayName"],
Expand Down
2 changes: 1 addition & 1 deletion pkg/kudoctl/cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func validateCrds(crds *bundle.PackageCRDs, skipInstance bool) error {
parameters := crds.OperatorVersion.Spec.Parameters
missingParameters := []string{}
for _, p := range parameters {
if p.Required && p.Default == "" {
if p.Required && p.Default == nil {
_, ok := crds.Instance.Spec.Parameters[p.Name]
if !ok {
missingParameters = append(missingParameters, p.Name)
Expand Down
5 changes: 3 additions & 2 deletions pkg/kudoctl/cmd/install/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/kudobuilder/kudo/pkg/kudoctl/bundle"
util "github.com/kudobuilder/kudo/pkg/util/kudo"

"github.com/kudobuilder/kudo/pkg/apis/kudo/v1alpha1"
"github.com/kudobuilder/kudo/pkg/client/clientset/versioned/fake"
Expand Down Expand Up @@ -94,9 +95,9 @@ func TestParameterValidation_InstallCrds(t *testing.T) {
skipInstance bool
err string
}{
{"all parameters with defaults", []v1alpha1.Parameter{{Name: "param", Required: true, Default: "aaa"}}, map[string]string{}, false, ""},
{"all parameters with defaults", []v1alpha1.Parameter{{Name: "param", Required: true, Default: util.String("aaa")}}, map[string]string{}, false, ""},
{"missing parameter provided", []v1alpha1.Parameter{{Name: "param", Required: true}}, map[string]string{"param": "value"}, false, ""},
{"missing parameter", []v1alpha1.Parameter{{Name: "param", Required: true}}, map[string]string{}, false, "missing required parameters during installation: param"},
{"missing parameter", []v1alpha1.Parameter{{Name: "param", Required: true, Default: nil}}, map[string]string{}, false, "missing required parameters during installation: param"},
{"multiple missing parameter", []v1alpha1.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", []v1alpha1.Parameter{{Name: "param", Required: true}}, map[string]string{}, true, ""},
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/util/kudo/convert_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kudo

// String returns a pointer to the string value passed in.
func String(v string) *string {
return &v
}

// StringValue returns the value of the string pointer passed in or
// "" if the pointer is nil.
func StringValue(v *string) string {
if v != nil {
return *v
}
return ""
}

0 comments on commit 38a8a20

Please sign in to comment.