Skip to content

Commit cbd59c6

Browse files
committed
Use different context for embedded etcd
Signed-off-by: Marko Mudrinić <mudrinic.mare@gmail.com>
1 parent 2a161d8 commit cbd59c6

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

cmd/cache-server/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"context"
2021
"flag"
2122
"os"
2223
"strings"
@@ -89,9 +90,13 @@ func main() {
8990
}
9091

9192
ctx := genericapiserver.SetupSignalContext()
92-
// the etcd server must be up before NewServer because storage decorators access it right away
93+
// the etcd server must be up before NewServer because storage decorators access it right away.
94+
// Use a separate context for etcd so it outlives the apiserver during graceful shutdown.
95+
// This prevents CRD watch cache lazy initialization from blocking on a dead etcd.
96+
etcdCtx, etcdCancel := context.WithCancel(context.Background())
97+
defer etcdCancel()
9398
if completedConfig.EmbeddedEtcd.Config != nil {
94-
if err := embeddedetcd.NewServer(completedConfig.EmbeddedEtcd).Run(ctx); err != nil {
99+
if err := embeddedetcd.NewServer(completedConfig.EmbeddedEtcd).Run(etcdCtx); err != nil {
95100
return err
96101
}
97102
}

cmd/kcp/kcp.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"os"
2223
"strings"
@@ -143,9 +144,13 @@ func main() {
143144
return err
144145
}
145146

146-
// the etcd server must be up before NewServer because storage decorators access it right away
147+
// the etcd server must be up before NewServer because storage decorators access it right away.
148+
// Use a separate context for etcd so it outlives the apiserver during graceful shutdown.
149+
// This prevents CRD watch cache lazy initialization from blocking on a dead etcd.
150+
etcdCtx, etcdCancel := context.WithCancel(context.Background())
151+
defer etcdCancel()
147152
if completedConfig.EmbeddedEtcd.Config != nil {
148-
if err := embeddedetcd.NewServer(completedConfig.EmbeddedEtcd).Run(ctx); err != nil {
153+
if err := embeddedetcd.NewServer(completedConfig.EmbeddedEtcd).Run(etcdCtx); err != nil {
149154
return err
150155
}
151156
}

0 commit comments

Comments
 (0)