@@ -14,6 +14,7 @@ import (
1414 "io"
1515 "os"
1616 "path/filepath"
17+ "sync"
1718 "syscall"
1819 "time"
1920
@@ -51,6 +52,8 @@ type Component struct {
5152 executablePath string
5253 confPath string
5354 importsPath string
55+
56+ stop func ()
5457}
5558
5659func NewComponent (logLevel string , vars * config.CfgVars , profile * workerconfig.Profile ) * Component {
@@ -88,7 +91,7 @@ func (c *Component) Init(ctx context.Context) error {
8891}
8992
9093// Run runs containerd.
91- func (c * Component ) Start (ctx context.Context ) error {
94+ func (c * Component ) Start (ctx context.Context ) ( err error ) {
9295 log := logrus .WithField ("component" , "containerd" )
9396 log .Info ("Starting containerd" )
9497
@@ -114,11 +117,32 @@ func (c *Component) Start(ctx context.Context) error {
114117 return err
115118 }
116119
117- go c .watchDropinConfigs (ctx )
120+ cctx , cancel := context .WithCancelCause (context .Background ())
121+ var wg sync.WaitGroup
122+ stop := func () {
123+ cancel (errors .New ("containerd component is stopping" ))
124+ c .supervisor .Stop ()
125+ wg .Wait ()
126+ }
127+
128+ defer func () {
129+ if err == nil {
130+ c .stop = stop
131+ } else {
132+ stop ()
133+ }
134+ }()
135+
136+ wg .Add (1 )
137+ go func () {
138+ defer wg .Done ()
139+ wait .UntilWithContext (cctx , c .watchDropinConfigs , 30 * time .Second )
140+ log .Info ("Stopped to watch for drop-ins" )
141+ }()
118142
119143 log .Debug ("Waiting for containerd" )
120144 var lastErr error
121- err : = wait .ExponentialBackoffWithContext (ctx , wait.Backoff {
145+ err = wait .ExponentialBackoffWithContext (ctx , wait.Backoff {
122146 Duration : 100 * time .Millisecond , Factor : 1.2 , Jitter : 0.05 , Steps : 30 ,
123147 }, func (ctx context.Context ) (bool , error ) {
124148 rt := containerruntime .NewContainerRuntime (Endpoint (c .K0sVars .RunDir ))
@@ -276,8 +300,8 @@ func (c *Component) restart() {
276300
277301// Stop stops containerd.
278302func (c * Component ) Stop () error {
279- if c . supervisor != nil {
280- c . supervisor . Stop ()
303+ if stop := c . stop ; stop != nil {
304+ stop ()
281305 }
282306 return nil
283307}
0 commit comments