Skip to content

Commit

Permalink
Bump API v2 comprehensively (#743)
Browse files Browse the repository at this point in the history
* switch external/internal data format into unified json

* Fix format and vendor

* Fix integration test

* Fix more race condition

* Use spectool to wrap all possible json package

* Fix wasm error

* revert dynamic object

* Support accept header for system APIs

* rename spectool to codectool

* fix format

* use vendor megaease yaml

* Fix wasm compilation error
  • Loading branch information
xxx7xxxx authored Aug 4, 2022
1 parent 06e096c commit 27f1cb0
Show file tree
Hide file tree
Showing 166 changed files with 2,080 additions and 2,259 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ build/cache
build/bin

easegress.pid
running_objects.json

*.exe

running_objects.yaml
nacos/
nacos/
2 changes: 1 addition & 1 deletion build/test/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

func makeURL(template string, a ...interface{}) string {
return "https://127.0.0.1:12381/apis/v1" + fmt.Sprintf(template, a...)
return "https://127.0.0.1:12381/apis/v2" + fmt.Sprintf(template, a...)
}

func successfulStatusCode(code int) bool {
Expand Down
71 changes: 36 additions & 35 deletions build/test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ func TestPipeline(t *testing.T) {

// fail to create Pipeline because of invalid yaml
yamlStr := `
name: pipeline-fail
kind: Pipeline
name: pipeline-fail
kind: Pipeline
flow:
- filter: proxy
- filter: proxy
filters:
- name: proxy
kind: Proxy
- name: proxy
kind: Proxy
pools:
- servers:
- url: 127.0.0.1:8888
Expand All @@ -54,27 +54,27 @@ filters:
// pipeline-success1 and pipeline-success2
yamlStr = `
name: pipeline-success1
kind: Pipeline
kind: Pipeline
flow:
- filter: proxy
- filter: proxy
filters:
- name: proxy
kind: Proxy
- name: proxy
kind: Proxy
pools:
- servers:
- url: https://127.0.0.1:8888
- url: https://127.0.0.1:8888
`
ok, msg = createObject(t, yamlStr)
assert.True(ok, msg)

yamlStr = `
name: pipeline-success2
kind: Pipeline
name: pipeline-success2
kind: Pipeline
flow:
- filter: proxy
- filter: proxy
filters:
- name: proxy
kind: Proxy
- name: proxy
kind: Proxy
pools:
- servers:
- url: https://127.0.0.1:8888
Expand All @@ -84,22 +84,23 @@ filters:

// list Pipeline and find them by using name
ok, msg = listObject(t)
fmt.Printf("ok %v, msg %s\n", ok, msg)
assert.True(ok)
assert.True(strings.Contains(msg, "name: pipeline-success2"))
assert.True(strings.Contains(msg, "name: pipeline-success1"))
assert.True(strings.Contains(msg, `"name":"pipeline-success2"`))
assert.True(strings.Contains(msg, `"name":"pipeline-success1"`))

// update Pipeline and use list to find it
yamlStr = `
name: pipeline-success2
kind: Pipeline
name: pipeline-success2
kind: Pipeline
flow:
- filter: proxy
- filter: proxy
filters:
- name: proxy
kind: Proxy
- name: proxy
kind: Proxy
pools:
- servers:
- url: https://update-pipeline-success2:8888
- url: https://update-pipeline-success2:8888
`
ok, msg = updateObject(t, "pipeline-success2", yamlStr)
assert.True(ok, msg)
Expand All @@ -116,16 +117,16 @@ filters:

ok, msg = listObject(t)
assert.True(ok)
assert.False(strings.Contains(msg, "name: pipeline-success1"))
assert.False(strings.Contains(msg, "name: pipeline-success2"))
assert.False(strings.Contains(msg, `"name":"pipeline-success1"`))
assert.False(strings.Contains(msg, `"name":"pipeline-success2"`))
}

func TestHTTPServer(t *testing.T) {
assert := assert.New(t)

// fail to create HTTPServer because of invalid yaml
yamlStr := `
name: httpserver-fail
name: httpserver-fail
kind: HTTPServer
`
ok, msg := createObject(t, yamlStr)
Expand All @@ -148,7 +149,7 @@ rules:
// list HTTPServer and find it by name
ok, msg = listObject(t)
assert.True(ok)
assert.True(strings.Contains(msg, "name: httpserver-success"))
assert.True(strings.Contains(msg, `"name":"httpserver-success"`))

// update HTTPServer and use list to find it
yamlStr = `
Expand All @@ -158,22 +159,22 @@ port: 10080
rules:
- paths:
- pathPrefix: /api
backend: update-httpserver-success
backend: update-httpserver-success
`
ok, msg = updateObject(t, "httpserver-success", yamlStr)
assert.True(ok, msg)

ok, msg = listObject(t)
assert.True(ok)
assert.True(strings.Contains(msg, "backend: update-httpserver-success"))
assert.True(strings.Contains(msg, `"backend":"update-httpserver-success"`))

// delete all HTTPServer
ok, msg = deleteObject(t, "httpserver-success")
assert.True(ok, msg)

ok, msg = listObject(t)
assert.True(ok)
assert.False(strings.Contains(msg, "name: httpserver-success"))
assert.False(strings.Contains(msg, `"name":"httpserver-success"`))
}

func TestHTTPServerAndPipeline(t *testing.T) {
Expand All @@ -200,15 +201,15 @@ rules:
// create pipeline
yamlStr = `
name: pipeline-test
kind: Pipeline
kind: Pipeline
flow:
- filter: proxy
- filter: proxy
filters:
- name: proxy
kind: Proxy
- name: proxy
kind: Proxy
pools:
- servers:
- url: https://127.0.0.1:8888
- url: https://127.0.0.1:8888
`
ok, msg = createObject(t, yamlStr)
assert.True(ok, msg)
Expand Down
32 changes: 20 additions & 12 deletions cmd/client/command/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ import (
"io"
"net/http"

yamljsontool "github.com/ghodss/yaml"
"github.com/megaease/easegress/pkg/util/codectool"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

type (
Expand All @@ -37,16 +36,16 @@ type (

// APIErr is the standard return of error.
APIErr struct {
Code int `yaml:"code"`
Message string `yaml:"message"`
Code int `json:"code"`
Message string `json:"message"`
}
)

// CommandlineGlobalFlags is the singleton of GlobalFlags.
var CommandlineGlobalFlags GlobalFlags

const (
apiURL = "/apis/v1"
apiURL = "/apis/v2"

healthURL = apiURL + "/healthz"

Expand Down Expand Up @@ -123,8 +122,17 @@ func successfulStatusCode(code int) bool {
return code >= 200 && code < 300
}

func handleRequest(httpMethod string, url string, reqBody []byte, cmd *cobra.Command) {
req, err := http.NewRequest(httpMethod, url, bytes.NewReader(reqBody))
func handleRequest(httpMethod string, url string, yamlBody []byte, cmd *cobra.Command) {
var jsonBody []byte
if yamlBody != nil {
var err error
jsonBody, err = codectool.YAMLToJSON(yamlBody)
if err != nil {
ExitWithErrorf("yaml %s to json failed: %v", yamlBody, err)
}
}

req, err := http.NewRequest(httpMethod, url, bytes.NewReader(jsonBody))
if err != nil {
ExitWithError(err)
}
Expand All @@ -143,7 +151,7 @@ func handleRequest(httpMethod string, url string, reqBody []byte, cmd *cobra.Com
if !successfulStatusCode(resp.StatusCode) {
msg := string(body)
apiErr := &APIErr{}
err = yaml.Unmarshal(body, apiErr)
err = codectool.Unmarshal(body, apiErr)
if err == nil {
msg = apiErr.Message
}
Expand All @@ -159,13 +167,13 @@ func printBody(body []byte) {
var output []byte
switch CommandlineGlobalFlags.OutputFormat {
case "yaml":
output = body
case "json":
var err error
output, err = yamljsontool.YAMLToJSON(body)
output, err = codectool.JSONToYAML(body)
if err != nil {
ExitWithErrorf("yaml %s to json failed: %v", body, err)
ExitWithErrorf("json %s to yaml failed: %v", body, err)
}
case "json":
output = body
}

fmt.Printf("%s", output)
Expand Down
24 changes: 12 additions & 12 deletions doc/cookbook/faas.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

## Easegress works with FaaS

* Isolation: separate Control logic and Business logic
* Isolation: separate Control logic and Business logic
* Traffic originated: Original near traffic, easier to integrate
* Resource saving: reusing Easegress+K8s machine resources.
* Pay what you used: reducing small customize features' developing and maintenance cost.
Expand Down Expand Up @@ -62,11 +62,11 @@ autoScaleType: "concurrency"
autoScaleValue: "100"
minReplica: 0
maxReplica: 1
limitCPU: "1000m"
limitMemory: "1000Mi"
requestCPU: "80m"
requestMemory: "20Mi"
requestAdaptor:
limitCPU: "1000m"
limitMemory: "1000Mi"
requestCPU: "80m"
requestMemory: "20Mi"
requestAdaptor:
header:
set:
X-Func: demo
Expand All @@ -75,15 +75,15 @@ requestAdaptor:
* Save it into `/home/easegress/function.yaml`, using command to deploy it in Easegress:

``` bash
$ curl --data-binary @/home/easegress/function.yaml -X POST -H 'Content-Type: text/vnd.yaml' https://127.0.0.1:2381/apis/v1/faas/faascontroller
$ curl --data-binary @/home/easegress/function.yaml -X POST -H 'Content-Type: text/vnd.yaml' https://127.0.0.1:2381/apis/v2/faas/faascontroller
```
**Note** this command should be run in Easegress' instance environment and 2381 is the default admin traffic port. If your Easegress instance uses different port, please change 2381 to the correct port.


1. Get the function's status, make sure it is in `active` status

``` bash
$ curl https://127.0.0.1:2381/apis/v1/faas/faascontroller/demo
$ curl https://127.0.0.1:2381/apis/v2/faas/faascontroller/demo
spec:
name: demo
image: gcr.io/knative-samples/helloworld-go
Expand Down Expand Up @@ -146,15 +146,15 @@ maxReplica: 50
1. Stop the function execution by using command

``` bash
$ curl https://127.0.0.1:2381/apis/v1/faas/faascontroller/demo/stop -X PUT
$ curl https://127.0.0.1:2381/apis/v2/faas/faascontroller/demo/stop -X PUT
```

* The function will become `inactive` then we can update the resource limitation safely.

2. Update the function's spec

``` bash
$ curl --data-binary @/home/easegress/function.yaml -X PUT -H 'Content-Type: text/vnd.yaml' https://127.0.0.1:2381/apis/v1/faas/faascontroller/demo
$ curl --data-binary @/home/easegress/function.yaml -X PUT -H 'Content-Type: text/vnd.yaml' https://127.0.0.1:2381/apis/v2/faas/faascontroller/demo
```

3. Verify the update
Expand Down Expand Up @@ -228,7 +228,7 @@ limitedMemory: "200Mi"
limitedCPU: "180m"
requireMemory: "100Mi"
requireCPU: "100m"
minReplica: 1
minReplica: 1
maxReplica: 50
```

Expand All @@ -244,6 +244,6 @@ limitedMemory: "200Mi"
limitedCPU: "180m"
requireMemory: "100Mi"
requireCPU: "100m"
minReplica: 0
minReplica: 0
maxReplica: 50
```
Loading

0 comments on commit 27f1cb0

Please sign in to comment.