Skip to content

Commit ebc71c8

Browse files
authored
Minimize downtime during pod recycling (#2233)
* Minimize downtime during pod recycling * Add missing imports * Fix PR comments * PR comments fixes * Update go.sum * Fix panic in ContinuousHealthCheck * Add UT for .WithPreStopHook, rename parameter * Move waiting for LB IP provisioning to separate step * Use forwarding for local and svc for in cluster execution * Fix conditional variable assignement
1 parent 2eaa89b commit ebc71c8

File tree

17 files changed

+355
-20
lines changed

17 files changed

+355
-20
lines changed

NOTICE.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,35 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17221722
SOFTWARE.
17231723

17241724

1725+
--------------------------------------------------------------------------------
1726+
Module : github.com/tsenart/vegeta
1727+
Version : v12.7.0+incompatible
1728+
Time : 2019-07-21 12:37:32 +0000 UTC
1729+
1730+
Contents of probable licence file $GOMODCACHE/github.com/tsenart/vegeta@v12.7.0+incompatible/LICENSE:
1731+
1732+
The MIT License (MIT)
1733+
1734+
Copyright (c) 2013-2016 Tomás Senart
1735+
1736+
Permission is hereby granted, free of charge, to any person obtaining a copy of
1737+
this software and associated documentation files (the "Software"), to deal in
1738+
the Software without restriction, including without limitation the rights to
1739+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
1740+
the Software, and to permit persons to whom the Software is furnished to do so,
1741+
subject to the following conditions:
1742+
1743+
The above copyright notice and this permission notice shall be included in all
1744+
copies or substantial portions of the Software.
1745+
1746+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1747+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
1748+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
1749+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
1750+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1751+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1752+
1753+
17251754
--------------------------------------------------------------------------------
17261755
Module : go.uber.org/zap
17271756
Version : v1.12.0

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ require (
66
github.com/Masterminds/goutils v1.1.0 // indirect
77
github.com/Masterminds/semver v1.4.2 // indirect
88
github.com/Masterminds/sprig v2.20.0+incompatible
9+
github.com/bmizerany/perks v0.0.0-20141205001514-d9a9656a3a4b // indirect
910
github.com/davecgh/go-spew v1.1.1
11+
github.com/dgryski/go-gk v0.0.0-20140819190930-201884a44051 // indirect
1012
github.com/docker/spdystream v0.0.0-20181023171402-6480d4af844c // indirect
1113
github.com/elastic/go-ucfg v0.7.0
1214
github.com/elazarl/goproxy v0.0.0-20190711103511-473e67f1d7d2 // indirect
@@ -22,6 +24,7 @@ require (
2224
github.com/hashicorp/vault/api v1.0.4
2325
github.com/huandu/xstrings v1.2.0 // indirect
2426
github.com/imdario/mergo v0.3.8
27+
github.com/influxdata/tdigest v0.0.1 // indirect
2528
github.com/magiconair/properties v1.8.1
2629
github.com/onsi/ginkgo v1.10.1 // indirect
2730
github.com/onsi/gomega v1.7.0 // indirect
@@ -35,7 +38,9 @@ require (
3538
github.com/spf13/jwalterweatherman v1.1.0 // indirect
3639
github.com/spf13/pflag v1.0.5
3740
github.com/spf13/viper v1.4.0
41+
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25 // indirect
3842
github.com/stretchr/testify v1.4.0
43+
github.com/tsenart/vegeta v12.7.0+incompatible
3944
go.uber.org/zap v1.12.0
4045
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
4146
golang.org/x/net v0.0.0-20191011234655-491137f69257 // indirect

go.sum

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
4646
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
4747
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
4848
github.com/blang/semver v3.5.0+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
49+
github.com/bmizerany/perks v0.0.0-20141205001514-d9a9656a3a4b h1:AP/Y7sqYicnjGDfD5VcY4CIfh1hRXBUavxrvELjTiOE=
50+
github.com/bmizerany/perks v0.0.0-20141205001514-d9a9656a3a4b/go.mod h1:ac9efd0D1fsDb3EJvhqgXRbFx7bs2wqZ10HQPeU8U/Q=
4951
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
5052
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
5153
github.com/coreos/bbolt v1.3.1-coreos.6/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
@@ -66,6 +68,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
6668
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
6769
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6870
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
71+
github.com/dgryski/go-gk v0.0.0-20140819190930-201884a44051 h1:ByJUvQYyTtNNCVfYNM48q6uYUT4fAlN0wNmd3th4BSo=
72+
github.com/dgryski/go-gk v0.0.0-20140819190930-201884a44051/go.mod h1:qm+vckxRlDt0aOla0RYJJVeqHZlWfOm2UIxHaqPB46E=
6973
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
7074
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
7175
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
@@ -244,6 +248,8 @@ github.com/imdario/mergo v0.3.8 h1:CGgOkSJeqMRmt0D9XLWExdT4m4F1vd3FV3VPt+0VxkQ=
244248
github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
245249
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
246250
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
251+
github.com/influxdata/tdigest v0.0.1 h1:XpFptwYmnEKUqmkcDjrzffswZ3nvNeevbUSLPP/ZzIY=
252+
github.com/influxdata/tdigest v0.0.1/go.mod h1:Z0kXnxzbTC2qrx4NaIzYkE1k66+6oEDQTvL95hQFh5Y=
247253
github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo=
248254
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
249255
github.com/json-iterator/go v0.0.0-20180612202835-f2b4162afba3/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
@@ -270,8 +276,10 @@ github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP
270276
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
271277
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
272278
github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
279+
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329 h1:2gxZ0XQIU/5z3Z3bUBu+FXuk2pFbkN6tcwi/pjyaDic=
273280
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
274281
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
282+
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63 h1:nTT4s92Dgz2HlrB2NaMgvlfqHH39OgMhA7z3PK7PGD4=
275283
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
276284
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
277285
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
@@ -404,6 +412,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
404412
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
405413
github.com/spf13/viper v1.4.0 h1:yXHLWeravcrgGyFSyCgdYpXQ9dR9c/WED3pg1RhxqEU=
406414
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
415+
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25 h1:7z3LSn867ex6VSaahyKadf4WtSsJIgne6A1WLOAGM8A=
416+
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25/go.mod h1:lbP8tGiBjZ5YWIc2fzuRpTaz0b/53vT6PEs3QuAWzuU=
407417
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
408418
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
409419
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
@@ -415,6 +425,8 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
415425
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
416426
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
417427
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
428+
github.com/tsenart/vegeta v12.7.0+incompatible h1:sGlrv11EMxQoKOlDuMWR23UdL90LE5VlhKw/6PWkZmU=
429+
github.com/tsenart/vegeta v12.7.0+incompatible/go.mod h1:Smz/ZWfhKRcyDDChZkG3CyTHdj87lHzio/HOCkbndXM=
418430
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
419431
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
420432
github.com/xiang90/probing v0.0.0-20160813154853-07dd2e8dfe18/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
@@ -453,8 +465,10 @@ golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8U
453465
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
454466
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
455467
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
468+
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
456469
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
457470
golang.org/x/exp v0.0.0-20190125153040-c74c464bbbf2/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
471+
golang.org/x/exp v0.0.0-20190312203227-4b39c73a6495 h1:I6A9Ag9FpEKOjcKrRNjQkPHawoXIhKyTGfvvjFAiiAk=
458472
golang.org/x/exp v0.0.0-20190312203227-4b39c73a6495/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
459473
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
460474
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
@@ -541,6 +555,7 @@ golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxb
541555
golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0 h1:xQwXv67TxFo9nC1GJFyab5eq/5B590r6RlnL/G8Sz7w=
542556
golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
543557
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
558+
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
544559
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
545560
golang.org/x/tools v0.0.0-20181011042414-1f849cf54d09/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
546561
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
@@ -564,8 +579,12 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbO
564579
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
565580
gomodules.xyz/jsonpatch/v2 v2.0.1 h1:xyiBuvkD2g5n7cYzx6u2sxQvsAy4QJsZFCzGVdzOXZ0=
566581
gomodules.xyz/jsonpatch/v2 v2.0.1/go.mod h1:IhYNNY4jnS53ZnfE4PAmpKtDpTCj1JFXc+3mwe7XcUU=
582+
gonum.org/v1/gonum v0.0.0-20181121035319-3f7ecaa7e8ca/go.mod h1:Y+Yx5eoAFn32cQvJDxZx5Dpnq+c3wtXuadVZAcxbbBo=
583+
gonum.org/v1/gonum v0.0.0-20190331200053-3d26580ed485 h1:OB/uP/Puiu5vS5QMRPrXCDWUPb+kt8f1KW8oQzFejQw=
567584
gonum.org/v1/gonum v0.0.0-20190331200053-3d26580ed485/go.mod h1:2ltnJ7xHfj0zHS40VVPYEAAMTa3ZGguvHGBSJeRWqE0=
585+
gonum.org/v1/netlib v0.0.0-20181029234149-ec6d1f5cefe6/go.mod h1:wa6Ws7BG/ESfp6dHfk7C6KdzKA7wR7u/rKwOGE66zvw=
568586
gonum.org/v1/netlib v0.0.0-20190313105609-8cb42192e0e0/go.mod h1:wa6Ws7BG/ESfp6dHfk7C6KdzKA7wR7u/rKwOGE66zvw=
587+
gonum.org/v1/netlib v0.0.0-20190331212654-76723241ea4e h1:jRyg0XfpwWlhEV8mDfdNGBeSJM2fuyh9Yjrnd8kF2Ts=
569588
gonum.org/v1/netlib v0.0.0-20190331212654-76723241ea4e/go.mod h1:kS+toOQn6AQKjmKJ7gzohV1XkqsFehRA2FbsbkopSuQ=
570589
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
571590
google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs=

pkg/controller/common/defaults/pod_template.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,16 @@ func (b *PodTemplateBuilder) WithResources(resources corev1.ResourceRequirements
315315
}
316316
return b
317317
}
318+
319+
func (b *PodTemplateBuilder) WithPreStopHook(handler corev1.Handler) *PodTemplateBuilder {
320+
if b.Container.Lifecycle == nil {
321+
b.Container.Lifecycle = &corev1.Lifecycle{}
322+
}
323+
324+
if b.Container.Lifecycle.PreStop == nil {
325+
// no user-provided hook, we can use our own
326+
b.Container.Lifecycle.PreStop = &handler
327+
}
328+
329+
return b
330+
}

pkg/controller/common/defaults/pod_template_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,3 +1047,88 @@ func TestPodTemplateBuilder_WithDefaultResources(t *testing.T) {
10471047
})
10481048
}
10491049
}
1050+
1051+
func TestPodTemplateBuilder_WithPreStopHook(t *testing.T) {
1052+
containerName := "mycontainer"
1053+
defaultHook := corev1.Handler{Exec: &corev1.ExecAction{Command: []string{"default", "command"}}}
1054+
userHook := &corev1.Handler{}
1055+
tests := []struct {
1056+
name string
1057+
podTemplate corev1.PodTemplateSpec
1058+
preStopHook corev1.Handler
1059+
wantPreStop corev1.Handler
1060+
wantPostStart *corev1.Handler
1061+
}{
1062+
{
1063+
name: "no pre stop hook in pod template: use default one",
1064+
podTemplate: corev1.PodTemplateSpec{},
1065+
preStopHook: defaultHook,
1066+
wantPreStop: defaultHook,
1067+
wantPostStart: nil,
1068+
},
1069+
{
1070+
name: "user provided post start hook, but no pre stop hook in pod template: use default one",
1071+
podTemplate: corev1.PodTemplateSpec{
1072+
Spec: corev1.PodSpec{
1073+
Containers: []corev1.Container{
1074+
{
1075+
Name: containerName,
1076+
Lifecycle: &corev1.Lifecycle{
1077+
PostStart: userHook,
1078+
},
1079+
},
1080+
},
1081+
},
1082+
},
1083+
preStopHook: defaultHook,
1084+
wantPreStop: defaultHook,
1085+
wantPostStart: userHook,
1086+
},
1087+
{
1088+
name: "pre stop hook in pod template: use provided one",
1089+
podTemplate: corev1.PodTemplateSpec{
1090+
Spec: corev1.PodSpec{
1091+
Containers: []corev1.Container{
1092+
{
1093+
Name: containerName,
1094+
Lifecycle: &corev1.Lifecycle{
1095+
PreStop: userHook,
1096+
},
1097+
},
1098+
},
1099+
}},
1100+
preStopHook: *userHook,
1101+
wantPostStart: nil,
1102+
},
1103+
{
1104+
name: "user provided post start hook and pre stop hook in pod template: use provided one",
1105+
podTemplate: corev1.PodTemplateSpec{
1106+
Spec: corev1.PodSpec{
1107+
Containers: []corev1.Container{
1108+
{
1109+
Name: containerName,
1110+
Lifecycle: &corev1.Lifecycle{
1111+
PostStart: &corev1.Handler{},
1112+
PreStop: userHook,
1113+
},
1114+
},
1115+
},
1116+
},
1117+
},
1118+
preStopHook: *userHook,
1119+
wantPostStart: userHook,
1120+
},
1121+
}
1122+
for _, tt := range tests {
1123+
t.Run(tt.name, func(t *testing.T) {
1124+
b := NewPodTemplateBuilder(tt.podTemplate, "mycontainer")
1125+
got := b.WithPreStopHook(tt.preStopHook).Container.Lifecycle
1126+
if !reflect.DeepEqual(got.PreStop, &tt.wantPreStop) {
1127+
t.Errorf("PreStop after PodTemplateBuilder.WithPreStopHook() = %v, want %v", got.PreStop, tt.wantPreStop)
1128+
}
1129+
if !reflect.DeepEqual(got.PostStart, tt.wantPostStart) {
1130+
t.Errorf("PostStart after PodTemplateBuilder.WithPreStopHook() = %v, want %v", got.PostStart, tt.wantPostStart)
1131+
}
1132+
})
1133+
}
1134+
}

