server

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 25, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

Crossplane Function Server

Crossplane Function Server is a high-level runtime based on the Crossplane Function SDK. It aims to solve two issues with the current implementation of Go composition functions:

  • Provide a simpler and more abstract API to implement functions since the native Go SDK is very bare-metal and requires developers to deal with low-level Protobuf structs.
  • Serve multiple functions at once using the same image to allow complex Crossplane platforms to reduce the required number of function images to a bare minimum. With the original SDK one image can only run a single function.

Features

High Level Function API

Function Server provides a high-level API that requires users to only deal with runtime.Objects in order to define the desired resources to be created. This allows developers to focus on the actual implementation of their function logic without worrying about writing boilerplate or type conversion code.

Serve Multiple Functions at Once

Function Server is able to serve multiple composition functions at once by implementing his own high-level virtual function API called ServerFunction. One can register as many server functions during startup as needed.

From a Crossplane perspective a Function Server still acts and looks like a single function. Which server function should be executed is determined by the input that is defined in every composition.

Example

See examples.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	WithReadyIsReady     = WithReady(fnapi.Ready_READY_TRUE)
	WithReadyIsNotReady  = WithReady(fnapi.Ready_READY_FALSE)
	WithReadyUnspecified = WithReady(fnapi.Ready_READY_UNSPECIFIED)
)

WithReady shorthand

Functions

func IsNotFoundError

func IsNotFoundError(err error) bool

Types

type ResourceModifier

type ResourceModifier func(r *fnapi.Resource)

ResourceModifier applies modifications to a bare-metal Crossplane function resource.

func WithConnectionDetails

func WithConnectionDetails(connectionDetails map[string][]byte) ResourceModifier

WithConnectionDetails sets the given connection details to a resource.

func WithReady

func WithReady(ready fnapi.Ready) ResourceModifier

WithReady applies the desired ready state to a resource.

type RunServerFunctionRequest

type RunServerFunctionRequest struct {
	Req         *fnapi.RunFunctionRequest
	ServerInput *v1alpha1.ServerInput
}

func (*RunServerFunctionRequest) GetComposed

func (r *RunServerFunctionRequest) GetComposed(name string, target runtime.Object) error

func (*RunServerFunctionRequest) GetComposite

func (r *RunServerFunctionRequest) GetComposite(target runtime.Object) error

func (*RunServerFunctionRequest) GetInput

func (r *RunServerFunctionRequest) GetInput(target any) error

func (*RunServerFunctionRequest) GetNativeRequest

func (r *RunServerFunctionRequest) GetNativeRequest() *fnapi.RunFunctionRequest

type RunServerFunctionResponse

type RunServerFunctionResponse struct {
	DesiredComposite *fnapi.Resource
	DesiredComposed  map[string]*fnapi.Resource
	DesiredContext   *structpb.Struct
	Results          []*fnapi.Result
}

func (*RunServerFunctionResponse) GetComposed

func (r *RunServerFunctionResponse) GetComposed(name string, target runtime.Object) error

func (*RunServerFunctionResponse) GetComposite

func (r *RunServerFunctionResponse) GetComposite(target runtime.Object) error

func (*RunServerFunctionResponse) SetComposed

func (r *RunServerFunctionResponse) SetComposed(name string, o runtime.Object, mods ...ResourceModifier) error

func (*RunServerFunctionResponse) SetComposedRaw

func (r *RunServerFunctionResponse) SetComposedRaw(name string, res *fnapi.Resource)

func (*RunServerFunctionResponse) SetComposite

func (r *RunServerFunctionResponse) SetComposite(o runtime.Object, mods ...ResourceModifier) error

func (*RunServerFunctionResponse) SetCompositeRaw

func (r *RunServerFunctionResponse) SetCompositeRaw(res *fnapi.Resource)

func (*RunServerFunctionResponse) SetContextField

func (r *RunServerFunctionResponse) SetContextField(key string, value any) error

func (*RunServerFunctionResponse) SetNativeResults

func (r *RunServerFunctionResponse) SetNativeResults(results []*fnapi.Result)

