function

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: Apache-2.0 Imports: 12 Imported by: 23

README

function-sdk-go

CI GitHub release (latest SemVer) Go Reference

The Go SDK for writing composition functions.

This SDK is currently a beta. We try to avoid breaking changes, but it will not have a stable API until it reaches v1.0.0. It follows the same contributing guidelines as Crossplane.

To learn how to use this SDK:

If you just want to jump in and get started, consider using the function-template-go template repository.

Documentation

Overview

Package function is an SDK for building Composition Functions.

Example
package main

import (
	"fmt"

	"google.golang.org/protobuf/encoding/protojson"

	"github.com/crossplane/function-sdk-go/proto/v1beta1"
	"github.com/crossplane/function-sdk-go/request"
	"github.com/crossplane/function-sdk-go/resource"
	"github.com/crossplane/function-sdk-go/resource/composed"
	"github.com/crossplane/function-sdk-go/response"
)

var req = &v1beta1.RunFunctionRequest{
	Observed: &v1beta1.State{
		Composite: &v1beta1.Resource{
			Resource: resource.MustStructJSON(`{"spec":{"widgets":9001}}`),
		},
	},
}

func main() {
	// Create a response to the request passed to your RunFunction method.
	rsp := response.To(req, response.DefaultTTL)

	// Get the observed composite resource (XR) from the request.
	oxr, _ := request.GetObservedCompositeResource(req)

	// Read the desired number of widgets from our observed XR.
	widgets, _ := oxr.Resource.GetInteger("spec.widgets")

	// Get any existing desired composed resources from the request.
	// Desired composed resources would exist if a previous Function in the
	// pipeline added them.
	desired, _ := request.GetDesiredComposedResources(req)

	// Create a desired composed resource using unstructured data.
	desired["new"] = &resource.DesiredComposed{Resource: composed.New()}
	desired["new"].Resource.SetAPIVersion("example.org/v1")
	desired["new"].Resource.SetKind("CoolResource")

	// Set the desired composed resource's widgets to the value extracted from
	// the observed XR.
	desired["new"].Resource.SetInteger("spec.widgets", widgets)

	// Create a desired composed resource using structured data.
	// db, _ := composed.From(&v1beta1.Instance{})
	// desired["database"] = &resource.DesiredComposed{Resource: db}

	// Add a label to our new desired resource, and any other.
	for _, r := range desired {
		r.Resource.SetLabels(map[string]string{"coolness": "high"})
	}

	// Set our updated desired composed resource in the response we'll return.
	_ = response.SetDesiredComposedResources(rsp, desired)

	j, _ := protojson.Marshal(rsp)
	fmt.Println(string(j))

}
Output:

{"meta":{"ttl":"60s"},"desired":{"resources":{"new":{"resource":{"apiVersion":"example.org/v1","kind":"CoolResource","metadata":{"labels":{"coolness":"high"}},"spec":{"widgets":9001}}}}}}

Index

Examples

Constants

View Source
const (
	DefaultNetwork = "tcp"
	DefaultAddress = ":9443"
)

Default ServeOptions.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(debug bool) (logging.Logger, error)

NewLogger returns a new logger.

func Serve

Serve the supplied Function by creating a gRPC server and listening for RunFunctionRequests. Blocks until the server returns an error.

Types

type ServeOption

type ServeOption func(o *ServeOptions) error

A ServeOption configures how a Function is served.

func Insecure

func Insecure(insecure bool) ServeOption

Insecure specifies whether this Function should be served insecurely - i.e. without mTLS authentication. This is only useful for testing and development. Crossplane will always send requests using mTLS.

func Listen

func Listen(network, address string) ServeOption

Listen configures the network and address on which the Function will listen for RunFunctionRequests.

func MTLSCertificates

func MTLSCertificates(dir string) ServeOption

MTLSCertificates specifies a directory from which to load mTLS certificates. The directory must contain the server certificate (tls.key and tls.crt), as well as a CA certificate (ca.crt) that will be used to authenticate clients.

type ServeOptions

type ServeOptions struct {
	Network     string
	Address     string
	Credentials credentials.TransportCredentials
}

ServeOptions configure how a Function is served.

Directories

Path Synopsis
Package context contains utilities for working with Function context.
Package context contains utilities for working with Function context.
Package errors is a github.com/pkg/errors compatible API for native errors.
Package errors is a github.com/pkg/errors compatible API for native errors.
Package logging provides function's recommended logging interface.
Package logging provides function's recommended logging interface.
proto
Package request contains utilities for working with RunFunctionRequests.
Package request contains utilities for working with RunFunctionRequests.
Package resource contains utilities to convert protobuf representations of Crossplane resources to unstructured Go types, often with convenient getters and setters.
Package resource contains utilities to convert protobuf representations of Crossplane resources to unstructured Go types, often with convenient getters and setters.
composed
Package composed contains an unstructured composed resource.
Package composed contains an unstructured composed resource.
composite
Package composite contains an unstructured composite resource (XR).
Package composite contains an unstructured composite resource (XR).
Package response contains utilities for working with RunFunctionResponses.
Package response contains utilities for working with RunFunctionResponses.

Jump to

Keyboard shortcuts

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