Skip to content

Commit dc29425

Browse files
authored
include external traffic policy comparison into service diffing (#2956)
1 parent bcd729b commit dc29425

File tree

2 files changed

+67
-14
lines changed

2 files changed

+67
-14
lines changed

pkg/cluster/cluster.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,10 @@ func (c *Cluster) compareServices(old, new *v1.Service) (bool, string) {
845845
return false, "new service's selector does not match the current one"
846846
}
847847

848+
if old.Spec.ExternalTrafficPolicy != new.Spec.ExternalTrafficPolicy {
849+
return false, "new service's ExternalTrafficPolicy does not match the current one"
850+
}
851+
848852
return true, ""
849853
}
850854

pkg/cluster/cluster_test.go

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,14 +1341,21 @@ func TestCompareEnv(t *testing.T) {
13411341
}
13421342
}
13431343

1344-
func newService(ann map[string]string, svcT v1.ServiceType, lbSr []string) *v1.Service {
1344+
func newService(
1345+
annotations map[string]string,
1346+
svcType v1.ServiceType,
1347+
sourceRanges []string,
1348+
selector map[string]string,
1349+
policy v1.ServiceExternalTrafficPolicyType) *v1.Service {
13451350
svc := &v1.Service{
13461351
Spec: v1.ServiceSpec{
1347-
Type: svcT,
1348-
LoadBalancerSourceRanges: lbSr,
1352+
Selector: selector,
1353+
Type: svcType,
1354+
LoadBalancerSourceRanges: sourceRanges,
1355+
ExternalTrafficPolicy: policy,
13491356
},
13501357
}
1351-
svc.Annotations = ann
1358+
svc.Annotations = annotations
13521359
return svc
13531360
}
13541361

@@ -1365,13 +1372,18 @@ func TestCompareServices(t *testing.T) {
13651372
},
13661373
}
13671374

1375+
defaultPolicy := v1.ServiceExternalTrafficPolicyTypeCluster
1376+
13681377
serviceWithOwnerReference := newService(
13691378
map[string]string{
13701379
constants.ZalandoDNSNameAnnotation: "clstr.acid.zalan.do",
13711380
constants.ElbTimeoutAnnotationName: constants.ElbTimeoutAnnotationValue,
13721381
},
13731382
v1.ServiceTypeClusterIP,
1374-
[]string{"128.141.0.0/16", "137.138.0.0/16"})
1383+
[]string{"128.141.0.0/16", "137.138.0.0/16"},
1384+
nil,
1385+
defaultPolicy,
1386+
)
13751387

