-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmd.go
158 lines (138 loc) · 4 KB
/
cmd.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
package main
import (
"encoding/base64"
"log"
"golang.org/x/crypto/ssh"
v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"go.uber.org/zap"
)
var logger, _ = zap.NewDevelopment()
const VERSION = "0.3.0"
const SSHServicePort int32 = 22
type Services []v1.Service
type GroupedServices map[string]Services
type Keys struct {
SSHPiperPrivateKey string
DownstreamPublicKey string
}
type ServiceKeys map[string]map[string]Keys
func newClient() (kubernetes.Interface, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
return kubernetes.NewForConfig(config)
}
func getServiceList(client kubernetes.Interface) (*v1.ServiceList, error) {
return client.CoreV1().Services("").List(meta_v1.ListOptions{})
}
func hasPort(servicePorts []v1.ServicePort, port int32) bool {
for _, servicePort := range servicePorts {
if servicePort.Port == port {
return true
}
}
return false
}
func filterSSHServices(services Services) Services {
var SSHServices Services
for _, service := range services {
if hasPort(service.Spec.Ports, SSHServicePort) {
SSHServices = append(SSHServices, service)
logger.Info("Service found", zap.String("name", service.Name), zap.String("namespace", service.Namespace))
}
}
return SSHServices
}
func groupByNamespace(services Services) GroupedServices {
groupedServices := GroupedServices{}
for _, service := range services {
groupedServices[service.Namespace] = append(groupedServices[service.Namespace], service)
}
return groupedServices
}
func getKeys(client kubernetes.Interface, namespace string, name string) (Keys, error) {
secret, err := client.CoreV1().Secrets(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
return Keys{}, err
}
publicKey, _, _, _, err := ssh.ParseAuthorizedKey(secret.Data["downstream_id_rsa.pub"])
if err != nil {
return Keys{}, err
}
return Keys{
SSHPiperPrivateKey: string(secret.Data["sshpiper_id_rsa"]),
DownstreamPublicKey: base64.StdEncoding.EncodeToString(publicKey.Marshal()),
}, nil
}
func getServiceKeys(client kubernetes.Interface, services GroupedServices) (ServiceKeys, error) {
serviceKeys := ServiceKeys{}
for namespace, services := range services {
serviceKeys[namespace] = map[string]Keys{}
for _, service := range services {
keys, err := getKeys(client, namespace, service.Name)
if err != nil {
return ServiceKeys{}, err
}
serviceKeys[namespace][service.Name] = keys
}
}
return serviceKeys, nil
}
func registerServices(registry *Registry, services GroupedServices, serviceKeys ServiceKeys) error {
for namespace, services := range services {
switch len(services) {
case 1:
service := services[0]
keys := serviceKeys[namespace][service.Name]
if _, err := registry.RegisterUpstream(&Upstream{
Name: service.Name,
Username: service.Namespace,
Address: service.Spec.ClusterIP,
SSHPiperPrivateKey: keys.SSHPiperPrivateKey,
DownstreamPublicKey: keys.DownstreamPublicKey,
}); err != nil {
return err
}
case 2:
logger.Info("Registration process is skipped. There are two or more services using port for service in namespace.\n", zap.String("namespace", namespace), zap.Int32("port", SSHServicePort))
default:
continue
}
}
return nil
}
func initialize() error {
var services GroupedServices
registry := NewRegistry()
if err := registry.ConnectDatabase(); err != nil {
return err
}
if err := registry.TruncateAll(); err != nil {
return err
}
client, err := newClient()
if err != nil {
return err
}
serviceList, err := getServiceList(client)
if err != nil {
return err
}
services = groupByNamespace(filterSSHServices(serviceList.Items))
serviceKeys, err := getServiceKeys(client, services)
if err != nil {
return err
}
return registerServices(registry, services, serviceKeys)
}
func main() {
logger.Info("Started", zap.String("version", VERSION))
err := initialize()
if err != nil {
log.Fatal(err)
}
}