This repository has been archived by the owner on Oct 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
service.go
240 lines (218 loc) · 6.84 KB
/
service.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
// Copyright (c) 2017 Northwestern Mutual.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package spec
import (
"errors"
"net/http"
"strings"
"sync"
"github.com/Sirupsen/logrus"
"github.com/northwesternmutual/kanali/config"
"github.com/spf13/viper"
"k8s.io/kubernetes/pkg/api"
)
// Service in an internal representation of a Kubernetes Service
type Service struct {
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
ClusterIP string `json:"clusterIP,omitempty"`
Port int64 `json:"port,omitempty"`
Labels Labels `json:"labels,omitempty"`
}
// Labels represents labels on a Kubernetes service
type Labels []Label
// Label represents a Kubernetes service label.
// It also represents a Kubernetes as defined in
// a proxy spec
type Label struct {
Name string `json:"name,omitempty"`
Header string `json:"header,omitempty"`
Value string `json:"value,omitempty"`
}
type services []Service
// ServiceFactory is factory that implements a concurrency safe store for Kubernetes services
type ServiceFactory struct {
mutex sync.RWMutex
serviceMap map[string]services
}
// ServiceStore holds all Kubernetes services that Kanali has discovered
// in a cluster. It should not be mutated directly!
var ServiceStore *ServiceFactory
func init() {
ServiceStore = &ServiceFactory{sync.RWMutex{}, map[string]services{}}
}
// Clear will remove all services from the store
func (s *ServiceFactory) Clear() {
s.mutex.Lock()
defer s.mutex.Unlock()
for svc := range s.serviceMap {
delete(s.serviceMap, svc)
}
}
// Update will update a service
func (s *ServiceFactory) Update(obj interface{}) error {
s.mutex.Lock()
defer s.mutex.Unlock()
service, ok := obj.(Service)
if !ok {
return errors.New("grrr - you're only allowed add services to the services store.... duh")
}
return s.set(service)
}
// Set takes a Service and either adds it to the store
// or updates it
func (s *ServiceFactory) Set(obj interface{}) error {
s.mutex.Lock()
defer s.mutex.Unlock()
service, ok := obj.(Service)
if !ok {
return errors.New("grrr - you're only allowed add services to the services store.... duh")
}
return s.set(service)
}
func (s *ServiceFactory) set(service Service) error {
logrus.Infof("Adding new Service named %s", service.Name)
if s.serviceMap[service.Namespace] == nil {
s.serviceMap[service.Namespace] = []Service{service}
return nil
}
for i, svc := range s.serviceMap[service.Namespace] {
if svc.Name == service.Name {
s.serviceMap[service.Namespace][i] = service
return nil
}
}
s.serviceMap[service.Namespace] = append(s.serviceMap[service.Namespace], service)
return nil
}
// IsEmpty reports whether the service store is empty
func (s *ServiceFactory) IsEmpty() bool {
s.mutex.RLock()
defer s.mutex.RUnlock()
return len(s.serviceMap) == 0
}
// Get retrieves a particual service in the store. If not found, nil is returned.
func (s *ServiceFactory) Get(params ...interface{}) (interface{}, error) {
s.mutex.RLock()
defer s.mutex.RUnlock()
if len(params) != 2 {
return nil, errors.New("getting a service requires 2 parameters")
}
svc, ok := params[0].(Service)
if !ok {
return nil, errors.New("first argument should be a service")
}
headers, ok := params[1].(http.Header)
if !ok {
if params[1] != nil {
return nil, errors.New("second argument should either be nil or http.Header")
}
}
if _, ok := s.serviceMap[svc.Namespace]; !ok {
return nil, nil
}
for _, item := range s.serviceMap[svc.Namespace] {
if svc.Name == "" {
if svc.Labels.isSubset(item.Labels, headers) {
return item, nil
}
} else {
if item.Name == svc.Name {
return item, nil
}
}
}
return nil, nil
}
// Delete will remove a particular service from the store
func (s *ServiceFactory) Delete(obj interface{}) (interface{}, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
service, ok := obj.(Service)
if !ok {
return nil, errors.New("there's no way this service could've gotten in here")
}
if _, ok := s.serviceMap[service.Namespace]; !ok {
return nil, nil
}
i, svc := s.serviceMap[service.Namespace].indexOf(service)
if i < 0 {
return nil, nil
}
if len(s.serviceMap[service.Namespace]) == 1 {
delete(s.serviceMap, service.Namespace)
} else {
s.serviceMap[service.Namespace] = append(s.serviceMap[service.Namespace][:i], s.serviceMap[service.Namespace][i+1:]...)
}
return *svc, nil
}
// CreateService transforms a Kubernetes service
// of type api.Service into type Service
func CreateService(s api.Service) Service {
l := Labels{}
for k, v := range s.ObjectMeta.Labels {
l = append(l, Label{Name: k, Value: v})
}
return Service{
Name: s.ObjectMeta.Name,
Namespace: s.ObjectMeta.Namespace,
ClusterIP: s.Spec.ClusterIP,
Labels: l,
}
}
func (one Labels) isSubset(other Labels, headers http.Header) bool {
for _, item := range one {
if !other.contains(item, headers) {
return false
}
}
return true
}
func (one Labels) contains(other Label, headers http.Header) bool {
for _, item := range one {
if other.equals(item, headers) {
return true
}
}
return false
}
func (one Label) equals(other Label, headers http.Header) bool {
if strings.ToLower(one.Name) != strings.ToLower(other.Name) {
return false
}
if len(one.Header) < 1 {
return other.Value == one.Value
}
if len(headers.Get(one.Header)) > 0 {
return strings.ToLower(headers.Get(one.Header)) == strings.ToLower(other.Value)
}
defaultHeaderValue, ok := viper.GetStringMapString(config.FlagProxyDefaultHeaderValues.GetLong())[one.Header]
return ok && len(defaultHeaderValue) > 0 && strings.ToLower(defaultHeaderValue) == strings.ToLower(other.Value)
}
func (a services) indexOf(s Service) (int, *Service) {
// since the name of a service in a namespace
// must be unique, that's all we have to look at
for i, item := range a {
if strings.Compare(item.Name, s.Name) == 0 {
return i, &item
}
}
return -1, nil
}