From 04be17ce49ed24ddd5e325195d426aaf475bd79f Mon Sep 17 00:00:00 2001 From: Alex Ellis Date: Fri, 31 Mar 2017 16:15:02 +0000 Subject: [PATCH] Add marshall_request to watchdog for passing header to functions --- watchdog/.gitignore | 1 + watchdog/Dockerfile | 12 ++++++----- watchdog/Dockerfile.armhf | 12 ++++++----- watchdog/build.armhf.sh | 3 +-- watchdog/build.sh | 2 +- watchdog/main.go | 33 +++++++++++++++++++++--------- watchdog/readconfig.go | 4 ++++ watchdog/types/types.go | 43 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 87 insertions(+), 23 deletions(-) create mode 100644 watchdog/types/types.go diff --git a/watchdog/.gitignore b/watchdog/.gitignore index cd8ffecc..745147fe 100644 --- a/watchdog/.gitignore +++ b/watchdog/.gitignore @@ -1,2 +1,3 @@ fwatchdog watchdog +fwatchdog-armhf diff --git a/watchdog/Dockerfile b/watchdog/Dockerfile index d01a5ca7..c5b20d51 100644 --- a/watchdog/Dockerfile +++ b/watchdog/Dockerfile @@ -1,13 +1,15 @@ FROM golang:1.7.5 -RUN mkdir -p /go/src/app -WORKDIR /go/src/app +RUN mkdir -p /go/src/github.com/alexellis/faas/watchdog +WORKDIR /go/src/github.com/alexellis/faas/watchdog + COPY main.go . COPY readconfig.go . COPY config_test.go . +COPY requesthandler_test.go . +COPY types types -WORKDIR /go/src/app -RUN go get -d -v +#RUN go get -d -v RUN go test -RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o watchdog . diff --git a/watchdog/Dockerfile.armhf b/watchdog/Dockerfile.armhf index cc9a2502..ecbf6ce2 100644 --- a/watchdog/Dockerfile.armhf +++ b/watchdog/Dockerfile.armhf @@ -1,12 +1,14 @@ FROM alexellis2/go-armhf:1.7.4 -RUN mkdir -p /go/src/app -WORKDIR /go/src/app +RUN mkdir -p /go/src/github.com/alexellis/faas/watchdog +WORKDIR /go/src/github.com/alexellis/faas/watchdog + COPY main.go . COPY readconfig.go . COPY config_test.go . +COPY requesthandler_test.go . +COPY types types -WORKDIR /go/src/app -RUN go get -d -v +#RUN go get -d -v RUN go test -RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o watchdog . diff --git a/watchdog/build.armhf.sh b/watchdog/build.armhf.sh index bc47c2be..b732fbc9 100755 --- a/watchdog/build.armhf.sh +++ b/watchdog/build.armhf.sh @@ -10,6 +10,5 @@ docker build --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_p docker create --name buildoutput functions/watchdog:build-armhf echo -docker cp buildoutput:/go/src/app/app ./fwatchdog-armhf +docker cp buildoutput:/go/src/github.com/alexellis/faas/watchdog/watchdog ./fwatchdog-armhf docker rm buildoutput - diff --git a/watchdog/build.sh b/watchdog/build.sh index 364c445f..3cc5d5ee 100755 --- a/watchdog/build.sh +++ b/watchdog/build.sh @@ -6,5 +6,5 @@ docker build --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy \ -t functions/watchdog:build . docker create --name buildoutput functions/watchdog:build echo -docker cp buildoutput:/go/src/app/app ./fwatchdog +docker cp buildoutput:/go/src/github.com/alexellis/faas/watchdog/watchdog ./fwatchdog docker rm buildoutput diff --git a/watchdog/main.go b/watchdog/main.go index da69c2fb..5a5f8b7e 100644 --- a/watchdog/main.go +++ b/watchdog/main.go @@ -10,15 +10,25 @@ import ( "strings" "sync" "time" + + "github.com/alexellis/faas/watchdog/types" ) -// OsEnv implements interface to wrap os.Getenv -type OsEnv struct { -} +func buildFunctionInput(config *WatchdogConfig, r *http.Request) ([]byte, error) { + var res []byte + var requestBytes []byte + var err error -// Getenv wraps os.Getenv -func (OsEnv) Getenv(key string) string { - return os.Getenv(key) + defer r.Body.Close() + requestBytes, _ = ioutil.ReadAll(r.Body) + if config.marshallRequest { + marshalRes, marshallErr := types.MarshalRequest(requestBytes, &r.Header) + err = marshallErr + res = marshalRes + } else { + res = requestBytes + } + return res, err } func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request) { @@ -34,8 +44,12 @@ func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request) var wg sync.WaitGroup wg.Add(2) - res, _ = ioutil.ReadAll(r.Body) - defer r.Body.Close() + res, buildInputErr := buildFunctionInput(config, r) + if buildInputErr != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(buildInputErr.Error())) + return + } go func() { defer wg.Done() @@ -54,7 +68,6 @@ func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request) if config.writeDebug == true { log.Println(targetCmd, err) } - w.WriteHeader(500) response := bytes.NewBufferString(err.Error()) w.Write(response.Bytes()) @@ -83,7 +96,7 @@ func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http. } func main() { - osEnv := OsEnv{} + osEnv := types.OsEnv{} readConfig := ReadConfig{} config := readConfig.Read(osEnv) diff --git a/watchdog/readconfig.go b/watchdog/readconfig.go index 102a6e20..fd5a8777 100644 --- a/watchdog/readconfig.go +++ b/watchdog/readconfig.go @@ -57,6 +57,8 @@ func (ReadConfig) Read(hasEnv HasEnv) WatchdogConfig { cfg.writeDebug = parseBoolValue(hasEnv.Getenv("write_debug")) + cfg.marshallRequest = parseBoolValue(hasEnv.Getenv("marshall_request")) + return cfg } @@ -69,4 +71,6 @@ type WatchdogConfig struct { // writeDebug write console stdout statements to the container writeDebug bool + + marshallRequest bool } diff --git a/watchdog/types/types.go b/watchdog/types/types.go new file mode 100644 index 00000000..909099da --- /dev/null +++ b/watchdog/types/types.go @@ -0,0 +1,43 @@ +package types + +import ( + "encoding/json" + "net/http" + "os" +) + +// OsEnv implements interface to wrap os.Getenv +type OsEnv struct { +} + +// Getenv wraps os.Getenv +func (OsEnv) Getenv(key string) string { + return os.Getenv(key) +} + +type MarshalBody struct { + Raw []byte `json:"raw"` +} + +type MarshalReq struct { + Header http.Header `json:"header"` + Body MarshalBody `json:"body"` +} + +func UnmarshalRequest(data []byte) (*MarshalReq, error) { + request := MarshalReq{} + err := json.Unmarshal(data, &request) + return &request, err +} + +func MarshalRequest(data []byte, header *http.Header) ([]byte, error) { + req := MarshalReq{ + Body: MarshalBody{ + Raw: data, + }, + Header: *header, + } + + res, marshalErr := json.Marshal(&req) + return res, marshalErr +}