Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update deletion error handling #192

Merged
merged 2 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ build:
build_redist:
./build_redist.sh

unit_test:
go test $(shell go list ./... | grep -v /vendor/ | grep -v /template/) -cover
test-unit:
go test $(shell go list ./... | grep -v /vendor/ | grep -v /template/ | grep -v build) -cover
21 changes: 15 additions & 6 deletions proxy/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (
)

// DeleteFunction delete a function from the FaaS server
func DeleteFunction(gateway string, functionName string) {
func DeleteFunction(gateway string, functionName string) error {
var err error

gateway = strings.TrimRight(gateway, "/")
delReq := requests.DeleteFunctionRequest{FunctionName: functionName}
reqBytes, _ := json.Marshal(&delReq)
Expand All @@ -25,9 +27,10 @@ func DeleteFunction(gateway string, functionName string) {
req, _ := http.NewRequest("DELETE", gateway+"/system/functions", reader)
req.Header.Set("Content-Type", "application/json")
delRes, delErr := c.Do(req)

if delErr != nil {
fmt.Printf("Error removing existing function: %s, gateway=%s, functionName=%s\n", delErr.Error(), gateway, functionName)
return
return delErr
}

if delRes.Body != nil {
Expand All @@ -36,13 +39,19 @@ func DeleteFunction(gateway string, functionName string) {

switch delRes.StatusCode {
case 200, 201, 202:
fmt.Println("Removing old service.")
fmt.Println("Removing old function.")
case 404:
fmt.Println("No existing service to remove")
fmt.Println("No existing function to remove")
default:
bytesOut, err := ioutil.ReadAll(delRes.Body)
if err == nil {
var bodyReadErr error
bytesOut, bodyReadErr := ioutil.ReadAll(delRes.Body)
if bodyReadErr != nil {
err = bodyReadErr
} else {
err = fmt.Errorf("server returned unexpected status code %d %s", delRes.StatusCode, string(bytesOut))
fmt.Println("Server returned unexpected status code", delRes.StatusCode, string(bytesOut))
}
}

return err
}
8 changes: 4 additions & 4 deletions proxy/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func Test_DeleteFunction(t *testing.T) {
DeleteFunction(s.URL, "function-to-delete")
})

r := regexp.MustCompile(`(?m:Removing old service.)`)
r := regexp.MustCompile(`(?m:Removing old function.)`)
if !r.MatchString(stdout) {
t.Fatalf("Output not matched: %s", stdout)
t.Fatalf("Want: %s, got: %s", "Removing old function", stdout)
}
}

Expand All @@ -35,9 +35,9 @@ func Test_DeleteFunction_404(t *testing.T) {
DeleteFunction(s.URL, "function-to-delete")
})

r := regexp.MustCompile(`(?m:No existing service to remove)`)
r := regexp.MustCompile(`(?m:No existing function to remove)`)
if !r.MatchString(stdout) {
t.Fatalf("Output not matched: %s", stdout)
t.Fatalf("Want: %s, got: %s", "No existing function to remove", stdout)
}
}

Expand Down
8 changes: 7 additions & 1 deletion proxy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"strings"
"time"

"os"

"github.com/openfaas/faas/gateway/requests"
)

Expand Down Expand Up @@ -39,7 +41,11 @@ func DeployFunction(fprocess string, gateway string, functionName string, image
gateway = strings.TrimRight(gateway, "/")

if replace {
DeleteFunction(gateway, functionName)
if deleteError := DeleteFunction(gateway, functionName); deleteError != nil {
fmt.Printf("Error while deleting function, so skipping deployment. %s\n", deleteError)
os.Exit(-1)
return
}
}

req := requests.CreateFunctionRequest{
Expand Down