-
Notifications
You must be signed in to change notification settings - Fork 8
/
expose.go
707 lines (584 loc) · 19.6 KB
/
expose.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
// 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"
"encoding/json"
"errors"
"fmt"
"net"
"net/url"
"regexp"
"strconv"
"strings"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/nerdctl/pkg/labels"
"github.com/containerd/nerdctl/pkg/portutil"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8s "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"namespacelabs.dev/foundation/framework/rpcerrors"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/executor"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/providers/nscloud/api"
"namespacelabs.dev/foundation/internal/providers/nscloud/ctl"
"namespacelabs.dev/foundation/std/tasks"
)
func NewExposeCmd() *cobra.Command {
// `nsc expose` acts as an alias for `nsc expose container`
cmd := newExposeContainerCmd("expose [instance-id]", true)
cmd.AddCommand(newExposeContainerCmd("container [instance-id]", false))
cmd.AddCommand(newExposeKubernetesCmd())
return cmd
}
func newExposeContainerCmd(use string, hidden bool) *cobra.Command {
cmd := &cobra.Command{
Use: use,
Short: "Opens a public ingress to the specified exported port.",
Args: cobra.MaximumNArgs(1),
Hidden: hidden,
}
source := cmd.Flags().String("source", "", "Where to lookup the container.")
containerName := cmd.Flags().String("container", "", "Which container to export.")
containerPorts := cmd.Flags().IntSlice("container_port", nil, "If specified, only exposes the specified ports.")
name := cmd.Flags().String("name", "", "If specified, set the name of the exposed ingress. Only permitted when exposing a single port. By default, ingress names are generated.")
output := cmd.Flags().StringP("output", "o", "plain", "One of plain or json.")
all := cmd.Flags().Bool("all", false, "If set to true, exports one ingress for each exported port of each running container.")
ingressRules := cmd.Flags().StringToString("ingress", map[string]string{}, "Specify ingress rules for ports; specify * to apply rules to any port; separate each rule with ;.")
cmd.RunE = fncobra.RunE(func(ctx context.Context, args []string) error {
if *containerName == "" && !*all {
return fnerrors.New("one of --all or --container is required")
} else if *containerName != "" && *all {
return fnerrors.New("only one of --all or --container may be specified")
}
if hidden && *output == "plain" {
out := console.Warnings(ctx)
fmt.Fprintf(out, "`nsc %s` is deprecated. For backwards compatibility it behaves as `nsc expose container [instance-id]`.\n", use)
fmt.Fprintln(out, "To expose an exported port on a running container `nsc expose container [instance-id].")
fmt.Fprintln(out, "To exponse a Kubernentes Load Balancer use `nsc expose kubernetes [instance-id]`.")
}
cluster, _, err := SelectRunningCluster(ctx, args)
if err != nil {
return err
}
if cluster == nil {
return nil
}
ports, err := selectPorts(ctx, cluster, *source, containerFilter{*all, *containerName})
if err != nil {
return err
}
if len(*containerPorts) > 0 {
filtered, err := filterPorts(ports, *containerPorts)
if err != nil {
return err
}
ports = filtered
}
if *name != "" && len(ports) > 1 {
return fnerrors.New("--name can only be used when exposing a single port")
}
if len(ports) == 0 && *output == "plain" {
fmt.Fprintf(console.Stdout(ctx), "Found no ports to export.\n")
podCount, nscContainerCount, err := countApps(ctx, cluster)
if err != nil {
fmt.Fprintf(console.Debug(ctx), "Failed to create apps summary: %v\n", err)
// Do not propagate errors to the user as this call is best-effort.
return nil
}
if podCount > 0 && nscContainerCount == 0 {
fmt.Fprintf(console.Stdout(ctx), "To expose apps from Kubernetes, run `nsc expose kubernetes` instead.\n")
}
}
portNumbers := make([]int32, len(ports))
for k, x := range ports {
portNumbers[k] = x.ContainerPort
}
filledIn, err := fillInIngressRules(portNumbers, *ingressRules)
if err != nil {
return err
}
var exps []exported
for k, port := range ports {
resp, err := api.RegisterIngress(ctx, api.Methods, api.RegisterIngressRequest{
ClusterId: cluster.ClusterId,
Name: *name,
BackendEndpoint: &api.IngressBackendEndpoint{
Port: port.ExportedPort,
},
HttpMatchRule: filledIn[k].HttpIngressRules,
})
if err != nil {
return err
}
exps = append(exps, exported{
ContainerID: port.ContainerID,
ContainerName: port.ContainerName,
ContainerPort: port.ContainerPort,
URL: "https://" + resp.Fqdn,
})
if *output == "plain" {
fmt.Fprintf(console.Stdout(ctx), "Exported port %d from %s (%s):\n https://%s\n\n",
port.ContainerPort, port.ContainerName, substr(port.ContainerID), resp.Fqdn)
}
}
if *output == "json" {
slices.SortFunc(exps, func(a, b exported) int {
return int(a.ContainerPort - b.ContainerPort)
})
return json.NewEncoder(console.Stdout(ctx)).Encode(exps)
}
return nil
})
return cmd
}
func filterPorts(ports []containerPort, acceptable []int) ([]containerPort, error) {
var filtered []containerPort
matched := map[int]struct{}{}
for _, p := range ports {
if slices.Contains(acceptable, int(p.ContainerPort)) {
filtered = append(filtered, p)
matched[int(p.ContainerPort)] = struct{}{}
}
}
var unmatched []int
for _, p := range acceptable {
if _, ok := matched[p]; !ok {
unmatched = append(unmatched, p)
}
}
switch len(unmatched) {
case 0:
return filtered, nil
case 1:
return nil, fnerrors.New("specified port %d is not exported", unmatched[0])
default:
return nil, fnerrors.New("specified ports %s are not exported", strings.Join(stringify(unmatched), ", "))
}
}
func stringify(values []int) []string {
result := make([]string, len(values))
for k, v := range values {
result[k] = fmt.Sprintf("%d", v)
}
return result
}
type exported struct {
ContainerID string `json:"container_id"`
ContainerName string `json:"container_name"`
ContainerPort int32 `json:"container_port"`
URL string `json:"url"`
}
type containerPort struct {
ContainerID string
ContainerName string
ContainerPort int32
ExportedPort int32
}
type portMap map[int]containerPort
type containerFilter struct {
all bool
containerName string
}
func selectPorts(ctx context.Context, cluster *api.KubernetesCluster, source string, filter containerFilter) ([]containerPort, error) {
return tasks.Return(ctx, tasks.Action("nsc.expose").HumanReadablef("Querying exported ports"), func(ctx context.Context) ([]containerPort, error) {
if source != "" && source != "docker" && source != "containerd" {
return nil, fnerrors.New("--source can be either empty, or one of %q or %q", "docker", "containerd")
}
eg := executor.New(ctx, "port selector")
ports := make([][]containerPort, 2)
if source == "" || source == "docker" {
eg.Go(func(ctx context.Context) error {
dockerPorts, err := selectDockerPorts(ctx, cluster, filter)
if err != nil {
return err
}
ports[0] = dockerPorts
return nil
})
}
if source == "" || source == "containerd" {
eg.Go(func(ctx context.Context) error {
containerdPorts, err := selectContainerdPorts(ctx, cluster, filter)
if err != nil {
return err
}
ports[1] = containerdPorts
return nil
})
}
if err := eg.Wait(); err != nil {
return nil, err
}
return append(ports[0], ports[1]...), nil
})
}
func selectDockerPorts(ctx context.Context, cluster *api.KubernetesCluster, filter containerFilter) ([]containerPort, error) {
// We must fetch a token with our parent context, so we get a task sink etc.
token, err := fnapi.FetchToken(ctx)
if err != nil {
return nil, err
}
docker, err := client.NewClientWithOpts(client.WithDialContext(func(ctx context.Context, network, addr string) (net.Conn, error) {
return connectToDocker(ctx, token, cluster)
}))
if err != nil {
return nil, err
}
defer docker.Close()
data, err := dockerFilterToContainers(ctx, docker, filter)
if err != nil {
return nil, err
}
return buildContainersPortMap(ctx, data...)
}
func dockerFilterToContainers(ctx context.Context, docker *client.Client, filter containerFilter) ([]types.ContainerJSON, error) {
if filter.all {
list, err := docker.ContainerList(ctx, types.ContainerListOptions{})
if err != nil {
return nil, err
}
actual := make([]types.ContainerJSON, len(list))
for k, l := range list {
res, err := docker.ContainerInspect(ctx, l.ID)
if err != nil {
return nil, err
}
actual[k] = res
}
return actual, nil
}
data, err := docker.ContainerInspect(ctx, filter.containerName)
if err != nil {
if client.IsErrNotFound(err) {
return nil, nil
}
return nil, err
}
return []types.ContainerJSON{data}, nil
}
func buildContainersPortMap(ctx context.Context, data ...types.ContainerJSON) ([]containerPort, error) {
exported := portMap{}
for _, data := range data {
internalName := parseContainerName(data.Name)
for port, mapping := range data.NetworkSettings.Ports {
if port.Proto() == "tcp" {
for _, m := range mapping {
if m.HostIP == "0.0.0.0" || m.HostIP == "::" {
parsedPort, err := strconv.ParseInt(m.HostPort, 10, 32)
if err != nil {
return nil, err
}
exported[port.Int()] = containerPort{
ContainerID: data.ID,
ContainerName: internalName,
ContainerPort: int32(port.Int()),
ExportedPort: int32(parsedPort),
}
} else {
fmt.Fprintf(console.Warnings(ctx), "%s: Skipping %d/%s exported to %s (unsupported)\n", data.Name, port.Int(), port.Proto(), m.HostIP)
}
}
} else {
fmt.Fprintf(console.Warnings(ctx), "%s: Skipping unsupported protocol %q, port %d\n", data.Name, port.Proto(), port.Int())
}
}
}
return maps.Values(exported), nil
}
func parseContainerName(name string) string {
internalName := strings.TrimPrefix(name, "/") // docker returns names prefixed by /
return internalName
}
func withContainerd(ctx context.Context, cluster *api.KubernetesCluster, callback func(context.Context, *containerd.Client) error) error {
// We must fetch a token with our parent context, so we get a task sink etc.
token, err := fnapi.FetchToken(ctx)
if err != nil {
return err
}
conn, err := grpc.DialContext(ctx, fmt.Sprintf("%s-containerd", cluster.ClusterId),
grpc.WithBlock(),
grpc.WithContextDialer(func(ctx context.Context, s string) (net.Conn, error) {
vars := url.Values{}
vars.Set("name", "containerd-socket")
return api.DialHostedServiceWithToken(ctx, token, cluster, "unixsocket", vars)
}),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return err
}
defer conn.Close()
ctr, err := containerd.NewWithConn(conn)
if err != nil {
return err
}
defer ctr.Conn()
ctx = namespaces.WithNamespace(ctx, "default")
return callback(ctx, ctr)
}
func selectContainerdPorts(ctx context.Context, cluster *api.KubernetesCluster, filter containerFilter) ([]containerPort, error) {
exported := portMap{}
var filters []string
if filter.containerName != "" {
filters = append(filters,
fmt.Sprintf("labels.%q==%s", labels.Name, filter.containerName),
fmt.Sprintf("id~=^%s.*$", regexp.QuoteMeta(filter.containerName)),
)
}
if err := withContainerd(ctx, cluster, func(ctx context.Context, ctr *containerd.Client) error {
containers, err := ctr.Containers(ctx, filters...)
if err != nil {
return err
}
if len(containers) > 1 && filter.containerName != "" {
return rpcerrors.Errorf(codes.InvalidArgument, "container name matches multiple containers")
}
for _, ctr := range containers {
l, err := ctr.Labels(ctx)
if err != nil {
return err
}
ports, err := portutil.ParsePortsLabel(l)
if err != nil {
return err
}
internalName := parseContainerName(l[labels.Name])
for _, p := range ports {
if p.Protocol == "tcp" && (p.HostIP == "0.0.0.0" || p.HostIP == "::") {
exported[int(p.ContainerPort)] = containerPort{
ContainerID: ctr.ID(),
ContainerName: internalName,
ContainerPort: p.ContainerPort,
ExportedPort: p.HostPort,
}
} else {
fmt.Fprintf(console.Warnings(ctx), "Skipping %d/%s exported to %s (unsupported)\n", p.ContainerPort, p.Protocol, p.HostIP)
}
}
}
return nil
}); err != nil {
return nil, err
}
return maps.Values(exported), nil
}
func substr(id string) string {
if len(id) > 8 {
return id[8:]
}
return id
}
func countApps(ctx context.Context, cluster *api.KubernetesCluster) (int, int, error) {
response, err := api.GetClusterSummary(ctx, api.Methods, cluster.ClusterId, []string{"pods", "nsc/containers"})
if err != nil {
return 0, 0, err
}
var podCount, nscContainerCount int
for _, r := range response.Summary {
switch r.Resource {
case "nsc/containers":
nscContainerCount += len(r.PerResource)
case "pods":
for _, r := range r.PerResource {
if !slices.Contains(ctl.SystemNamespaces, r.Namespace) {
podCount++
}
}
}
}
return podCount, nscContainerCount, nil
}
func newExposeKubernetesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "kubernetes [instance-id]",
Short: "Opens a public ingress to the specified Kubernentes service load balancer.",
Args: cobra.MaximumNArgs(1),
}
name := cmd.Flags().String("name", "", "If specified, set the name of the exposed ingress. Only permitted when exposing a single port. By default, ingress names are generated.")
namespace := cmd.Flags().String("namespace", "", "Namespace of the service load balancer to expose.")
service := cmd.Flags().String("service", "", "Name of the service load balancer to expose.")
port := cmd.Flags().Int32("port", 0, "Which exported Load Balancer port to expose.")
output := cmd.Flags().StringP("output", "o", "plain", "One of plain or json.")
ingressRules := cmd.Flags().StringSlice("ingress", []string{}, "Specify ingress rules. Separate each rule with `,`.")
wait := cmd.Flags().Bool("wait", false, "Wait until the provided service got has a valid ingress to expose.")
wildcard := cmd.Flags().Bool("wildcard", false, "If set, generate a wildcard ingress for the exposed service.")
cmd.RunE = fncobra.RunE(func(ctx context.Context, args []string) error {
cluster, _, err := SelectRunningCluster(ctx, args)
if err != nil {
return err
}
if cluster == nil {
return nil
}
if *namespace == "" {
return fnerrors.New("--namespace is required")
}
if *service == "" {
return fnerrors.New("--service is required")
}
backend, err := selectBackend(ctx, cluster, *namespace, *service, *port, *wait)
if err != nil {
return err
}
var rules []*api.ContainerPort_HttpMatchRule
for _, rule := range *ingressRules {
parsed, err := parseRule(rule)
if err != nil {
return err
}
rules = append(rules, parsed)
}
resp, err := api.RegisterIngress(ctx, api.Methods, api.RegisterIngressRequest{
ClusterId: cluster.ClusterId,
Name: *name,
BackendEndpoint: backend,
HttpMatchRule: rules,
Wildcard: *wildcard,
})
if err != nil {
return err
}
switch *output {
case "json":
var exported = struct {
Name string `json:"string"`
Port int32 `json:"port"`
URL string `json:"url"`
}{
Name: *name,
Port: backend.Port,
URL: "https://" + resp.Fqdn,
}
if err := json.NewEncoder(console.Stdout(ctx)).Encode(exported); err != nil {
return fnerrors.InternalError("failed to encode ingress output as JSON output: %w", err)
}
default:
if *output != "" && *output != "plain" {
fmt.Fprintf(console.Warnings(ctx), "unsupported output %q, defaulting to plain\n", *output)
}
fmt.Fprintf(console.Stdout(ctx), "Exported port %d from %s/%s:\n https://%s\n\n",
backend.Port, *namespace, *service, resp.Fqdn)
}
return nil
})
return cmd
}
func selectBackend(ctx context.Context, cluster *api.KubernetesCluster, ns, service string, port int32, wait bool) (*api.IngressBackendEndpoint, error) {
return tasks.Return(ctx, tasks.Action("nsc.expose-lb").HumanReadablef("Querying exported service load balancers"), func(ctx context.Context) (*api.IngressBackendEndpoint, error) {
cfg := clientcmd.NewDefaultClientConfig(ctl.MakeConfig(cluster), nil)
restcfg, err := cfg.ClientConfig()
if err != nil {
return nil, fnerrors.New("failed to load kubernetes configuration: %w", err)
}
cli, err := k8s.NewForConfig(restcfg)
if err != nil {
return nil, fnerrors.New("failed to create kubernetes client: %w", err)
}
svc, err := cli.CoreV1().Services(ns).Get(ctx, service, metav1.GetOptions{})
if err != nil {
return nil, fnerrors.InvocationError("kubernetes", "failed to query service %q: %w", service, err)
}
if svc.Spec.Type != corev1.ServiceTypeLoadBalancer {
return nil, fnerrors.New("service %q is not of type %s (found type %s)", service, corev1.ServiceTypeLoadBalancer, svc.Spec.Type)
}
port, err := selectPort(svc, port)
if err != nil {
return nil, err
}
if wait {
w, err := cli.CoreV1().Services(ns).Watch(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
defer w.Stop()
for ev := range w.ResultChan() {
fmt.Fprintf(console.Debug(ctx), "saw a new event\n")
svc, ok := ev.Object.(*corev1.Service)
if !ok {
continue
}
if svc.Name != service {
continue
}
ipAddr, err := selectIpAddr(svc)
if err != nil {
var noIp noIpError
if errors.As(err, &noIp) {
continue
}
return nil, err
}
return &api.IngressBackendEndpoint{
IpAddr: ipAddr,
Port: port,
}, nil
}
}
ipAddr, err := selectIpAddr(svc)
if err != nil {
return nil, err
}
return &api.IngressBackendEndpoint{
IpAddr: ipAddr,
Port: port,
}, nil
})
}
func selectPort(svc *corev1.Service, requestedPort int32) (int32, error) {
var ports []int32
for _, port := range svc.Spec.Ports {
ports = append(ports, port.Port)
}
if requestedPort != 0 {
if !slices.Contains(ports, requestedPort) {
return 0, fnerrors.New("service %q does not export port %d. Found ports: %v.", svc.Name, requestedPort, ports)
}
return requestedPort, nil
}
switch len(ports) {
case 0:
return 0, fnerrors.New("service %q exposes no ports", svc.Name)
case 1:
return ports[0], nil
default:
return 0, fnerrors.New("Service %q exports multiple ports %v. Please select one with --port.", svc.Name, ports)
}
}
type noIpError struct {
service string
}
func (e noIpError) Error() string {
return fmt.Sprintf("Service %q has no exported ip addresses. This is unexpected. Please contact support@namespace.so.", e.service)
}
func selectIpAddr(svc *corev1.Service) (string, error) {
var ipAddrs []string
for _, ingress := range svc.Status.LoadBalancer.Ingress {
if ingress.IP == "" {
continue
}
ipAddrs = append(ipAddrs, ingress.IP)
}
switch len(ipAddrs) {
case 0:
return "", noIpError{svc.Name}
case 1:
return ipAddrs[0], nil
default:
return "", fnerrors.New("Service %q has multiple exported ip addresses %v. This is unexpected. Please contact support@namespace.so.", svc.Name, ipAddrs)
}
}