/
register.go
146 lines (132 loc) · 3.49 KB
/
register.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
// Copyright 2019-2023 The Liqo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package discovery
import (
"context"
"errors"
"fmt"
"net"
"github.com/grandcat/zeroconf"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
liqoconst "github.com/liqotech/liqo/pkg/consts"
)
func (discovery *Controller) register(ctx context.Context) {
authPort, err := discovery.getAuthServicePort(ctx)
if err != nil {
klog.Error(err)
return
}
discovery.serverMux.Lock()
discovery.mdnsServerAuth, err = zeroconf.Register(
discovery.LocalCluster.ClusterID,
discovery.mdnsConfig.Service,
discovery.mdnsConfig.Domain,
authPort, nil, discovery.getInterfaces(),
uint32(discovery.mdnsConfig.TTL.Seconds()))
discovery.serverMux.Unlock()
if err != nil {
klog.Error(err)
return
}
defer discovery.shutdownServer()
<-ctx.Done()
}
func (discovery *Controller) shutdownServer() {
discovery.serverMux.Lock()
defer discovery.serverMux.Unlock()
discovery.mdnsServerAuth.Shutdown()
}
// get the NodePort of AuthService.
func (discovery *Controller) getAuthServicePort(ctx context.Context) (int, error) {
var svc v1.Service
key := types.NamespacedName{Namespace: discovery.namespace, Name: liqoconst.AuthServiceName}
if err := discovery.namespacedClient.Get(ctx, key, &svc); err != nil {
klog.Error(err)
return 0, err
}
if svc.Spec.Type != v1.ServiceTypeNodePort {
err := fmt.Errorf("this service has not %s type", v1.ServiceTypeNodePort)
klog.Error(err)
return 0, err
}
if len(svc.Spec.Ports) == 0 || svc.Spec.Ports[0].NodePort == 0 {
err := errors.New("this service has no nodePort")
klog.Error(err)
return 0, err
}
return int(svc.Spec.Ports[0].NodePort), nil
}
func (discovery *Controller) getInterfaces() []net.Interface {
var interfaces []net.Interface
ifaces, err := net.Interfaces()
if err != nil {
return nil
}
podNets, err := discovery.getPodNets()
if err != nil {
return nil
}
for _, ifi := range ifaces {
addrs, err := ifi.Addrs()
if err != nil {
continue
}
// select interfaces with IP addresses not in pod local network
sel := false
for _, addr := range addrs {
ip := getIP(addr)
if !isPod(podNets, ip) {
if ip != nil && ip.To4() != nil {
sel = true
}
}
}
if !sel {
continue
}
if (ifi.Flags & net.FlagUp) == 0 {
continue
}
if (ifi.Flags & net.FlagMulticast) > 0 {
interfaces = append(interfaces, ifi)
}
}
return interfaces
}
func (discovery *Controller) getPodNets() ([]*net.IPNet, error) {
var nodes v1.NodeList
if err := discovery.List(context.TODO(), &nodes); err != nil {
return nil, err
}
res := make([]*net.IPNet, 0, len(nodes.Items))
for i := range nodes.Items {
_, ipnet, err := net.ParseCIDR(nodes.Items[i].Spec.PodCIDR)
if err != nil {
klog.Error(err, err.Error())
continue
}
res = append(res, ipnet)
}
return res, nil
}
func isPod(podNets []*net.IPNet, ip net.IP) bool {
for _, ipnet := range podNets {
if ipnet.Contains(ip) {
return true
}
}
return false
}