/
conversion.go
252 lines (229 loc) · 9.21 KB
/
conversion.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
245
246
247
248
249
250
251
252
// 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 kube
import (
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/gateway-api/apis/v1beta1"
"istio.io/api/annotation"
"istio.io/api/label"
"istio.io/istio/pilot/pkg/features"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pilot/pkg/serviceregistry/provider"
"istio.io/istio/pkg/cluster"
"istio.io/istio/pkg/config/constants"
"istio.io/istio/pkg/config/host"
"istio.io/istio/pkg/config/kube"
"istio.io/istio/pkg/config/visibility"
"istio.io/istio/pkg/spiffe"
"istio.io/istio/pkg/util/sets"
)
func convertPort(port corev1.ServicePort) *model.Port {
return &model.Port{
Name: port.Name,
Port: int(port.Port),
Protocol: kube.ConvertProtocol(port.Port, port.Name, port.Protocol, port.AppProtocol),
}
}
func ConvertService(svc corev1.Service, domainSuffix string, clusterID cluster.ID) *model.Service {
addrs := []string{constants.UnspecifiedIP}
resolution := model.ClientSideLB
externalName := ""
nodeLocal := false
if svc.Spec.Type == corev1.ServiceTypeExternalName && svc.Spec.ExternalName != "" {
externalName = svc.Spec.ExternalName
if features.EnableExternalNameAlias {
resolution = model.Alias
} else {
resolution = model.DNSLB
}
}
if svc.Spec.InternalTrafficPolicy != nil && *svc.Spec.InternalTrafficPolicy == corev1.ServiceInternalTrafficPolicyLocal {
nodeLocal = true
}
if svc.Spec.ClusterIP == corev1.ClusterIPNone { // headless services should not be load balanced
resolution = model.Passthrough
} else if svc.Spec.ClusterIP != "" {
addrs[0] = svc.Spec.ClusterIP
if len(svc.Spec.ClusterIPs) > 1 {
addrs = svc.Spec.ClusterIPs
}
}
ports := make([]*model.Port, 0, len(svc.Spec.Ports))
for _, port := range svc.Spec.Ports {
ports = append(ports, convertPort(port))
}
var exportTo sets.Set[visibility.Instance]
serviceaccounts := make([]string, 0)
if svc.Annotations[annotation.AlphaCanonicalServiceAccounts.Name] != "" {
serviceaccounts = append(serviceaccounts, strings.Split(svc.Annotations[annotation.AlphaCanonicalServiceAccounts.Name], ",")...)
}
if svc.Annotations[annotation.AlphaKubernetesServiceAccounts.Name] != "" {
for _, ksa := range strings.Split(svc.Annotations[annotation.AlphaKubernetesServiceAccounts.Name], ",") {
serviceaccounts = append(serviceaccounts, kubeToIstioServiceAccount(ksa, svc.Namespace))
}
}
if svc.Annotations[annotation.NetworkingExportTo.Name] != "" {
namespaces := strings.Split(svc.Annotations[annotation.NetworkingExportTo.Name], ",")
exportTo = sets.NewWithLength[visibility.Instance](len(namespaces))
for _, ns := range namespaces {
exportTo.Insert(visibility.Instance(ns))
}
}
istioService := &model.Service{
Hostname: ServiceHostname(svc.Name, svc.Namespace, domainSuffix),
ClusterVIPs: model.AddressMap{
Addresses: map[cluster.ID][]string{
clusterID: addrs,
},
},
Ports: ports,
DefaultAddress: addrs[0],
ServiceAccounts: serviceaccounts,
MeshExternal: len(externalName) > 0,
Resolution: resolution,
CreationTime: svc.CreationTimestamp.Time,
ResourceVersion: svc.ResourceVersion,
Attributes: model.ServiceAttributes{
ServiceRegistry: provider.Kubernetes,
Name: svc.Name,
Namespace: svc.Namespace,
Labels: svc.Labels,
ExportTo: exportTo,
LabelSelectors: svc.Spec.Selector,
},
}
switch svc.Spec.Type {
case corev1.ServiceTypeNodePort:
if _, ok := svc.Annotations[annotation.TrafficNodeSelector.Name]; !ok {
// only do this for istio ingress-gateway services
break
}
// store the service port to node port mappings
portMap := make(map[uint32]uint32)
for _, p := range svc.Spec.Ports {
portMap[uint32(p.Port)] = uint32(p.NodePort)
}
istioService.Attributes.ClusterExternalPorts = map[cluster.ID]map[uint32]uint32{clusterID: portMap}
// address mappings will be done elsewhere
case corev1.ServiceTypeLoadBalancer:
if len(svc.Status.LoadBalancer.Ingress) > 0 {
var lbAddrs []string
for _, ingress := range svc.Status.LoadBalancer.Ingress {
if len(ingress.IP) > 0 {
lbAddrs = append(lbAddrs, ingress.IP)
} else if len(ingress.Hostname) > 0 {
// DO NOT resolve the DNS here. In environments like AWS, the ELB hostname
// does not have a repeatable DNS address and IPs resolved at an earlier point
// in time may not work. So, when we get just hostnames instead of IPs, we need
// to smartly switch from EDS to strict_dns rather than doing the naive thing of
// resolving the DNS name and hoping the resolution is one-time task.
lbAddrs = append(lbAddrs, ingress.Hostname)
}
}
if len(lbAddrs) > 0 {
if istioService.Attributes.ClusterExternalAddresses == nil {
istioService.Attributes.ClusterExternalAddresses = &model.AddressMap{}
}
istioService.Attributes.ClusterExternalAddresses.SetAddressesFor(clusterID, lbAddrs)
}
}
}
istioService.Attributes.Type = string(svc.Spec.Type)
istioService.Attributes.ExternalName = externalName
istioService.Attributes.NodeLocal = nodeLocal
if len(svc.Spec.ExternalIPs) > 0 {
if istioService.Attributes.ClusterExternalAddresses == nil {
istioService.Attributes.ClusterExternalAddresses = &model.AddressMap{}
}
istioService.Attributes.ClusterExternalAddresses.AddAddressesFor(clusterID, svc.Spec.ExternalIPs)
}
return istioService
}
func ExternalNameEndpoints(svc *model.Service) []*model.IstioEndpoint {
if svc.Attributes.ExternalName == "" {
return nil
}
out := make([]*model.IstioEndpoint, 0, len(svc.Ports))
discoverabilityPolicy := model.AlwaysDiscoverable
if features.EnableMCSServiceDiscovery {
// MCS spec does not allow export of external name services.
// See https://github.com/kubernetes/enhancements/tree/master/keps/sig-multicluster/1645-multi-cluster-services-api#exporting-services.
discoverabilityPolicy = model.DiscoverableFromSameCluster
}
for _, portEntry := range svc.Ports {
out = append(out, &model.IstioEndpoint{
Address: svc.Attributes.ExternalName,
EndpointPort: uint32(portEntry.Port),
ServicePortName: portEntry.Name,
Labels: svc.Attributes.Labels,
DiscoverabilityPolicy: discoverabilityPolicy,
})
}
return out
}
// ServiceHostname produces FQDN for a k8s service
func ServiceHostname(name, namespace, domainSuffix string) host.Name {
return host.Name(name + "." + namespace + "." + "svc" + "." + domainSuffix) // Format: "%s.%s.svc.%s"
}
// ServiceHostnameForKR calls ServiceHostname with the name and namespace of the given kubernetes resource.
func ServiceHostnameForKR(obj metav1.Object, domainSuffix string) host.Name {
return ServiceHostname(obj.GetName(), obj.GetNamespace(), domainSuffix)
}
// kubeToIstioServiceAccount converts a K8s service account to an Istio service account
func kubeToIstioServiceAccount(saname string, ns string) string {
return spiffe.MustGenSpiffeURI(ns, saname)
}
// SecureNamingSAN creates the secure naming used for SAN verification from pod metadata
func SecureNamingSAN(pod *corev1.Pod) string {
return spiffe.MustGenSpiffeURI(pod.Namespace, pod.Spec.ServiceAccountName)
}
// PodTLSMode returns the tls mode associated with the pod if pod has been injected with sidecar
func PodTLSMode(pod *corev1.Pod) string {
if pod == nil {
return model.DisabledTLSModeLabel
}
return model.GetTLSModeFromEndpointLabels(pod.Labels)
}
// IsAutoPassthrough determines if a listener should use auto passthrough mode. This is used for
// multi-network. In the Istio API, this is an explicit tls.Mode. However, this mode is not part of
// the gateway-api, and leaks implementation details. We already have an API to declare a Gateway as
// a multi-network gateway, so we will use this as a signal.
// A user who wishes to expose multi-network connectivity should create a listener named "tls-passthrough"
// with TLS.Mode Passthrough.
// For some backwards compatibility, we assume any listener with TLS specified and a port matching
// 15443 (or the label-override for gateway port) is auto-passthrough as well.
func IsAutoPassthrough(gwLabels map[string]string, l v1beta1.Listener) bool {
if l.TLS == nil {
return false
}
if hasListenerMode(l, constants.ListenerModeAutoPassthrough) {
return true
}
_, networkSet := gwLabels[label.TopologyNetwork.Name]
if !networkSet {
return false
}
expectedPort := "15443"
if port, f := gwLabels[label.NetworkingGatewayPort.Name]; f {
expectedPort = port
}
return fmt.Sprint(l.Port) == expectedPort
}
func hasListenerMode(l v1beta1.Listener, mode string) bool {
// TODO if we add a hybrid mode for detecting HBONE/passthrough, also check that here
return l.TLS != nil && l.TLS.Options != nil && string(l.TLS.Options[constants.ListenerModeOption]) == mode
}