-
Notifications
You must be signed in to change notification settings - Fork 9
/
registry.go
69 lines (53 loc) · 1.56 KB
/
registry.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package runtime
import (
"context"
"strings"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/std/cfg"
)
var (
registrations = map[string]InstantiateClassFunc{}
)
type InstantiateClassFunc func(context.Context, cfg.Configuration) (Class, error)
func Register(name string, r InstantiateClassFunc) {
registrations[strings.ToLower(name)] = r
}
func HasRuntime(name string) bool {
_, ok := registrations[strings.ToLower(name)]
return ok
}
func ClusterFor(ctx context.Context, env cfg.Context) (Cluster, error) {
deferred, err := ClassFor(ctx, env)
if err != nil {
return nil, err
}
return deferred.AttachToCluster(ctx, env.Configuration())
}
func PlannerFor(ctx context.Context, env cfg.Context) (Planner, error) {
cluster, err := ClusterFor(ctx, env)
if err != nil {
return nil, err
}
return cluster.Planner(env), nil
}
func NamespaceFor(ctx context.Context, env cfg.Context) (ClusterNamespace, error) {
cluster, err := ClusterFor(ctx, env)
if err != nil {
return nil, err
}
return cluster.Bind(env)
}
func ClassFor(ctx context.Context, env cfg.Context) (Class, error) {
rt := strings.ToLower(env.Environment().Runtime)
if obtain, ok := registrations[rt]; ok {
r, err := obtain(ctx, env.Configuration())
if err != nil {
return nil, err
}
return r, nil
}
return nil, fnerrors.InternalError("%s: no such runtime", rt)
}