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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package events
import (
"context"
"errors"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
eventsv1alpha1 "github.com/johnandersen777/socialweb-computer-kcp/api/events/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/atprotorecord"
"github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/atprotorecordwatch"
"github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/runtime/kube"
"github.com/johnandersen777/socialweb-computer-kcp/internal/eventstream"
)
type ClientFor func(workspace string) (client.Client, error)
var (
_ atprotorecordwatch.Children = recordWatchChildren{}
_ atprotorecordwatch.Repos = recordWatchRepos{}
)
func RecordWatch(clientFor ClientFor) (atprotorecordwatch.Children, atprotorecordwatch.Repos, atprotorecordwatch.ValueReader) {
return recordWatchChildren{clientFor: clientFor}, recordWatchRepos{clientFor: clientFor}, recordWatchValues(clientFor)
}
func (f ClientFor) client(workspace string) (client.Client, error) {
if f == nil {
return nil, errors.New("events: the record watch has no workspace clients")
}
return f(workspace)
}
type recordWatchChildren struct {
clientFor ClientFor
}
func (r recordWatchChildren) ApplyEventStream(ctx context.Context, workspace string, obj *eventsv1alpha1.EventStream) error {
return r.apply(ctx, workspace, obj)
}
func (r recordWatchChildren) ApplyEventSource(ctx context.Context, workspace string, obj *eventsv1alpha1.EventSource) error {
return r.apply(ctx, workspace, obj)
}
func (r recordWatchChildren) ApplyPdsEventSource(ctx context.Context, workspace string, obj *eventsv1alpha1.PdsEventSource) error {
return r.apply(ctx, workspace, obj)
}
func (r recordWatchChildren) EventStreams(ctx context.Context, workspace, watch string) ([]eventsv1alpha1.EventStream, error) {
c, err := r.clientFor.client(workspace)
if err != nil {
return nil, err
}
var list eventsv1alpha1.EventStreamList
if err := c.List(ctx, &list, client.MatchingLabels{eventstream.RecordWatchLabel: watch}); err != nil {
return nil, err
}
return list.Items, nil
}
func (r recordWatchChildren) DeleteEventStream(ctx context.Context, workspace, name string) error {
c, err := r.clientFor.client(workspace)
if err != nil {
return err
}
obj := &eventsv1alpha1.EventStream{ObjectMeta: metav1.ObjectMeta{Name: name}}
if err := c.Delete(ctx, obj, client.PropagationPolicy(metav1.DeletePropagationBackground)); err != nil && !apierrors.IsNotFound(err) {
return err
}
return nil
}
func (r recordWatchChildren) apply(ctx context.Context, workspace string, obj client.Object) error {
c, err := r.clientFor.client(workspace)
if err != nil {
return err
}
gvk, err := apiutil.GVKForObject(obj, c.Scheme())
if err != nil {
return err
}
if err := kube.ApplyWith(ctx, c, obj, kube.ApplyOptions{FieldManager: atprotorecordwatch.FieldManager, Force: true}); err != nil {
return fmt.Errorf("events: applying %s %s in %s: %w", gvk.Kind, obj.GetName(), workspace, err)
}
return nil
}
type recordWatchRepos struct {
clientFor ClientFor
}
func (r recordWatchRepos) AtprotoRepo(ctx context.Context, workspace, name string) (*v1alpha1.AtprotoRepo, error) {
return get[v1alpha1.AtprotoRepo](ctx, r.clientFor, workspace, name)
}
func (r recordWatchRepos) LocalRepo(ctx context.Context, workspace, name string) (*v1alpha1.LocalRepo, error) {
return get[v1alpha1.LocalRepo](ctx, r.clientFor, workspace, name)
}
func (r recordWatchRepos) RemoteRepo(ctx context.Context, workspace, name string) (*v1alpha1.RemoteRepo, error) {
return get[v1alpha1.RemoteRepo](ctx, r.clientFor, workspace, name)
}
func get[T any, P interface {
*T
client.Object
}](ctx context.Context, clientFor ClientFor, workspace, name string) (*T, error) {
c, err := clientFor.client(workspace)
if err != nil {
return nil, err
}
out := P(new(T))
if err := c.Get(ctx, client.ObjectKey{Name: name}, out); err != nil {
return nil, err
}
return (*T)(out), nil
}
func recordWatchValues(clientFor ClientFor) atprotorecordwatch.ValueReader {
return func(ctx context.Context, workspace string, from v1alpha1.AtprotoValueFrom) (map[string]any, error) {
c, err := clientFor.client(workspace)
if err != nil {
return nil, err
}
gvk, err := c.RESTMapper().KindFor(atprotorecord.ReferenceResource(from))
if err != nil {
return nil, err
}
mapping, err := c.RESTMapper().RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return nil, err
}
key := client.ObjectKey{Name: from.Name}
if mapping.Scope.Name() == meta.RESTScopeNameNamespace {
key.Namespace = from.Namespace
}
return kube.GetWith(ctx, c, gvk, key)
}
}