-
Notifications
You must be signed in to change notification settings - Fork 8
/
dial.go
126 lines (98 loc) · 3.19 KB
/
dial.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
// 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 api
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"time"
"github.com/gorilla/websocket"
"github.com/jpillora/chisel/share/cnet"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/std/tasks"
)
func DialPort(ctx context.Context, cluster *KubernetesCluster, targetPort int) (net.Conn, error) {
token, err := fnapi.FetchToken(ctx)
if err != nil {
return nil, err
}
return DialPortWithToken(ctx, token, cluster, targetPort)
}
func DialEndpoint(ctx context.Context, endpoint string, opts ...Option) (net.Conn, error) {
token, err := fnapi.FetchToken(ctx)
if err != nil {
return nil, err
}
return DialEndpointWithToken(ctx, token, endpoint, opts...)
}
func DialPortWithToken(ctx context.Context, token fnapi.Token, cluster *KubernetesCluster, targetPort int) (net.Conn, error) {
return DialEndpointWithToken(ctx, token, fmt.Sprintf("wss://gate.%s/%s/%d", cluster.IngressDomain, cluster.ClusterId, targetPort))
}
func DialHostedServiceWithToken(ctx context.Context, token fnapi.Token, cluster *KubernetesCluster, serviceName string, vars url.Values) (net.Conn, error) {
u := url.URL{
Scheme: "wss",
Host: fmt.Sprintf("gate.%s", cluster.IngressDomain),
Path: fmt.Sprintf("/%s/hsvc.%s", cluster.ClusterId, serviceName),
RawQuery: vars.Encode(),
}
return DialEndpointWithToken(ctx, token, u.String())
}
type Option func(*dialOptions)
type dialOptions struct {
refreshClusterID string
}
func WithRefresh(clusterID string) Option {
return func(do *dialOptions) {
do.refreshClusterID = clusterID
}
}
func DialEndpointWithToken(ctx context.Context, token fnapi.Token, endpoint string, opts ...Option) (net.Conn, error) {
d := websocket.Dialer{
HandshakeTimeout: 15 * time.Second,
}
hdrs := http.Header{}
hdrs.Add("Authorization", fnapi.BearerToken(token))
var o dialOptions
for _, opt := range opts {
opt(&o)
}
wsConn, _, err := d.DialContext(ctx, endpoint, hdrs)
if err != nil {
return nil, err
}
conn := cnet.NewWebSocketConn(wsConn)
if o.refreshClusterID != "" {
cancel := StartBackgroundRefreshing(ctx, o.refreshClusterID)
return forwardClose{conn, cancel}, nil
}
return conn, nil
}
func StartBackgroundRefreshing(ctx context.Context, clusterID string) func() {
sink := tasks.SinkFrom(ctx)
backgroundCtx := tasks.WithSink(context.Background(), sink)
ctx, done := context.WithCancel(backgroundCtx)
tasks.Action("endpoint.cluster.refresh").Arg("cluster_id", clusterID).LogLevel(1).LogToSink(sink)
go func() {
_ = StartRefreshing(ctx, Methods, clusterID, func(err error) error {
if !errors.Is(err, context.Canceled) {
fmt.Fprintf(console.Warnings(ctx), "Failed to refresh cluster: %v\n", err)
}
return nil
})
tasks.Action("endpoint.cluster.refresh.done").Arg("cluster_id", clusterID).LogLevel(1).LogToSink(sink)
}()
return done
}
type forwardClose struct {
net.Conn
close func()
}
func (an forwardClose) Close() error {
an.close()
return an.Conn.Close()
}