Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-inf committed May 23, 2024
1 parent 9359def commit 96b40c7
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 55 deletions.
15 changes: 9 additions & 6 deletions apis/apps/v1alpha1/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -979,16 +979,19 @@ type EnvVar struct {
// +optional
ValueFrom *VarSource `json:"valueFrom,omitempty"`

// Expression is a Go template expression that will be applied to the resolved value of the var.
// A Go template expression that will be applied to the resolved value of the var.
//
// The expression will only be evaluated if the var is successfully resolved to a non-credential value.
//
// The resolved value can be accessed by its name within the expression, and other non-credential vars can be used
// within the expression in the same way.
// All expressions are evaluated in the order the vars are defined. If a var has a dependent var that also has
// an expression defined, be mindful of the evaluation order as it may use an intermediate value.
// within the expression in the same way. Notice that, when accessing vars by its name, you should replace all
// the "-" in the name with "_", because of that "-" is not a valid identifier in Go.
//
// All expressions are evaluated in the order the vars are defined. If a var depends on any vars that also
// have expressions defined, be careful about the evaluation order as it may use intermediate values.
//
// The result of evaluating the expression will be used as the final value of the var. If the expression
// evaluation fails, an error will be returned.
// The result of evaluation will be used as the final value of the var. If the expression fails to evaluate,
// the resolving of var will also be considered failed.
//
// +optional
Expression *string `json:"expression,omitempty"`
Expand Down
26 changes: 14 additions & 12 deletions config/crd/bases/apps.kubeblocks.io_componentdefinitions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13339,18 +13339,20 @@ spec:
Pod/Action or the template of config/script.
properties:
expression:
description: "Expression is a Go template expression that will
be applied to the resolved value of the var. \n The expression
will only be evaluated if the var is successfully resolved
to a non-credential value. The resolved value can be accessed
by its name within the expression, and other non-credential
vars can be used within the expression in the same way. All
expressions are evaluated in the order the vars are defined.
If a var has a dependent var that also has an expression defined,
be mindful of the evaluation order as it may use an intermediate
value. \n The result of evaluating the expression will be
used as the final value of the var. If the expression evaluation
fails, an error will be returned."
description: "A Go template expression that will be applied
to the resolved value of the var. \n The expression will only
be evaluated if the var is successfully resolved to a non-credential
value. \n The resolved value can be accessed by its name within
the expression, and other non-credential vars can be used
within the expression in the same way. Notice that, when accessing
vars by its name, you should replace all the \"-\" in the
name with \"_\", because of that \"-\" is not a valid identifier
in Go. \n All expressions are evaluated in the order the vars
are defined. If a var depends on any vars that also have expressions
defined, be careful about the evaluation order as it may use
intermediate values. \n The result of evaluation will be used
as the final value of the var. If the expression fails to
evaluate, the resolving of var will also be considered failed."
type: string
name:
description: Name of the variable. Must be a C_IDENTIFIER.
Expand Down
26 changes: 14 additions & 12 deletions deploy/helm/crds/apps.kubeblocks.io_componentdefinitions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13339,18 +13339,20 @@ spec:
Pod/Action or the template of config/script.
properties:
expression:
description: "Expression is a Go template expression that will
be applied to the resolved value of the var. \n The expression
will only be evaluated if the var is successfully resolved
to a non-credential value. The resolved value can be accessed
by its name within the expression, and other non-credential
vars can be used within the expression in the same way. All
expressions are evaluated in the order the vars are defined.
If a var has a dependent var that also has an expression defined,
be mindful of the evaluation order as it may use an intermediate
value. \n The result of evaluating the expression will be
used as the final value of the var. If the expression evaluation
fails, an error will be returned."
description: "A Go template expression that will be applied
to the resolved value of the var. \n The expression will only
be evaluated if the var is successfully resolved to a non-credential
value. \n The resolved value can be accessed by its name within
the expression, and other non-credential vars can be used
within the expression in the same way. Notice that, when accessing
vars by its name, you should replace all the \"-\" in the
name with \"_\", because of that \"-\" is not a valid identifier
in Go. \n All expressions are evaluated in the order the vars
are defined. If a var depends on any vars that also have expressions
defined, be careful about the evaluation order as it may use
intermediate values. \n The result of evaluation will be used
as the final value of the var. If the expression fails to
evaluate, the resolving of var will also be considered failed."
type: string
name:
description: Name of the variable. Must be a C_IDENTIFIER.
Expand Down
17 changes: 9 additions & 8 deletions docs/developer_docs/api-reference/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -11342,14 +11342,15 @@ string
</td>
<td>
<em>(Optional)</em>
<p>Expression is a Go template expression that will be applied to the resolved value of the var.</p>
<p>The expression will only be evaluated if the var is successfully resolved to a non-credential value.
The resolved value can be accessed by its name within the expression, and other non-credential vars can be used
within the expression in the same way.
All expressions are evaluated in the order the vars are defined. If a var has a dependent var that also has
an expression defined, be mindful of the evaluation order as it may use an intermediate value.</p>
<p>The result of evaluating the expression will be used as the final value of the var. If the expression
evaluation fails, an error will be returned.</p>
<p>A Go template expression that will be applied to the resolved value of the var.</p>
<p>The expression will only be evaluated if the var is successfully resolved to a non-credential value.</p>
<p>The resolved value can be accessed by its name within the expression, and other non-credential vars can be used
within the expression in the same way. Notice that, when accessing vars by its name, you should replace all
the &ldquo;-&rdquo; in the name with &ldquo;_&rdquo;, because of that &ldquo;-&rdquo; is not a valid identifier in Go.</p>
<p>All expressions are evaluated in the order the vars are defined. If a var depends on any vars that also
have expressions defined, be careful about the evaluation order as it may use intermediate values.</p>
<p>The result of evaluation will be used as the final value of the var. If the expression fails to evaluate,
the resolving of var will also be considered failed.</p>
</td>
</tr>
</tbody>
Expand Down
34 changes: 20 additions & 14 deletions pkg/controller/component/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func resolveBuiltinNObjectRefVars(ctx context.Context, cli client.Reader, synthe
return nil, nil, err
}
vars = append(vars, vars1...)
if err = evaluateObjectVarsExpression(definedVars, vars); err != nil {
if err = evaluateObjectVarsExpression(definedVars, vars2, &vars); err != nil {
return nil, nil, err
}
return vars, vars2, nil
Expand Down Expand Up @@ -379,17 +379,22 @@ func buildEnv4UserDefined(annotations map[string]string) ([]corev1.EnvVar, error
return vars, nil
}

func evaluateObjectVarsExpression(definedVars []appsv1alpha1.EnvVar, vars []corev1.EnvVar) error {
func evaluateObjectVarsExpression(definedVars []appsv1alpha1.EnvVar, credentialVars []corev1.EnvVar, vars *[]corev1.EnvVar) error {
var (
isValues = make(map[string]bool)
values = make(map[string]string)
)
for _, v := range vars {
if v.ValueFrom == nil {
isValues[v.Name] = true
values[v.Name] = v.Value
} else {
isValues[v.Name] = false
normalize := func(name string) string {
return strings.ReplaceAll(name, "-", "_")
}
for _, v := range [][]corev1.EnvVar{*vars, credentialVars} {
for _, vv := range v {
if vv.ValueFrom == nil {
isValues[vv.Name] = true
values[normalize(vv.Name)] = vv.Value
} else {
isValues[vv.Name] = false
}
}
}

Expand All @@ -398,24 +403,25 @@ func evaluateObjectVarsExpression(definedVars []appsv1alpha1.EnvVar, vars []core
return false
}
isValue, ok := isValues[v.Name]
// !ok is for vars that defined and resolved successfully, but have nil value.
return !ok || isValue
}

update := func(name, value string) {
if val, exist := values[name]; exist {
if val, exist := values[normalize(name)]; exist {
if val != value {
for i := range vars {
if vars[i].Name == name {
vars[i].Value = value
for i := range *vars {
if (*vars)[i].Name == name {
(*vars)[i].Value = value
break
}
}
}
} else {
// TODO: insert the var to keep orders?
vars = append(vars, corev1.EnvVar{Name: name, Value: value})
*vars = append(*vars, corev1.EnvVar{Name: name, Value: value})
}
values[name] = value
values[normalize(name)] = value
}

eval := func(v appsv1alpha1.EnvVar) error {
Expand Down
Loading

0 comments on commit 96b40c7

Please sign in to comment.