-
Notifications
You must be signed in to change notification settings - Fork 5
/
lazy_source.go
146 lines (125 loc) · 3.79 KB
/
lazy_source.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 Aeraki 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 controller
import (
"context"
"fmt"
"sort"
"github.com/aeraki-mesh/lazyxds/cmd/lazyxds/app/config"
"github.com/aeraki-mesh/lazyxds/pkg/model"
"github.com/aeraki-mesh/lazyxds/pkg/utils"
)
func (c *AggregationController) syncLazyService(ctx context.Context, id string) (err error) {
// todo if something wrong, need put back to queue
lazySvc := c.lazyServices[id]
if lazySvc == nil {
return c.deleteLazyService(ctx, id)
}
return c.reconcileLazyService(ctx, lazySvc)
}
func (c *AggregationController) tryReconcileLazyService(ctx context.Context, svc *model.Service) (err error) {
id := svc.ID()
v, ok := c.namespaces.Load(svc.Namespace)
if !ok {
return fmt.Errorf("namespace %s not found", svc.Namespace)
}
ns := v.(*model.Namespace)
svc.UpdateNSLazy(ns.LazyStatus)
if !svc.Status.LazyEnabled && svc.Spec.LazyEnabled {
c.lazyServices[id] = svc
c.lazyServiceController.Add(id)
} else if svc.Status.LazyEnabled && !svc.Spec.LazyEnabled {
delete(c.lazyServices, id)
c.lazyServiceController.Add(id)
}
return nil
}
func (c *AggregationController) reconcileLazyService(ctx context.Context, lazySvc *model.Service) (err error) {
defer func() {
if err == nil {
lazySvc.FinishReconcileLazy()
}
}()
if err := c.syncEnvoyFilterOfLazySource(ctx, lazySvc); err != nil {
return err
}
if err := c.syncSidecarOfLazySource(ctx, lazySvc); err != nil {
return err
}
return nil
}
func (c *AggregationController) reconcileAllLazyServices(ctx context.Context) error {
var err error
for _, ls := range c.lazyServices {
c.lazyServiceController.Add(ls.ID())
}
return err
}
func (c *AggregationController) deleteLazyService(ctx context.Context, id string) error {
defer func() {
v, ok := c.services.Load(id)
if ok {
svc := v.(*model.Service)
svc.FinishReconcileLazy()
}
}()
name, namespace := utils.ParseID(id)
if err := c.removeEnvoyFilter(ctx, name, namespace); err != nil {
return err
}
if err := c.removeSidecar(ctx, name, namespace); err != nil {
return err
}
return nil
}
func (c *AggregationController) visibleServiceOfLazyService(lazySvc *model.Service) map[string]struct{} {
egress := make(map[string]struct{})
for svcID := range lazySvc.EgressService {
binding := c.getHTTPServiceBinding(svcID)
for id := range binding {
egress[id] = struct{}{}
}
}
// currently all tcp service should be visible
c.services.Range(func(key, value interface{}) bool {
svc := value.(*model.Service)
if svc.Namespace == config.IstioNamespace { // istio-system always exported
return true
}
if len(svc.Spec.TCPPorts) > 0 {
egress[key.(string)] = struct{}{}
}
return true
})
return egress
}
func (c *AggregationController) egressListOfLazySource(lazySvc *model.Service) []string {
var list []string
for id := range c.visibleServiceOfLazyService(lazySvc) {
list = append(list, utils.ServiceID2EgressString(id))
}
c.serviceEntries.Range(func(key, value interface{}) bool {
_, ns := utils.ParseID(key.(string))
hosts := value.([]string)
for _, host := range hosts {
list = append(list, fmt.Sprintf("%s/%s", ns, host))
}
return true
})
sort.Slice(list, func(i, j int) bool {
return list[i] < list[j]
})
list = append([]string{"istio-system/*"}, list...)
return list
}