-
Notifications
You must be signed in to change notification settings - Fork 9
/
service.go
272 lines (227 loc) · 7.87 KB
/
service.go
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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cuefrontendopaque
import (
"bytes"
"context"
"encoding/json"
"strings"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/types/known/anypb"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
// Needs to be consistent with JSON names of cueResourceClass fields.
var serviceFields = []string{"kind", "port", "exportedPort", "ingress", "annotations", "probe", "probes"}
type cueService struct {
Kind string `json:"kind"`
Port int32 `json:"port"`
ExportedPort int32 `json:"exportedPort"`
Ingress cueIngress `json:"ingress"`
Annotations map[string]string `json:"annotations,omitempty"`
ReadinessProbe *cueServiceProbe `json:"probe"` // `probe: http: "/"`
Probes map[string]*cueServiceProbe `json:"probes"` // `probes: readiness: http: "/"`
}
type cueServiceProbe struct {
Path string `json:"http"`
}
type cueIngress struct {
Enabled bool
LoadBalancer bool
Details CueIngressDetails
}
type CueIngressDetails struct {
// Key is domain.
HttpRoutes map[string][]string `json:"httpRoutes"`
ProviderClass string `json:"provider"`
Domains []string `json:"domains"`
AllowedOrigins []string `json:"allowed_origins"`
Annotations map[string]string `json:"annotations,omitempty"`
}
var _ json.Unmarshaler = &cueIngress{}
func (i *cueIngress) UnmarshalJSON(contents []byte) error {
if contents == nil {
return nil
}
dec := json.NewDecoder(bytes.NewReader(contents))
tok, err := dec.Token()
if err != nil {
return err
}
if tok == json.Delim('{') {
if err := json.Unmarshal(contents, &i.Details); err != nil {
return err
}
i.Enabled = true
return nil
} else if str, ok := tok.(string); ok {
switch strings.ToLower(str) {
case "true":
i.Enabled = true
return nil
case "loadbalancer":
i.LoadBalancer = true
return nil
default:
return fnerrors.New("ingress: expected 'true', 'LoadBalancer', or a full ingress definition got %q", str)
}
} else if b, ok := tok.(bool); ok {
if b {
i.Enabled = true
return nil
}
return fnerrors.New("ingress: expected 'true', 'LoadBalancer', or a full ingress definition got \"%v\"", b)
} else {
return fnerrors.New("ingress: bad value %v, expected 'true', 'LoadBalancer', or a full ingress definition", tok)
}
}
var knownKinds = []string{"tcp", schema.ClearTextGrpcProtocol, schema.GrpcProtocol, schema.HttpProtocol, schema.HttpsProtocol}
func parseService(ctx context.Context, pl pkggraph.PackageLoader, loc pkggraph.Location, name string, svc cueService) (*schema.Server_ServiceSpec, []*schema.Probe, error) {
if svc.Kind != "" && !slices.Contains(knownKinds, svc.Kind) {
return nil, nil, fnerrors.NewWithLocation(loc, "service kind is not supported: %s (support %v)", svc.Kind, strings.Join(knownKinds, ", "))
}
var endpointType schema.Endpoint_Type
if svc.Ingress.Enabled {
endpointType = schema.Endpoint_INTERNET_FACING
} else if svc.Ingress.LoadBalancer {
if err := parsing.RequireFeature(loc.Module, "experimental/service/loadbalancer"); err != nil {
return nil, nil, err
}
endpointType = schema.Endpoint_LOAD_BALANCER
} else {
endpointType = schema.Endpoint_PRIVATE
}
urlMap := &schema.HttpUrlMap{}
for domain, routes := range svc.Ingress.Details.HttpRoutes {
if domain != "*" {
return nil, nil, fnerrors.New("unsupported domain, only support * for now")
}
for _, route := range routes {
urlMap.Entry = append(urlMap.Entry, &schema.HttpUrlMap_Entry{
PathPrefix: route,
})
}
}
var details *anypb.Any
if len(urlMap.Entry) > 0 {
details = &anypb.Any{}
if err := details.MarshalFrom(urlMap); err != nil {
return nil, nil, err
}
}
// For the time being, having a grpc service implies exporting all GRPC services.
if svc.Kind == schema.GrpcProtocol || svc.Kind == schema.ClearTextGrpcProtocol {
if details != nil {
return nil, nil, fnerrors.New("service metadata was already set")
}
details = &anypb.Any{}
if err := details.MarshalFrom(&schema.GrpcExportAllServices{}); err != nil {
return nil, nil, fnerrors.New("failed to serialize grpc configuration: %w", err)
}
}
parsed := &schema.Server_ServiceSpec{
Name: name,
Port: &schema.Endpoint_Port{Name: name, ContainerPort: svc.Port},
ExportedPort: svc.ExportedPort,
EndpointType: endpointType,
}
if svc.Kind != "" {
parsed.Metadata = append(parsed.Metadata, &schema.ServiceMetadata{Protocol: svc.Kind, Details: details})
} else if details != nil {
return nil, nil, fnerrors.New("service metadata is specified without kind")
}
if len(svc.Ingress.Details.AllowedOrigins) > 0 {
if svc.Kind != schema.HttpProtocol {
return nil, nil, fnerrors.New("can only specify CORS when protocol is http")
}
cors := &schema.HttpCors{Enabled: true, AllowedOrigin: svc.Ingress.Details.AllowedOrigins}
packedCors, err := anypb.New(cors)
if err != nil {
return nil, nil, fnerrors.New("failed to pack CORS' configuration: %v", err)
}
parsed.Metadata = append(parsed.Metadata, &schema.ServiceMetadata{
Kind: "http-extension",
Details: packedCors,
})
}
if svc.Ingress.Details.ProviderClass != "" {
ref, err := pkggraph.ParseAndLoadRef(ctx, pl, loc, svc.Ingress.Details.ProviderClass)
if err != nil {
return nil, nil, err
}
parsed.IngressProvider = ref
}
for _, domain := range svc.Ingress.Details.Domains {
parsed.IngressDomain = append(parsed.IngressDomain, &schema.DomainSpec{
Fqdn: domain,
Managed: schema.Domain_USER_SPECIFIED_TLS_MANAGED,
})
}
if len(svc.Ingress.Details.Annotations) > 0 {
if err := parsing.RequireFeature(loc.Module, "experimental/service/ingress_annotations"); err != nil {
return nil, nil, err
}
ingressAnnotations := &schema.ServiceAnnotations{}
for key, value := range svc.Ingress.Details.Annotations {
ingressAnnotations.KeyValue = append(ingressAnnotations.KeyValue, &schema.ServiceAnnotations_KeyValue{
Key: key,
Value: value,
})
}
parsed.IngressAnnotations = ingressAnnotations
}
if len(svc.Annotations) > 0 {
if err := parsing.RequireFeature(loc.Module, "experimental/service/annotations"); err != nil {
return nil, nil, err
}
x := &schema.ServiceAnnotations{}
for key, value := range svc.Annotations {
x.KeyValue = append(x.KeyValue, &schema.ServiceAnnotations_KeyValue{Key: key, Value: value})
}
slices.SortFunc(x.KeyValue, func(a, b *schema.ServiceAnnotations_KeyValue) bool {
if a.Key == b.Key {
return strings.Compare(a.Value, b.Value) < 0
}
return strings.Compare(a.Key, b.Key) < 0
})
serialized, err := anypb.New(x)
if err != nil {
return nil, nil, fnerrors.InternalError("failed to serialize annotations: %w", err)
}
parsed.Metadata = append(parsed.Metadata, &schema.ServiceMetadata{
Details: serialized,
})
}
if svc.Probes != nil && svc.ReadinessProbe != nil {
return nil, nil, fnerrors.AttachLocation(loc, fnerrors.BadInputError("probes and probe are exclusive"))
}
if svc.ReadinessProbe != nil {
probe := &schema.Probe{
Kind: runtime.FnServiceReadyz,
Http: &schema.Probe_Http{
ContainerPort: svc.Port,
Path: svc.ReadinessProbe.Path,
},
}
return parsed, []*schema.Probe{probe}, nil
}
var probes []*schema.Probe
for name, data := range svc.Probes {
kind, err := parseProbeKind(name)
if err != nil {
return nil, nil, fnerrors.AttachLocation(loc, err)
}
probes = append(probes, &schema.Probe{
Kind: kind,
Http: &schema.Probe_Http{
ContainerPort: svc.Port,
Path: data.Path,
},
})
}
return parsed, probes, nil
}