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
92
package bus
import (
"bufio"
"context"
"maps"
"strings"
"sync"
"k8s.io/apimachinery/pkg/runtime/schema"
commonv1 "github.com/johnandersen777/socialweb-computer-kcp/api/common/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/deploy"
)
const SystemKind = "System"
type Caller struct {
Ref commonv1.ObjectRef
Labels map[string]string
}
func (c Caller) System() bool {
return strings.EqualFold(strings.TrimSpace(c.Ref.Kind), SystemKind)
}
func (c Caller) Complete() bool {
return strings.TrimSpace(c.Ref.Workspace) != "" && strings.TrimSpace(c.Ref.Kind) != "" && strings.TrimSpace(c.Ref.Name) != ""
}
type callerKey struct{}
func WithCaller(ctx context.Context, ref commonv1.ObjectRef) context.Context {
return WithCallerLabels(ctx, ref, nil)
}
func WithCallerLabels(ctx context.Context, ref commonv1.ObjectRef, labels map[string]string) context.Context {
return context.WithValue(ctx, callerKey{}, Caller{Ref: ref, Labels: maps.Clone(labels)})
}
func WithSystemCaller(ctx context.Context, name string) context.Context {
return WithCaller(ctx, commonv1.ObjectRef{Kind: SystemKind, Name: strings.TrimSpace(name)})
}
func CallerFrom(ctx context.Context) (Caller, bool) {
caller, ok := ctx.Value(callerKey{}).(Caller)
return caller, ok
}
var bootstrap = sync.OnceValue(func() map[string]bool {
return ParseBootstrapCallers(deploy.BusBootstrapCallers)
})
func ParseBootstrapCallers(data string) map[string]bool {
out := map[string]bool{}
scanner := bufio.NewScanner(strings.NewReader(data))
header := true
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
if header {
header = false
continue
}
name, _, _ := strings.Cut(line, "\t")
if name = strings.TrimSpace(name); name != "" {
out[name] = true
}
}
return out
}
func BootstrapCaller(name string) bool {
return bootstrap()[strings.TrimSpace(name)]
}
type Object interface {
GetName() string
GetLabels() map[string]string
GroupVersionKind() schema.GroupVersionKind
}
func WithObject(ctx context.Context, workspace string, obj Object) context.Context {
if obj == nil {
return ctx
}
gvk := obj.GroupVersionKind()
return WithCallerLabels(ctx, commonv1.ObjectRef{Group: gvk.Group, Kind: gvk.Kind, Name: obj.GetName(), Workspace: workspace}, obj.GetLabels())
}