Skip to content
This repository has been archived by the owner on Feb 4, 2020. It is now read-only.

Update to faas 0.8.1 #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion handlers/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"io/ioutil"
"net/http"

"github.com/alexellis/faas/gateway/requests"
"github.com/openfaas-incubator/faas-rancher/rancher"
"github.com/openfaas/faas/gateway/requests"
)

// MakeDeleteHandler delete a function
Expand Down
2 changes: 1 addition & 1 deletion handlers/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"net/http"
"regexp"

"github.com/alexellis/faas/gateway/requests"
"github.com/openfaas-incubator/faas-rancher/rancher"
"github.com/openfaas/faas/gateway/requests"
client "github.com/rancher/go-rancher/v2"
)

Expand Down
12 changes: 12 additions & 0 deletions handlers/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package handlers

import "net/http"

// MakeHealthHandler returns 200/OK when healthy
func MakeHealthHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

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

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

"github.com/openfaas/faas-provider/types"
)

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

//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()
}

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

jsonOut, marshalErr := json.Marshal(infoRequest)
if marshalErr != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonOut)
}
}
11 changes: 6 additions & 5 deletions handlers/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"encoding/json"
"net/http"

"github.com/alexellis/faas/gateway/requests"
"github.com/openfaas-incubator/faas-rancher/rancher"
"github.com/openfaas/faas/gateway/requests"
)

// MakeFunctionReader handler for reading functions deployed in the cluster as deployments.
Expand Down Expand Up @@ -50,10 +50,11 @@ func getServiceList(client rancher.BridgeClient) ([]requests.Function, error) {
// filter to faas function services
replicas := uint64(service.Scale)
function := requests.Function{
Name: service.Name,
Replicas: replicas,
Image: service.LaunchConfig.ImageUuid,
InvocationCount: 0,
Name: service.Name,
Replicas: replicas,
AvailableReplicas: replicas,
Image: service.LaunchConfig.ImageUuid,
InvocationCount: 0,
}
functions = append(functions, function)

Expand Down
2 changes: 1 addition & 1 deletion handlers/replicas.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"net/http"
"strconv"

"github.com/alexellis/faas/gateway/requests"
"github.com/openfaas-incubator/faas-rancher/rancher"
"github.com/openfaas-incubator/faas-rancher/types"
"github.com/openfaas/faas/gateway/requests"
)

// MakeReplicaUpdater updates desired count of replicas
Expand Down
15 changes: 15 additions & 0 deletions handlers/secret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package handlers;

import (
"net/http"
)

// MakeSecretHandler makes a handler for Create/List/Delete/Update of
//secrets in the Rancher API
func MakeSecretHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.WriteHeader(http.StatusNotImplemented)
w.Write([]byte("Not implemented\n"))
}
}
126 changes: 126 additions & 0 deletions handlers/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright (c) Alex Ellis 2017, Ken Fukuyama 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package handlers

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"

"github.com/openfaas-incubator/faas-rancher/rancher"
"github.com/openfaas/faas/gateway/requests"
client "github.com/rancher/go-rancher/v2"
)

