Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

[cel] parse variables in context #717

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
47 changes: 44 additions & 3 deletions cel/pkg/reconciler/cel/celrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ package cel
import (
"context"
"fmt"

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/experimental/cel/test"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing"
"github.com/tektoncd/pipeline/test/diff"

"strings"
"testing"
"time"

"github.com/tektoncd/pipeline/test/names"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -36,9 +41,6 @@ import (
"knative.dev/pkg/controller"
"knative.dev/pkg/logging"
"knative.dev/pkg/reconciler"
"strings"
"testing"
"time"
)

func TestReconcileCelRun(t *testing.T) {
Expand Down Expand Up @@ -122,6 +124,45 @@ func TestReconcileCelRun(t *testing.T) {
Value: "true",
}},
expectedEvents: []string{"Normal RunReconciled Run reconciled: \"foo/cel-run\""},
}, {
name: "expressions with context variables successful",
run: &v1alpha1.Run{
ObjectMeta: metav1.ObjectMeta{
Name: "cel-run",
Namespace: "foo",
Labels: map[string]string{
"myTestLabel": "myTestLabelValue",
},
Annotations: map[string]string{
"myTestAnnotation": "myTestAnnotationValue",
},
},
Spec: v1alpha1.RunSpec{
Params: []v1beta1.Param{{
Name: "expr1",
Value: v1beta1.ArrayOrString{Type: v1beta1.ParamTypeString, StringVal: "type(100)"},
}, {
Name: "expr2",
Value: v1beta1.ArrayOrString{Type: v1beta1.ParamTypeString, StringVal: "expr1 == 'int'"},
}},
Ref: &v1alpha1.TaskRef{
APIVersion: apiVersion,
Kind: kind,
Name: "a-celrun",
},
},
},
expectedStatus: corev1.ConditionTrue,
expectedReason: ReasonEvaluationSuccess,
expectedMessage: "CEL expressions were evaluated successfully",
expectedResults: []v1alpha1.RunResult{{
Name: "expr1",
Value: "int",
}, {
Name: "expr2",
Value: "true",
}},
expectedEvents: []string{"Normal RunReconciled Run reconciled: \"foo/cel-run\""},
}, {
name: "CEL expressions with invalid type",
run: &v1alpha1.Run{
Expand Down