Skip to content

Commit

Permalink
Merge pull request #113 from grycap/dev-srisco
Browse files Browse the repository at this point in the history
Implement OF Scaler
  • Loading branch information
srisco authored Jun 15, 2021
2 parents 935a8e4 + 92ced5c commit 2dde81e
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 12 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/grycap/cdmi-client-go v0.1.1
github.com/minio/minio v0.0.0-20210311070216-f92b7a562103
github.com/openfaas/faas-netes v0.0.0-20210305091056-d05bda90c357
github.com/prometheus/client_golang v1.9.0
github.com/smartystreets/assertions v1.2.0 // indirect
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ github.com/bcicen/jstream v1.0.1/go.mod h1:9ielPxqFry7Y4Tg3j4BfjPocfJ3TbsRtXOAYX
github.com/beevik/ntp v0.3.0/go.mod h1:hIHWr+l3+/clUnF44zdK+CWW7fO8dR5cIylAQ76NRpg=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cheggaaa/pb v1.0.29/go.mod h1:W40334L7FMC5JKWldsTWbdGjLo0RxUKK73K+TuPxX30=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
Expand Down Expand Up @@ -377,6 +379,7 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGw
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down Expand Up @@ -447,6 +450,7 @@ github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHX
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.35/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
Expand Down Expand Up @@ -488,6 +492,7 @@ github.com/montanaflynn/stats v0.5.0 h1:2EkzeTSqBB4V4bJwWrt5gIIrZmpJBcoIRGS2kWLg
github.com/montanaflynn/stats v0.5.0/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5VglpSg=
Expand Down Expand Up @@ -565,19 +570,22 @@ github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5Fsn
github.com/prometheus/client_golang v1.3.0/go.mod h1:hJaj2vgQTGQmVCsAACORcieXFeDPbaTKGT+JTgUa3og=
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
github.com/prometheus/client_golang v1.8.0/go.mod h1:O9VU6huf47PktckDQfMTX0Y8tY0/7TSWwj+ITvv0TnM=
github.com/prometheus/client_golang v1.9.0 h1:Rrch9mh17XcxvEu9D9DEpb4isxjGBtcevQjKvxPRQIU=
github.com/prometheus/client_golang v1.9.0/go.mod h1:FqZLKOZnGdFAhOK4nqGHa7D66IdsO+O441Eve7ptJDU=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.1.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/common v0.7.0/go.mod h1:DjGbpBbp5NYNiECxcL/VnbXCCaQpKd3tt26CguLLsqA=
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
github.com/prometheus/common v0.14.0/go.mod h1:U+gB1OBLb1lF3O42bTCL+FK18tX9Oar16Clt/msog/s=
github.com/prometheus/common v0.15.0 h1:4fgOnadei3EZvgRwxJ7RMpG1k1pOZth5Pc13tyspaKM=
github.com/prometheus/common v0.15.0/go.mod h1:U+gB1OBLb1lF3O42bTCL+FK18tX9Oar16Clt/msog/s=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
Expand All @@ -586,6 +594,7 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/procfs v0.3.0 h1:Uehi/mxLK0eiUc0H0++5tpMGTexB8wZ598MIgU8VpDM=
github.com/prometheus/procfs v0.3.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ func main() {

switch cfg.ServerlessBackend {
case "openfaas":
back = backends.MakeOpenfaasBackend(kubeClientset, kubeConfig, cfg)
ofBack := backends.MakeOpenfaasBackend(kubeClientset, kubeConfig, cfg)
back = ofBack
// Start OpenFaaS Scaler as a goroutine
if cfg.OpenfaasScalerEnable {
go ofBack.StartScaler()
}
// case "knative":
// back = backends.MakeKnativeBackend()
default:
Expand Down
8 changes: 8 additions & 0 deletions pkg/backends/openfaas.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net/http"

"github.com/grycap/oscar/v2/pkg/types"
"github.com/grycap/oscar/v2/pkg/utils"
ofv1 "github.com/openfaas/faas-netes/pkg/apis/openfaas/v1"
ofclientset "github.com/openfaas/faas-netes/pkg/client/clientset/versioned"

Expand All @@ -45,6 +46,7 @@ type OpenfaasBackend struct {
ofClientset *ofclientset.Clientset
namespace string
gatewayEndpoint string
scaler *utils.OpenfaasScaler
}

// MakeOpenfaasBackend makes a OpenfaasBackend from the provided k8S clientset and config
Expand All @@ -59,6 +61,7 @@ func MakeOpenfaasBackend(kubeClientset *kubernetes.Clientset, kubeConfig *rest.C
ofClientset: ofClientset,
namespace: cfg.ServicesNamespace,
gatewayEndpoint: fmt.Sprintf("gateway.%s:%d", cfg.OpenfaasNamespace, cfg.OpenfaasPort),
scaler: utils.NewOFScaler(kubeClientset, cfg),
}
}

Expand Down Expand Up @@ -327,3 +330,8 @@ func (of *OpenfaasBackend) createOFFunctionDefinition(service *types.Service) *o
},
}
}