// MakeUpdateHandler creates a handler to create new functions in the cluster
func MakeUpdateHandler(client rancher.BridgeClient) VarsHandler {
return func(w http.ResponseWriter, r *http.Request, vars map[string]string) {

defer r.Body.Close()

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

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

if len(request.Service) == 0 {
w.WriteHeader(http.StatusBadRequest)
return
}

serviceSpec, findErr := client.FindServiceByName(request.Service)
if findErr != nil {
w.WriteHeader(http.StatusInternalServerError)
return
} else if serviceSpec == nil {
w.WriteHeader(http.StatusNotFound)
return
}

fmt.Println(serviceSpec.State)

if serviceSpec.State != "active" {
fmt.Println("Service to upgrade not in active state.")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Service is not active. Maybe another update is currently in progress?"))
return
}

upgradeSpec := makeUpgradeSpec(request)
_, err = client.UpgradeService(serviceSpec, upgradeSpec)
if err != nil {
fmt.Println("UPGRADE ERROR", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
go func() {
fmt.Println("Waiting for upgrade to finish")
for pollCounter := 20; pollCounter > 0; pollCounter-- {
pollResult, pollErr := client.FindServiceByName(request.Service)
fmt.Println(pollResult.State)
if pollErr != nil {
fmt.Println("POLL ERROR")
continue
}
time.Sleep(1 * time.Second)

if pollResult.State == "upgraded" {
fmt.Println("Finishing upgrade")
_, err = client.FinishUpgradeService(pollResult)
if err != nil {
fmt.Println("FINISH ERROR", err)
return
}
fmt.Println("Upgrade finished")
return

}
}
fmt.Println("Poll timeout!")
}()

fmt.Println("Updated service - " + request.Service)
w.WriteHeader(http.StatusAccepted)
}
}

func makeUpgradeSpec(request requests.CreateFunctionRequest) *client.ServiceUpgrade {

envVars := make(map[string]interface{})
for k, v := range request.EnvVars {
envVars[k] = v
}

if len(request.EnvProcess) > 0 {
envVars["fprocess"] = request.EnvProcess
}

labels := make(map[string]interface{})
labels[FaasFunctionLabel] = request.Service
labels["io.rancher.container.pull_image"] = "always"

launchConfig := &client.LaunchConfig{
Environment: envVars,
ImageUuid: "docker:" + request.Image, // not sure if it's ok to just prefix with 'docker:'
Labels: labels,
}

spec := &client.ServiceUpgrade{
InServiceStrategy: &client.InServiceUpgradeStrategy{
BatchSize: 1,
StartFirst: true,
LaunchConfig: launchConfig,
SecondaryLaunchConfigs: []client.SecondaryLaunchConfig{},
},
}

return spec
}
22 changes: 21 additions & 1 deletion rancher/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package rancher
import (
"fmt"

"github.com/rancher/go-rancher/v2"
client "github.com/rancher/go-rancher/v2"
)

// BridgeClient is the interface for Rancher API
Expand All @@ -16,6 +16,8 @@ type BridgeClient interface {
CreateService(spec *client.Service) (*client.Service, error)
DeleteService(spec *client.Service) error
UpdateService(spec *client.Service, updates map[string]string) (*client.Service, error)
UpgradeService(spec *client.Service, upgrade *client.ServiceUpgrade) (*client.Service, error)
FinishUpgradeService(spec *client.Service) (*client.Service, error)
}

// Client is the REST client type
Expand Down Expand Up @@ -132,3 +134,21 @@ func (c *Client) UpdateService(spec *client.Service, updates map[string]string)
}
return service, nil
}

// UpgradeService starts service upgrade of the specified service in rancher
func (c *Client) UpgradeService(spec *client.Service, upgrade *client.ServiceUpgrade) (*client.Service, error) {
service, err := c.rancherClient.Service.ActionUpgrade(spec, upgrade)
if err != nil {
return nil, err
}
return service, nil
}

// FinishUpgradeService finishes service upgrade of the specified service in rancher
func (c *Client) FinishUpgradeService(spec *client.Service) (*client.Service, error) {
service, err := c.rancherClient.Service.ActionFinishupgrade(spec)
if err != nil {
return nil, err
}
return service, nil
}
8 changes: 6 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"os"
"time"

bootstrap "github.com/alexellis/faas-provider"
bootTypes "github.com/alexellis/faas-provider/types"
"github.com/openfaas-incubator/faas-rancher/handlers"
"github.com/openfaas-incubator/faas-rancher/rancher"
bootstrap "github.com/openfaas/faas-provider"
bootTypes "github.com/openfaas/faas-provider/types"
)

const (
Expand Down Expand Up @@ -66,6 +66,10 @@ func main() {
FunctionReader: handlers.MakeFunctionReader(rancherClient).ServeHTTP,
ReplicaReader: handlers.MakeReplicaReader(rancherClient).ServeHTTP,
ReplicaUpdater: handlers.MakeReplicaUpdater(rancherClient).ServeHTTP,
UpdateHandler: handlers.MakeUpdateHandler(rancherClient).ServeHTTP,
Health: handlers.MakeHealthHandler(),
InfoHandler: handlers.MakeInfoHandler("0.8.1", ""),
SecretHandler: handlers.MakeSecretHandler(),
}

// Todo: AE - parse port and parse timeout from env-vars
Expand Down
4 changes: 2 additions & 2 deletions vendor.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
github.com/gorilla/mux v1.4.0 https://github.com/gorilla/mux.git
github.com/alexellis/faas 0.6.4 https://github.com/alexellis/faas.git
github.com/alexellis/faas-provider 0.1 https://github.com/alexellis/faas-provider.git
github.com/openfaas/faas 0.8.1 https://github.com/openfaas/faas.git
github.com/openfaas/faas-provider 0.8.1 https://github.com/openfaas/faas-provider.git
github.com/rancher/go-rancher 821d581
github.com/gorilla/websocket 1551221275a7bd42978745a376b2531f791d88f3
github.com/pkg/errors 1d2e60385a13aaa66134984235061c2f9302520e
Expand Down
16 changes: 0 additions & 16 deletions vendor/github.com/alexellis/faas-provider/.gitignore

This file was deleted.

24 changes: 0 additions & 24 deletions vendor/github.com/alexellis/faas-provider/Dockerfile

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/alexellis/faas-provider/README.md

This file was deleted.

Loading