-
Notifications
You must be signed in to change notification settings - Fork 8
/
sync.go
244 lines (209 loc) · 8.37 KB
/
sync.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package cli
import (
"context"
"fmt"
"strings"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
ctv1 "github.com/flomesh-io/fsm/pkg/apis/connector/v1alpha1"
"github.com/flomesh-io/fsm/pkg/connector"
"github.com/flomesh-io/fsm/pkg/connector/ctok"
"github.com/flomesh-io/fsm/pkg/connector/ktoc"
"github.com/flomesh-io/fsm/pkg/connector/ktog"
"github.com/flomesh-io/fsm/pkg/constants"
configClientset "github.com/flomesh-io/fsm/pkg/gen/client/config/clientset/versioned"
"github.com/flomesh-io/fsm/pkg/messaging"
)
func (c *client) syncCtoK() {
ctx, cancelFunc := context.WithCancel(context.Background())
c.cancelFuncs = append(c.cancelFuncs, cancelFunc)
if c.GetC2KWithGateway() {
c.waitViaGatewayReady(ctx, c.configClient)
}
syncer := ctok.NewCtoKSyncer(c, c.discClient, c.kubeClient, ctx, Cfg.FsmNamespace)
source := ctok.NewCtoKSource(c, syncer, c.discClient, Cfg.TrustDomain)
syncer.SetMicroAggregator(source)
syncer.Ready()
go source.Run(ctx)
// Build the controller and start it
ctl := &connector.CacheController{Resource: syncer}
go ctl.Run(ctx.Done())
}
func (c *client) syncKtoC() {
ctx, cancelFunc := context.WithCancel(context.Background())
c.cancelFuncs = append(c.cancelFuncs, cancelFunc)
if c.GetK2CWithGateway() {
c.waitViaGatewayReady(ctx, c.configClient)
}
syncer := ktoc.NewKtoCSyncer(c, c.discClient)
go syncer.Run(ctx)
msgBroker := messaging.NewBroker(ctx.Done())
serviceSource := ktoc.NewKtoCSource(c, syncer, ctx, msgBroker, c.kubeClient, c.discClient)
cacheCtl := &connector.CacheController{Resource: serviceSource}
go serviceSource.BroadcastListener(ctx.Done(), c.GetSyncPeriod())
go cacheCtl.Run(ctx.Done())
}
func (c *client) syncKtoG() {
ctx, cancelFunc := context.WithCancel(context.Background())
c.cancelFuncs = append(c.cancelFuncs, cancelFunc)
ingressAddr, egressAddr, clusterIP, externalIP := waitGatewayReady(ctx, c.kubeClient,
c.GetViaIngressIPSelector(),
c.GetViaEgressIPSelector(),
int32(c.GetViaIngressHTTPPort()),
int32(c.GetViaEgressHTTPPort()),
int32(c.GetViaIngressGRPCPort()),
int32(c.GetViaEgressGRPCPort()))
meshConfigClient := c.configClient.ConfigV1alpha3().MeshConfigs(Cfg.FsmNamespace)
meshConfig, err := meshConfigClient.Get(ctx, Cfg.FsmMeshConfigName, metav1.GetOptions{})
if err != nil {
log.Fatal().Err(err)
}
meshConfigChanged := false
viaGateway := &meshConfig.Spec.Connector.ViaGateway
if !strings.EqualFold(viaGateway.IngressAddr, ingressAddr) ||
!strings.EqualFold(viaGateway.EgressAddr, egressAddr) ||
!strings.EqualFold(viaGateway.ClusterIP, clusterIP) ||
!strings.EqualFold(viaGateway.ExternalIP, externalIP) ||
viaGateway.IngressHTTPPort != c.GetViaIngressHTTPPort() ||
viaGateway.IngressGRPCPort != c.GetViaIngressGRPCPort() ||
viaGateway.EgressHTTPPort != c.GetViaEgressHTTPPort() ||
viaGateway.EgressGRPCPort != c.GetViaEgressGRPCPort() {
viaGateway.ClusterIP = clusterIP
viaGateway.ExternalIP = externalIP
viaGateway.IngressAddr = ingressAddr
viaGateway.IngressHTTPPort = c.GetViaIngressHTTPPort()
viaGateway.IngressGRPCPort = c.GetViaIngressGRPCPort()
viaGateway.EgressAddr = egressAddr
viaGateway.EgressHTTPPort = c.GetViaEgressHTTPPort()
viaGateway.EgressGRPCPort = c.GetViaEgressGRPCPort()
meshConfigChanged = true
}
if meshConfigChanged {
_, err = meshConfigClient.Update(ctx, meshConfig, metav1.UpdateOptions{})
if err != nil {
log.Fatal().Err(err)
}
}
gatewaySource := &ktog.GatewaySource{}
syncer := ktog.NewKtoGSyncer(c, gatewaySource)
serviceResource := ktog.NewKtoGSource(
c, syncer, gatewaySource,
Cfg.FsmNamespace,
c.kubeClient, c.gatewayClient,
ctx)
gatewaySource.SetServiceResource(serviceResource)
// Build the controller and start it
gwCtl := &connector.CacheController{Resource: gatewaySource}
// Build the controller and start it
svcCtl := &connector.CacheController{Resource: serviceResource}
go syncer.Run(ctx, gwCtl, svcCtl)
go gwCtl.Run(ctx.Done())
go svcCtl.Run(ctx.Done())
}
func (c *client) waitViaGatewayReady(ctx context.Context, configClient configClientset.Interface) {
meshConfigClient := configClient.ConfigV1alpha3().MeshConfigs(Cfg.FsmNamespace)
for {
meshConfig, err := meshConfigClient.Get(ctx, Cfg.FsmMeshConfigName, metav1.GetOptions{})
if err != nil {
log.Warn().Err(err)
} else {
viaGateway := &meshConfig.Spec.Connector.ViaGateway
if len(viaGateway.IngressAddr) > 0 && len(viaGateway.EgressAddr) > 0 {
c.SetViaIngressAddr(viaGateway.IngressAddr)
c.SetViaIngressHTTPPort(viaGateway.IngressHTTPPort)
c.SetViaIngressGRPCPort(viaGateway.IngressGRPCPort)
c.SetViaEgressAddr(viaGateway.EgressAddr)
c.SetViaEgressHTTPPort(viaGateway.EgressHTTPPort)
c.SetViaEgressGRPCPort(viaGateway.EgressGRPCPort)
break
}
}
time.Sleep(time.Second * 5)
}
}
func waitGatewayReady(ctx context.Context, kubeClient kubernetes.Interface, ingressIPSelector, egressIPSelector ctv1.AddrSelector, viaPorts ...int32) (ingressAddr, egressAddr, clusterIP, externalIP string) {
gatewaySvcName := fmt.Sprintf("%s-%s-%s", constants.FSMGatewayName, Cfg.FsmNamespace, constants.ProtocolTCP)
for {
if fgwSvc, err := kubeClient.CoreV1().Services(Cfg.FsmNamespace).Get(ctx, gatewaySvcName, metav1.GetOptions{}); err == nil {
if fgwSvc != nil {
if foundPorts, uncheckPorts := checkGatewayPorts(viaPorts, fgwSvc); foundPorts {
ingressAddr, egressAddr, clusterIP, externalIP = checkGatewayIPs(fgwSvc, ingressIPSelector, egressIPSelector)
if len(ingressAddr) == 0 {
log.Warn().Msgf("not find %s from fsm gateway: %s", ingressIPSelector, gatewaySvcName)
} else if len(egressAddr) == 0 {
log.Warn().Msgf("not find %s from fsm gateway: %s", egressIPSelector, gatewaySvcName)
} else {
return
}
} else {
log.Warn().Msgf("not find matched port[HTTP:%v] from fsm gateway: %s", uncheckPorts, gatewaySvcName)
}
} else {
log.Warn().Msgf("not find fsm gateway: %s", gatewaySvcName)
}
} else {
log.Warn().Err(err)
}
time.Sleep(time.Second * 5)
}
}
func checkGatewayIPs(fgwSvc *corev1.Service, ingressIPSelector, egressIPSelector ctv1.AddrSelector) (ingressAddr, egressAddr, clusterIP, externalIP string) {
if len(externalIP) == 0 && len(fgwSvc.Spec.ExternalIPs) > 0 && len(fgwSvc.Spec.ExternalIPs[0]) > 0 {
externalIP = fgwSvc.Spec.ExternalIPs[0]
}
if len(externalIP) == 0 && len(fgwSvc.Status.LoadBalancer.Ingress) > 0 && len(fgwSvc.Status.LoadBalancer.Ingress[0].IP) > 0 {
externalIP = fgwSvc.Status.LoadBalancer.Ingress[0].IP
}
if len(clusterIP) == 0 && len(fgwSvc.Spec.ClusterIPs) > 0 && len(fgwSvc.Spec.ClusterIPs[0]) > 0 {
clusterIP = fgwSvc.Spec.ClusterIPs[0]
}
ingressAddr = selectIP(ingressAddr, ingressIPSelector, ctv1.ExternalIP, fgwSvc.Spec.ExternalIPs)
ingressAddr = selectIngressIP(ingressAddr, ingressIPSelector, ctv1.ExternalIP, fgwSvc.Status.LoadBalancer.Ingress)
ingressAddr = selectIP(ingressAddr, ingressIPSelector, ctv1.ClusterIP, fgwSvc.Spec.ClusterIPs)
egressAddr = selectIP(egressAddr, egressIPSelector, ctv1.ExternalIP, fgwSvc.Spec.ExternalIPs)
egressAddr = selectIngressIP(egressAddr, egressIPSelector, ctv1.ExternalIP, fgwSvc.Status.LoadBalancer.Ingress)
egressAddr = selectIP(egressAddr, egressIPSelector, ctv1.ClusterIP, fgwSvc.Spec.ClusterIPs)
return
}
func selectIP(ip string, ipSelector, ipVia ctv1.AddrSelector, ips []string) string {
if len(ip) == 0 && strings.EqualFold(string(ipSelector), string(ipVia)) && len(ips) > 0 && len(ips[0]) > 0 {
return ips[0]
}
return ip
}
func selectIngressIP(ip string, ipSelector, ipVia ctv1.AddrSelector, ingress []corev1.LoadBalancerIngress) string {
if len(ip) == 0 && strings.EqualFold(string(ipSelector), string(ipVia)) && len(ingress) > 0 && len(ingress[0].IP) > 0 {
return ingress[0].IP
}
return ip
}
func checkGatewayPorts(viaPorts []int32, fgwSvc *corev1.Service) (bool, map[int32]bool) {
foundPorts := false
uncheckPorts := make(map[int32]bool)
if len(viaPorts) > 0 {
for _, viaPort := range viaPorts {
if viaPort > 0 {
uncheckPorts[viaPort] = true
}
}
}
if len(fgwSvc.Spec.Ports) > 0 && len(uncheckPorts) > 0 {
for _, port := range fgwSvc.Spec.Ports {
for _, viaPort := range viaPorts {
if viaPort > 0 && port.Port == viaPort {
delete(uncheckPorts, viaPort)
break
}
}
if len(uncheckPorts) == 0 {
break
}
}
if len(uncheckPorts) == 0 {
foundPorts = true
}
}
return foundPorts, uncheckPorts
}