13761388
ownerRef := metav1.OwnerReference{
13771389
APIVersion: "acid.zalan.do/v1",
@@ -1397,14 +1409,16 @@ func TestCompareServices(t *testing.T) {
13971409
constants.ElbTimeoutAnnotationName: constants.ElbTimeoutAnnotationValue,
13981410
},
13991411
v1.ServiceTypeClusterIP,
1400-
[]string{"128.141.0.0/16", "137.138.0.0/16"}),
1412+
[]string{"128.141.0.0/16", "137.138.0.0/16"},
1413+
nil, defaultPolicy),
14011414
new: newService(
14021415
map[string]string{
14031416
constants.ZalandoDNSNameAnnotation: "clstr.acid.zalan.do",
14041417
constants.ElbTimeoutAnnotationName: constants.ElbTimeoutAnnotationValue,
14051418
},
14061419
v1.ServiceTypeClusterIP,
1407-
[]string{"128.141.0.0/16", "137.138.0.0/16"}),
1420+
[]string{"128.141.0.0/16", "137.138.0.0/16"},
1421+
nil, defaultPolicy),
14081422
match: true,
14091423
},
14101424
{
@@ -1415,14 +1429,16 @@ func TestCompareServices(t *testing.T) {
14151429
constants.ElbTimeoutAnnotationName: constants.ElbTimeoutAnnotationValue,
14161430
},
14171431
v1.ServiceTypeClusterIP,
1418-
[]string{"128.141.0.0/16", "137.138.0.0/16"}),
1432+
[]string{"128.141.0.0/16", "137.138.0.0/16"},
1433+
nil, defaultPolicy),
14191434
new: newService(
14201435
map[string]string{
14211436
constants.ZalandoDNSNameAnnotation: "clstr.acid.zalan.do",
14221437
constants.ElbTimeoutAnnotationName: constants.ElbTimeoutAnnotationValue,
14231438
},
14241439
v1.ServiceTypeLoadBalancer,
1425-
[]string{"128.141.0.0/16", "137.138.0.0/16"}),
1440+
[]string{"128.141.0.0/16", "137.138.0.0/16"},
1441+
nil, defaultPolicy),
14261442
match: false,
14271443
reason: `new service's type "LoadBalancer" does not match the current one "ClusterIP"`,
14281444
},
@@ -1434,14 +1450,16 @@ func TestCompareServices(t *testing.T) {
14341450
constants.ElbTimeoutAnnotationName: constants.ElbTimeoutAnnotationValue,
14351451
},
14361452
v1.ServiceTypeLoadBalancer,
1437-
[]string{"128.141.0.0/16", "137.138.0.0/16"}),
1453+
[]string{"128.141.0.0/16", "137.138.0.0/16"},
1454+
nil, defaultPolicy),
14381455
new: newService(
14391456
map[string]string{
14401457
constants.ZalandoDNSNameAnnotation: "clstr.acid.zalan.do",
14411458
constants.ElbTimeoutAnnotationName: constants.ElbTimeoutAnnotationValue,
14421459
},
14431460
v1.ServiceTypeLoadBalancer,
1444-
[]string{"185.249.56.0/22"}),
1461+
[]string{"185.249.56.0/22"},
1462+
nil, defaultPolicy),
14451463
match: false,
14461464
reason: `new service's LoadBalancerSourceRange does not match the current one`,
14471465
},
@@ -1453,14 +1471,16 @@ func TestCompareServices(t *testing.T) {
14531471
constants.ElbTimeoutAnnotationName: constants.ElbTimeoutAnnotationValue,
14541472
},
14551473
v1.ServiceTypeLoadBalancer,
1456-
[]string{"128.141.0.0/16", "137.138.0.0/16"}),
1474+
[]string{"128.141.0.0/16", "137.138.0.0/16"},
1475+
nil, defaultPolicy),
14571476
new: newService(
14581477
map[string]string{
14591478
constants.ZalandoDNSNameAnnotation: "clstr.acid.zalan.do",
14601479
constants.ElbTimeoutAnnotationName: constants.ElbTimeoutAnnotationValue,
14611480
},
14621481
v1.ServiceTypeLoadBalancer,
1463-
[]string{}),
1482+
[]string{},
1483+
nil, defaultPolicy),
14641484
match: false,
14651485
reason: `new service's LoadBalancerSourceRange does not match the current one`,
14661486
},
@@ -1472,10 +1492,39 @@ func TestCompareServices(t *testing.T) {
14721492
constants.ElbTimeoutAnnotationName: constants.ElbTimeoutAnnotationValue,
14731493
},
14741494
v1.ServiceTypeClusterIP,
1475-
[]string{"128.141.0.0/16", "137.138.0.0/16"}),
1495+
[]string{"128.141.0.0/16", "137.138.0.0/16"},
1496+
nil, defaultPolicy),
14761497
new: serviceWithOwnerReference,
14771498
match: false,
14781499
},
1500+
{
1501+
about: "new service has a label selector",
1502+
current: newService(
1503+
map[string]string{},
1504+
v1.ServiceTypeClusterIP,
1505+
[]string{},
1506+
nil, defaultPolicy),
1507+
new: newService(
1508+
map[string]string{},
1509+
v1.ServiceTypeClusterIP,
1510+
[]string{},
1511+
map[string]string{"cluster-name": "clstr", "spilo-role": "master"}, defaultPolicy),
1512+
match: false,
1513+
},
1514+
{
1515+
about: "services differ on external traffic policy",
1516+
current: newService(
1517+
map[string]string{},
1518+
v1.ServiceTypeClusterIP,
1519+
[]string{},
1520+
nil, defaultPolicy),
1521+
new: newService(
1522+
map[string]string{},
1523+
v1.ServiceTypeClusterIP,
1524+
[]string{},
1525+
nil, v1.ServiceExternalTrafficPolicyTypeLocal),
1526+
match: false,
1527+
},
14791528
}
14801529

14811530
for _, tt := range tests {

0 commit comments

Comments
 (0)