/
serviceexportcache.go
217 lines (174 loc) · 7.5 KB
/
serviceexportcache.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
// Copyright Istio 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 (
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
klabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"istio.io/istio/pilot/pkg/features"
"istio.io/istio/pilot/pkg/model"
kubesr "istio.io/istio/pilot/pkg/serviceregistry/kube"
"istio.io/istio/pkg/config"
"istio.io/istio/pkg/config/constants"
"istio.io/istio/pkg/config/host"
"istio.io/istio/pkg/kube/controllers"
"istio.io/istio/pkg/kube/kclient"
"istio.io/istio/pkg/kube/kubetypes"
"istio.io/istio/pkg/kube/mcs"
)
type exportedService struct {
namespacedName types.NamespacedName
discoverability map[host.Name]string
}
// serviceExportCache reads Kubernetes Multi-Cluster Services (MCS) ServiceExport resources in the
// cluster and generates discoverability policies for the endpoints.
type serviceExportCache interface {
// EndpointDiscoverabilityPolicy returns the policy for Service endpoints residing within the current cluster.
EndpointDiscoverabilityPolicy(svc *model.Service) model.EndpointDiscoverabilityPolicy
// ExportedServices returns the list of services that are exported in this cluster. Used for debugging.
ExportedServices() []exportedService
Run(stop <-chan struct{})
// HasSynced indicates whether the kube createClient has synced for the watched resources.
HasSynced() bool
}
// newServiceExportCache creates a new serviceExportCache that observes the given cluster.
func newServiceExportCache(c *Controller) serviceExportCache {
if features.EnableMCSServiceDiscovery {
ec := &serviceExportCacheImpl{
Controller: c,
}
ec.serviceExports = kclient.NewDelayedInformer[controllers.Object](ec.client, mcs.ServiceExportGVR, kubetypes.DynamicInformer, kclient.Filter{
ObjectFilter: ec.client.ObjectFilter(),
})
// Register callbacks for events.
registerHandlers(ec.Controller, ec.serviceExports, "ServiceExports", ec.onServiceExportEvent, nil)
// Set the discoverability policy for the clusterset.local host.
ec.clusterSetLocalPolicySelector = func(svc *model.Service) (policy model.EndpointDiscoverabilityPolicy) {
// If the service is exported in this cluster, allow the endpoints in this cluster to be discoverable
// anywhere in the mesh.
if ec.isExported(namespacedNameForService(svc)) {
return model.AlwaysDiscoverable
}
// Otherwise, endpoints are only discoverable from within the same cluster.
return model.DiscoverableFromSameCluster
}
// Set the discoverability policy for the cluster.local host.
if features.EnableMCSClusterLocal {
// MCS cluster.local mode is enabled. Allow endpoints for the cluster.local host to be
// discoverable only from within the same cluster.
ec.clusterLocalPolicySelector = func(svc *model.Service) (policy model.EndpointDiscoverabilityPolicy) {
return model.DiscoverableFromSameCluster
}
} else {
// MCS cluster.local mode is not enabled, so requests to the cluster.local host are not confined
// to the same cluster. Use the same discoverability policy as for clusterset.local.
ec.clusterLocalPolicySelector = ec.clusterSetLocalPolicySelector
}
return ec
}
// MCS Service discovery is disabled. Use a placeholder cache.
return disabledServiceExportCache{}
}
type discoverabilityPolicySelector func(*model.Service) model.EndpointDiscoverabilityPolicy
// serviceExportCache reads ServiceExport resources for a single cluster.
type serviceExportCacheImpl struct {
*Controller
serviceExports kclient.Untyped
// clusterLocalPolicySelector selects an appropriate EndpointDiscoverabilityPolicy for the cluster.local host.
clusterLocalPolicySelector discoverabilityPolicySelector
// clusterSetLocalPolicySelector selects an appropriate EndpointDiscoverabilityPolicy for the clusterset.local host.
clusterSetLocalPolicySelector discoverabilityPolicySelector
}
func (ec *serviceExportCacheImpl) onServiceExportEvent(_, obj controllers.Object, event model.Event) error {
se := controllers.Extract[*unstructured.Unstructured](obj)
if se == nil {
return nil
}
switch event {
case model.EventAdd, model.EventDelete:
ec.updateXDS(se)
default:
// Don't care about updates.
}
return nil
}
func (ec *serviceExportCacheImpl) updateXDS(se metav1.Object) {
for _, svc := range ec.servicesForNamespacedName(config.NamespacedName(se)) {
// Re-build the endpoints for this service with a new discoverability policy.
// Also update any internal caching.
endpoints := ec.buildEndpointsForService(svc, true)
shard := model.ShardKeyFromRegistry(ec)
ec.opts.XDSUpdater.EDSUpdate(shard, svc.Hostname.String(), se.GetNamespace(), endpoints)
}
}
func (ec *serviceExportCacheImpl) EndpointDiscoverabilityPolicy(svc *model.Service) model.EndpointDiscoverabilityPolicy {
if svc == nil {
// Default policy when the service doesn't exist.
return model.DiscoverableFromSameCluster
}
if strings.HasSuffix(svc.Hostname.String(), "."+constants.DefaultClusterSetLocalDomain) {
return ec.clusterSetLocalPolicySelector(svc)
}
return ec.clusterLocalPolicySelector(svc)
}
func (ec *serviceExportCacheImpl) isExported(name types.NamespacedName) bool {
return ec.serviceExports.Get(name.Name, name.Namespace) != nil
}
func (ec *serviceExportCacheImpl) ExportedServices() []exportedService {
// List all exports in this cluster.
exports := ec.serviceExports.List(metav1.NamespaceAll, klabels.Everything())
ec.RLock()
out := make([]exportedService, 0, len(exports))
for _, export := range exports {
uExport := export.(*unstructured.Unstructured)
es := exportedService{
namespacedName: config.NamespacedName(uExport),
discoverability: make(map[host.Name]string),
}
// Generate the map of all hosts for this service to their discoverability policies.
clusterLocalHost := kubesr.ServiceHostname(uExport.GetName(), uExport.GetNamespace(), ec.opts.DomainSuffix)
clusterSetLocalHost := serviceClusterSetLocalHostname(es.namespacedName)
for _, hostName := range []host.Name{clusterLocalHost, clusterSetLocalHost} {
if svc := ec.servicesMap[hostName]; svc != nil {
es.discoverability[hostName] = ec.EndpointDiscoverabilityPolicy(svc).String()
}
}
out = append(out, es)
}
ec.RUnlock()
return out
}
func (ec *serviceExportCacheImpl) Run(stop <-chan struct{}) {
}
func (ec *serviceExportCacheImpl) HasSynced() bool {
return ec.serviceExports.HasSynced()
}
type disabledServiceExportCache struct{}
var _ serviceExportCache = disabledServiceExportCache{}
func (c disabledServiceExportCache) EndpointDiscoverabilityPolicy(*model.Service) model.EndpointDiscoverabilityPolicy {
return model.AlwaysDiscoverable
}
func (c disabledServiceExportCache) Run(stop <-chan struct{}) {}
func (c disabledServiceExportCache) HasSynced() bool {
return true
}
func (c disabledServiceExportCache) ExportedServices() []exportedService {
// MCS is disabled - returning `nil`, which is semantically different here than an empty list.
return nil
}
func (c disabledServiceExportCache) HasCRDInstalled() bool {
return false
}