Skip to content

Commit

Permalink
Add env handler
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprodan committed Sep 9, 2018
1 parent 1d35304 commit 54f6d9f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Web API:
* `GET /status/{code}` returns the status code
* `GET /panic` crashes the process with exit code 255
* `POST /echo` forwards the call to the backend service and echos the posted content
* `GET /env` returns the environment variables as a JSON array
* `GET /headers` returns a JSON with the request HTTP headers
* `GET /delay/{seconds}` waits for the specified period
* `GET /configs` returns a JSON with configmaps and/or secrets mounted in the `config` volume
Expand Down
10 changes: 6 additions & 4 deletions docs/8-istio-openfaas.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ spec:
- "istio.example.com"
```

### Configure OpenFaaS
### Configure OpenFaaS mTLS and access policies

Create the OpenFaaS namespaces:

Expand Down Expand Up @@ -386,13 +386,14 @@ echo $password | faas-cli login -g https://openfaas.istio.example.com -u admin -

### Canary deployments for OpenFaaS functions

General available release v1.0.0:
Create a general available release for the `env` function version 1.0.0:

```yaml
apiVersion: openfaas.com/v1alpha2
kind: Function
metadata:
name: env
namespace: openfaas-fn
spec:
name: env
image: stefanprodan/of-env:1.0.0
Expand All @@ -405,13 +406,14 @@ spec:
cpu: "100m"
```

Canary release v1.1.0:
Create a canary release for version 1.1.0:

```yaml
apiVersion: openfaas.com/v1alpha2
kind: Function
metadata:
name: env-canaray
namespace: openfaas-fn
spec:
name: env-canaray
image: stefanprodan/of-env:1.1.0
Expand All @@ -424,7 +426,7 @@ spec:
cpu: "100m"
```

Istio virtual service with 10% traffic going to canary:
Create an Istio virtual service with 10% traffic going to canary:

```yaml
apiVersion: networking.istio.io/v1alpha3
Expand Down
11 changes: 11 additions & 0 deletions pkg/api/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package api

import (
"net/http"

"os"
)

func (s *Server) envHandler(w http.ResponseWriter, r *http.Request) {
s.JSONResponse(w, r, os.Environ())
}
35 changes: 35 additions & 0 deletions pkg/api/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package api

import (
"net/http"
"net/http/httptest"
"regexp"
"testing"
)

func TestEnvHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/api/env", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
srv := NewMockServer()
handler := http.HandlerFunc(srv.infoHandler)

handler.ServeHTTP(rr, req)

// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}

// Check the response body is what we expect.
expected := ".*hostname.*"
r := regexp.MustCompile(expected)
if !r.MatchString(rr.Body.String()) {
t.Fatalf("handler returned unexpected body:\ngot \n%v \nwant \n%s",
rr.Body.String(), expected)
}
}
1 change: 1 addition & 0 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (s *Server) registerHandlers() {
s.router.HandleFunc("/", s.infoHandler).Methods("GET")
s.router.HandleFunc("/version", s.versionHandler).Methods("GET")
s.router.HandleFunc("/echo", s.echoHandler).Methods("POST")
s.router.HandleFunc("/env", s.envHandler).Methods("GET", "POST")
s.router.HandleFunc("/headers", s.echoHeadersHandler).Methods("GET", "POST")
s.router.HandleFunc("/delay/{wait:[0-9]+}", s.delayHandler).Methods("GET").Name("delay")
s.router.HandleFunc("/healthz", s.healthzHandler).Methods("GET")
Expand Down

0 comments on commit 54f6d9f

Please sign in to comment.