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
package childapplication
import (
"context"
"sort"
"strings"
"sigs.k8s.io/controller-runtime/pkg/client"
policyv1alpha1 "github.com/johnandersen777/socialweb-computer-kcp/api/policy/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
ctlruntime "github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/runtime"
)
const (
PolicyExport = "policy"
coreUnitPrefix = "socialweb-core-"
coreUnitSuffix = "-dev.service"
coreObjectPrefix = "core-"
coreObjectSuffix = "-dev"
)
type component struct {
v1alpha1.ChildApplicationComponent
failed bool
}
type componentKind struct {
read func(ctx context.Context, l Lister, workspace string) ([]component, error)
watch func(mapFunc func(ctx context.Context, src ctlruntime.Request) []ctlruntime.Request) ctlruntime.Option
}
func kindOf[L any, PL interface {
*L
client.ObjectList
}, T any, PT interface {
*T
ctlruntime.Object
}](export, kind string, items func(*L) []T, state func(PT) (bool, bool)) componentKind {
return componentKind{
read: func(ctx context.Context, l Lister, workspace string) ([]component, error) {
var list L
if err := l.List(ctx, workspace, PL(&list)); err != nil {
return nil, err
}
src := items(&list)
out := make([]component, 0, len(src))
for i := range src {
obj := PT(&src[i])
ready, failed := state(obj)
out = append(out, component{ChildApplicationComponent: v1alpha1.ChildApplicationComponent{Kind: kind, Name: obj.GetName(), Ready: ready}, failed: failed})
}
return out, nil
},
watch: func(mapFunc func(ctx context.Context, src ctlruntime.Request) []ctlruntime.Request) ctlruntime.Option {
return ctlruntime.WatchesIn(export, func(ctx context.Context, src ctlruntime.Request, _ PT) []ctlruntime.Request {
return mapFunc(ctx, src)
})
},
}
}
func componentState(s v1alpha1.ComponentStatus) (bool, bool) {
return s.Ready, s.Phase == v1alpha1.ComponentPhaseFailed
}
var componentKinds = []componentKind{
kindOf(Export, "Bidder", func(l *v1alpha1.BidderList) []v1alpha1.Bidder { return l.Items },
func(o *v1alpha1.Bidder) (bool, bool) { return componentState(o.Status.ComponentStatus) }),
kindOf(Export, "GuestControlPlane", func(l *v1alpha1.GuestControlPlaneList) []v1alpha1.GuestControlPlane { return l.Items },
func(o *v1alpha1.GuestControlPlane) (bool, bool) { return componentState(o.Status) }),
kindOf(Export, "Pds", func(l *v1alpha1.PdsList) []v1alpha1.Pds { return l.Items },
func(o *v1alpha1.Pds) (bool, bool) { return componentState(o.Status) }),
kindOf(Export, "Requester", func(l *v1alpha1.RequesterList) []v1alpha1.Requester { return l.Items },
func(o *v1alpha1.Requester) (bool, bool) { return componentState(o.Status) }),
kindOf(Export, "GatewayAPI", func(l *v1alpha1.GatewayAPIList) []v1alpha1.GatewayAPI { return l.Items },
func(o *v1alpha1.GatewayAPI) (bool, bool) { return componentState(o.Status) }),
kindOf(Export, "SshServer", func(l *v1alpha1.SshServerList) []v1alpha1.SshServer { return l.Items },
func(o *v1alpha1.SshServer) (bool, bool) { return componentState(o.Status) }),
kindOf(Export, "Plc", func(l *v1alpha1.PlcList) []v1alpha1.Plc { return l.Items },
func(o *v1alpha1.Plc) (bool, bool) { return componentState(o.Status) }),
kindOf(Export, "Relay", func(l *v1alpha1.RelayList) []v1alpha1.Relay { return l.Items },
func(o *v1alpha1.Relay) (bool, bool) { return componentState(o.Status) }),
kindOf(Export, "IngressProxy", func(l *v1alpha1.IngressProxyList) []v1alpha1.IngressProxy { return l.Items },
func(o *v1alpha1.IngressProxy) (bool, bool) { return componentState(o.Status) }),
kindOf(Export, "DispatcherTLS", func(l *v1alpha1.DispatcherTLSList) []v1alpha1.DispatcherTLS { return l.Items },
func(o *v1alpha1.DispatcherTLS) (bool, bool) { return componentState(o.Status) }),
kindOf(PolicyExport, "PolicyEngine", func(l *policyv1alpha1.PolicyEngineList) []policyv1alpha1.PolicyEngine { return l.Items },
func(o *policyv1alpha1.PolicyEngine) (bool, bool) {
return o.Status.Ready, o.Status.Phase == policyv1alpha1.PolicyEngineFailed
}),
}
func (c *Controller) observeCore(ctx context.Context, instance string) (string, bool, bool, error) {
var list v1alpha1.HostProcessList
if err := c.objects.List(ctx, DevWorkspacePrefix+":"+instance, &list); err != nil {
if absent(err) {
return "", false, false, nil
}
return "", false, false, err
}
for i := range list.Items {
item := &list.Items[i]
unit := item.Spec.Unit
if !coreUnit(unit, item.Name) {
continue
}
ref := unit
if ref == "" {
ref = item.Name
}
phase := item.Status.Phase
ready := phase == v1alpha1.HostProcessPhaseRunning
failed := phase == v1alpha1.HostProcessPhaseFailed || phase == v1alpha1.HostProcessPhaseLost
return ref, ready, failed, nil
}
return "", false, false, nil
}
func coreUnit(unit, name string) bool {
if strings.HasPrefix(unit, coreUnitPrefix) && strings.HasSuffix(unit, coreUnitSuffix) {
return true
}
return strings.HasPrefix(name, coreObjectPrefix) && strings.HasSuffix(name, coreObjectSuffix)
}
func (c *Controller) observeComponents(ctx context.Context, workspace string) ([]v1alpha1.ChildApplicationComponent, bool, bool, error) {
var out []v1alpha1.ChildApplicationComponent
failed := false
for _, kind := range componentKinds {
items, err := kind.read(ctx, c.objects, workspace+":"+ComponentWorkspace)
if absent(err) {
continue
}
if err != nil {
return nil, false, false, err
}
for _, item := range items {
failed = failed || item.failed
out = append(out, item.ChildApplicationComponent)
}
}
sortComponents(out)
return out, failed, allReady(out), nil
}
func fallbackComponents(appStatus *v1alpha1.NetworkAppStatus) ([]v1alpha1.ChildApplicationComponent, bool, bool) {
if appStatus == nil {
return nil, false, false
}
out := make([]v1alpha1.ChildApplicationComponent, 0, len(appStatus.Components))
failed := false
for _, component := range appStatus.Components {
if component.Phase == string(v1alpha1.ComponentPhaseFailed) {
failed = true
}
out = append(out, v1alpha1.ChildApplicationComponent{Kind: component.Kind, Name: component.Name, Ready: component.Ready})
}
sortComponents(out)
return out, failed, allReady(out)
}
func sortComponents(components []v1alpha1.ChildApplicationComponent) {
sort.Slice(components, func(i, j int) bool {
if components[i].Kind != components[j].Kind {
return components[i].Kind < components[j].Kind
}
return components[i].Name < components[j].Name
})
}
func allReady(components []v1alpha1.ChildApplicationComponent) bool {
if len(components) == 0 {
return false
}
for _, component := range components {
if !component.Ready {
return false
}
}
return true
}