Skip to content

Commit

Permalink
parse variables in context
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pli committed Mar 4, 2021
1 parent 459268c commit 14a0ac3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
15 changes: 15 additions & 0 deletions cel/examples/celrun-with-context.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: tekton.dev/v1alpha1
kind: Run
metadata:
generateName: celrun-with-context-
spec:
ref:
apiVersion: cel.tekton.dev/v1alpha1
kind: CEL
params:
- name: name
value: "'hello'"
- name: types
value: "type(name)"
- name: expression-use-context
value: "name + ' is type of ' + types"
14 changes: 13 additions & 1 deletion cel/pkg/reconciler/cel/celrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package cel
import (
"context"
"fmt"

"github.com/google/cel-go/cel"
"github.com/google/cel-go/checker/decls"
"github.com/google/cel-go/common/types"
"github.com/tektoncd/pipeline/pkg/client/injection/reconciler/pipeline/v1alpha1/run"
"knative.dev/pkg/apis"
Expand Down Expand Up @@ -94,6 +96,8 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, run *v1alpha1.Run) recon
}

var runResults []v1alpha1.RunResult
contextExpressions := map[string]interface{}{}

for _, param := range run.Spec.Params {
// Combine the Parse and Check phases CEL program compilation to produce an Ast and associated issues
ast, iss := env.Compile(param.Value.StringVal)
Expand All @@ -114,7 +118,7 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, run *v1alpha1.Run) recon
}

// Evaluate the CEL expression (Ast)
out, _, err := prg.Eval(map[string]interface{}{})
out, _, err := prg.Eval(contextExpressions)
if err != nil {
logger.Errorf("CEL expression %s could not be evaluated when reconciling Run %s/%s: %v", param.Name, run.Namespace, run.Name, err)
run.Status.MarkRunFailed(ReasonEvaluationError,
Expand All @@ -128,6 +132,14 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, run *v1alpha1.Run) recon
Name: param.Name,
Value: fmt.Sprintf("%s", out.ConvertToType(types.StringType).Value()),
})
contextExpressions[param.Name] = fmt.Sprintf("%s", out.ConvertToType(types.StringType).Value())
env, err = env.Extend(cel.Declarations(decls.NewVar(param.Name, decls.Any)))
if err != nil {
logger.Errorf("CEL expression %s could not be add to context env when reconciling Run %s/%s: %v", param.Name, run.Namespace, run.Name, err)
run.Status.MarkRunFailed(ReasonEvaluationError,
"CEL expression %s could not be add to context env", param.Name, err)
return nil
}
}

// All CEL expressions were evaluated successfully
Expand Down

0 comments on commit 14a0ac3

Please sign in to comment.