generated from kyma-project/template-repository
/
node_port.go
157 lines (136 loc) · 4.02 KB
/
node_port.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
package registry
import (
"context"
"fmt"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"math/rand"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
dockerRegistryNodePort = 32_137
//Available ports according to documentation https://kubernetes.io/docs/concepts/services-networking/service/#type-nodeport
maxNodePort = 32_767
minNodePort = 30_000
)
const (
dockerRegistryService = "serverless-docker-registry"
dockerRegistryPortName = "http-registry"
allNamespaces = ""
)
type nodePortFinder func() int32
type NodePortResolver struct {
nodePortFinder
}
func NewNodePortResolver(finder nodePortFinder) *NodePortResolver {
return &NodePortResolver{nodePortFinder: finder}
}
func (npr *NodePortResolver) ResolveDockerRegistryNodePortFn(ctx context.Context, k8sClient client.Client, namespace string) (int32, error) {
svc, err := getService(ctx, k8sClient, namespace, dockerRegistryService)
if err != nil {
return 0, errors.Wrap(err, fmt.Sprintf("while checking if %s service is installed on cluster", dockerRegistryService))
}
if svc != nil && svc.Spec.Type == corev1.ServiceTypeNodePort {
if isDefaultNodePortValue(svc) {
return dockerRegistryNodePort, nil
}
currentNodePort := getNodePort(svc)
return currentNodePort, nil
}
svcs, err := getAllNodePortServices(ctx, k8sClient)
if err != nil {
return 0, errors.Wrap(err, "while fetching all services from cluster")
}
if possibleConflict(svcs) {
newPort, err := npr.drawEmptyPortNumber(svcs)
if err != nil {
return 0, errors.Wrap(err, "while drawing available port number")
}
return newPort, nil
}
return dockerRegistryNodePort, nil
}
func (npr *NodePortResolver) drawEmptyPortNumber(svcs *corev1.ServiceList) (int32, error) {
nodePorts := map[int32]struct{}{}
for _, svc := range svcs.Items {
for _, port := range svc.Spec.Ports {
nodePorts[port.NodePort] = struct{}{}
}
}
retries := 100
var emptyPort int32
for i := 0; i < retries; i++ {
possibleEmptyPort := npr.nodePortFinder()
if _, ok := nodePorts[possibleEmptyPort]; !ok {
emptyPort = possibleEmptyPort
break
}
}
if emptyPort == 0 {
return 0, errors.New("couldn't draw available port number, try again")
}
return emptyPort, nil
}
func getNodePort(svc *corev1.Service) int32 {
for _, port := range svc.Spec.Ports {
if port.Name == dockerRegistryPortName {
return port.NodePort
}
}
return dockerRegistryNodePort
}
func getService(ctx context.Context, k8sClient client.Client, namespace, name string) (*corev1.Service, error) {
svc := corev1.Service{}
err := k8sClient.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, &svc)
if client.IgnoreNotFound(err) != nil {
return nil, errors.Wrap(err, fmt.Sprintf("while getting %s servicce", name))
}
return &svc, nil
}
func isDefaultNodePortValue(svc *corev1.Service) bool {
ports := svc.Spec.Ports
for _, port := range ports {
if port.NodePort == dockerRegistryNodePort {
return true
}
}
return false
}
func getAllNodePortServices(ctx context.Context, k8sClient client.Client) (*corev1.ServiceList, error) {
svcs := corev1.ServiceList{}
err := k8sClient.List(ctx, &svcs, &client.ListOptions{Namespace: allNamespaces})
if err != nil {
return nil, errors.Wrap(err, "while getting list of all services")
}
nodePortSvcs := &corev1.ServiceList{}
for _, svc := range svcs.Items {
if svc.Spec.Type == corev1.ServiceTypeNodePort {
nodePortSvcs.Items = append(nodePortSvcs.Items, svc)
}
if svc.Spec.Type == corev1.ServiceTypeLoadBalancer {
for _, port := range svc.Spec.Ports {
if port.NodePort != 0 {
nodePortSvcs.Items = append(nodePortSvcs.Items, svc)
break
}
}
}
}
return nodePortSvcs, nil
}
func possibleConflict(svcs *corev1.ServiceList) bool {
for _, svc := range svcs.Items {
ports := svc.Spec.Ports
for _, port := range ports {
if port.NodePort == dockerRegistryNodePort {
return true
}
}
}
return false
}
var _ nodePortFinder = RandomNodePort
func RandomNodePort() int32 {
number := rand.Int31n(maxNodePort - minNodePort)
return minNodePort + number
}