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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package compose
import (
"net"
"net/url"
"strings"
commonv1 "github.com/johnandersen777/socialweb-computer-kcp/api/common/v1alpha1"
exposurev1alpha1 "github.com/johnandersen777/socialweb-computer-kcp/api/exposure/v1alpha1"
policyv1alpha1 "github.com/johnandersen777/socialweb-computer-kcp/api/policy/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
)
const (
DefaultIngressProxyHost = "xrpc.fedproxy.com"
OpenTunnelExecutable = "opentunnel"
)
func ServiceNameFor(kind, name string) string {
return "svc-" + strings.ToLower(kind) + "-" + name
}
func ServiceDesired(ws, kind, name string, protocol exposurev1alpha1.ServiceProtocol, handler string) Desired {
return ServiceDesiredNamed(ws, kind, name, ServiceNameFor(kind, name), protocol, handler)
}
func ServiceDesiredNamed(ws, kind, name, serviceName string, protocol exposurev1alpha1.ServiceProtocol, handler string) Desired {
return Desired{Kind: ComponentKindService, Name: serviceName, Spec: exposurev1alpha1.ServiceSpec{
TargetRef: commonv1.ObjectRef{Workspace: ws, Kind: kind, Name: name},
Protocol: protocol,
Handler: handler,
}}
}
func ExposureDesired(kind, name, suffix string, mode exposurev1alpha1.ExposureMode, hostname, path, ingressRef string,
tls exposurev1alpha1.ExposureTLS) Desired {
return ExposureDesiredForService(ServiceNameFor(kind, name), suffix, mode, hostname, path, ingressRef, tls)
}
func ExposureDesiredForService(serviceName, suffix string, mode exposurev1alpha1.ExposureMode, hostname, path, ingressRef string,
tls exposurev1alpha1.ExposureTLS) Desired {
return Desired{Kind: ComponentKindExposure, Name: serviceName + suffix, Spec: exposurev1alpha1.ExposureSpec{
ServiceRef: serviceName,
Mode: mode,
Hostname: hostname,
Path: path,
TLS: tls,
IngressRef: ingressRef,
}}
}
func ExposureTLS(network *v1alpha1.MarketNetworkValue, enabled bool) exposurev1alpha1.ExposureTLS {
out := exposurev1alpha1.ExposureTLS{Enabled: enabled}
if enabled && network != nil {
out.CABundle = network.CABundle
}
return out
}
func FirstHost(hostnames []string) string {
for _, host := range hostnames {
if strings.TrimSpace(host) != "" {
return host
}
}
return ""
}
func LocalExposure(kind, name string, tls exposurev1alpha1.ExposureTLS) Desired {
return ExposureDesired(kind, name, ExposureLocalSuffix, exposurev1alpha1.ExposureModeLocal, "", "", "", tls)
}
func firstIngress(ingress []*v1alpha1.IngressConfig) *v1alpha1.IngressConfig {
for _, value := range ingress {
if value != nil {
return value
}
}
return nil
}
func IngressProviderFor(ingress *v1alpha1.IngressConfig) string {
switch provider := ingress.ProviderName(); provider {
case v1alpha1.IngressProviderXRPCSubscription, v1alpha1.IngressProviderOpenTunnel, v1alpha1.IngressProviderIroh, v1alpha1.IngressProviderWebRTC:
return provider
}
return ""
}
func IngressProxyName(ingress *v1alpha1.IngressConfig, network *v1alpha1.MarketNetworkValue) string {
if ingress != nil && ingress.IngressRef != nil {
if name := strings.TrimSpace(ingress.IngressRef.Name); name != "" {
return name
}
}
if IngressProviderFor(ingress) != "" {
return AppIngressName
}
if network != nil {
return NetworkIngressName
}
return ""
}
func IngressHost(value string) string {
value = strings.TrimSpace(value)
if parsed, err := url.Parse(value); err == nil && parsed.Host != "" {
return parsed.Host
}
return value
}
func IngressHostname(value string) string {
host := IngressHost(value)
if name, _, err := net.SplitHostPort(host); err == nil {
return name
}
return strings.Trim(host, "[]")
}
func IngressProxyDesired(network *v1alpha1.MarketNetworkValue, ingress *v1alpha1.IngressConfig) (Desired, bool) {
name := IngressProxyName(ingress, nil)
provider := IngressProviderFor(ingress)
if ingress == nil || name == "" || provider == "" {
return Desired{}, false
}
spec := v1alpha1.IngressProxySpec{
ComponentSpec: NewComponentSpec("", network),
Provider: provider,
PublicHost: strings.TrimSpace(ingress.PublicHost),
}
if ingress.Xrpc != nil {
spec.IngressProxyHost = strings.TrimSpace(ingress.Xrpc.IngressProxyHost)
spec.RelayURL = strings.TrimSpace(ingress.Xrpc.RelayURL)
spec.RelayRef = ingress.Xrpc.RelayRef
spec.IdentityRef = ingress.Xrpc.IdentityRef
}
if ingress.OpenTunnel != nil {
spec.OpenTunnel = &v1alpha1.IngressProxyOpenTunnel{
Command: append([]string(nil), ingress.OpenTunnel.Command...),
RouteName: strings.TrimSpace(ingress.OpenTunnel.RouteName),
ProxyProtocol: strings.TrimSpace(ingress.OpenTunnel.ProxyProtocol),
LocalHost: strings.TrimSpace(ingress.OpenTunnel.LocalHost),
}
}
if handle := IngressHostname(spec.PublicHost); handle != "" {
spec.Hostnames = []string{handle}
spec.Identity = &v1alpha1.ComponentIdentity{Handle: handle}
}
return Desired{Kind: ComponentKindIngressProxy, Name: name, Spec: spec}, true
}
func IngressExposure(kind, name, hostname string, network *v1alpha1.MarketNetworkValue, ingress ...*v1alpha1.IngressConfig) Desired {
cfg := firstIngress(ingress)
ingressRef := IngressProxyName(cfg, network)
out := ExposureDesired(kind, name, ExposureIngressSuffix, exposurev1alpha1.ExposureModeIngress, hostname, "/",
ingressRef, ExposureTLS(network, true))
spec := out.Spec.(exposurev1alpha1.ExposureSpec)
if cfg != nil {
if cfg.Xrpc != nil {
spec.IngressAddress = IngressHost(cfg.Xrpc.IngressProxyHost)
}
} else if network != nil {
spec.IngressAddress = network.IngressProxyHost
spec.IngressDial = ingressConnectAddress(*network)
}
out.Spec = spec
return out
}
func ingressConnectAddress(n v1alpha1.MarketNetworkValue) string {
_, port, err := net.SplitHostPort(n.IngressProxyHost)
if err != nil || port == "" {
return ""
}
for _, host := range n.Hosts {
if _, candidate, splitErr := net.SplitHostPort(host.Address); splitErr == nil && candidate == port {
return host.Address
}
}
return n.IngressProxyHost
}
func HTTPServiceExposures(ws, kind, name, handler string, network *v1alpha1.MarketNetworkValue, hostnames []string,
local, ingress bool, ingressConfig ...*v1alpha1.IngressConfig) []Desired {
out := []Desired{ServiceDesired(ws, kind, name, exposurev1alpha1.ServiceProtocolHTTP, handler)}
if local {
out = append(out, LocalExposure(kind, name, exposurev1alpha1.ExposureTLS{}))
}
if ingress {
out = append(out, IngressExposure(kind, name, FirstHost(hostnames), network, ingressConfig...))
}
return out
}
func PdsExposures(ws, name string, network *v1alpha1.MarketNetworkValue, hostnames []string, ingress ...*v1alpha1.IngressConfig) []Desired {
cfg := firstIngress(ingress)
out := make([]Desired, 0, 4)
if proxy, ok := IngressProxyDesired(network, cfg); ok {
out = append(out, proxy)
}
return append(out, HTTPServiceExposures(ws, ComponentKindPds, name, "http", network, hostnames, true, true, cfg)...)
}
func SSHExposures(ws, name string) []Desired {
serviceName := ServiceNameFor(ComponentKindSshServer, name) + "-ssh"
return []Desired{
ServiceDesiredNamed(ws, ComponentKindSshServer, name, serviceName, exposurev1alpha1.ServiceProtocolSSH, "ssh"),
ExposureDesiredForService(serviceName, ExposureLocalSuffix, exposurev1alpha1.ExposureModeLocal, "", "", "",
exposurev1alpha1.ExposureTLS{}),
}
}
func PolicyEngineDesired() Desired {
return Desired{Kind: ComponentKindPolicyEngine, Name: PolicyEngineComponent,
Spec: policyv1alpha1.PolicyEngineSpec{BundledActionsEnabled: true}}
}
func DefaultIngressConfig() *v1alpha1.IngressConfig {
return &v1alpha1.IngressConfig{
Provider: v1alpha1.IngressProviderXRPCSubscription,
IngressRef: ComponentRefFor(ComponentKindIngressProxy, AppIngressName),
Xrpc: &v1alpha1.IngressXrpcConfig{IngressProxyHost: DefaultIngressProxyHost},
}
}
func AppIngressOrDefault(defaultIngress bool, spec *v1alpha1.IngressConfig, network *v1alpha1.MarketNetworkValue) *v1alpha1.IngressConfig {
if spec != nil {
return spec
}
if network != nil && network.Ingress != nil {
return nil
}
if defaultIngress {
return DefaultIngressConfig()
}
return nil
}
func NetworkIngressConfig(spec *v1alpha1.IngressConfig, workspace string) *v1alpha1.IngressConfig {
out := &v1alpha1.IngressConfig{}
if spec != nil {
out = &v1alpha1.IngressConfig{
Provider: spec.Provider,
PublicHost: spec.PublicHost,
Xrpc: spec.Xrpc,
OpenTunnel: spec.OpenTunnel,
IngressRef: spec.IngressRef,
}
}
if IngressProviderFor(out) == "" && strings.TrimSpace(out.Provider) == "" {
out.Provider = v1alpha1.IngressProviderXRPCSubscription
}
out.IngressRef = ComponentRefFor(ComponentKindIngressProxy, NetworkIngressName)
out.IngressRef.Workspace = strings.TrimSpace(workspace)
return out
}
func ComponentIdentity(id *v1alpha1.MarketIdentity, handle string) *v1alpha1.ComponentIdentity {
out := &v1alpha1.ComponentIdentity{Handle: handle}
if id != nil {
out.MarketIdentityRef = ComponentRefFor(KindMarketIdentity, id.Name)
out.Did = id.Status.Did
if out.Handle == "" {
out.Handle = id.Status.Handle
}
}
return out
}
func ComponentServe(host string, port int32) *v1alpha1.ComponentServe {
if host == "" {
host = "127.0.0.1"
}
return &v1alpha1.ComponentServe{Host: host, Port: port}
}