-
Notifications
You must be signed in to change notification settings - Fork 9
/
golang.go
324 lines (271 loc) · 8.93 KB
/
golang.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// 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 golang
import (
"context"
"fmt"
"path/filepath"
"strings"
"golang.org/x/mod/semver"
"google.golang.org/protobuf/types/known/anypb"
"namespacelabs.dev/foundation/internal/build"
"namespacelabs.dev/foundation/internal/build/assets"
source "namespacelabs.dev/foundation/internal/codegen"
"namespacelabs.dev/foundation/internal/codegen/protos"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/gosupport"
"namespacelabs.dev/foundation/internal/integrations"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/internal/planning"
"namespacelabs.dev/foundation/internal/production"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/internal/sdk/golang"
"namespacelabs.dev/foundation/internal/sdk/host"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/execution"
"namespacelabs.dev/foundation/std/execution/defs"
"namespacelabs.dev/foundation/std/pkggraph"
)
const (
grpcNode schema.PackageName = "namespacelabs.dev/foundation/std/go/grpc"
httpNode schema.PackageName = "namespacelabs.dev/foundation/std/go/http"
)
func Register() {
integrations.Register(schema.Framework_GO, impl{})
planning.RegisterEndpointProvider(schema.Framework_GO, impl{})
execution.RegisterFuncs(execution.Funcs[*OpGenNode]{
Handle: func(ctx context.Context, _ *schema.SerializedInvocation, x *OpGenNode) (*execution.HandleResult, error) {
loader, err := execution.Get(ctx, pkggraph.PackageLoaderInjection)
if err != nil {
return nil, err
}
loc, err := loader.Resolve(ctx, schema.PackageName(x.Node.PackageName))
if err != nil {
return nil, err
}
return nil, generateNode(ctx, loader, loc, x.Node, x.LoadedNode, loc.Module.ReadWriteFS())
},
})
execution.RegisterFuncs(execution.Funcs[*OpGenServer]{
Handle: func(ctx context.Context, _ *schema.SerializedInvocation, x *OpGenServer) (*execution.HandleResult, error) {
loader, err := execution.Get(ctx, pkggraph.PackageLoaderInjection)
if err != nil {
return nil, err
}
loc, err := loader.Resolve(ctx, schema.PackageName(x.Server.PackageName))
if err != nil {
return nil, err
}
return nil, generateServer(ctx, loader, loc, x.Server, loc.Module.ReadWriteFS())
},
})
}
type impl struct {
integrations.MaybeTidy
integrations.NoDev
}
func (impl) PrepareBuild(ctx context.Context, _ assets.AvailableBuildAssets, server planning.Server, isFocus bool) (build.Spec, error) {
ext := &FrameworkExt{}
if err := parsing.MustExtension(server.Proto().Ext, ext); err != nil {
return nil, fnerrors.AttachLocation(server.Location, err)
}
bin := &GoBinary{
PackageName: server.Location.PackageName,
GoModulePath: ext.GoModulePath,
GoModule: ext.GoModule,
GoVersion: ext.GoVersion,
SourcePath: server.Location.Rel(),
BinaryName: "server",
}
return bin, nil
}
func (impl) PrepareRun(ctx context.Context, t planning.Server, run *runtime.ContainerRunOpts) error {
run.Command = []string{"/server"}
run.ReadOnlyFilesystem = true
run.RunAs = production.NonRootRunAs(production.Distroless)
return nil
}
func (impl) TidyServer(ctx context.Context, env cfg.Context, pkgs pkggraph.PackageLoader, loc pkggraph.Location, server *schema.Server) error {
ext := &FrameworkExt{}
if err := parsing.MustExtension(server.Ext, ext); err != nil {
return fnerrors.AttachLocation(loc, err)
}
sdk, err := golang.MatchSDK(ext.GoVersion, host.HostPlatform())
if err != nil {
return fnerrors.AttachLocation(loc, err)
}
localSDK, err := compute.GetValue(ctx, sdk)
if err != nil {
return fnerrors.AttachLocation(loc, err)
}
const foundationModule = "namespacelabs.dev/foundation"
var foundationVersion string
for _, dep := range loc.Module.Workspace.Dep {
if dep.ModuleName == foundationModule {
foundationVersion = dep.Version
}
}
if foundationVersion != "" {
mod, _, err := gosupport.LookupGoModule(loc.Abs())
if err != nil {
return err
}
// XXX resolve version back to a tag, to support tag based comparison.
for _, require := range mod.Require {
if v := require.Mod; v.Path == foundationModule {
if pr := semver.Prerelease(v.Version); strings.HasPrefix(pr, "-0.") {
// v0.0.44-0.20220629111102-a3b57dceff40
// Prelease(): -0.20220629111102-a3b57dceff40
parts := strings.Split(pr[3:], "-")
if len(parts) == 2 && len(parts[1]) >= 12 && strings.HasPrefix(foundationVersion, parts[1]) {
foundationVersion = "" // Nothing to do.
break
}
}
}
}
if foundationVersion != "" {
if err := RunGo(ctx, loc, localSDK, "get", "-u", fmt.Sprintf("%s@%s", foundationModule, foundationVersion)); err != nil {
return fnerrors.AttachLocation(loc, err)
}
}
}
if err := RunGo(ctx, loc, localSDK, "mod", "tidy"); err != nil {
return fnerrors.AttachLocation(loc, err)
}
return nil
}
func (impl) GenerateNode(pkg *pkggraph.Package, nodes []*schema.Node) ([]*schema.SerializedInvocation, error) {
var dl defs.DefList
dl.Add("Generate Go node dependencies", &OpGenNode{
Node: pkg.Node(),
LoadedNode: nodes,
}, pkg.PackageName())
var list []*protos.FileDescriptorSetAndDeps
for _, dl := range pkg.Provides {
list = append(list, dl)
}
for _, svc := range pkg.Services {
list = append(list, svc)
}
if len(list) > 0 {
protos, err := protos.Merge(list...)
if err != nil {
return nil, err
}
dl.Add("Generate Go proto sources", &source.OpProtoGen{
PackageName: pkg.PackageName().String(),
Protos: protos,
Framework: schema.Framework_GO,
})
}
return dl.Serialize()
}
func (impl) GenerateServer(pkg *pkggraph.Package, nodes []*schema.Node) ([]*schema.SerializedInvocation, error) {
var dl defs.DefList
dl.Add("Generate Go server dependencies", &OpGenServer{Server: pkg.Server, LoadedNode: nodes}, pkg.PackageName())
return dl.Serialize()
}
func (impl) PreParseServer(ctx context.Context, loc pkggraph.Location, ext *parsing.ServerFrameworkExt) error {
f, gomodFile, err := gosupport.LookupGoModule(loc.Abs())
if err != nil {
return err
}
if f.Go == nil {
return fnerrors.BadInputError("%s: no go definition", gomodFile)
}
rel, err := filepath.Rel(loc.Module.Abs(), gomodFile)
if err != nil {
return err
}
ext.FrameworkSpecific, err = anypb.New(&FrameworkExt{
GoVersion: f.Go.Version,
GoModule: f.Module.Mod.Path,
GoModulePath: filepath.Dir(rel),
})
if err != nil {
return err
}
ext.Include = append(ext.Include, grpcNode)
return nil
}
func (impl) PostParseServer(ctx context.Context, sealed *parsing.Sealed) error {
var needGatewayCount int
for _, dep := range sealed.Deps {
svc := dep.Service
if svc == nil {
continue
}
if svc.ExportServicesAsHttp {
needGatewayCount++
}
// XXX this should be done upstream.
for _, p := range svc.ExportHttp {
sealed.Proto.Server.UrlMap = append(sealed.Proto.Server.UrlMap, &schema.Server_URLMapEntry{
PathPrefix: p.Path,
IngressName: svc.IngressServiceName,
Kind: p.Kind,
PackageName: svc.PackageName,
})
}
}
if len(sealed.Proto.Server.UrlMap) > 0 && !sealed.HasDep(httpNode) {
return fnerrors.NewWithLocation(sealed.Location, "server exposes HTTP paths, it must depend on %s", httpNode)
}
return nil
}
func (impl) DevelopmentPackages() []schema.PackageName {
return nil
}
func packageFrom(loc pkggraph.Location) (string, error) {
return gosupport.ComputeGoPackage(loc.Abs())
}
func (impl) InternalEndpoints(_ *schema.Environment, srv *schema.Server, ports []*schema.Endpoint_Port) ([]*schema.InternalEndpoint, error) {
// XXX have these defined in std/go/core/fn.cue so they're versioned.
var internals = [][2]string{
{"/metrics", "prometheus.io/metrics"},
{"/livez", runtime.FnServiceLivez},
{"/readyz", runtime.FnServiceReadyz},
}
var serverPortName = "server-port"
var serverPort *schema.Endpoint_Port
for _, port := range ports {
if port.Name == serverPortName {
serverPort = port
break
}
}
if serverPort == nil {
// No server port is available during e.g. shutdown.
return nil, nil
}
metadata, err := toServiceMetadata(internals, "http")
if err != nil {
return nil, err
}
return []*schema.InternalEndpoint{{
ServerOwner: srv.PackageName,
Port: serverPort,
ServiceMetadata: metadata,
}}, nil
}
func toServiceMetadata(internals [][2]string, protocol string) ([]*schema.ServiceMetadata, error) {
var metadata []*schema.ServiceMetadata
for _, internal := range internals {
metrics, err := anypb.New(&schema.HttpExportedService{
Path: internal[0],
})
if err != nil {
return nil, err
}
metadata = append(metadata, &schema.ServiceMetadata{
Kind: internal[1],
Protocol: protocol,
Details: metrics,
})
}
return metadata, nil
}