Skip to content

Commit

Permalink
Support URL trigger sources (#66)
Browse files Browse the repository at this point in the history
Sensor yamls can now specify the workflow location as a url path with this change.

Refs #41
  • Loading branch information
shrinandj authored and magaldima committed Jul 26, 2018
1 parent f493709 commit 38d6adc
Show file tree
Hide file tree
Showing 8 changed files with 611 additions and 289 deletions.
21 changes: 21 additions & 0 deletions examples/url-sensor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
name: url-example
labels:
sensors.argoproj.io/controller-instanceid: axis
spec:
signals:
- name: time
calendar:
interval: 10s
triggers:
- name: url-workflow-trigger
resource:
namespace: default
group: argoproj.io
version: v1alpha1
kind: Workflow
source:
url:
path: "https://raw.githubusercontent.com/argoproj/argo/master/examples/hello-world.yaml"
776 changes: 487 additions & 289 deletions pkg/apis/sensor/v1alpha1/generated.pb.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions pkg/apis/sensor/v1alpha1/generated.proto

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

6 changes: 6 additions & 0 deletions pkg/apis/sensor/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ type ArtifactLocation struct {
S3 *S3Artifact `json:"s3,omitempty" protobuf:"bytes,1,opt,name=s3"`
Inline string `json:"inline,omitempty" protobuf:"bytes,2,opt,name=inline"`
File *FileArtifact `json:"file,omitempty" protobuf:"bytes,3,opt,name=file"`
URL *URLArtifact `json:"url,omitempty" protobuf:"bytes,4,opt,name=url"`
}

// S3Artifact contains information about an artifact in S3
Expand All @@ -466,6 +467,11 @@ type FileArtifact struct {
Path string `json:"path,omitempty" protobuf:"bytes,1,opt,name=path"`
}

// URLArtifact contains information about an artifact at an http endpoint.
type URLArtifact struct {
Path string `json:"path,omitempty" protobuf:"bytes,1,opt,name=path"`
}

// S3Bucket contains information for an S3 Bucket
type S3Bucket struct {
Endpoint string `json:"endpoint,omitempty" protobuf:"bytes,1,opt,name=endpoint"`
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/sensor/v1alpha1/zz_generated.deepcopy.go

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

2 changes: 2 additions & 0 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func GetArtifactReader(loc *ss_v1alpha1.ArtifactLocation, creds *Credentials) (A
return NewInlineReader(loc.Inline)
} else if loc.File != nil {
return NewFileReader(loc.File)
} else if loc.URL != nil {
return NewURLReader(loc.URL)
}
return nil, fmt.Errorf(fmt.Sprintf("unknown artifact location: %v", *loc))
}
Expand Down
49 changes: 49 additions & 0 deletions store/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package store

import (
"crypto/tls"
"errors"
"io/ioutil"
"log"
"net/http"

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

// URLReader implements the ArtifactReader interface for urls
type URLReader struct {
urlArtifact *v1alpha1.URLArtifact
}

// NewURLReader creates a new ArtifactReader for workflows at URL endpoints.
func NewURLReader(urlArtifact *v1alpha1.URLArtifact) (ArtifactReader, error) {
if urlArtifact == nil {
panic("URLArtifact cannot be empty")
}
return &URLReader{urlArtifact}, nil
}

func (reader *URLReader) Read() ([]byte, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Get(reader.urlArtifact.Path)
if err != nil {
log.Printf("Failed to read url %s. Err: %s\n", reader.urlArtifact.Path, err)
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
log.Printf("Failed to read %s. Status code: %d\n", reader.urlArtifact.Path, resp.StatusCode)
return nil, errors.New("Status code " + string(resp.StatusCode))
}

content, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Failed to read url body for %s. Err %s\n", reader.urlArtifact.Path, err)
return nil, err
}
return content, nil
}
18 changes: 18 additions & 0 deletions store/url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package store

import (
"testing"

"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
"github.com/stretchr/testify/assert"
)

func TestURLReader(t *testing.T) {
urlArtifact := v1alpha1.URLArtifact{Path: "https://raw.githubusercontent.com/argoproj/argo/master/examples/hello-world.yaml"}
urlReader, err := NewURLReader(&urlArtifact)
assert.NotNil(t, urlReader)
assert.Nil(t, err)
data, err := urlReader.Read()
assert.NotNil(t, data)
assert.Nil(t, err)
}

0 comments on commit 38d6adc

Please sign in to comment.