Skip to content

Commit

Permalink
[faas]: Add FaaSController
Browse files Browse the repository at this point in the history
  • Loading branch information
benja-wu committed Jun 26, 2021
1 parent ff96337 commit d6af0dc
Show file tree
Hide file tree
Showing 22 changed files with 3,581 additions and 413 deletions.
249 changes: 249 additions & 0 deletions doc/faascontroller.md

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion go.mod
Expand Up @@ -31,7 +31,6 @@ require (
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
github.com/robfig/cron/v3 v3.0.1
github.com/rs/cors v1.7.0
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
Expand All @@ -47,4 +46,12 @@ require (
go.etcd.io/etcd/server/v3 v3.5.0
go.uber.org/zap v1.17.0
gopkg.in/yaml.v2 v2.4.0
gotest.tools/v3 v3.0.3
k8s.io/api v0.20.7
k8s.io/apimachinery v0.20.7
k8s.io/cli-runtime v0.20.7 // indirect
knative.dev/client v0.23.1
knative.dev/serving v0.23.1-0.20210614141420-380a090c2039
)

replace github.com/go-openapi/spec => github.com/go-openapi/spec v0.19.3
932 changes: 919 additions & 13 deletions go.sum

Large diffs are not rendered by default.

146 changes: 0 additions & 146 deletions pkg/object/function/cronjob.go

This file was deleted.

125 changes: 125 additions & 0 deletions pkg/object/function/faascontroller.go
@@ -0,0 +1,125 @@
/*
* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package function

import (
"fmt"

"github.com/megaease/easegress/pkg/logger"
"github.com/megaease/easegress/pkg/object/function/spec"
"github.com/megaease/easegress/pkg/object/function/worker"
"github.com/megaease/easegress/pkg/supervisor"
"github.com/megaease/easegress/pkg/v"
"gopkg.in/yaml.v2"
)

const (
// Category is the category of FaasController.
Category = supervisor.CategoryBusinessController

// Kind is the kind of FaaSController.
Kind = "FaaSController"
)

func init() {
supervisor.Register(&FaasController{})
}

type (
// FaasController is Function controller.
FaasController struct {
super *supervisor.Supervisor
superSpec *supervisor.Spec
spec *spec.Admin

worker *worker.Worker
}
)

// Category returns the category of FaasController.
func (f *FaasController) Category() supervisor.ObjectCategory {
return Category
}

// Kind returns the kind of FaasController.
func (f *FaasController) Kind() string {
return Kind
}

// DefaultSpec returns the default spec of Function.
func (f *FaasController) DefaultSpec() interface{} {
return &spec.Admin{
SyncInterval: "10s",
Provider: spec.ProviderKnative,
Knative: &spec.Knative{
Namespace: "default",
Timeout: "2s",
},
}
}

func (f *FaasController) Validate() error {
switch f.spec.Provider {
case spec.ProviderKnative:
//
default:
return fmt.Errorf("unknown faas provider: %s", f.spec.Provider)
}

buff, err := yaml.Marshal(f.spec.HTTPServer)
if err != nil {
err = fmt.Errorf("BUG: marshal %#v to yaml failed: %v",
f.spec.HTTPServer, err)
logger.Errorf(err.Error())
return err
}

vr := v.Validate(f.spec.HTTPServer, buff)
if !vr.Valid() {
return fmt.Errorf("%s", vr.Error())
}
return nil
}

// Init initializes Function.
func (f *FaasController) Init(superSpec *supervisor.Spec, super *supervisor.Supervisor) {
f.superSpec, f.spec, f.super = superSpec, superSpec.ObjectSpec().(*spec.Admin), super
f.reload()
}

// Inherit inherits previous generation of Function.
func (f *FaasController) Inherit(superSpec *supervisor.Spec,
previousGeneration supervisor.Object, super *supervisor.Supervisor) {

previousGeneration.Close()
f.Init(superSpec, super)
}

func (f *FaasController) reload() {
f.worker = worker.NewWorker(f.superSpec, f.super)
}

// Status returns Status generated by Runtime.
func (f *FaasController) Status() *supervisor.Status {
return &supervisor.Status{}
}

// Close closes Function.
func (f *FaasController) Close() {
f.worker.Close()
}

0 comments on commit d6af0dc

Please sign in to comment.