-
Notifications
You must be signed in to change notification settings - Fork 8
/
proxy.go
214 lines (171 loc) · 5.54 KB
/
proxy.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
// 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 cluster
import (
"context"
"fmt"
"net"
"os"
"os/exec"
"syscall"
"time"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/providers/nscloud/api"
)
const (
buildCluster = "build-cluster"
buildClusterArm64 = "build-cluster-arm64"
)
func NewProxyCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "proxy",
Short: "Runs a unix socket proxy for a well known service.",
Hidden: true,
Args: cobra.NoArgs,
}
kind := cmd.Flags().String("kind", "", "The service being proxied.")
sockPath := cmd.Flags().String("sock_path", "", "If specified listens on the specified path.")
cluster := cmd.Flags().String("cluster", "", "Cluster ID to proxy; or 'build-cluster' and 'build-cluster-arm64' to proxy the build cluster.")
background := cmd.Flags().String("background", "", "If specified runs the proxy in the background, and writes the process PID to the specified path.")
cmd.RunE = fncobra.RunE(func(ctx context.Context, args []string) error {
if *cluster == "" || *kind == "" {
return fnerrors.New("--cluster and --kind are required")
}
if *background != "" {
if *sockPath == "" {
return fnerrors.New("--background requires --sock_path")
}
// Make sure the cluster exists before going to the background.
resolved, err := compatEnsureCluster(ctx, *cluster)
if err != nil {
return err
}
return setupBackgroundProxy(ctx, resolved.ClusterId, *kind, *sockPath, *background)
}
return deprecateRunProxy(ctx, *cluster, *kind, *sockPath)
})
return cmd
}
func setupBackgroundProxy(ctx context.Context, clusterId, kind, sockPath, pidFile string) error {
cmd := exec.Command(os.Args[0], "cluster", "proxy", "--kind", kind, "--sock_path", sockPath, "--cluster", clusterId, "--region", api.RegionName)
cmd.SysProcAttr = &syscall.SysProcAttr{
Foreground: false,
Setsid: true,
}
if err := cmd.Start(); err != nil {
return err
}
pid := cmd.Process.Pid
// Make sure the child process is not cleaned up on exit.
if err := cmd.Process.Release(); err != nil {
return err
}
ctx, done := context.WithTimeout(ctx, 5*time.Second)
defer done()
// Wait until the socket is up.
if err := waitForFile(ctx, sockPath); err != nil {
return fnerrors.New("socket didn't come up in time: %v", err)
}
if pidFile != "" {
return os.WriteFile(pidFile, []byte(fmt.Sprintf("%d", pid)), 0644)
}
return nil
}
func deprecateRunProxy(ctx context.Context, clusterReq, kind, socketPath string) error {
if clusterReq == "" || kind == "" {
return fnerrors.New("--cluster and --kind are required")
}
cluster, err := compatEnsureCluster(ctx, clusterReq)
if err != nil {
return err
}
var connect func(context.Context) (net.Conn, error)
switch kind {
case "buildkit":
buildkitSvc := api.ClusterService(cluster.Cluster, "buildkit")
if buildkitSvc == nil || buildkitSvc.Endpoint == "" {
return fnerrors.New("cluster is missing buildkit")
}
if buildkitSvc.Status != "READY" {
return fnerrors.New("expected buildkit to be READY, saw %q", buildkitSvc.Status)
}
connect = func(ctx context.Context) (net.Conn, error) {
return api.DialEndpoint(ctx, buildkitSvc.Endpoint)
}
case "docker":
connect = func(ctx context.Context) (net.Conn, error) {
token, err := fnapi.FetchToken(ctx)
if err != nil {
return nil, err
}
return connectToDocker(ctx, token, cluster.Cluster)
}
default:
return fnerrors.New("unrecognized kind %q", kind)
}
ctx, cancel := context.WithCancel(ctx)
go func() {
_ = api.StartRefreshing(ctx, api.Methods, cluster.ClusterId, func(err error) error {
fmt.Fprintf(console.Warnings(ctx), "Failed to refresh cluster: %v\n", err)
return nil
})
}()
defer cancel()
if _, err := runUnixSocketProxy(ctx, cluster.ClusterId, unixSockProxyOpts{
Kind: kind,
SocketPath: socketPath,
Blocking: true,
Connect: connect,
AnnounceSocket: func(socketPath string) {
fmt.Fprintf(console.Stdout(ctx), "Listening on %s\n", socketPath)
},
}); err != nil {
return err
}
return nil
}
func compatEnsureCluster(ctx context.Context, clusterID string) (*api.CreateClusterResult, error) {
// This is to be removed; but we used to select the build cluster by passing "build-cluster" as the cluster ID.
if platform, ok := compatClusterIDAsBuildPlatform(clusterID); ok {
return ensureBuildCluster(ctx, platform)
}
return ensureCluster(ctx, clusterID)
}
func compatClusterIDAsBuildPlatform(clusterID string) (api.BuildPlatform, bool) {
switch clusterID {
case buildCluster:
return "amd64", true
case buildClusterArm64:
return "arm64", true
}
return "", false
}
func ensureCluster(ctx context.Context, clusterID string) (*api.CreateClusterResult, error) {
response, err := api.EnsureCluster(ctx, api.Methods, clusterID)
if err != nil {
return nil, err
}
return &api.CreateClusterResult{
ClusterId: response.Cluster.ClusterId,
Cluster: response.Cluster,
Registry: response.Registry,
}, nil
}
func waitForFile(ctx context.Context, path string) error {
for {
if _, err := os.Stat(path); err == nil {
return nil
} else if !os.IsNotExist(err) {
return err
}
if err := ctx.Err(); err != nil {
return err
}
time.Sleep(500 * time.Millisecond)
}
}