diff --git a/README.md b/README.md index 66fa8ff1..fc10bca8 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docs/8-istio-openfaas.md b/docs/8-istio-openfaas.md index 0768133c..329854a9 100644 --- a/docs/8-istio-openfaas.md +++ b/docs/8-istio-openfaas.md @@ -171,7 +171,7 @@ spec: - "istio.example.com" ``` -### Configure OpenFaaS +### Configure OpenFaaS mTLS and access policies Create the OpenFaaS namespaces: @@ -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 @@ -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 @@ -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 diff --git a/pkg/api/env.go b/pkg/api/env.go new file mode 100644 index 00000000..95384ff5 --- /dev/null +++ b/pkg/api/env.go @@ -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()) +} diff --git a/pkg/api/env_test.go b/pkg/api/env_test.go new file mode 100644 index 00000000..0d53ce68 --- /dev/null +++ b/pkg/api/env_test.go @@ -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) + } +} diff --git a/pkg/api/server.go b/pkg/api/server.go index b1199a15..71024e72 100644 --- a/pkg/api/server.go +++ b/pkg/api/server.go @@ -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")