forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.go
131 lines (107 loc) · 3.48 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
package hybrid
import (
"time"
"github.com/openshift/origin/pkg/proxy/userspace"
unidlingapi "github.com/openshift/origin/pkg/unidling/api"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/proxy"
"k8s.io/kubernetes/pkg/proxy/config"
"k8s.io/kubernetes/pkg/types"
"github.com/golang/glog"
)
type HybridProxier struct {
unidlingProxy *userspace.Proxier
unidlingLoadBalancer config.EndpointsConfigHandler
mainProxy proxy.ProxyProvider
mainLoadBalancer config.EndpointsConfigHandler
serviceConfig *config.ServiceConfig
// 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{}
syncPeriod time.Duration
}
func NewHybridProxier(unidlingLoadBalancer config.EndpointsConfigHandler, unidlingProxy *userspace.Proxier, mainLoadBalancer config.EndpointsConfigHandler, mainProxy proxy.ProxyProvider, syncPeriod time.Duration, serviceConfig *config.ServiceConfig) (*HybridProxier, error) {
return &HybridProxier{
unidlingProxy: unidlingProxy,
mainProxy: mainProxy,
unidlingLoadBalancer: unidlingLoadBalancer,
mainLoadBalancer: mainLoadBalancer,
serviceConfig: serviceConfig,
usingUserspace: nil,
syncPeriod: syncPeriod,
}, nil
}
func (p *HybridProxier) OnServiceUpdate(services []api.Service) {
forIPTables := make([]api.Service, 0, len(services))
forUserspace := []api.Service{}
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.mainProxy.OnServiceUpdate(forIPTables)
}
func (p *HybridProxier) updateUsingUserspace(endpoints []api.Endpoints) {
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) OnEndpointsUpdate(endpoints []api.Endpoints) {
p.updateUsingUserspace(endpoints)
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)
}
}
p.unidlingLoadBalancer.OnEndpointsUpdate(endpoints)
p.mainLoadBalancer.OnEndpointsUpdate(forIPTables)
p.OnServiceUpdate(p.serviceConfig.Config())
}
// 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()
}
}