This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 279
/
client.go
223 lines (187 loc) · 7.53 KB
/
client.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
package kube
import (
"net"
mapset "github.com/deckarep/golang-set"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"github.com/openservicemesh/osm/pkg/configurator"
"github.com/openservicemesh/osm/pkg/endpoint"
"github.com/openservicemesh/osm/pkg/identity"
"github.com/openservicemesh/osm/pkg/k8s"
"github.com/openservicemesh/osm/pkg/service"
)
// Ensure interface compliance
var _ endpoint.Provider = (*client)(nil)
var _ service.Provider = (*client)(nil)
// NewClient returns a client that has all components necessary to connect to and maintain state of a Kubernetes cluster.
func NewClient(kubeController k8s.Controller, cfg configurator.Configurator) *client { //nolint: revive // unexported-return
return &client{
kubeController: kubeController,
meshConfigurator: cfg,
}
}
// GetID returns a string descriptor / identifier of the compute provider.
// Required by interfaces: EndpointsProvider, ServiceProvider
func (c *client) GetID() string {
return providerName
}
// ListEndpointsForService retrieves the list of IP addresses for the given service
func (c *client) ListEndpointsForService(svc service.MeshService) []endpoint.Endpoint {
log.Trace().Msgf("Getting Endpoints for MeshService %s on Kubernetes", svc)
kubernetesEndpoints, err := c.kubeController.GetEndpoints(svc)
if err != nil || kubernetesEndpoints == nil {
log.Info().Msgf("No k8s endpoints found for MeshService %s", svc)
return nil
}
var endpoints []endpoint.Endpoint
for _, kubernetesEndpoint := range kubernetesEndpoints.Subsets {
for _, port := range kubernetesEndpoint.Ports {
// If a TargetPort is specified for the service, filter the endpoint by this port.
// This is required to ensure we do not attempt to filter the endpoints when the endpoints
// are being listed for a MeshService whose TargetPort is not known.
if svc.TargetPort != 0 && port.Port != int32(svc.TargetPort) {
// k8s service's port does not match MeshService port, ignore this port
continue
}
for _, address := range kubernetesEndpoint.Addresses {
if svc.Subdomain() != "" && svc.Subdomain() != address.Hostname {
// if there's a subdomain on this meshservice, make sure it matches the endpoint's hostname
continue
}
ip := net.ParseIP(address.IP)
if ip == nil {
log.Error().Msgf("Error parsing endpoint IP address %s for MeshService %s", address.IP, svc)
continue
}
ept := endpoint.Endpoint{
IP: ip,
Port: endpoint.Port(port.Port),
}
endpoints = append(endpoints, ept)
}
}
}
log.Trace().Msgf("Endpoints for MeshService %s: %v", svc, endpoints)
return endpoints
}
// ListEndpointsForIdentity retrieves the list of IP addresses for the given service account
// Note: ServiceIdentity must be in the format "name.namespace" [https://github.com/openservicemesh/osm/issues/3188]
func (c *client) ListEndpointsForIdentity(serviceIdentity identity.ServiceIdentity) []endpoint.Endpoint {
sa := serviceIdentity.ToK8sServiceAccount()
log.Trace().Msgf("[%s] (ListEndpointsForIdentity) Getting Endpoints for service account %s on Kubernetes", c.GetID(), sa)
var endpoints []endpoint.Endpoint
for _, pod := range c.kubeController.ListPods() {
if pod.Namespace != sa.Namespace {
continue
}
if pod.Spec.ServiceAccountName != sa.Name {
continue
}
for _, podIP := range pod.Status.PodIPs {
ip := net.ParseIP(podIP.IP)
if ip == nil {
log.Error().Msgf("[%s] Error parsing IP address %s", c.GetID(), podIP.IP)
break
}
ept := endpoint.Endpoint{IP: ip}
endpoints = append(endpoints, ept)
}
}
log.Trace().Msgf("[%s][ListEndpointsForIdentity] Endpoints for service identity (serviceAccount=%s) %s: %+v", c.GetID(), serviceIdentity, sa, endpoints)
return endpoints
}
// GetServicesForServiceIdentity retrieves a list of services for the given service identity.
func (c *client) GetServicesForServiceIdentity(svcIdentity identity.ServiceIdentity) []service.MeshService {
var meshServices []service.MeshService
svcSet := mapset.NewSet() // mapset is used to avoid duplicate elements in the output list
svcAccount := svcIdentity.ToK8sServiceAccount()
for _, pod := range c.kubeController.ListPods() {
if pod.Namespace != svcAccount.Namespace {
continue
}
if pod.Spec.ServiceAccountName != svcAccount.Name {
continue
}
podLabels := pod.ObjectMeta.Labels
meshServicesForPod := c.getServicesByLabels(podLabels, pod.Namespace)
for _, svc := range meshServicesForPod {
if added := svcSet.Add(svc); added {
meshServices = append(meshServices, svc)
}
}
}
log.Trace().Msgf("[%s] Services for service account %s: %v", c.GetID(), svcAccount, meshServices)
return meshServices
}
// getServicesByLabels gets Kubernetes services whose selectors match the given labels
func (c *client) getServicesByLabels(podLabels map[string]string, targetNamespace string) []service.MeshService {
var finalList []service.MeshService
serviceList := c.kubeController.ListServices()
for _, svc := range serviceList {
// TODO: #1684 Introduce APIs to dynamically allow applying selectors, instead of callers implementing
// filtering themselves
if svc.Namespace != targetNamespace {
continue
}
svcRawSelector := svc.Spec.Selector
// service has no selectors, we do not need to match against the pod label
if len(svcRawSelector) == 0 {
continue
}
selector := labels.Set(svcRawSelector).AsSelector()
if selector.Matches(labels.Set(podLabels)) {
finalList = append(finalList, k8s.ServiceToMeshServices(c.kubeController, *svc)...)
}
}
return finalList
}
// GetResolvableEndpointsForService returns the expected endpoints that are to be reached when the service
// FQDN is resolved
func (c *client) GetResolvableEndpointsForService(svc service.MeshService) []endpoint.Endpoint {
var endpoints []endpoint.Endpoint
// Check if the service has been given Cluster IP
kubeService := c.kubeController.GetService(svc)
if kubeService == nil {
log.Info().Msgf("No k8s services found for MeshService %s", svc)
return nil
}
if len(kubeService.Spec.ClusterIP) == 0 || kubeService.Spec.ClusterIP == corev1.ClusterIPNone {
// If service has no cluster IP or cluster IP is <none>, use final endpoint as resolvable destinations
return c.ListEndpointsForService(svc)
}
// Cluster IP is present
ip := net.ParseIP(kubeService.Spec.ClusterIP)
if ip == nil {
log.Error().Msgf("[%s] Could not parse Cluster IP %s", c.GetID(), kubeService.Spec.ClusterIP)
return nil
}
for _, svcPort := range kubeService.Spec.Ports {
endpoints = append(endpoints, endpoint.Endpoint{
IP: ip,
Port: endpoint.Port(svcPort.Port),
})
}
return endpoints
}
// ListServices returns a list of services that are part of monitored namespaces
func (c *client) ListServices() []service.MeshService {
var services []service.MeshService
for _, svc := range c.kubeController.ListServices() {
services = append(services, k8s.ServiceToMeshServices(c.kubeController, *svc)...)
}
return services
}
// ListServiceIdentitiesForService lists the service identities associated with the given mesh service.
func (c *client) ListServiceIdentitiesForService(svc service.MeshService) []identity.ServiceIdentity {
serviceAccounts, err := c.kubeController.ListServiceIdentitiesForService(svc)
if err != nil {
log.Error().Err(err).Msgf("Error getting ServiceAccounts for Service %s", svc)
return nil
}
var serviceIdentities []identity.ServiceIdentity
for _, svcAccount := range serviceAccounts {
serviceIdentity := svcAccount.ToServiceIdentity()
serviceIdentities = append(serviceIdentities, serviceIdentity)
}
return serviceIdentities
}