-
Notifications
You must be signed in to change notification settings - Fork 9
/
runtime.go
91 lines (77 loc) · 2.3 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package runtime
import (
"errors"
"reflect"
"sync"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
listerappsv1 "k8s.io/client-go/listers/apps/v1"
rfv1beta3 "github.com/refunc/refunc/pkg/apis/refunc/v1beta3"
"github.com/refunc/refunc/pkg/utils/rfutil"
)
// A Interface interface
type Interface interface {
Name() string
// IsPodReady checks if the given pod is runnable
IsPodReady(pod *corev1.Pod) bool
// GetDeploymentTemplate returns a deployment of the runner
GetDeploymentTemplate(tpl *rfv1beta3.Xenv) *appsv1.Deployment
// InitPod initialize given pod
// Note: one should not assume that the workDir still persist after InitPod being called
InitPod(pod *corev1.Pod, funcinst *rfv1beta3.Funcinst, refunc *rfv1beta3.Funcdef, tpl *rfv1beta3.Xenv, workDir string) error
}
// well known errors
var (
ErrRuntimeAlreadyExist = errors.New("runtime: A runtime with the same name already registered")
)
var registry struct {
sync.Mutex
runtimes map[string]Interface // runtime instances
}
// Register adds a new versioned runner to registry
func Register(r Interface) error {
registry.Lock()
defer registry.Unlock()
if _, ok := registry.runtimes[r.Name()]; !ok {
registry.runtimes[r.Name()] = r
return nil
}
return ErrRuntimeAlreadyExist
}
// ForXenv returns runtime object for given xenv
func ForXenv(xenv *rfv1beta3.Xenv) Interface {
registry.Lock()
defer registry.Unlock()
rtType := xenv.Spec.Type
if rtType == "" {
rtType = "agent"
}
if r, ok := registry.runtimes[rtType]; ok {
return r
}
return nil
}
// GetXenvPoolDeployment returns runner template deployment for given refunc
func GetXenvPoolDeployment(lister listerappsv1.DeploymentLister, xenv *rfv1beta3.Xenv) (*appsv1.Deployment, error) {
if xenv == nil {
return nil, errors.New("runtime: xenv is nil")
}
deps, err := lister.Deployments(xenv.Namespace).List(labels.Set(rfutil.XenvLabels(xenv)).AsSelectorPreValidated())
if err != nil {
return nil, err
}
if len(deps) > 0 {
ownerRef := xenv.AsOwner()
for i := range deps {
if ctlRef := metav1.GetControllerOf(deps[i]); ctlRef != nil && reflect.DeepEqual(ctlRef, ownerRef) {
return deps[i], nil
}
}
}
return nil, nil
}
func init() {
registry.runtimes = make(map[string]Interface)
}