// StartScaler starts the OpenFaaS Scaler
func (of *OpenfaasBackend) StartScaler() {
of.scaler.Start()
}
78 changes: 67 additions & 11 deletions pkg/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,21 @@ import (
)

const (
defaultMinioTLSVerify = true
defaultMinIOEndpoint = "https://minio-service.minio:9000"
defaultMinIORegion = "us-east-1"
defaultTimeout = time.Duration(300) * time.Second
defaultServiceName = "oscar"
defaultServicePort = 8080
defaultNamespace = "oscar"
defaultServicesNamespace = "oscar-svc"
defaultOpenfaasNamespace = "openfaas"
defaultOpenfaasPort = 8080
defaultMinioTLSVerify = true
defaultMinIOEndpoint = "https://minio-service.minio:9000"
defaultMinIORegion = "us-east-1"
defaultTimeout = time.Duration(300) * time.Second
defaultServiceName = "oscar"
defaultServicePort = 8080
defaultNamespace = "oscar"
defaultServicesNamespace = "oscar-svc"
defaultOpenfaasNamespace = "openfaas"
defaultOpenfaasPort = 8080
defaultOpenfaasBasicAuthSecret = "basic-auth"
defaultOpenfaasPrometheusPort = 9090
defaultOpenfaasScalerEnable = false
defaultOpenfaasScalerInterval = "2m"
defaultOpenfaasScalerInactivityDuration = "10m"
)

// Config stores the configuration for the OSCAR server
Expand All @@ -62,7 +67,7 @@ type Config struct {
ServicePort int `json:"-"`

// Serverless framework used to deploy services (Openfaas | Knative)
// If not defined only async invokations allowed (Using KubeBackend)
// If not defined only async invocations allowed (Using KubeBackend)
ServerlessBackend string `json:"serverless_backend,omitempty"`

// OpenfaasNamespace namespace where the OpenFaaS gateway is deployed
Expand All @@ -71,6 +76,21 @@ type Config struct {
// OpenfaasPort service port where the OpenFaaS gateway is exposed
OpenfaasPort int `json:"-"`

// OpenfaasBasicAuthSecret name of the secret used to store the OpenFaaS credentials
OpenfaasBasicAuthSecret string `json:"-"`

// OpenfaasPrometheusPort service port where the OpenFaaS' Prometheus is exposed
OpenfaasPrometheusPort int `json:"-"`

// OpenfaasScalerEnable option to enable the Openfaas scaler
OpenfaasScalerEnable bool `json:"-"`

// OpenfaasScalerInterval time interval to check if any function could be scaled
OpenfaasScalerInterval string `json:"-"`

// OpenfaasScalerInactivityDuration
OpenfaasScalerInactivityDuration string `json:"-"`

// HTTP timeout for reading the payload (default: 300)
ReadTimeout time.Duration `json:"-"`

Expand Down Expand Up @@ -182,6 +202,42 @@ func ReadConfig() (*Config, error) {
} else {
config.OpenfaasPort = defaultOpenfaasPort
}

if len(os.Getenv("OPENFAAS_BASIC_AUTH_SECRET")) > 0 {
config.OpenfaasBasicAuthSecret = os.Getenv("OPENFAAS_BASIC_AUTH_SECRET")
} else {
config.OpenfaasBasicAuthSecret = defaultOpenfaasBasicAuthSecret
}

if len(os.Getenv("OPENFAAS_PROMETHEUS_PORT")) > 0 {
config.OpenfaasPrometheusPort, err = strconv.Atoi(os.Getenv("OPENFAAS_PROMETHEUS_PORT"))
if err != nil {
return nil, fmt.Errorf("The OPENFAAS_PORT value is not valid. Error: %s", err)
}
} else {
config.OpenfaasPrometheusPort = defaultOpenfaasPrometheusPort
}

if len(os.Getenv("OPENFAAS_SCALER_ENABLE")) > 0 {
config.OpenfaasScalerEnable, err = strconv.ParseBool(os.Getenv("OPENFAAS_SCALER_ENABLE"))
if err != nil {
return nil, fmt.Errorf("The OPENFAAS_SCALER_ENABLE value must be a boolean")
}
} else {
config.OpenfaasScalerEnable = defaultOpenfaasScalerEnable
}

if len(os.Getenv("OPENFAAS_SCALER_INTERVAL")) > 0 {
config.OpenfaasScalerInterval = os.Getenv("OPENFAAS_SCALER_INTERVAL")
} else {
config.OpenfaasScalerInterval = defaultOpenfaasScalerInterval
}

if len(os.Getenv("OPENFAAS_SCALER_INACTIVITY_DURATION")) > 0 {
config.OpenfaasScalerInactivityDuration = os.Getenv("OPENFAAS_SCALER_INACTIVITY_DURATION")
} else {
config.OpenfaasScalerInactivityDuration = defaultOpenfaasScalerInactivityDuration
}
}

if len(os.Getenv("READ_TIMEOUT")) > 0 {
Expand Down
Loading

0 comments on commit 2dde81e

Please sign in to comment.