Skip to content

Commit

Permalink
add-resource-params, fix filter, testing on minikube, updated quickst…
Browse files Browse the repository at this point in the history
…art (#61)

* add-resource-params, fixing filtering, testing on minikube, updated quickstart guide

* adding tidwall/sjson dependency
  • Loading branch information
magaldima authored and shrinandj committed Jul 25, 2018
1 parent bfea608 commit 711ce7a
Show file tree
Hide file tree
Showing 30 changed files with 1,501 additions and 388 deletions.
1 change: 1 addition & 0 deletions DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
| Shopify/sarama | MIT |
| stretchr/testify | https://github.com/stretchr/testify/blob/master/LICENSE |
| tidwall/gjson | MIT |
| tidwall/sjson | MIT |
9 changes: 8 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion controller/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var sampleSensor = v1alpha1.Sensor{
Version: "v1alpha1",
Kind: "workflow",
},
ArtifactLocation: &v1alpha1.ArtifactLocation{
Source: v1alpha1.ArtifactLocation{
S3: &v1alpha1.S3Artifact{},
},
Labels: map[string]string{"test-label": "test-value"},
Expand Down
63 changes: 24 additions & 39 deletions controller/signal-filter.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
/*
Copyright 2018 BlackRock, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"

"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
"github.com/ghodss/yaml"
"github.com/tidwall/gjson"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -34,6 +47,12 @@ func filterTime(timeFilter *v1alpha1.TimeFilter, eventTime *metav1.Time) bool {
// values are only enforced if they are non-zero values
// map types check that the expected map is a subset of the actual map
func filterContext(expected *v1alpha1.EventContext, actual *v1alpha1.EventContext) bool {
if expected == nil {
return true
}
if actual == nil {
return false
}
res := true
if expected.EventType != "" {
res = res && expected.EventType == actual.EventType
Expand All @@ -57,14 +76,6 @@ func filterContext(expected *v1alpha1.EventContext, actual *v1alpha1.EventContex
return res && eExtensionRes
}

// various supported media types
// TODO: add support for XML
const (
MediaTypeJSON string = "application/json"
//MediaTypeXML string = "application/xml"
MediaTypeYAML string = "application/yaml"
)

// applyDataFilter runs the dataFilter against the event's data
// returns (true, nil) when data passes filters, false otherwise
// TODO: split this function up into smaller pieces
Expand All @@ -77,38 +88,12 @@ func filterData(dataFilters []*v1alpha1.DataFilter, event *v1alpha1.Event) (bool
if event.Data == nil || len(event.Data) == 0 {
return true, nil
}
raw := event.Data
var data map[string]interface{}
// contentType is formatted as: '{type}; charset="xxx"'
contents := strings.Split(event.Context.ContentType, ";")
if len(contents) < 1 {
return false, fmt.Errorf("event context ContentType not found: %s", contents)
}
switch contents[0] {
case MediaTypeJSON:
if err := json.Unmarshal(raw, &data); err != nil {
return false, err
}
/*
case MediaTypeXML:
if err := xml.Unmarshal(raw, &data); err != nil {
return false, err
}
*/
case MediaTypeYAML:
if err := yaml.Unmarshal(raw, &data); err != nil {
return false, err
}
default:
return false, fmt.Errorf("unsupported event content type: %s", event.Context.ContentType)
}
// now let's marshal the data back into json in order to do gjson processing
json, err := json.Marshal(data)
js, err := renderEventDataAsJSON(event)
if err != nil {
return false, err
}
for _, f := range dataFilters {
res := gjson.Get(string(json), f.Path)
res := gjson.GetBytes(js, f.Path)
if !res.Exists() {
return false, nil
}
Expand Down
35 changes: 35 additions & 0 deletions controller/signal-filter_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
Copyright 2018 BlackRock, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller

import (
Expand Down Expand Up @@ -113,6 +128,26 @@ func Test_filterContext(t *testing.T) {
args args
want bool
}{
{
name: "nil expected",
args: args{
expected: nil,
actual: &v1alpha1.EventContext{
EventType: "argo.io.event",
},
},
want: true,
},
{
name: "nil actual, non-nil expected",
args: args{
expected: &v1alpha1.EventContext{
EventType: "argo.io.event",
},
actual: nil,
},
want: false,
},
{
name: "eventType",
args: args{expected: &v1alpha1.EventContext{
Expand Down
67 changes: 67 additions & 0 deletions controller/trigger-params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2018 BlackRock, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
"fmt"

"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)

// apply the params to the resource json object
func applyParams(jsonObj []byte, params []v1alpha1.ResourceParameter, events map[string]v1alpha1.Event) ([]byte, error) {
tmp := make([]byte, len(jsonObj))
for _, param := range params {
// let's grab the param value
v, err := resolveParamValue(param.Src, events)
if err != nil {
return nil, err
}

// now let's set the value
tmp, err = sjson.SetBytes(jsonObj, param.Dest, v)
if err != nil {
return nil, err
}
jsonObj = tmp
}
return jsonObj, nil
}

// helper method to resolve the parameter's value from the src
// returns an error if the Path is invalid/not found and the default value is nil OR if the signal event doesn't exist and default value is nil
func resolveParamValue(src *v1alpha1.ResourceParameterSource, events map[string]v1alpha1.Event) (string, error) {
if e, ok := events[src.Signal]; ok {
js, err := renderEventDataAsJSON(&e)
if err != nil {
if src.Value != nil {
return *src.Value, nil
}
return "", err
}
res := gjson.GetBytes(js, src.Path)
if res.Exists() {
return res.String(), nil
}
}
if src.Value != nil {
return *src.Value, nil
}
return "", fmt.Errorf("unable to resolve '%s' parameter value. verify the path: '%s' is valid and/or set a default value for this param", src.Signal, src.Path)
}
Loading

0 comments on commit 711ce7a

Please sign in to comment.