Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Edward Wilde <[email protected]>
  • Loading branch information
ewilde committed May 4, 2019
0 parents commit 3606d67
Show file tree
Hide file tree
Showing 571 changed files with 391,331 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.idea
.vscode
examples/
dist
faas-inmemory
dive.log
deployment/k8s/yaml/gateway-dep.yml
72 changes: 72 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
GO_FILES?=$$(find . -name '*.go' |grep -v vendor)
TAG?=latest
SQUASH?=false

default: install build-resources lint vet build test testacc

.PHONY: test
test: goimportscheck
go test -v . .

.PHONY: testacc
testacc: goimportscheck
go test -count=1 -v . -run="TestAcc" -timeout 20m

.PHONY: build
build:
docker build -t ewilde/faas-inmemory:$(TAG) . --squash=${SQUASH}

.PHONY: build-local
build-local:
go build --ldflags "-s -w \
-X github.com/ewilde/faas-inmemory/version.GitCommitSHA=${GIT_COMMIT_SHA} \
-X \"github.com/ewilde/faas-inmemory/version.GitCommitMessage=${GIT_COMMIT_MESSAGE}\" \
-X github.com/ewilde/faas-inmemory/version.Version=${VERSION}" \
-o faas-inmemory .

.PHONY: up-local-deps
up-local-deps:
docker-compose -f./docker-compose.local.yml up -d

.PHONY: up-local
up-local: build-local
-pkill faas-inmemory
docker-compose -f ./docker-compose.local.yml up -d
env port=8081 ./faas-inmemory

.PHONY: release
release:
go get github.com/goreleaser/goreleaser; \
goreleaser; \

.PHONY: clean
clean:
rm -rf pkg/

.PHONY: goimports
goimports:
goimports -w $(GO_FILES)

.PHONY: goimportscheck
goimportscheck:
@sh -c "'$(CURDIR)/scripts/goimportscheck.sh'"

.PHONY: vet
vet:
@echo "go vet ."
@go vet $$(go list ./... | grep -v vendor/) ; if [ $$? -eq 1 ]; then \
echo ""; \
echo "Vet found suspicious constructs. Please check the reported constructs"; \
echo "and fix them if necessary before submitting the code for review."; \
exit 1; \
fi

.PHONY: lint
lint:
@echo "golint ."
@golint -set_exit_status $$(go list ./... | grep -v vendor/) ; if [ $$? -eq 1 ]; then \
echo ""; \
echo "Lint found errors in the source code. Please check the reported errors"; \
echo "and fix them if necessary before submitting the code for review."; \
exit 1; \
fi
38 changes: 38 additions & 0 deletions handlers/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Edward Wilde 2018. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package handlers

import (
"encoding/json"
"github.com/openfaas/faas/gateway/requests"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
)

// MakeDeleteHandler delete a function
func MakeDeleteHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Info("deletsirupsene request")
defer r.Body.Close()

body, _ := ioutil.ReadAll(r.Body)
request := requests.DeleteFunctionRequest{}
if err := json.Unmarshal(body, &request); err != nil {
log.Errorln(err)
w.WriteHeader(http.StatusBadRequest)
return
}

if len(request.FunctionName) == 0 {
log.Errorln("can not delete a function, request function name is empty")
w.WriteHeader(http.StatusBadRequest)
return
}

delete(functions, request.FunctionName)

log.Infof("delete request %s successful", request.FunctionName)
}
}
44 changes: 44 additions & 0 deletions handlers/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Edward Wilde 2018. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package handlers

import (
"encoding/json"
"github.com/openfaas/faas/gateway/requests"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
)

// watchdogPort for the OpenFaaS function watchdog
const watchdogPort = 8080

// initialReplicasCount how many replicas to start of creating for a function
const initialReplicasCount = 1

var functions = map[string]*requests.Function{}

// MakeDeployHandler creates a handler to create new functions in the cluster
func MakeDeployHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

log.Info("deployment request")
defer r.Body.Close()

body, _ := ioutil.ReadAll(r.Body)

request := requests.CreateFunctionRequest{}
if err := json.Unmarshal(body, &request); err != nil {
log.Errorln("error during unmarshal of create function request. ", err)
w.WriteHeader(http.StatusBadRequest)
return
}

