Skip to content

Commit ea49546

Browse files
Merge pull request #3 from stevendborrelli/refactor-conditional
change fields from condition/expression to condition
2 parents f221df1 + 6c1285a commit ea49546

File tree

13 files changed

+61
-72
lines changed

13 files changed

+61
-72
lines changed

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@ GO := go
44

55
GOLANGCI_VERSION := 1.55.2
66

7+
generate:
8+
$(GO) generate ./...
9+
710
test:
811
$(GO) test ./...
912

1013
lint:
1114
$(DOCKER) run --rm -v $(CURDIR):/app -v ~/.cache/golangci-lint/v$(GOLANGCI_VERSION):/root/.cache -w /app golangci/golangci-lint:v$(GOLANGCI_VERSION) golangci-lint run --fix
1215

13-
reviewable: test lint
16+
reviewable: generate test lint
17+
18+
# run a local process for crossplane render
19+
run-local:
20+
$(GO) run . --debug --insecure

condition.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"github.com/crossplane/crossplane-runtime/pkg/errors"
99

1010
fnv1beta1 "github.com/crossplane/function-sdk-go/proto/v1beta1"
11-
12-
"github.com/stevendborrelli/function-conditional-patch-and-transform/input/v1beta1"
1311
)
1412

1513
// NewCELEnvironment sets up the CEL Environment
@@ -30,8 +28,8 @@ func ToCELVars(req *fnv1beta1.RunFunctionRequest) map[string]any {
3028
}
3129

