Skip to content

Commit

Permalink
feat: Change field of IntOrStringStore
Browse files Browse the repository at this point in the history
  • Loading branch information
rainzm committed Jun 12, 2020
1 parent 454c20c commit 655cf96
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 40 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ vet:
generate: controller-gen
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

# Generate doc
generate-doc:
./scripts/gen-doc.sh

# Build the docker image
docker-build: test
docker build . -t $(REGISTRY)/onecloud-service-operator:$(VERSION)
Expand Down
26 changes: 13 additions & 13 deletions api/v1/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ type IValue interface {

// +kubebuilder:object:generate=false
type IStore interface {
Value(ctx context.Context) (IValue, error)
GetValue(ctx context.Context) (IValue, error)
}

type StringStore struct {
// +optional
Direct string `json:"direct,omitempty"`
Value string `json:"value,omitempty"`
// +optional
Indirect *ObjectFieldReference `json:"indirect,omitempty"`
Reference *ObjectFieldReference `json:"reference,omitempty"`
}

type String string
Expand All @@ -54,11 +54,11 @@ func (sv String) Interface() interface{} {
return sv.String()
}

func (st *StringStore) Value(ctx context.Context) (IValue, error) {
if len(st.Direct) > 0 {
return String(st.Direct), nil
func (st *StringStore) GetValue(ctx context.Context) (IValue, error) {
if len(st.Value) > 0 {
return String(st.Value), nil
}
in, err := st.Indirect.Value(ctx)
in, err := st.Reference.Value(ctx)
if err != nil {
return nil, err
}
Expand All @@ -75,9 +75,9 @@ func (st *StringStore) Value(ctx context.Context) (IValue, error) {

type IntOrStringStore struct {
// +optional
Direct *IntOrString `json:"direct,omitempty"`
Value *IntOrString `json:"value,omitempty"`
// +optional
Indirect *ObjectFieldReference `json:"indirect,omitempty"`
Reference *ObjectFieldReference `json:"reference,omitempty"`
}

type IntOrString struct {
Expand Down Expand Up @@ -121,11 +121,11 @@ func (isv *IntOrString) Interface() interface{} {
return isv
}

func (ist *IntOrStringStore) Value(ctx context.Context) (IValue, error) {
if ist.Direct != nil {
return ist.Direct, nil
func (ist *IntOrStringStore) GetValue(ctx context.Context) (IValue, error) {
if ist.Value != nil {
return ist.Value, nil
}
in, err := ist.Indirect.Value(ctx)
in, err := ist.Reference.Value(ctx)
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions api/v1/zz_generated.deepcopy.go

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

28 changes: 14 additions & 14 deletions config/crd/bases/onecloud.yunion.io_ansibleplaybooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,7 @@ spec:
vars:
additionalProperties:
properties:
direct:
anyOf:
- type: integer
- type: string
type: object
x-kubernetes-int-or-string: true
indirect:
reference:
properties:
fieldPath:
type: string
Expand All @@ -69,6 +63,12 @@ spec:
- name
- namespace
type: object
value:
anyOf:
- type: integer
- type: string
type: object
x-kubernetes-int-or-string: true
type: object
description: Vars describes the unique values ​​of the VirtualMachine
corresponding to the variables in the AnsiblePlaybookTemplate.
Expand Down Expand Up @@ -148,13 +148,7 @@ spec:
vars:
additionalProperties:
properties:
direct:
anyOf:
- type: integer
- type: string
type: object
x-kubernetes-int-or-string: true
indirect:
reference:
properties:
fieldPath:
type: string
Expand All @@ -174,6 +168,12 @@ spec:
- name
- namespace
type: object
value:
anyOf:
- type: integer
- type: string
type: object
x-kubernetes-int-or-string: true
type: object
description: Vars describe the public value about Vars in AnsiblePlaybookTemplate.
type: object
Expand Down
6 changes: 3 additions & 3 deletions config/samples/jenkins-slave.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ spec:
name: jenkins-slave1
vars:
jenkins_slave_name:
direct: slave1
value: slave1
- virtualMachine:
name: jenkins-slave2
vars:
jenkins_slave_name:
direct: slave2
value: slave2
vars:
jenkins_master_hostname:
indirect:
reference:
kind: VirtualMachine
namespace: onecloud
name: jenkins-master
Expand Down
8 changes: 4 additions & 4 deletions controllers/ansibleplaybook_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ func (r *AnsiblePlaybookReconciler) Reconcile(req ctrl.Request) (ctrl.Result, er
vars := make(map[string]interface{}, len(host.Vars))
for _, temVar := range playbookTemplate.Spec.Vars {
if value, ok := host.Vars[temVar.Name]; ok {
v, err := value.Value(ctx)
v, err := value.GetValue(ctx)
if err != nil {
// invalid
log.Error(err, "StringStore.Value")
log.Error(err, "StringStore.GetValue")
ansiblePlaybook.SetResourcePhase(onecloudv1.ResourceInvalid,
fmt.Sprintf("The value of var '%s' is valid: %s", temVar.Name, err.Error()),
)
Expand Down Expand Up @@ -232,10 +232,10 @@ func (r *AnsiblePlaybookReconciler) Reconcile(req ctrl.Request) (ctrl.Result, er
// build common vars
commonVars := make(map[string]interface{}, len(ansiblePlaybook.Spec.Vars))
for varName, sv := range ansiblePlaybook.Spec.Vars {
vv, err := sv.Value(ctx)
vv, err := sv.GetValue(ctx)
if err != nil {
// invalid
log.Error(err, "StringStore.Value")
log.Error(err, "StringStore.GetValue")
ansiblePlaybook.SetResourcePhase(onecloudv1.ResourceInvalid,
fmt.Sprintf("The value of var '%s' is valid: %s", varName, err.Error()),
)
Expand Down

0 comments on commit 655cf96

Please sign in to comment.