/
handler.go
128 lines (95 loc) · 2.96 KB
/
handler.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package service
import (
"fmt"
"strconv"
"github.com/siderolabs/gen/slices"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
)
// IPMapper is an interface for creating or removing port mappings for services.
type IPMapper interface {
Add(svcName string, hostPort, svcPort int) error
Remove(svcName string)
}
// Handler is a handler for services.
type Handler struct {
ipMapper IPMapper
logger *zap.Logger
annotationKey string
}
// NewHandler returns a new Handler.
func NewHandler(annotationKey string, ipMapper IPMapper, logger *zap.Logger) (*Handler, error) {
if logger == nil {
logger = zap.NewNop()
}
errs := validation.ValidateAnnotations(map[string]string{annotationKey: "65535"}, field.NewPath("metadata", "annotations"))
if len(errs) > 0 {
return nil, fmt.Errorf("invalid annotation key: %w", errs.ToAggregate())
}
if ipMapper == nil {
return nil, fmt.Errorf("ipMapper must not be nil")
}
return &Handler{
annotationKey: annotationKey,
ipMapper: ipMapper,
logger: logger,
}, nil
}
// Handle handles a service.
func (s *Handler) Handle(svc *corev1.Service) error {
if svc == nil {
return fmt.Errorf("service must not be nil")
}
svcName := svc.Name + "." + svc.Namespace
logger := s.logger.With(zap.String("svc-name", svcName))
logger.Debug("handle Service")
annotationIsSet := false
hostPortStr := ""
for key, val := range svc.GetAnnotations() {
if key == s.annotationKey {
annotationIsSet = true
hostPortStr = val
break
}
}
if !annotationIsSet {
logger.Debug("annotation is not set on service")
s.ipMapper.Remove(svcName)
return nil
}
logger.Debug("found annotation", zap.String("key", s.annotationKey), zap.String("value", hostPortStr))
hostPort, err := strconv.Atoi(hostPortStr)
if err != nil {
return fmt.Errorf("invalid host port %q: %w", hostPortStr, err)
}
svcTCPPorts := slices.Filter(svc.Spec.Ports, func(port corev1.ServicePort) bool {
return port.Protocol == corev1.ProtocolTCP
})
if len(svcTCPPorts) == 0 {
logger.Debug("no TCP ports on Service")
s.ipMapper.Remove(svcName)
return nil
}
svcPort := int(svcTCPPorts[0].Port)
if len(svcTCPPorts) > 1 {
logger.Info("more than one TCP port on Service, using the first one", zap.Int("svc-port", svcPort))
}
if err = s.ipMapper.Add(svcName, hostPort, svcPort); err != nil {
return fmt.Errorf("failed to register host port: %w", err)
}
return nil
}
// HandleDelete handles a service deletion.
func (s *Handler) HandleDelete(svcName string) error {
if svcName == "" {
return fmt.Errorf("svcName must not be empty")
}
s.logger.Debug("handle Service delete", zap.String("svc-name", svcName))
s.ipMapper.Remove(svcName)
return nil
}