This repository has been archived by the owner on Oct 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
runtime.go
58 lines (46 loc) · 1.77 KB
/
runtime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"context"
"github.com/crossplane/crossplane-runtime/pkg/errors"
pkgv1beta1 "github.com/crossplane/crossplane/apis/pkg/v1beta1"
)
// AnnotationKeyRuntime can be added to a Function to control what runtime is
// used to run it locally.
const AnnotationKeyRuntime = "xrender.crossplane.io/runtime"
// RuntimeType is a type of Function runtime.
type RuntimeType string
// Supported runtimes.
const (
// The Docker runtime uses a Docker daemon to run a Function. It uses the
// standard DOCKER_ environment variables to determine how to connect to the
// daemon.
AnnotationValueRuntimeDocker RuntimeType = "Docker"
// The Development runtime expects you to deploy a Function locally. This is
// mostly useful when developing a Function. The Function must be running
// with the --insecure flag, i.e. without transport security.
AnnotationValueRuntimeDevelopment RuntimeType = "Development"
AnnotationValueRuntimeDefault = AnnotationValueRuntimeDocker
)
// A Runtime runs a Function.
type Runtime interface {
// Start the Function.
Start(ctx context.Context) (RuntimeContext, error)
}
// RuntimeContext contains context on how a Function is being run.
type RuntimeContext struct {
// Target for RunFunctionRequest gRPCs.
Target string
// Stop the running Function.
Stop func(context.Context) error
}
// GetRuntime for the supplied Function, per its annotations.
func GetRuntime(fn pkgv1beta1.Function) (Runtime, error) {
switch r := RuntimeType(fn.GetAnnotations()[AnnotationKeyRuntime]); r {
case AnnotationValueRuntimeDocker, "":
return GetRuntimeDocker(fn)
case AnnotationValueRuntimeDevelopment:
return GetRuntimeDevelopment(fn), nil
default:
return nil, errors.Errorf("unsupported %q annotation value %q (unknown runtime)", AnnotationKeyRuntime, r)
}
}