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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package componentcore
import (
"context"
"errors"
"strings"
"sync"
tenancyv1alpha1 "github.com/kcp-dev/sdk/apis/tenancy/v1alpha1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
kruntime "k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
eventsv1alpha1 "github.com/johnandersen777/socialweb-computer-kcp/api/events/v1alpha1"
exposurev1alpha1 "github.com/johnandersen777/socialweb-computer-kcp/api/exposure/v1alpha1"
pdsv1alpha1 "github.com/johnandersen777/socialweb-computer-kcp/api/pds/v1alpha1"
policyv1alpha1 "github.com/johnandersen777/socialweb-computer-kcp/api/policy/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/childapplication"
"github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/marketpresence"
"github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/openbaoserver"
"github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/runtime/kube"
)
var schemes = []func(*kruntime.Scheme) error{v1alpha1.AddToScheme, pdsv1alpha1.AddToScheme, policyv1alpha1.AddToScheme,
eventsv1alpha1.AddToScheme, exposurev1alpha1.AddToScheme}
type workspaceClients struct {
base *rest.Config
scheme *kruntime.Scheme
mu sync.Mutex
clients map[string]client.Client
dynamics map[string]*kube.Client
}
var (
_ childapplication.Lister = (*workspaceClients)(nil)
_ childapplication.Workspaces = workspaceTree{}
_ openbaoserver.Grants = guestGrants{}
_ marketpresence.Bidders = bidders{}
)
func newWorkspaceClients(base *rest.Config) (*workspaceClients, error) {
if base == nil {
return nil, errors.New("componentcore: the workspace clients need a rest config")
}
scheme := kruntime.NewScheme()
for _, add := range append([]func(*kruntime.Scheme) error{clientgoscheme.AddToScheme, tenancyv1alpha1.AddToScheme}, schemes...) {
if err := add(scheme); err != nil {
return nil, err
}
}
return &workspaceClients{base: base, scheme: scheme, clients: map[string]client.Client{}, dynamics: map[string]*kube.Client{}}, nil
}
func (w *workspaceClients) client(workspace string) (client.Client, error) {
workspace = strings.TrimSpace(workspace)
if workspace == "" {
workspace = DefaultWorkspace
}
w.mu.Lock()
defer w.mu.Unlock()
if c, ok := w.clients[workspace]; ok {
return c, nil
}
cfg := rest.CopyConfig(w.base)
host, _, _ := strings.Cut(strings.TrimSuffix(cfg.Host, "/"), "/clusters/")
cfg.Host = host + "/clusters/" + workspace
c, err := client.New(cfg, client.Options{Scheme: w.scheme})
if err != nil {
return nil, err
}
w.clients[workspace] = c
return c, nil
}
func (w *workspaceClients) dynamic(workspace string) (*kube.Client, error) {
workspace = strings.TrimSpace(workspace)
if workspace == "" {
workspace = DefaultWorkspace
}
w.mu.Lock()
defer w.mu.Unlock()
if c, ok := w.dynamics[workspace]; ok {
return c, nil
}
c, err := kube.ForWorkspace(w.base, workspace)
if err != nil {
return nil, err
}
w.dynamics[workspace] = c
return c, nil
}
func (w *workspaceClients) List(ctx context.Context, workspace string, list client.ObjectList) error {
c, err := w.client(workspace)
if err != nil {
return err
}
err = c.List(ctx, list)
if apierrors.IsNotFound(err) || apierrors.IsForbidden(err) || meta.IsNoMatchError(err) {
return nil
}
return err
}
type workspaceTree struct {
clients *workspaceClients
}
func (t workspaceTree) Get(ctx context.Context, parent, name string) (childapplication.Workspace, bool, error) {
c, err := t.clients.client(parent)
if err != nil {
return childapplication.Workspace{}, false, err
}
var ws tenancyv1alpha1.Workspace
if err := c.Get(ctx, client.ObjectKey{Name: name}, &ws); err != nil {
if apierrors.IsNotFound(err) {
return childapplication.Workspace{}, false, nil
}
return childapplication.Workspace{}, false, err
}
return workspaceOf(ws), true, nil
}
func (t workspaceTree) ByLabel(ctx context.Context, parent, key, value string) (childapplication.Workspace, bool, error) {
c, err := t.clients.client(parent)
if err != nil {
return childapplication.Workspace{}, false, err
}
var list tenancyv1alpha1.WorkspaceList
if err := c.List(ctx, &list, client.MatchingLabels{key: value}); err != nil {
if apierrors.IsNotFound(err) {
return childapplication.Workspace{}, false, nil
}
return childapplication.Workspace{}, false, err
}
if len(list.Items) == 0 {
return childapplication.Workspace{}, false, nil
}
return workspaceOf(list.Items[0]), true, nil
}
func workspaceOf(ws tenancyv1alpha1.Workspace) childapplication.Workspace {
return childapplication.Workspace{Name: ws.Name, Phase: string(ws.Status.Phase), Deleting: !ws.DeletionTimestamp.IsZero()}
}
type guestGrants struct {
clients *workspaceClients
}
func (g guestGrants) List(ctx context.Context, workspace string) ([]v1alpha1.GuestIdentityGrant, error) {
var list v1alpha1.GuestIdentityGrantList
if err := g.clients.List(ctx, workspace, &list); err != nil {
return nil, err
}
return list.Items, nil
}
type bidders struct {
clients *workspaceClients
}
func (b bidders) Get(ctx context.Context, workspace, name string) (*v1alpha1.Bidder, error) {
return get[v1alpha1.Bidder](ctx, b.clients, workspace, name)
}
func get[T any, P interface {
*T
client.Object
}](ctx context.Context, clients *workspaceClients, workspace, name string) (*T, error) {
c, err := clients.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
}