-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.go
189 lines (154 loc) · 4.13 KB
/
client.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
package k8s
import (
"bytes"
"context"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
"github.com/forest33/warthog/business/entity"
"github.com/forest33/warthog/pkg/logger"
)
// Client object capable of interacting with Client.
type Client struct {
ctx context.Context
cfg *entity.Settings
log *logger.Zerolog
}
// New creates a new Client.
func New(ctx context.Context, log *logger.Zerolog) *Client {
return &Client{
ctx: ctx,
log: log,
}
}
// SetSettings sets application settings.
func (c *Client) SetSettings(cfg *entity.Settings) {
c.cfg = cfg
}
// PortForward port forward.
func (c *Client) PortForward(r *entity.K8SPortForward) (entity.PortForwardControl, error) {
config, client, err := c.createClient(r.ClientConfig)
if err != nil {
return nil, err
}
var (
podName string
)
if r.PodName != "" {
podName = r.PodName
} else if r.PodNameSelector != "" {
if podName, err = c.findPod(client, r.Namespace, r.PodNameSelector); err != nil {
return nil, err
}
} else {
return nil, entity.ErrK8SPodNotFound
}
ctrl := &PortForwardControl{
stopCh: make(chan struct{}, 1),
out: &bytes.Buffer{},
errOut: &bytes.Buffer{},
}
readyCh := make(chan struct{})
writeError := func(err error) {
if _, err := ctrl.errOut.Write([]byte(err.Error())); err != nil {
c.log.Error().Msgf("failed write to error stream: %v", err)
}
close(readyCh)
}
if r.ErrHandler != nil {
runtime.ErrorHandlers = []func(error){r.ErrHandler}
}
go func() {
path := fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", r.Namespace, podName)
u, err := url.Parse(config.Host)
if err != nil {
writeError(err)
return
}
transport, upgrader, err := spdy.RoundTripperFor(config)
if err != nil {
writeError(err)
return
}
httpClient := &http.Client{
Transport: transport,
Timeout: time.Duration(*c.cfg.K8SRequestTimeout) * time.Second,
}
dialer := spdy.NewDialer(upgrader, httpClient, http.MethodPost, &url.URL{Scheme: "https", Path: path, Host: u.Host})
fw, err := portforward.New(dialer, []string{fmt.Sprintf("%d:%d", r.LocalPort, r.PodPort)}, ctrl.stopCh, readyCh, ctrl.out, ctrl.errOut)
if err != nil {
writeError(err)
return
}
if err := fw.ForwardPorts(); err != nil {
writeError(err)
return
}
}()
<-readyCh
if ctrl.errOut.Len() != 0 {
return nil, errors.New(ctrl.errOut.String())
}
return ctrl, nil
}
func (c *Client) createClient(cfg *entity.K8SClientConfig) (*rest.Config, *kubernetes.Clientset, error) {
var (
restConfig *rest.Config
err error
)
if cfg.GCSAuth != nil && cfg.GCSAuth.Enabled {
ctx, cancel := context.WithTimeout(c.ctx, time.Duration(*c.cfg.K8SRequestTimeout)*time.Second)
defer cancel()
restConfig, err = c.gcsAuth(ctx, cfg.GCSAuth)
if err != nil {
return nil, nil, err
}
} else {
kubeConfig := cfg.KubeConfigFile
if kubeConfig == "" {
if home := homeDir(); home != "" {
kubeConfig = filepath.Join(home, ".kube", "config")
}
}
restConfig, err = clientcmd.BuildConfigFromFlags("", kubeConfig)
if err != nil {
return nil, nil, err
}
restConfig.BearerToken = cfg.BearerToken
}
restConfig.Timeout = time.Duration(*c.cfg.K8SRequestTimeout) * time.Second
client, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return nil, nil, err
}
return restConfig, client, nil
}
func (c *Client) findPod(client *kubernetes.Clientset, namespace, selector string) (string, error) {
ctx, cancel := context.WithTimeout(c.ctx, time.Duration(*c.cfg.K8SRequestTimeout)*time.Second)
defer cancel()
pods, err := client.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{LabelSelector: selector})
if err != nil {
return "", err
}
if len(pods.Items) == 0 {
return "", entity.ErrK8SPodNotFound
}
return pods.Items[0].Name, nil
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE")
}