forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.go
164 lines (141 loc) · 4.47 KB
/
proxy.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
package hybrid
import (
"fmt"
"sync"
"time"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/kubernetes/pkg/api"
kcorelisters "k8s.io/kubernetes/pkg/client/listers/core/internalversion"
"k8s.io/kubernetes/pkg/proxy"
"k8s.io/kubernetes/pkg/proxy/config"
unidlingapi "github.com/openshift/origin/pkg/unidling/api"
"k8s.io/kubernetes/pkg/proxy/userspace"
)
type HybridProxier struct {
unidlingProxy *userspace.Proxier
unidlingLoadBalancer config.EndpointsConfigHandler
mainEndpointsHandler config.EndpointsConfigHandler
mainServicesHandler config.ServiceConfigHandler
mainProxy proxy.ProxyProvider
syncPeriod time.Duration
serviceLister kcorelisters.ServiceLister
// TODO(directxman12): figure out a good way to avoid duplicating this information
// (it's saved in the individual proxies as well)
usingUserspace map[types.NamespacedName]struct{}
usingUserspaceLock sync.Mutex
}
func NewHybridProxier(
unidlingLoadBalancer config.EndpointsConfigHandler,
unidlingProxy *userspace.Proxier,
mainEndpointsHandler config.EndpointsConfigHandler,
mainProxy proxy.ProxyProvider,
mainServicesHandler config.ServiceConfigHandler,
syncPeriod time.Duration,
serviceLister kcorelisters.ServiceLister,
) (*HybridProxier, error) {
return &HybridProxier{
unidlingLoadBalancer: unidlingLoadBalancer,
unidlingProxy: unidlingProxy,
mainEndpointsHandler: mainEndpointsHandler,
mainProxy: mainProxy,
mainServicesHandler: mainServicesHandler,
syncPeriod: syncPeriod,
serviceLister: serviceLister,
usingUserspace: nil,
}, nil
}
func (p *HybridProxier) OnServiceUpdate(services []*api.Service) {
forIPTables := make([]*api.Service, 0, len(services))
forUserspace := []*api.Service{}
p.usingUserspaceLock.Lock()
defer p.usingUserspaceLock.Unlock()
for _, service := range services {
if !api.IsServiceIPSet(service) {
// Skip service with no ClusterIP set
continue
}
svcName := types.NamespacedName{
Namespace: service.Namespace,
Name: service.Name,
}
if _, ok := p.usingUserspace[svcName]; ok {
forUserspace = append(forUserspace, service)
} else {
forIPTables = append(forIPTables, service)
}
}
p.unidlingProxy.OnServiceUpdate(forUserspace)
p.mainServicesHandler.OnServiceUpdate(forIPTables)
}
func (p *HybridProxier) updateUsingUserspace(endpoints []*api.Endpoints) {
p.usingUserspaceLock.Lock()
defer p.usingUserspaceLock.Unlock()
p.usingUserspace = make(map[types.NamespacedName]struct{}, len(endpoints))
for _, endpoint := range endpoints {
hasEndpoints := false
for _, subset := range endpoint.Subsets {
if len(subset.Addresses) > 0 {
hasEndpoints = true
break
}
}
if !hasEndpoints {
if _, ok := endpoint.Annotations[unidlingapi.IdledAtAnnotation]; ok {
svcName := types.NamespacedName{
Namespace: endpoint.Namespace,
Name: endpoint.Name,
}
p.usingUserspace[svcName] = struct{}{}
}
}
}
}
func (p *HybridProxier) getIPTablesEndpoints(endpoints []*api.Endpoints) []*api.Endpoints {
p.usingUserspaceLock.Lock()
defer p.usingUserspaceLock.Unlock()
forIPTables := []*api.Endpoints{}
for _, endpoint := range endpoints {
svcName := types.NamespacedName{
Namespace: endpoint.Namespace,
Name: endpoint.Name,
}
if _, ok := p.usingUserspace[svcName]; !ok {
forIPTables = append(forIPTables, endpoint)
}
}
return forIPTables
}
func (p *HybridProxier) OnEndpointsUpdate(endpoints []*api.Endpoints) {
p.updateUsingUserspace(endpoints)
p.unidlingLoadBalancer.OnEndpointsUpdate(endpoints)
forIPTables := p.getIPTablesEndpoints(endpoints)
p.mainEndpointsHandler.OnEndpointsUpdate(forIPTables)
services, err := p.serviceLister.List(labels.Everything())
if err != nil {
utilruntime.HandleError(fmt.Errorf("Error while listing services from cache: %v", err))
return
}
if services == nil {
services = []*api.Service{}
}
p.OnServiceUpdate(services)
}
// Sync is called to immediately synchronize the proxier state to iptables
func (p *HybridProxier) Sync() {
p.mainProxy.Sync()
p.unidlingProxy.Sync()
}
// SyncLoop runs periodic work. This is expected to run as a goroutine or as the main loop of the app. It does not return.
func (p *HybridProxier) SyncLoop() {
t := time.NewTicker(p.syncPeriod)
defer t.Stop()
for {
<-t.C
glog.V(6).Infof("Periodic sync")
p.mainProxy.Sync()
p.unidlingProxy.Sync()
}
}