Skip to content

Commit

Permalink
support yaml string
Browse files Browse the repository at this point in the history
  • Loading branch information
rainzm committed Jun 17, 2020
1 parent 1341f22 commit e1ac25c
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 15 deletions.
4 changes: 2 additions & 2 deletions api/v1/ansibleplaybook_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type AnsiblePlaybookSpec struct {

// Vars describe the public value about Vars in AnsiblePlaybookTemplate.
// +optional
Vars map[string]IntOrStringStore `json:"vars,omitempty"`
Vars map[string]IntOrStringOrYamlStore `json:"vars,omitempty"`

// Nil or Non-positive number means unlimited.
// +optional
Expand All @@ -65,7 +65,7 @@ type AnsiblePlaybookHost struct {
// Vars describes the unique values ​​of the VirtualMachine
// corresponding to the variables in the AnsiblePlaybookTemplate.
// +optional
Vars map[string]IntOrStringStore `json:"vars,omitempty"`
Vars map[string]IntOrStringOrYamlStore `json:"vars,omitempty"`
}

type AnsiblePlaybookInfo struct {
Expand Down
72 changes: 61 additions & 11 deletions api/v1/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"reflect"

"github.com/go-yaml/yaml"
"k8s.io/apimachinery/pkg/util/intstr"
)

Expand Down Expand Up @@ -54,7 +55,7 @@ func (sv String) Interface() interface{} {
return sv.String()
}

func (st *StringStore) GetValue(ctx context.Context) (IValue, error) {
func (st StringStore) GetValue(ctx context.Context) (IValue, error) {
if len(st.Value) > 0 {
return String(st.Value), nil
}
Expand All @@ -81,24 +82,21 @@ type IntOrString struct {
intstr.IntOrString `json:",inline"`
}

func (isv *IntOrString) String() (string, bool) {
func (isv IntOrString) String() (string, bool) {
if isv.Type == intstr.String {
return isv.StrVal, true
}
return "", false
}

func (isv *IntOrString) Int() (int32, bool) {
func (isv IntOrString) Int() (int32, bool) {
if isv.Type == intstr.Int {
return isv.IntVal, true
}
return 0, false
}

func (isv *IntOrString) IsZero() bool {
if isv == nil {
return true
}
func (isv IntOrString) IsZero() bool {
if s, ok := isv.String(); ok {
return s == ""
}
Expand All @@ -108,7 +106,7 @@ func (isv *IntOrString) IsZero() bool {
return true
}

func (isv *IntOrString) Interface() interface{} {
func (isv IntOrString) Interface() interface{} {
if s, ok := isv.String(); ok {
return s
}
Expand All @@ -118,9 +116,9 @@ func (isv *IntOrString) Interface() interface{} {
return isv
}

func (ist *IntOrStringStore) GetValue(ctx context.Context) (IValue, error) {
func (ist IntOrStringStore) GetValue(ctx context.Context) (IValue, error) {
if ist.Value != nil {
return ist.Value, nil
return *ist.Value, nil
}
in, err := ist.Reference.Value(ctx)
if err != nil || in == nil {
Expand All @@ -141,5 +139,57 @@ func (ist *IntOrStringStore) GetValue(ctx context.Context) (IValue, error) {
ts := reflect.TypeOf(in).String()
return nil, fmt.Errorf("Type of ObjectFieldReference' Value in not 'string' but '%s'", ts)
}
return &IntOrString{is}, nil
return IntOrString{is}, nil
}

type IntOrStringOrYamlStore struct {
// IsYaml determines whether the string in IntOrStringStore is a yaml string
// +optional
IsYaml *bool `json:"isYaml,omitempty"`

IntOrStringStore `json:",inline"`
}

type Yaml []byte

func (y Yaml) MarshalYAML() (interface{}, error) {
s := y
if s[0] == '-' {
var in []map[string]interface{}
err := yaml.Unmarshal(s, &in)
if err != nil {
return nil, err
}
return in, nil
}
var in map[string]interface{}
err := yaml.Unmarshal(s, &in)
if err != nil {
return string(s), nil
}
return in, nil
}

func (y Yaml) IsZero() bool {
return len(y) == 0
}

func (y Yaml) Interface() interface{} {
return y
}

func (isys IntOrStringOrYamlStore) GetValue(ctx context.Context) (IValue, error) {
value, err := isys.IntOrStringStore.GetValue(ctx)
if err != nil {
return nil, err
}
if isys.IsYaml == nil && !*isys.IsYaml {
return value, nil
}
is := value.(IntOrString)
s, ok := is.String()
if !ok {
return nil, fmt.Errorf("Not support yaml string for integer")
}
return Yaml(s), nil
}
44 changes: 42 additions & 2 deletions api/v1/zz_generated.deepcopy.go

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

8 changes: 8 additions & 0 deletions config/crd/bases/onecloud.yunion.io_ansibleplaybooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ spec:
vars:
additionalProperties:
properties:
isYaml:
description: IsYaml determines whether the string in IntOrStringStore
is a yaml string
type: boolean
reference:
properties:
fieldPath:
Expand Down Expand Up @@ -148,6 +152,10 @@ spec:
vars:
additionalProperties:
properties:
isYaml:
description: IsYaml determines whether the string in IntOrStringStore
is a yaml string
type: boolean
reference:
properties:
fieldPath:
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ go 1.13
require (
github.com/coredns/coredns v1.3.0 // indirect
github.com/go-logr/logr v0.1.0
github.com/go-yaml/yaml v2.1.0+incompatible
github.com/mcuadros/go-lookup v0.0.0-20200330054200-b4062b0c4c85
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.8.1
k8s.io/apiextensions-apiserver v0.17.2
k8s.io/apimachinery v0.17.3
k8s.io/client-go v9.0.0+incompatible
k8s.io/kubernetes v1.16.0 // indirect
Expand Down

0 comments on commit e1ac25c

Please sign in to comment.