forked from anthhub/forwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
178 lines (146 loc) · 3.93 KB
/
utils.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
package forwarder
import (
"context"
"fmt"
"strings"
"golang.org/x/sync/errgroup"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
)
var (
Quiet = true
)
func parseSource(source string) (*Option, error) {
list := strings.Split(source, "/")
if len(list) != 2 {
return nil, fmt.Errorf("invalid source: %v", source)
}
kind := list[0]
name := list[1]
if kind == "svc" || kind == "service" || kind == "services" {
return &Option{ServiceName: name}, nil
}
if kind == "po" || kind == "pod" || kind == "pods" {
return &Option{PodName: name}, nil
}
return nil, fmt.Errorf("invalid source: %v", source)
}
func parseOptions(options []*Option) ([]*Option, error) {
newOptions := []*Option{}
for _, o := range options {
o := o
if o.Namespace == "" {
o.Namespace = "default"
}
if o.Source != "" {
opt, err := parseSource(o.Source)
if err != nil {
return nil, err
}
if opt.ServiceName != "" {
o.ServiceName = opt.ServiceName
}
if opt.PodName != "" {
o.PodName = opt.PodName
}
}
if o.Timeout == 0 {
o.Timeout = defaultTimeout
}
if o.PodName == "" && o.ServiceName == "" {
return nil, fmt.Errorf("please provide a name of pod or service")
}
newOptions = append(newOptions, o)
}
return newOptions, nil
}
func handleOptions(ctx context.Context, options []*Option, config *restclient.Config) ([]*PodOption, error) {
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
podOptions := make([]*PodOption, len(options))
var g errgroup.Group
for index, option := range options {
option := option
index := index
g.Go(func() error {
if option.PodName != "" {
pod, err := clientset.CoreV1().Pods(option.Namespace).Get(ctx, option.PodName, metav1.GetOptions{})
if err != nil {
return err
}
if pod == nil {
return fmt.Errorf("no such pod: %v", option.PodName)
}
podOptions[index] = buildPodOption(option, pod)
return nil
}
svc, err := clientset.CoreV1().Services(option.Namespace).Get(ctx, option.ServiceName, metav1.GetOptions{})
if err != nil {
return err
}
if svc == nil {
return fmt.Errorf("no such service: %+v", option.ServiceName)
}
labels := []string{}
for key, val := range svc.Spec.Selector {
labels = append(labels, key+"="+val)
}
label := strings.Join(labels, ",")
pods, err := clientset.CoreV1().Pods(option.Namespace).List(ctx, metav1.ListOptions{LabelSelector: label})
if err != nil {
return err
}
if len(pods.Items) == 0 {
return fmt.Errorf("no such pods of the service of %v", option.ServiceName)
}
var pod *v1.Pod
findPod:
for _, item := range pods.Items {
for _, condition := range item.Status.Conditions {
if condition.Type == v1.PodReady {
if condition.Status == v1.ConditionTrue {
pod = &item
printf("pod(%s) selected in service(%s)\n", item.Name, option.ServiceName)
break findPod
}
printf("pod(%s) in service(%s) is not ready\n", item.Name, option.ServiceName)
}
}
}
if pod == nil {
return fmt.Errorf("ready pod not found in service(%s)", option.ServiceName)
}
printf("forwarding service(%s) in namespace(%s) to pod(%s) ...\n", option.ServiceName, option.Namespace, pod.Name)
podOptions[index] = buildPodOption(option, pod)
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
return podOptions, nil
}
func buildPodOption(option *Option, pod *v1.Pod) *PodOption {
if option.RemotePort == 0 {
option.RemotePort = int(pod.Spec.Containers[0].Ports[0].ContainerPort)
}
return &PodOption{
LocalPort: option.LocalPort,
PodPort: option.RemotePort,
Pod: v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: pod.Name,
Namespace: pod.Namespace,
},
},
}
}
func printf(format string, v ...any) {
if !Quiet {
fmt.Printf(format+"\n", v...)
}
}