functions[request.Service] = createToRequest(request)

log.Infof("deployment request for function %s", request.Service)

w.WriteHeader(http.StatusOK)
}
}
16 changes: 16 additions & 0 deletions handlers/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package handlers

import (
"net/http"

log "github.com/sirupsen/logrus"
)

// MakeHealthHandler returns 200/OK when healthy
func MakeHealthHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
log.Info("health check request")
w.WriteHeader(http.StatusOK)
}
}
47 changes: 47 additions & 0 deletions handlers/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package handlers

import (
"encoding/json"
"net/http"

"github.com/openfaas/faas-provider/types"
log "github.com/sirupsen/logrus"
)

const (
//OrchestrationIdentifier identifier string for provider orchestration
OrchestrationIdentifier = "inmemory"
//ProviderName name of the provider
ProviderName = "faas-inmemory"
)

//MakeInfoHandler creates handler for /system/info endpoint
func MakeInfoHandler(version, sha string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Body != nil {
defer r.Body.Close()
}

log.Info("info request")

infoRequest := types.InfoRequest{
Orchestration: OrchestrationIdentifier,
Provider: ProviderName,
Version: types.ProviderVersion{
Release: version,
SHA: sha,
},
}

jsonOut, marshalErr := json.Marshal(infoRequest)
if marshalErr != nil {
log.Error("Error during unmarshal of info request ", marshalErr)
w.WriteHeader(http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonOut)
}
}
24 changes: 24 additions & 0 deletions handlers/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package handlers

import (
"github.com/gorilla/mux"
"net/http"
)

// MakeProxy creates a proxy for HTTP web requests which can be routed to a function.
func MakeProxy() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]

v, okay := functions[name]
if !okay {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("{ \"status\" : \"Not found\"}"))
}

v.InvocationCount = v.InvocationCount + 1
responseBody := "{ \"status\" : \"Okay\"}"
w.Write([]byte(responseBody))
}
}
54 changes: 54 additions & 0 deletions handlers/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) Edward Wilde 2018. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package handlers

import (
"encoding/json"
"net/http"

"github.com/openfaas/faas/gateway/requests"
log "github.com/sirupsen/logrus"
)

// MakeFunctionReader handler for reading functions deployed in the cluster as deployments.
func MakeFunctionReader() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

log.Info("read request")
functions, err := readServices()
if err != nil {
log.Printf("Error getting service list: %s\n", err.Error())

w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

functionBytes, _ := json.Marshal(functions)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(functionBytes)
}
}

func readServices() ([]*requests.Function, error) {
var list []*requests.Function
for _, v := range functions{
list = append(list, v)
}

return list, nil
}

func createToRequest(request requests.CreateFunctionRequest) *requests.Function {
return &requests.Function{
Name: request.Service,
Annotations: request.Annotations,
EnvProcess: request.EnvProcess,
Image: request.Image,
Labels: request.Labels,
AvailableReplicas: 1,
Replicas: 1,
}
}
40 changes: 40 additions & 0 deletions handlers/replicas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Edward Wilde 2018. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package handlers

import (
"encoding/json"
"github.com/openfaas/faas/gateway/requests"
"net/http"

"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)

// MakeReplicaUpdater updates desired count of replicas
func MakeReplicaUpdater() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Info("update replicas, nothing to do here")

}
}

// MakeReplicaReader reads the amount of replicas for a deployment
func MakeReplicaReader() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Info("read replicas")

vars := mux.Vars(r)
functionName := vars["name"]

var found *requests.Function
found.Name = functionName
found.AvailableReplicas = 1

functionBytes, _ := json.Marshal(found)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
w.Write(functionBytes)
}
}
36 changes: 36 additions & 0 deletions handlers/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package handlers

import (
"encoding/json"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"

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

// MakeUpdateHandler update specified function
func MakeUpdateHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Info("update request")

defer r.Body.Close()

body, _ := ioutil.ReadAll(r.Body)

request := requests.CreateFunctionRequest{}
if err := json.Unmarshal(body, &request); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

_, okay := functions[request.Service]
if !okay {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("{ \"status\" : \"Not found\"}"))
return
}

functions[request.Service] = createToRequest(request)
}
}
Loading

0 comments on commit 3606d67

Please sign in to comment.