-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
245 lines (211 loc) · 7.49 KB
/
main.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
// 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 main
import (
"context"
"encoding/base64"
"fmt"
"strings"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"namespacelabs.dev/foundation/framework/kubernetes/kubedef"
"namespacelabs.dev/foundation/framework/kubernetes/kubetool"
"namespacelabs.dev/foundation/framework/provisioning"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/internal/runtime/kubernetes/networking/ingress/nginx"
"namespacelabs.dev/foundation/schema"
)
const (
transcoderServiceName = "grpc-http-transcoder"
gatewayServer = "namespacelabs.dev/foundation/std/networking/gateway/server"
)
func main() {
h := provisioning.NewHandlers()
henv := h.MatchEnv(&schema.Environment{Runtime: "kubernetes"})
henv.HandleStack(configuration{})
provisioning.Handle(h)
}
type configuration struct{}
func (configuration) Apply(ctx context.Context, req provisioning.StackRequest, out *provisioning.ApplyOutput) error {
var transcoderEndpoint *schema.Endpoint
for _, endpoint := range req.Stack.Endpoint {
if endpoint.ServerOwner == gatewayServer && endpoint.ServiceName == transcoderServiceName {
transcoderEndpoint = endpoint
break
}
}
if transcoderEndpoint == nil {
return fnerrors.New("%s: missing endpoint", transcoderServiceName)
}
computedNaming := &schema.ComputedNaming{}
if ok, err := req.CheckUnpackInput(computedNaming); err != nil {
return err
} else if !ok {
return fnerrors.InternalError("ComputedNaming is required")
}
type service struct {
ProtoService string
Server *schema.Stack_Entry
Endpoint *schema.Endpoint
Transcoding *schema.GrpcHttpTranscoding
GrpcProtocol string
}
var endpoints []service
for _, endpoint := range req.Stack.Endpoint {
var protoService, grpcProtocol string
for _, md := range endpoint.ServiceMetadata {
if md.Protocol == schema.ClearTextGrpcProtocol || md.Protocol == schema.GrpcProtocol {
protoService = md.Kind
grpcProtocol = md.Protocol
break
}
}
if endpoint.GetServerOwnerPackage() != req.Focus.GetPackageName() {
continue
}
if protoService == "" || endpoint.Port == nil {
continue
}
for _, md := range endpoint.ServiceMetadata {
t := &schema.GrpcHttpTranscoding{}
if !md.Details.MessageIs(t) {
continue
}
if err := md.Details.UnmarshalTo(t); err != nil {
return fnerrors.New("failed to unmarshal GrpcHttpTranscoding: %w", err)
}
endpoints = append(endpoints, service{
ProtoService: protoService,
Server: req.Focus,
Endpoint: endpoint,
Transcoding: t,
GrpcProtocol: grpcProtocol,
})
break
}
}
// This block embeds runtime logic into the transcoding handler; this means
// that version will be at odds with the fn scheduler. Ideally we'd receive
// these set of domains in.
ingressName := fmt.Sprintf("grpc-gateway-%s", req.Focus.Server.Id)
// If a suffix is being used, constrain the prefix size.
// XXX work in progress.
if computedNaming.DomainFragmentSuffix != "" {
ingressName = "grpc-" + req.Focus.Server.Id[:4]
}
domains, err := runtime.CalculateDomains(req.Env, computedNaming, runtime.DomainsRequest{
ServerID: req.Focus.Server.Id,
Key: ingressName,
Alias: "grpc",
})
if err != nil {
return err
}
proxyBodySize := &nginx.ProxyBodySize{
// Remove the body size limit on nginx level. This is useful for large streaming RPCs as nginx limit
// applies to the cumulative body size constructed out of all the JSON payloads.
// Note Envoy still improses the (default) buffer limit of 1M on unary requests and individual payloads.
Limit: "0",
}
packedProxyBodySize, err := anypb.New(proxyBodySize)
if err != nil {
return fnerrors.New("failed to pack ProxyBodySize configuration: %v", err)
}
cors := &schema.HttpCors{Enabled: true, AllowedOrigin: []string{"*"}}
packedCors, err := anypb.New(cors)
if err != nil {
return fnerrors.New("failed to pack CORS' configuration: %v", err)
}
kr, err := kubetool.FromRequest(req)
if err != nil {
return err
}
for _, x := range endpoints {
fds, err := proto.Marshal(x.Transcoding.FileDescriptorSet)
if err != nil {
return fnerrors.New("failed to marshal FileDescriptorSet: %w", err)
}
annotations, err := kubedef.MakeServiceAnnotations(x.Endpoint)
if err != nil {
return fnerrors.New("failed to calculate annotations: %w", err)
}
out.Invocations = append(out.Invocations, kubedef.Apply{
Description: fmt.Sprintf("HTTP/gRPC transcoder: %s", x.ProtoService),
SetNamespace: kr.CanSetNamespace,
Resource: &httpGrpcTranscoder{
TypeMeta: metav1.TypeMeta{
Kind: "HttpGrpcTranscoder",
APIVersion: "k8s.namespacelabs.dev/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: strings.ToLower(fmt.Sprintf("%s-%s", x.ProtoService, x.Server.Server.Id)),
Namespace: kr.Namespace,
Labels: kubedef.MakeLabels(req.Env, x.Server.Server),
Annotations: annotations,
},
Spec: httpGrpcTranscoderSpec{
FullyQualifiedProtoServiceName: x.ProtoService,
ServiceAddress: x.Endpoint.AllocatedName,
ServicePort: int(x.Endpoint.Port.ContainerPort),
BackendTLS: x.GrpcProtocol == schema.GrpcProtocol,
EncodedProtoDescriptor: base64.StdEncoding.EncodeToString(fds),
},
},
// This instructs the runtime to wait until the CRD above has a
// status.conditions of type Applied, which matches the observed
// generation.
CheckGenerationCondition: &kubedef.CheckGenerationCondition{
Type: "Applied",
},
})
// Only emit ingress entries for internet facing services.
if x.Endpoint.Type != schema.Endpoint_INTERNET_FACING {
continue
}
// XXX handle method level routing.
for _, domain := range domains {
fragment, err := anypb.New(&schema.IngressFragment{
Name: ingressName,
Domain: domain,
Owner: req.Focus.Server.PackageName,
Endpoint: x.Endpoint,
// Point HTTP calls under /{serviceName}/ to Envoy.
HttpPath: []*schema.IngressFragment_IngressHttpPath{{
Path: fmt.Sprintf("/%s/", x.ProtoService),
Owner: x.Endpoint.EndpointOwner,
Service: transcoderEndpoint.AllocatedName,
Port: transcoderEndpoint.Port,
}},
Extension: []*anypb.Any{packedCors, packedProxyBodySize},
Manager: "namespacelabs.dev/foundation/std/grpc/httptranscoding",
})
if err != nil {
return err
}
out.Computed = append(out.Computed, &schema.ComputedConfiguration{
Owner: "namespacelabs.dev/foundation/std/grpc/httptranscoding",
Impl: fragment,
})
}
}
return nil
}
func (configuration) Delete(context.Context, provisioning.StackRequest, *provisioning.DeleteOutput) error {
// XXX unimplemented
return nil
}
type httpGrpcTranscoder struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec httpGrpcTranscoderSpec `json:"spec"`
}
type httpGrpcTranscoderSpec struct {
FullyQualifiedProtoServiceName string `json:"fullyQualifiedProtoServiceName"`
ServiceAddress string `json:"serviceAddress"`
ServicePort int `json:"servicePort"`
BackendTLS bool `json:"backendTls"`
EncodedProtoDescriptor string `json:"encodedProtoDescriptor"`
}