Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Engine Using Options #2771

Merged
merged 1 commit into from
Feb 9, 2022
Merged
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
4 changes: 2 additions & 2 deletions porch/apiserver/pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ func (c completedConfig) New() (*PorchServer, error) {

cache := cache.NewCache(c.ExtraConfig.CacheDirectory)
cad, err := engine.NewCaDEngine(
cache,
c.ExtraConfig.FunctionRunnerAddress,
engine.WithCache(cache),
engine.WithGRPCFunctionRuntime(c.ExtraConfig.FunctionRunnerAddress),
)

porchGroup, err := porch.NewRESTStorage(Scheme, Codecs, cad, coreClient)
Expand Down
38 changes: 5 additions & 33 deletions porch/engine/pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@ import (
api "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
configapi "github.com/GoogleContainerTools/kpt/porch/controllers/pkg/apis/porch/v1alpha1"
"github.com/GoogleContainerTools/kpt/porch/engine/pkg/kpt"
"github.com/GoogleContainerTools/kpt/porch/func/evaluator"
"github.com/GoogleContainerTools/kpt/porch/repository/pkg/cache"
"github.com/GoogleContainerTools/kpt/porch/repository/pkg/repository"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"k8s.io/klog/v2"
)

type CaDEngine interface {
Expand All @@ -45,36 +41,12 @@ type CaDEngine interface {
ListFunctions(ctx context.Context, repositoryObj *configapi.Repository, auth repository.AuthOptions) ([]repository.Function, error)
}

func NewCaDEngine(cache *cache.Cache, functionRunnerAddress string) (CaDEngine, error) {
runtime, err := createFunctionRuntime(functionRunnerAddress)
if err != nil {
return nil, fmt.Errorf("failed to create function runtime: %w", err)
}

return &cadEngine{
cache: cache,
renderer: kpt.NewRenderer(),
runtime: runtime,
}, nil
}

func createFunctionRuntime(address string) (kpt.FunctionRuntime, error) {
if address == "" {
klog.Warningf("Using simple kpt function runner (in-process)")
return kpt.NewSimpleFunctionRuntime(), nil
func NewCaDEngine(opts ...EngineOption) (CaDEngine, error) {
engine := &cadEngine{}
for _, opt := range opts {
opt.apply(engine)
}

klog.Infof("Dialing grpc function runner %q", address)

cc, err := grpc.Dial(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, fmt.Errorf("failed to dial grpc function evaluator: %w", err)
}

return &grpcRuntime{
cc: cc,
client: evaluator.NewFunctionEvaluatorClient(cc),
}, err
return engine, nil
}

type cadEngine struct {
Expand Down
96 changes: 96 additions & 0 deletions porch/engine/pkg/engine/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2022 Google LLC
//
// 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 engine

import (
"fmt"

"github.com/GoogleContainerTools/kpt/pkg/fn"
"github.com/GoogleContainerTools/kpt/porch/engine/pkg/kpt"
"github.com/GoogleContainerTools/kpt/porch/func/evaluator"
"github.com/GoogleContainerTools/kpt/porch/repository/pkg/cache"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"k8s.io/klog/v2"
)

type EngineOption interface {
apply(engine *cadEngine) error
}

type EngineOptionFunc func(engine *cadEngine) error

var _ EngineOption = EngineOptionFunc(nil)

func (f EngineOptionFunc) apply(engine *cadEngine) error {
return f(engine)
}

func WithCache(cache *cache.Cache) EngineOption {
return EngineOptionFunc(func(engine *cadEngine) error {
engine.cache = cache
return nil
})
}

func WithGRPCFunctionRuntime(address string) EngineOption {
return EngineOptionFunc(func(engine *cadEngine) error {
runtime, err := createFunctionRuntime(address)
if err != nil {
return fmt.Errorf("failed to create function runtime: %w", err)
}
engine.runtime = runtime
return nil
})
}

func WithFunctionRuntime(runtime fn.FunctionRuntime) EngineOption {
return EngineOptionFunc(func(engine *cadEngine) error {
engine.runtime = runtime
return nil
})
}

func WithSimpleFunctionRuntime() EngineOption {
return EngineOptionFunc(func(engine *cadEngine) error {
engine.runtime = kpt.NewSimpleFunctionRuntime()
return nil
})
}

func WithRenderer(renderer fn.Renderer) EngineOption {
return EngineOptionFunc(func(engine *cadEngine) error {
engine.renderer = renderer
return nil
})
}

func createFunctionRuntime(address string) (kpt.FunctionRuntime, error) {
if address == "" {
return nil, fmt.Errorf("address is required to instantiate gRPC function runtime")
}

klog.Infof("Dialing grpc function runner %q", address)

cc, err := grpc.Dial(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, fmt.Errorf("failed to dial grpc function evaluator: %w", err)
}

return &grpcRuntime{
cc: cc,
client: evaluator.NewFunctionEvaluatorClient(cc),
}, err
}