Skip to content

Commit

Permalink
feat: Add Data Filter Comparator (#544)
Browse files Browse the repository at this point in the history
* add data filter comparator

* added api docs from 'make codgen' output, updated types.go

* add data filter comparator

* feat: support json body as an option [POC] (#542)

* Idea POC for discussion

* Make codegen with docs

* JSONBody on AMQP, MQTT, and NATS gateways

* Protobuf struct tags

* Codegen docs

* added api docs from 'make codgen' output, updated types.go

* added tests for data filter comparator

Co-authored-by: Antonio Macías Ojeda <[email protected]>
Co-authored-by: Vaibhav <[email protected]>
  • Loading branch information
3 people authored Mar 19, 2020
1 parent f0b1eda commit 32eba6f
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 3 deletions.
24 changes: 24 additions & 0 deletions api/sensor.html
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,15 @@ <h3 id="argoproj.io/v1alpha1.Backoff">Backoff
</tr>
</tbody>
</table>
<h3 id="argoproj.io/v1alpha1.Comparator">Comparator
(<code>string</code> alias)</p></h3>
<p>
(<em>Appears on:</em>
<a href="#argoproj.io/v1alpha1.DataFilter">DataFilter</a>)
</p>
<p>
<p>Comparator refers to the comparator operator for a data filter</p>
</p>
<h3 id="argoproj.io/v1alpha1.ConfigmapArtifact">ConfigmapArtifact
</h3>
<p>
Expand Down Expand Up @@ -582,6 +591,21 @@ <h3 id="argoproj.io/v1alpha1.DataFilter">DataFilter
Nils this value is ignored</p>
</td>
</tr>
<tr>
<td>
<code>comparator</code></br>
<em>
<a href="#argoproj.io/v1alpha1.Comparator">
Comparator
</a>
</em>
</td>
<td>
<p>Comparator compares the event data with a user given value.
Can be &ldquo;&gt;=&rdquo;, &ldquo;&gt;&rdquo;, &ldquo;=&rdquo;, &ldquo;&lt;&rdquo;, or &ldquo;&lt;=&rdquo;.
Is optional, and if left blank treated as equality &ldquo;=&rdquo;.</p>
</td>
</tr>
</tbody>
</table>
<h3 id="argoproj.io/v1alpha1.DependencyGroup">DependencyGroup
Expand Down
48 changes: 48 additions & 0 deletions api/sensor.md
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,31 @@ Exit with error after this many steps

</table>

<h3 id="argoproj.io/v1alpha1.Comparator">

Comparator (<code>string</code> alias)

</p>

</h3>

<p>

(<em>Appears on:</em>
<a href="#argoproj.io/v1alpha1.DataFilter">DataFilter</a>)

</p>

<p>

<p>

Comparator refers to the comparator operator for a data filter

</p>

</p>

<h3 id="argoproj.io/v1alpha1.ConfigmapArtifact">

ConfigmapArtifact
Expand Down Expand Up @@ -1175,6 +1200,29 @@ strconv.ParseFloat() Strings are taken as is Nils this value is ignored

</tr>

<tr>

<td>

<code>comparator</code></br> <em>
<a href="#argoproj.io/v1alpha1.Comparator"> Comparator </a> </em>

</td>

<td>

<p>

Comparator compares the event data with a user given value. Can be
\>=”, “\>”, “=”, “\<”, or “\<=”. Is optional, and if left blank
treated as equality “=”.

</p>

</td>

</tr>

</tbody>

</table>
Expand Down
65 changes: 65 additions & 0 deletions examples/sensors/data-filter-comparator-webhook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
name: webhook-with-data-filter-comparator-sensor
labels:
# sensor controller with instanceId "argo-events" will process this sensor
sensors.argoproj.io/sensor-controller-instanceid: argo-events
spec:
template:
spec:
containers:
- name: sensor
image: argoproj/sensor:v0.13.0-rc
imagePullPolicy: Always
serviceAccountName: argo-events-sa
dependencies:
- name: test-dep
gatewayName: webhook-gateway
eventName: example
filters:
name: data-filter
data:
- path: body.value
type: number
comparator: ">"
value:
- "50.0"
subscription:
http:
port: 9300
triggers:
- template:
name: data-workflow
k8s:
group: argoproj.io
version: v1alpha1
resource: workflows
operation: create
source:
resource:
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: data-workflow-
spec:
entrypoint: whalesay
arguments:
parameters:
- name: message
# value will get overridden by the event payload
value: hello world
templates:
- name: whalesay
inputs:
parameters:
- name: message
container:
image: docker/whalesay:latest
command: [cowsay]
args: ["{{inputs.parameters.message}}"]
parameters:
- src:
dependencyName: test-dep
dataKey: body.value
dest: spec.arguments.parameters.0.value
63 changes: 63 additions & 0 deletions examples/sensors/data-filter-value-webhook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
name: webhook-with-data-filter-value-sensor
labels:
# sensor controller with instanceId "argo-events" will process this sensor
sensors.argoproj.io/sensor-controller-instanceid: argo-events
spec:
template:
spec:
containers:
- name: sensor
image: argoproj/sensor:v0.13.0-rc
imagePullPolicy: Always
serviceAccountName: argo-events-sa
dependencies:
- name: test-dep
gatewayName: webhook-gateway
eventName: example
filters:
name: data-filter
data:
- path: body.value
type: number
value:
- "50.0"
subscription:
http:
port: 9300
triggers:
- template:
name: data-workflow
k8s:
group: argoproj.io
version: v1alpha1
resource: workflows
operation: create
source:
resource:
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: data-workflow-
spec:
entrypoint: whalesay
arguments:
parameters:
- name: message
# value will get overridden by the event payload
value: hello world
templates:
- name: whalesay
inputs:
parameters:
- name: message
container:
image: docker/whalesay:latest
command: [cowsay]
args: ["{{inputs.parameters.message}}"]
parameters:
- src:
dependencyName: test-dep
dest: spec.arguments.parameters.0.value
16 changes: 16 additions & 0 deletions pkg/apis/sensor/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ const (
Resume ArgoWorkflowOperation = "resume" // resume a workflow
)

// Comparator refers to the comparator operator for a data filter
type Comparator string

const (
GreaterThanOrEqualTo Comparator = ">=" // Greater than or equal to value provided in data filter
GreaterThan Comparator = ">" // Greater than value provided in data filter
EqualTo Comparator = "=" // Equal to value provided in data filter
LessThan Comparator = "<" // Less than value provided in data filter
LessThanOrEqualTo Comparator = "<=" // Less than or equal to value provided in data filter
EmptyComparator = "" // Equal to value provided in data filter
)

// Sensor is the definition of a sensor resource
// +genclient
// +genclient:noStatus
Expand Down Expand Up @@ -242,6 +254,10 @@ type DataFilter struct {
// Strings are taken as is
// Nils this value is ignored
Value []string `json:"value" protobuf:"bytes,3,rep,name=value"`
// Comparator compares the event data with a user given value.
// Can be ">=", ">", "=", "<", or "<=".
// Is optional, and if left blank treated as equality "=".
Comparator Comparator `json:"comparator,omitempty" protobuf:"bytes,4,opt,name=comparator"`
}

// Trigger is an action taken, output produced, an event created, a message sent
Expand Down
27 changes: 24 additions & 3 deletions sensors/dependencies/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,33 @@ filter:

case v1alpha1.JSONTypeNumber:
for _, value := range f.Value {
val, err := strconv.ParseFloat(value, 64)
filterVal, err := strconv.ParseFloat(value, 64)
eventVal := res.Float()
if err != nil {
return false, err
}
if val == res.Float() {
continue filter

switch f.Comparator {
case v1alpha1.GreaterThanOrEqualTo:
if eventVal >= filterVal {
continue filter
}
case v1alpha1.GreaterThan:
if eventVal > filterVal {
continue filter
}
case v1alpha1.LessThan:
if eventVal < filterVal {
continue filter
}
case v1alpha1.LessThanOrEqualTo:
if eventVal <= filterVal {
continue filter
}
case v1alpha1.EqualTo, v1alpha1.EmptyComparator:
if eventVal == filterVal {
continue filter
}
}
}
return false, nil
Expand Down
75 changes: 75 additions & 0 deletions sensors/dependencies/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,81 @@ func TestFilterData(t *testing.T) {
want: true,
wantErr: false,
},
{
name: "comparator filter GreaterThan return true, JSON data",
args: args{data: []v1alpha1.DataFilter{
{
Path: "k",
Type: v1alpha1.JSONTypeNumber,
Value: []string{"1.0"},
Comparator: ">",
},
},
event: &apicommon.Event{
Context: apicommon.EventContext{
DataContentType: "application/json",
},
Data: []byte("{\"k\": \"2.0\"}"),
}},
want: true,
wantErr: false,
},
{
name: "comparator filter LessThanOrEqualTo return false, JSON data",
args: args{data: []v1alpha1.DataFilter{
{
Path: "k",
Type: v1alpha1.JSONTypeNumber,
Value: []string{"1.0"},
Comparator: "<=",
},
},
event: &apicommon.Event{
Context: apicommon.EventContext{
DataContentType: "application/json",
},
Data: []byte("{\"k\": \"2.0\"}"),
}},
want: false,
wantErr: false,
},
{
name: "comparator filter Equal, JSON data",
args: args{data: []v1alpha1.DataFilter{
{
Path: "k",
Type: v1alpha1.JSONTypeNumber,
Value: []string{"5.0"},
Comparator: "=",
},
},
event: &apicommon.Event{
Context: apicommon.EventContext{
DataContentType: "application/json",
},
Data: []byte("{\"k\": \"5.0\"}"),
}},
want: true,
wantErr: false,
},
{
name: "comparator filter empty, JSON data",
args: args{data: []v1alpha1.DataFilter{
{
Path: "k",
Type: v1alpha1.JSONTypeNumber,
Value: []string{"10.0"},
},
},
event: &apicommon.Event{
Context: apicommon.EventContext{
DataContentType: "application/json",
},
Data: []byte("{\"k\": \"10.0\"}"),
}},
want: true,
wantErr: false,
},
{
name: "multiple filters, nested JSON data",
args: args{
Expand Down

0 comments on commit 32eba6f

Please sign in to comment.