github.com/demonoid81/containerd@v1.3.4/runtime/restart/monitor/change.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package monitor 18 19 import ( 20 "context" 21 "syscall" 22 23 "github.com/containerd/containerd" 24 "github.com/containerd/containerd/cio" 25 ) 26 27 type stopChange struct { 28 container containerd.Container 29 } 30 31 func (s *stopChange) apply(ctx context.Context, client *containerd.Client) error { 32 return killTask(ctx, s.container) 33 } 34 35 type startChange struct { 36 container containerd.Container 37 logPath string 38 } 39 40 func (s *startChange) apply(ctx context.Context, client *containerd.Client) error { 41 log := cio.NullIO 42 if s.logPath != "" { 43 log = cio.LogFile(s.logPath) 44 } 45 killTask(ctx, s.container) 46 task, err := s.container.NewTask(ctx, log) 47 if err != nil { 48 return err 49 } 50 return task.Start(ctx) 51 } 52 53 func killTask(ctx context.Context, container containerd.Container) error { 54 task, err := container.Task(ctx, nil) 55 if err == nil { 56 wait, err := task.Wait(ctx) 57 if err != nil { 58 if _, derr := task.Delete(ctx); derr == nil { 59 return nil 60 } 61 return err 62 } 63 if err := task.Kill(ctx, syscall.SIGKILL, containerd.WithKillAll); err != nil { 64 if _, derr := task.Delete(ctx); derr == nil { 65 return nil 66 } 67 return err 68 } 69 <-wait 70 if _, err := task.Delete(ctx); err != nil { 71 return err 72 } 73 } 74 return nil 75 }