forked from anthhub/forwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
142 lines (118 loc) · 3.14 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
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"
)
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.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, Limit: 1})
if err != nil {
return err
}
if len(pods.Items) == 0 {
return fmt.Errorf("no such pods of the service of %v", option.ServiceName)
}
pod := pods.Items[0]
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,
},
},
}
}