3230
// EvaluateCondition will evaluate a CEL expression
33-
func EvaluateCondition(cs v1beta1.ConditionSpec, req *fnv1beta1.RunFunctionRequest) (bool, error) {
34-
if cs.Expression == "" {
31+
func EvaluateCondition(expression *string, req *fnv1beta1.RunFunctionRequest) (bool, error) {
32+
if expression == nil {
3533
return false, nil
3634
}
3735

@@ -40,7 +38,7 @@ func EvaluateCondition(cs v1beta1.ConditionSpec, req *fnv1beta1.RunFunctionReque
4038
return false, errors.Wrap(err, "CEL Env error")
4139
}
4240

43-
ast, iss := env.Parse(cs.Expression)
41+
ast, iss := env.Parse(*expression)
4442
if iss.Err() != nil {
4543
return false, errors.Wrap(iss.Err(), "CEL Parse error")
4644
}
@@ -56,7 +54,7 @@ func EvaluateCondition(cs v1beta1.ConditionSpec, req *fnv1beta1.RunFunctionReque
5654
if !reflect.DeepEqual(checked.OutputType(), cel.BoolType) {
5755
return false, errors.Errorf(
5856
"CEL Type error: expression '%s' must return a boolean, got %v instead",
59-
cs.Expression,
57+
*expression,
6058
checked.OutputType())
6159
}
6260

condition_test.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ func TestEvaluateCondition(t *testing.T) {
2020
oxr := `{"apiVersion":"nopexample.org/v1alpha1","kind":"XNopResource","metadata":{"name":"test-resource"},"spec":{"env":"dev","render":true},"status":{"id":"123","ready":false} }`
2121

2222
type args struct {
23-
cs v1beta1.ConditionSpec
24-
req *fnv1beta1.RunFunctionRequest
23+
condition v1beta1.Condition
24+
req *fnv1beta1.RunFunctionRequest
2525
}
2626
type want struct {
2727
ret bool
@@ -35,7 +35,7 @@ func TestEvaluateCondition(t *testing.T) {
3535
}{
3636
"CELParseError": {
3737
args: args{
38-
cs: v1beta1.ConditionSpec{Expression: "field = value"},
38+
condition: strPtr("field = value"),
3939
req: &fnv1beta1.RunFunctionRequest{
4040
Input: resource.MustStructObject(&v1beta1.Resources{
4141
Resources: []v1beta1.ComposedTemplate{
@@ -64,7 +64,7 @@ func TestEvaluateCondition(t *testing.T) {
6464
},
6565
"CELTypeError": {
6666
args: args{
67-
cs: v1beta1.ConditionSpec{Expression: "size(desired.resources)"},
67+
condition: strPtr("size(desired.resources)"),
6868
req: &fnv1beta1.RunFunctionRequest{
6969
Input: resource.MustStructObject(&v1beta1.Resources{
7070
Resources: []v1beta1.ComposedTemplate{
@@ -93,7 +93,7 @@ func TestEvaluateCondition(t *testing.T) {
9393
},
9494
"KeyError": {
9595
args: args{
96-
cs: v1beta1.ConditionSpec{Expression: "badkey"},
96+
condition: strPtr("badkey"),
9797
req: &fnv1beta1.RunFunctionRequest{
9898
Input: resource.MustStructObject(&v1beta1.Resources{
9999
Resources: []v1beta1.ComposedTemplate{
@@ -122,7 +122,7 @@ func TestEvaluateCondition(t *testing.T) {
122122
},
123123
"TrueDesired": {
124124
args: args{
125-
cs: v1beta1.ConditionSpec{Expression: "desired.composite.resource.spec.env == \"dev\" "},
125+
condition: strPtr("desired.composite.resource.spec.env == \"dev\" "),
126126
req: &fnv1beta1.RunFunctionRequest{
127127
Input: resource.MustStructObject(&v1beta1.Resources{
128128
Resources: []v1beta1.ComposedTemplate{
@@ -151,7 +151,7 @@ func TestEvaluateCondition(t *testing.T) {
151151
},
152152
"TrueDesiredBool": {
153153
args: args{
154-
cs: v1beta1.ConditionSpec{Expression: "desired.composite.resource.spec.render == true"},
154+
condition: strPtr("desired.composite.resource.spec.render == true"),
155155
req: &fnv1beta1.RunFunctionRequest{
156156
Input: resource.MustStructObject(&v1beta1.Resources{
157157
Resources: []v1beta1.ComposedTemplate{
@@ -180,7 +180,7 @@ func TestEvaluateCondition(t *testing.T) {
180180
},
181181
"FalseDesiredBool": {
182182
args: args{
183-
cs: v1beta1.ConditionSpec{Expression: "desired.composite.resource.spec.render == false"},
183+
condition: strPtr("desired.composite.resource.spec.render == false"),
184184
req: &fnv1beta1.RunFunctionRequest{
185185
Input: resource.MustStructObject(&v1beta1.Resources{
186186
Resources: []v1beta1.ComposedTemplate{
@@ -209,7 +209,7 @@ func TestEvaluateCondition(t *testing.T) {
209209
},
210210
"FalseObservedBool": {
211211
args: args{
212-
cs: v1beta1.ConditionSpec{Expression: "observed.composite.resource.status.ready == true"},
212+
condition: strPtr("observed.composite.resource.status.ready == true"),
213213
req: &fnv1beta1.RunFunctionRequest{
214214
Input: resource.MustStructObject(&v1beta1.Resources{
215215
Resources: []v1beta1.ComposedTemplate{
@@ -238,7 +238,7 @@ func TestEvaluateCondition(t *testing.T) {
238238
},
239239
"FalseLengthResources": {
240240
args: args{
241-
cs: v1beta1.ConditionSpec{Expression: "size(desired.resources) == 0"},
241+
condition: strPtr("size(desired.resources) == 0"),
242242
req: &fnv1beta1.RunFunctionRequest{
243243
Input: resource.MustStructObject(&v1beta1.Resources{
244244
Resources: []v1beta1.ComposedTemplate{
@@ -272,7 +272,7 @@ func TestEvaluateCondition(t *testing.T) {
272272
},
273273
"TrueResourceMapKeyExists": {
274274
args: args{
275-
cs: v1beta1.ConditionSpec{Expression: "\"test-resource\" in desired.resources"},
275+
condition: strPtr("\"test-resource\" in desired.resources"),
276276
req: &fnv1beta1.RunFunctionRequest{
277277
Input: resource.MustStructObject(&v1beta1.Resources{
278278
Resources: []v1beta1.ComposedTemplate{
@@ -306,7 +306,7 @@ func TestEvaluateCondition(t *testing.T) {
306306
},
307307
"FalseResourceMapKeyExists": {
308308
args: args{
309-
cs: v1beta1.ConditionSpec{Expression: "\"bad-resource\" in desired.resources"},
309+
condition: strPtr("\"bad-resource\" in desired.resources"),
310310
req: &fnv1beta1.RunFunctionRequest{
311311
Input: resource.MustStructObject(&v1beta1.Resources{
312312
Resources: []v1beta1.ComposedTemplate{
@@ -342,7 +342,7 @@ func TestEvaluateCondition(t *testing.T) {
342342

343343
for name, tc := range cases {
344344
t.Run(name, func(t *testing.T) {
345-
ret, err := EvaluateCondition(tc.args.cs, tc.args.req)
345+
ret, err := EvaluateCondition(tc.args.condition, tc.args.req)
346346

347347
if diff := cmp.Diff(tc.want.ret, ret); diff != "" {
348348
t.Errorf("%s\nEvaluateCondition(...): -want ret, +got ret:\n%s", tc.reason, diff)
@@ -357,3 +357,7 @@ func TestEvaluateCondition(t *testing.T) {
357357
})
358358
}
359359
}
360+
361+
func strPtr(str string) *string {
362+
return &str
363+
}

example/functions.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
apiVersion: pkg.crossplane.io/v1beta1
33
kind: Function
44
metadata:
5-
name: function-patch-and-transform
5+
name: function-conditional-patch-and-transform
66
spec:
7-
package: xpkg.upbound.io/crossplane-contrib/function-patch-and-transform:v0.1.4
7+
package: xpkg.upbound.io/crossplane-contrib/function-conditional-patch-and-transform:v0.1.4

examples/conditional-rendering/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ will not run.
1919

2020
## Running this example
2121

22-
- Until 1.14 is released, install a development version of Crossplane. See [install Crossplane master](https://docs.crossplane.io/latest/software/install/#install-the-crossplane-master-helm-chart)
22+
- Install Crossplane version 1.14 or newer. See <https://docs.crossplane.io/v1.14/software/install/>
2323
- Install the nop provider in `kubectl apply -f provider.yaml`
2424
- Install the XRD & Composition in `kubectl apply -f definition.yaml -f composition.yaml`
2525
- Install the Function `kubectl apply -f function.yaml`

examples/conditional-rendering/composition.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ spec:
1111
pipeline:
1212
- step: conditional-patch-and-transform
1313
functionRef:
14-
name: function-patch-and-transform
14+
name: function-conditional-patch-and-transform
1515
input:
16-
apiVersion: pt.fn.crossplane.io/v1beta1
16+
apiVersion: conditional-pt.fn.crossplane.io/v1beta1
1717
kind: Resources
18-
condition:
19-
expression: observed.composite.resource.spec.env == "dev" && observed.composite.resource.spec.render == true
18+
condition: observed.composite.resource.spec.env == "dev" && observed.composite.resource.spec.render == true
2019
resources:
2120
- name: test-resource
2221
base:
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
apiVersion: pkg.crossplane.io/v1beta1
22
kind: Function
33
metadata:
4-
name: function-patch-and-transform
4+
name: function-conditional-patch-and-transform
55
annotations:
6-
xrender.crossplane.io/runtime: Development
6+
render.crossplane.io/runtime: Development
77
spec:
8-
package: index.docker.io/steve/function-patch-and-transform:v0.2.0
8+
package: index.docker.io/steve/function-conditional-patch-and-transform:v0.2.0
99
packagePullPolicy: Always

fn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe
4848
// Evaluate any Conditions using the values from the Observed XR
4949
if input.Condition != nil {
5050
// Evaluate the condition to see if we should run
51-
run, err := EvaluateCondition(*input.Condition, req)
51+
run, err := EvaluateCondition(input.Condition, req)
5252
if err != nil {
5353
response.Fatal(rsp, errors.Wrap(err, conditionError))
5454
return rsp, nil
@@ -159,7 +159,7 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe
159159

160160
if t.Condition != nil {
161161
// Evaluate the condition to see if we should skip this template.
162-
run, err := EvaluateCondition(*t.Condition, req)
162+
run, err := EvaluateCondition(t.Condition, req)
163163
if err != nil {
164164
log.Info(err.Error())
165165
response.Fatal(rsp, errors.Wrap(err, conditionError))

input/v1beta1/conditions.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package v1beta1
22

3-
// ConditionSpec defines the condition for rendering.
3+
// Condition defines the condition for rendering.
44
// Conditions are defined using the Common Expression Language
55
// For more information refer to https://github.com/google/cel-spec
6-
type ConditionSpec struct {
7-
// Expression is the CEL expression to be evaluated. If the Expression
8-
// returns a true value, the function will render the resources
9-
Expression string `json:"expression"`
10-
}
6+
type Condition *string

input/v1beta1/resources.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Package v1beta1 contains the input type for the P&T Composition Function.
22
// +kubebuilder:object:generate=true
3-
// +groupName=pt.fn.crossplane.io
3+
// +groupName=conditional-pt.fn.crossplane.io
44
// +versionName=v1beta1
55
package v1beta1
66

@@ -20,8 +20,8 @@ type Resources struct {
2020
metav1.TypeMeta `json:",inline"`
2121
metav1.ObjectMeta `json:"metadata,omitempty"`
2222

23-
// Condition defines a CEL condition whether this function will render
24-
Condition *ConditionSpec `json:"condition,omitempty"`
23+
// If defines a CEL condition whether this function will render
24+
Condition Condition `json:"condition,omitempty"`
2525

2626
// PatchSets define a named set of patches that may be included by any
2727
// resource. PatchSets cannot themselves refer to other PatchSets.

0 commit comments

Comments
 (0)