type Server

type Server struct {
	fnapi.UnimplementedFunctionRunnerServiceServer
	// contains filtered or unexported fields
}

Server is a special Crossplane function which acts as a router that distributes request among several subroutines (aka server functions).

A server receives a dedicated input payload by the composition that tells the server which subroutine to call.

func NewServer

func NewServer(opts ...ServerOption) *Server

NewServer create a new Server instance that implements the Crossplane Function interface and is able to serve multiple subfunctions (aka server functions) at the same time.

ServerFunctions are registered via the WithFunction option:

server.NewServer(
	server.WithFunction(&MyFunction{})
	server.WithFunction(&MyOtherFunction{})
	// ...
)

func (*Server) RunFunction

type ServerFunction

type ServerFunction interface {
	// Run executes the ServerFunction for the given request.
	Run(ctx context.Context, req ServerFunctionRequest, res ServerFunctionResponse) error
}

A ServerFunction is a high-level subroutine of native Crossplane Go function.

type ServerFunctionRequest

type ServerFunctionRequest interface {
	// GetNativeRequest returns the native function request of the
	// underlying Crossplane function-sdk-go.
	GetNativeRequest() *fnapi.RunFunctionRequest

	// GetInput for this server function.
	//
	// Note that this is not the input of the Crossplane function-sdk-go.
	// To receive that use GetNativeRequest().GetInput().
	GetInput(target any) error

	// GetComposite copies the current state of the composite resource
	// into the given target object.
	GetComposite(target runtime.Object) error

	// GetComposed copies the current state of the composed resource identified
	// by the given name.
	//
	// If a no composed resource with the given name exists, target remains
	// unchanged.
	GetComposed(name string, target runtime.Object) error
}

ServerFunctionRequest provides ways to easily the request payload of a ServerFunction call.

type ServerFunctionResponse

type ServerFunctionResponse interface {
	// SetComposite save the given object as desired state of the composite
	// resource for the given name.
	SetComposite(o runtime.Object, mods ...ResourceModifier) error

	// GetComposite gets the current state of the composite resource of this
	// response and writes its contents into the given target object.
	GetComposite(target runtime.Object) error

	// SetCompositeRaw sets the desired response state directly using the
	// native SDK types.
	//
	// This is useful if the desired resource state already exists as a native
	// type so there is no need to work with runtime.Objects.
	SetCompositeRaw(res *fnapi.Resource)

	// SetComposed saves the given composed object as desired composed object
	// identified by the given name for this function's response.
	SetComposed(name string, o runtime.Object, mods ...ResourceModifier) error

	// GetComposed looks up the composed resource in the current response object
	// and writes its contents into the given target object.
	GetComposed(name string, target runtime.Object) error

	// SetComposedRaw sets the desired composed resource state directly using
	// the native SDK types.
	//
	// This is useful if the desired resource state already exists as a native
	// type so there is no need to work with runtime.Objects.
	SetComposedRaw(name string, res *fnapi.Resource)

	// SetContextField sets the value of the context field key to the given
	// value. The passed value must be convertable to protobuf.
	SetContextField(key string, value any) error

	// SetNativeResults of the underlying SDK requests.
	SetNativeResults(results []*fnapi.Result)
}

ServerFunctionResponse provides ways to easily define the response payload of a ServerFunction call.

type ServerOption

type ServerOption func(server *Server)

ServerOption that configures a function Server.

func WithFunction

func WithFunction(name string, fn ServerFunction) ServerOption

WithFunction registeres a ServerFunction at a Server with a given name.

Directories

Path Synopsis
apis
v1alpha1
+kubebuilder:object:generate=true Package v1alpha1 is the v1alpha1 version of the server.fn.crossplane.io API.
+kubebuilder:object:generate=true Package v1alpha1 is the v1alpha1 version of the server.fn.crossplane.io API.
examples
simple command
Package main implements a Server Composition Function that serves multiple ServerFunctions.
Package main implements a Server Composition Function that serves multiple ServerFunctions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL