Skip to content

Commit 98de6bd

Browse files
authored
Merge pull request kubernetes#91701 from elweb9858/sessionaffinity
Adding windows implementation for sessionaffinity
2 parents c18bc7e + 44096b8 commit 98de6bd

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

pkg/proxy/winkernel/hnsV2.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,37 @@ func (hns hnsV2) getLoadBalancer(endpoints []endpointsInfo, flags loadBalancerFl
232232
lbFlags |= hcn.LoadBalancerFlagsDSR
233233
}
234234

235-
lb, err := hcn.AddLoadBalancer(
236-
hnsEndpoints,
237-
lbFlags,
238-
lbPortMappingFlags,
239-
sourceVip,
240-
vips,
241-
protocol,
242-
internalPort,
243-
externalPort,
244-
)
235+
lbDistributionType := hcn.LoadBalancerDistributionNone
236+
237+
if flags.sessionAffinity {
238+
lbDistributionType = hcn.LoadBalancerDistributionSourceIP
239+
}
240+
241+
loadBalancer := &hcn.HostComputeLoadBalancer{
242+
SourceVIP: sourceVip,
243+
PortMappings: []hcn.LoadBalancerPortMapping{
244+
{
245+
Protocol: uint32(protocol),
246+
InternalPort: internalPort,
247+
ExternalPort: externalPort,
248+
DistributionType: lbDistributionType,
249+
Flags: lbPortMappingFlags,
250+
},
251+
},
252+
FrontendVIPs: vips,
253+
SchemaVersion: hcn.SchemaVersion{
254+
Major: 2,
255+
Minor: 0,
256+
},
257+
Flags: lbFlags,
258+
}
259+
260+
for _, endpoint := range hnsEndpoints {
261+
loadBalancer.HostComputeEndpoints = append(loadBalancer.HostComputeEndpoints, endpoint.Id)
262+
}
263+
264+
lb, err := loadBalancer.Create()
265+
245266
if err != nil {
246267
return nil, err
247268
}

pkg/proxy/winkernel/proxier.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,12 @@ type loadBalancerInfo struct {
9595
}
9696

9797
type loadBalancerFlags struct {
98-
isILB bool
99-
isDSR bool
100-
localRoutedVIP bool
101-
useMUX bool
102-
preserveDIP bool
98+
isILB bool
99+
isDSR bool
100+
localRoutedVIP bool
101+
useMUX bool
102+
preserveDIP bool
103+
sessionAffinity bool
103104
}
104105

105106
// internal struct for string service information
@@ -488,11 +489,12 @@ type Proxier struct {
488489
// precomputing some number of those and cache for future reuse.
489490
precomputedProbabilities []string
490491

491-
hns HostNetworkService
492-
network hnsNetworkInfo
493-
sourceVip string
494-
hostMac string
495-
isDSR bool
492+
hns HostNetworkService
493+
network hnsNetworkInfo
494+
sourceVip string
495+
hostMac string
496+
isDSR bool
497+
supportedFeatures hcn.SupportedFeatures
496498
}
497499

498500
type localPort struct {
@@ -649,6 +651,7 @@ func NewProxier(
649651
sourceVip: sourceVip,
650652
hostMac: hostMac,
651653
isDSR: isDSR,
654+
supportedFeatures: supportedFeatures,
652655
}
653656

654657
burstSyncs := 2
@@ -1226,9 +1229,15 @@ func (proxier *Proxier) syncProxyRules() {
12261229
if containsPublicIP || containsNodeIP {
12271230
sourceVip = proxier.nodeIP.String()
12281231
}
1232+
1233+
sessionAffinityClientIP := svcInfo.sessionAffinityType == v1.ServiceAffinityClientIP
1234+
if sessionAffinityClientIP && !proxier.supportedFeatures.SessionAffinity {
1235+
klog.Warningf("Session Affinity is not supported on this version of Windows.")
1236+
}
1237+
12291238
hnsLoadBalancer, err := hns.getLoadBalancer(
12301239
hnsEndpoints,
1231-
loadBalancerFlags{isDSR: proxier.isDSR},
1240+
loadBalancerFlags{isDSR: proxier.isDSR, sessionAffinity: sessionAffinityClientIP},
12321241
sourceVip,
12331242
svcInfo.clusterIP.String(),
12341243
Enum(svcInfo.protocol),
@@ -1253,7 +1262,7 @@ func (proxier *Proxier) syncProxyRules() {
12531262
}
12541263
hnsLoadBalancer, err := hns.getLoadBalancer(
12551264
nodePortEndpoints,
1256-
loadBalancerFlags{localRoutedVIP: true},
1265+
loadBalancerFlags{localRoutedVIP: true, sessionAffinity: sessionAffinityClientIP},
12571266
sourceVip,
12581267
"",
12591268
Enum(svcInfo.protocol),
@@ -1274,7 +1283,7 @@ func (proxier *Proxier) syncProxyRules() {
12741283
// Try loading existing policies, if already available
12751284
hnsLoadBalancer, err = hns.getLoadBalancer(
12761285
hnsEndpoints,
1277-
loadBalancerFlags{},
1286+
loadBalancerFlags{sessionAffinity: sessionAffinityClientIP},
12781287
sourceVip,
12791288
externalIP.ip,
12801289
Enum(svcInfo.protocol),
@@ -1297,7 +1306,7 @@ func (proxier *Proxier) syncProxyRules() {
12971306
}
12981307
hnsLoadBalancer, err := hns.getLoadBalancer(
12991308
lbIngressEndpoints,
1300-
loadBalancerFlags{isDSR: svcInfo.preserveDIP || proxier.isDSR, useMUX: svcInfo.preserveDIP, preserveDIP: svcInfo.preserveDIP},
1309+
loadBalancerFlags{isDSR: svcInfo.preserveDIP || proxier.isDSR, useMUX: svcInfo.preserveDIP, preserveDIP: svcInfo.preserveDIP, sessionAffinity: sessionAffinityClientIP},
13011310
sourceVip,
13021311
lbIngressIP.ip,
13031312
Enum(svcInfo.protocol),

0 commit comments

Comments
 (0)