pkg/controller/common/license/trial.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424
TrialStatusSecretKey = "trial-status"
2525
TrialPubkeyKey = "pubkey"
2626

27-
TrialLicenseSecretName = "trial.k8s.elastic.co/secret-name" // nolint
27+
TrialLicenseSecretName = "trial.k8s.elastic.co/secret-name" // nolint
2828
TrialLicenseSecretNamespace = "trial.k8s.elastic.co/secret-namespace" // nolint
2929
)
3030

pkg/controller/elasticsearch/configmap/configmap.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func ReconcileScriptsConfigMap(c k8s.Client, scheme *runtime.Scheme, es esv1.Ela
4141
types.NamespacedName{Namespace: es.Namespace, Name: esv1.ScriptsConfigMap(es.Name)},
4242
map[string]string{
4343
nodespec.ReadinessProbeScriptConfigKey: nodespec.ReadinessProbeScript,
44+
nodespec.PreStopHookScriptConfigKey: nodespec.PreStopHookScript,
4445
initcontainer.PrepareFsScriptConfigKey: fsScript,
4546
},
4647
)

pkg/controller/elasticsearch/nodespec/defaults.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ var (
5151
)
5252

5353
// DefaultEnvVars are environment variables injected into Elasticsearch pods.
54-
func DefaultEnvVars(httpCfg commonv1.HTTPConfig) []corev1.EnvVar {
54+
func DefaultEnvVars(httpCfg commonv1.HTTPConfig, headlessServiceName string) []corev1.EnvVar {
5555
return append(
5656
defaults.PodDownwardEnvVars,
5757
[]corev1.EnvVar{
5858
{Name: settings.EnvProbePasswordPath, Value: path.Join(esvolume.ProbeUserSecretMountPath, user.InternalProbeUserName)},
5959
{Name: settings.EnvProbeUsername, Value: user.InternalProbeUserName},
6060
{Name: settings.EnvReadinessProbeProtocol, Value: httpCfg.Protocol()},
61+
{Name: settings.HeadlessServiceName, Value: headlessServiceName},
6162

6263
// Disable curl/libnss use of sqlite caching to avoid triggering an issue in linux/kubernetes
6364
// where the kernel's dentry cache grows by 5mb every time curl is invoked. This cache usage
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package nodespec
6+
7+
import (
8+
"path"
9+
10+
"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/volume"
11+
v1 "k8s.io/api/core/v1"
12+
)
13+
14+
func NewPreStopHook() *v1.Handler {
15+
return &v1.Handler{
16+
Exec: &v1.ExecAction{
17+
Command: []string{"bash", "-c", path.Join(volume.ScriptsVolumeMountPath, PreStopHookScriptConfigKey)}},
18+
}
19+
}
20+
21+
const PreStopHookScriptConfigKey = "pre-stop-hook-script.sh"
22+
const PreStopHookScript = `#!/usr/bin/env bash
23+
24+
set -eux
25+
26+
# This script will wait for up to $MAX_WAIT_SECONDS for $POD_IP to disappear from DNS record,
27+
# then it will wait additional $ADDITIONAL_WAIT_SECONDS and exit. This slows down the process shutdown
28+
# and allows to make changes to the pool gracefully, without blackholing traffic when DNS
29+
# contains IP that is already inactive. Assumes $HEADLESS_SERVICE_NAME and $POD_IP env variables are defined.
30+
31+
# max time to wait for pods IP to disappear from DNS. As this runs in parallel to grace period
32+
# (defaulting to 30s) after which process is SIGKILLed, it should be set to allow enough time
33+
# for the process to gracefully terminate.
34+
MAX_WAIT_SECONDS=${MAX_WAIT_SECONDS:=20}
35+
36+
# additional wait, allows queries to successfully use IP from DNS from before pod termination
37+
# this gives a little bit more time for clients that resolved DNS just before DNS record
38+
# was updated.
39+
ADDITIONAL_WAIT_SECONDS=${ADDITIONAL_WAIT_SECONDS:=1}
40+
41+
START_TIME=$(date +%s)
42+
while true; do
43+
ELAPSED_TIME=$(($(date +%s) - $START_TIME))
44+
45+
if [ $ELAPSED_TIME -gt $MAX_WAIT_SECONDS ]; then
46+
exit 1
47+
fi
48+
49+
if ! getent hosts $HEADLESS_SERVICE_NAME | grep $POD_IP; then
50+
sleep $ADDITIONAL_WAIT_SECONDS
51+
exit 0
52+
fi
53+
54+
sleep 1
55+
done
56+
`

pkg/controller/elasticsearch/nodespec/podspec.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ func BuildPodTemplateSpec(
5656
WithPorts(DefaultContainerPorts).
5757
WithReadinessProbe(*NewReadinessProbe()).
5858
WithAffinity(DefaultAffinity(es.Name)).
59-
WithEnv(DefaultEnvVars(es.Spec.HTTP)...).
59+
WithEnv(DefaultEnvVars(es.Spec.HTTP, HeadlessServiceName(esv1.StatefulSet(es.Name, nodeSet.Name)))...).
6060
WithVolumes(volumes...).
6161
WithVolumeMounts(volumeMounts...).
6262
WithLabels(labels).
6363
WithInitContainers(initContainers...).
64+
WithPreStopHook(*NewPreStopHook()).
6465
WithInitContainerDefaults()
6566

6667
return builder.PodTemplate, nil

0 commit comments

Comments
 (0)