Skip to content

Commit

Permalink
chore: discontinue using ioutil (#1966)
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Wang <whynowy@gmail.com>
  • Loading branch information
whynowy committed Jun 9, 2022
1 parent d0f66db commit eaabcb6
Show file tree
Hide file tree
Showing 55 changed files with 190 additions and 185 deletions.
7 changes: 3 additions & 4 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/json"
"fmt"
"hash/fnv"
"io/ioutil"
"net/http"
"os"
"reflect"
Expand Down Expand Up @@ -151,7 +150,7 @@ func GetSecretFromVolume(selector *v1.SecretKeySelector) (string, error) {
if err != nil {
return "", err
}
data, err := ioutil.ReadFile(filePath)
data, err := os.ReadFile(filePath)
if err != nil {
return "", errors.Wrapf(err, "failed to get secret value of name: %s, key: %s", selector.Name, selector.Key)
}
Expand All @@ -175,7 +174,7 @@ func GetConfigMapFromVolume(selector *v1.ConfigMapKeySelector) (string, error) {
if err != nil {
return "", err
}
data, err := ioutil.ReadFile(filePath)
data, err := os.ReadFile(filePath)
if err != nil {
return "", errors.Wrapf(err, "failed to get configMap value of name: %s, key: %s", selector.Name, selector.Key)
}
Expand Down Expand Up @@ -259,7 +258,7 @@ func GetTLSConfig(config *apicommon.TLSConfig) (*tls.Config, error) {

c := &tls.Config{}
if len(caCertPath) > 0 {
caCert, err := ioutil.ReadFile(caCertPath)
caCert, err := os.ReadFile(caCertPath)
if err != nil {
return nil, errors.Wrapf(err, "failed to read ca cert file %s", caCertPath)
}
Expand Down
15 changes: 9 additions & 6 deletions controllers/sensor/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package sensor

import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"

Expand All @@ -31,14 +31,17 @@ import (

func TestValidateSensor(t *testing.T) {
dir := "../../examples/sensors"
files, dirErr := ioutil.ReadDir(dir)
require.NoError(t, dirErr)
dirEntries, err := os.ReadDir(dir)
require.NoError(t, err)

for _, file := range files {
for _, entry := range dirEntries {
if entry.IsDir() {
continue
}
t.Run(
fmt.Sprintf("test example load: %s/%s", dir, file.Name()),
fmt.Sprintf("test example load: %s/%s", dir, entry.Name()),
func(t *testing.T) {
content, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", dir, file.Name()))
content, err := os.ReadFile(fmt.Sprintf("%s/%s", dir, entry.Name()))
assert.NoError(t, err)

var sensor *v1alpha1.Sensor
Expand Down
86 changes: 42 additions & 44 deletions docs/sensors/triggers/http-trigger.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Argo Events offers HTTP trigger which can easily invoke serverless functions lik
<br/>

## Specification

The HTTP trigger specification is available [here](https://github.com/argoproj/argo-events/blob/master/api/sensor.md#httptrigger).

## REST API Calls
Expand All @@ -21,21 +22,20 @@ Consider a scenario where your REST API server needs to consume events from even
the integration yourself in the server code, although server logic has nothing to do any of the event-sources. This is where Argo Events HTTP trigger
can help. The HTTP trigger takes the task of consuming events from event-sources away from API server and seamlessly integrates these events via REST API calls.


We will set up a basic go http server and connect it with the Minio events.

1. The HTTP server simply prints the request body as follows.
1. The HTTP server simply prints the request body as follows.

package main

import (
"fmt"
"io/ioutil"
"io"
"net/http"
)

func hello(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
body, err := io.ReadAll(req.Body)
if err != nil {
fmt.Printf("%+v\n", err)
return
Expand All @@ -50,35 +50,34 @@ We will set up a basic go http server and connect it with the Minio events.
http.ListenAndServe(":8090", nil)
}

2. Deploy the HTTP server.
2. Deploy the HTTP server.

kubectl -n argo-events apply -f https://raw.githubusercontent.com/argoproj/argo-events/stable/examples/tutorials/09-http-trigger/http-server.yaml

3. Create a service to expose the http server.
3. Create a service to expose the http server.

kubectl -n argo-events apply -f https://raw.githubusercontent.com/argoproj/argo-events/stable/examples/tutorials/09-http-trigger/http-server-svc.yaml

4. Either use Ingress, OpenShift Route or port-forwarding to expose the http server.
4. Either use Ingress, OpenShift Route or port-forwarding to expose the http server.

kubectl -n argo-events port-forward <http-server-pod-name> 8090:8090

5. Our goals is to seamlessly integrate Minio S3 bucket notifications with REST API server created in previous step. So,
lets set up the Minio event-source available [here](https://argoproj.github.io/argo-events/setup/minio/).
Don't create the sensor as we will be deploying it in next step.
5. Our goals is to seamlessly integrate Minio S3 bucket notifications with REST API server created in previous step. So,
lets set up the Minio event-source available [here](https://argoproj.github.io/argo-events/setup/minio/).
Don't create the sensor as we will be deploying it in next step.

6. Create a sensor as follows.
6. Create a sensor as follows.

kubectl apply -n argo-events -f https://raw.githubusercontent.com/argoproj/argo-events/stable/examples/sensors/http-trigger.yaml

7. Now, drop a file onto `input` bucket in Minio server.

7. Now, drop a file onto `input` bucket in Minio server.

8. The sensor has triggered a http request to the http server. Take a look at the logs.
8. The sensor has triggered a http request to the http server. Take a look at the logs.

server is listening on 8090
{"type":"minio","bucket":"input"}

9. Great!!!
9. Great!!!

### Request Payload

Expand Down Expand Up @@ -122,6 +121,7 @@ you want to define a generic trigger template in the sensor and populate values
You can learn more about trigger parameterization [here](https://argoproj.github.io/argo-events/tutorials/02-parameterization/).

### Policy

Trigger policy helps you determine the status of the HTTP request and decide whether to stop or continue sensor.

To determine whether the HTTP request was successful or not, the HTTP trigger provides a `Status` policy.
Expand Down Expand Up @@ -155,32 +155,30 @@ The above HTTP trigger will be treated successful only if the HTTP request retur
OpenFaaS offers a simple way to spin up serverless functions. Lets see how we can leverage Argo Events HTTP trigger
to invoke OpenFaaS function.

1. If you don't have OpenFaaS installed, follow the [instructions](https://docs.openfaas.com/deployment/kubernetes/).

2. Let's create a basic function. You can follow the [steps](https://blog.alexellis.io/serverless-golang-with-openfaas/).
to set up the function.
1. If you don't have OpenFaaS installed, follow the [instructions](https://docs.openfaas.com/deployment/kubernetes/).

2. Let's create a basic function. You can follow the [steps](https://blog.alexellis.io/serverless-golang-with-openfaas/).
to set up the function.

package function
package function

import (
"fmt"
)

// Handle a serverless request
func Handle(req []byte) string {
return fmt.Sprintf("Hello, Go. You said: %s", string(req))
}
import (
"fmt"
)

// Handle a serverless request
func Handle(req []byte) string {
return fmt.Sprintf("Hello, Go. You said: %s", string(req))
}

3. Make sure the function pod is up and running.
3. Make sure the function pod is up and running.

4. We are going to invoke OpenFaaS function on a message on Redis Subscriber.
4. We are going to invoke OpenFaaS function on a message on Redis Subscriber.

5. Let's set up the Redis Database, Redis PubSub event-source as specified [here](https://argoproj.github.io/argo-events/setup/redis/).
Do not create the Redis sensor, we are going to create it in next step.
5. Let's set up the Redis Database, Redis PubSub event-source as specified [here](https://argoproj.github.io/argo-events/setup/redis/).
Do not create the Redis sensor, we are going to create it in next step.

6. Let's create the sensor with OpenFaaS trigger.
6. Let's create the sensor with OpenFaaS trigger.

apiVersion: argoproj.io/v1alpha1
kind: Sensor
Expand All @@ -202,32 +200,32 @@ to invoke OpenFaaS function.
dest: bucket
method: POST

7. Publish a message on `FOO` channel using `redis-cli`.
7. Publish a message on `FOO` channel using `redis-cli`.

PUBLISH FOO hello

8. As soon as you publish the message, the sensor will invoke the OpenFaaS function `gohash`.
8. As soon as you publish the message, the sensor will invoke the OpenFaaS function `gohash`.

## Kubeless

Similar to REST API calls, you can easily invoke Kubeless functions using HTTP trigger.

1. If you don't have Kubeless installed, follow the [installation](https://kubeless.io/docs/quick-start/).
1. If you don't have Kubeless installed, follow the [installation](https://kubeless.io/docs/quick-start/).

2. Lets create a basic function.
2. Lets create a basic function.

def hello(event, context):
print event
return event['data']

3. Make sure the function pod and service is created.
3. Make sure the function pod and service is created.

4. Now, we are going to invoke the Kubeless function when a message is placed on a NATS queue.
4. Now, we are going to invoke the Kubeless function when a message is placed on a NATS queue.

5. Let's set up the NATS event-source. Follow [instructions](https://argoproj.github.io/argo-events/setup/nats/#setup) for details.
Do not create the NATS sensor, we are going to create it in next step.
5. Let's set up the NATS event-source. Follow [instructions](https://argoproj.github.io/argo-events/setup/nats/#setup) for details.
Do not create the NATS sensor, we are going to create it in next step.

6. Let's create NATS sensor with HTTP trigger.
6. Let's create NATS sensor with HTTP trigger.

apiVersion: argoproj.io/v1alpha1
kind: Sensor
Expand All @@ -254,11 +252,11 @@ Similar to REST API calls, you can easily invoke Kubeless functions using HTTP t
dest: last_name
method: POST

7. Once event-source and sensor pod are up and running, dispatch a message on `foo` subject using nats client.
7. Once event-source and sensor pod are up and running, dispatch a message on `foo` subject using nats client.

go run main.go -s localhost foo '{"first_name": "foo", "last_name": "bar"}'

8. It will invoke Kubeless function `hello`.
8. It will invoke Kubeless function `hello`.

{'event-time': None, 'extensions': {'request': <LocalRequest: POST http://hello.kubeless.svc.cluster.local:8080/> }, 'event-type': None, 'event-namespace': None, 'data': '{"first_name":"foo","last_name":"bar"}', 'event-id': None}

Expand Down
13 changes: 6 additions & 7 deletions eventsources/common/naivewatcher/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package naivewatcher

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"syscall"
Expand Down Expand Up @@ -41,7 +40,7 @@ func TestWatcherAutoCheck(t *testing.T) {
}
defer watcher.Close()

tmpdir, err := ioutil.TempDir("", "naive-watcher-")
tmpdir, err := os.MkdirTemp("", "naive-watcher-")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -85,7 +84,7 @@ func TestWatcherAutoCheck(t *testing.T) {
}, events)

// Write a file
err = ioutil.WriteFile(filepath.Join(tmpdir, "bar"), []byte("wow"), 0666)
err = os.WriteFile(filepath.Join(tmpdir, "bar"), []byte("wow"), 0666)
if err != nil {
t.Fatal(err)
}
Expand All @@ -111,7 +110,7 @@ func TestWatcherAutoCheck(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(filepath.Join(tmpdir, "foo"), []byte("wowwow"), 0666)
err = os.WriteFile(filepath.Join(tmpdir, "foo"), []byte("wowwow"), 0666)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -158,7 +157,7 @@ func TestWatcherManualCheck(t *testing.T) {
}
defer watcher.Close()

tmpdir, err := ioutil.TempDir("", "naive-watcher-")
tmpdir, err := os.MkdirTemp("", "naive-watcher-")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -193,7 +192,7 @@ func TestWatcherManualCheck(t *testing.T) {
}, events)

// Write a file
err = ioutil.WriteFile(filepath.Join(tmpdir, "bar"), []byte("wow"), 0666)
err = os.WriteFile(filepath.Join(tmpdir, "bar"), []byte("wow"), 0666)
if err != nil {
t.Fatal(err)
}
Expand All @@ -217,7 +216,7 @@ func TestWatcherManualCheck(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(filepath.Join(tmpdir, "foo"), []byte("wowwow"), 0666)
err = os.WriteFile(filepath.Join(tmpdir, "foo"), []byte("wowwow"), 0666)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions eventsources/sources/amqp/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package amqp
import (
"context"
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/argoproj/argo-events/eventsources/sources"
Expand All @@ -35,7 +35,7 @@ func TestValidateEventSource(t *testing.T) {
assert.Error(t, err)
assert.Equal(t, "either url or urlSecret must be specified", err.Error())

content, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", sources.EventSourceDir, "amqp.yaml"))
content, err := os.ReadFile(fmt.Sprintf("%s/%s", sources.EventSourceDir, "amqp.yaml"))
assert.Nil(t, err)

var eventSource *v1alpha1.EventSource
Expand Down
7 changes: 4 additions & 3 deletions eventsources/sources/awssns/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"encoding/base64"
"encoding/json"
"encoding/pem"
"io/ioutil"
"io"
"net/http"
"net/url"
"reflect"
Expand Down Expand Up @@ -115,7 +115,8 @@ func (router *Router) HandleRoute(writer http.ResponseWriter, request *http.Requ
route.Metrics.EventProcessingDuration(route.EventSourceName, route.EventName, float64(time.Since(start)/time.Millisecond))
}(time.Now())

body, err := ioutil.ReadAll(request.Body)
request.Body = http.MaxBytesReader(writer, request.Body, 65536)
body, err := io.ReadAll(request.Body)
if err != nil {
logger.Errorw("failed to parse the request body", zap.Error(err))
common.SendErrorResponse(writer, err.Error())
Expand Down Expand Up @@ -318,7 +319,7 @@ func (m *httpNotification) verify() error {
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(io.LimitReader(res.Body, 65536))
if err != nil {
return errors.Wrap(err, "failed to read signing cert body")
}
Expand Down
4 changes: 2 additions & 2 deletions eventsources/sources/awssns/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package awssns
import (
"context"
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/ghodss/yaml"
Expand All @@ -36,7 +36,7 @@ func TestValidateEventSource(t *testing.T) {
assert.Error(t, err)
assert.Equal(t, "must specify topic arn", err.Error())

content, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", sources.EventSourceDir, "aws-sns.yaml"))
content, err := os.ReadFile(fmt.Sprintf("%s/%s", sources.EventSourceDir, "aws-sns.yaml"))
assert.Nil(t, err)

var eventSource *v1alpha1.EventSource
Expand Down
Loading

0 comments on commit eaabcb6

Please sign in to comment.