github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/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 "net/url" 22 "syscall" 23 24 "github.com/containerd/containerd" 25 "github.com/containerd/containerd/cio" 26 "github.com/pkg/errors" 27 "github.com/sirupsen/logrus" 28 ) 29 30 type stopChange struct { 31 container containerd.Container 32 } 33 34 func (s *stopChange) apply(ctx context.Context, client *containerd.Client) error { 35 return killTask(ctx, s.container) 36 } 37 38 type startChange struct { 39 container containerd.Container 40 logURI string 41 42 // Deprecated(in release 1.5): but recognized now, prefer to use logURI 43 logPath string 44 } 45 46 func (s *startChange) apply(ctx context.Context, client *containerd.Client) error { 47 log := cio.NullIO 48 49 if s.logURI != "" { 50 uri, err := url.Parse(s.logURI) 51 if err != nil { 52 return errors.Wrapf(err, "failed to parse %v into url", s.logURI) 53 } 54 log = cio.LogURI(uri) 55 } else if s.logPath != "" { 56 log = cio.LogFile(s.logPath) 57 } 58 59 if s.logURI != "" && s.logPath != "" { 60 logrus.Warnf("LogPathLabel=%v has been deprecated, using LogURILabel=%v", 61 s.logPath, s.logURI) 62 } 63 64 killTask(ctx, s.container) 65 task, err := s.container.NewTask(ctx, log) 66 if err != nil { 67 return err 68 } 69 return task.Start(ctx) 70 } 71 72 func killTask(ctx context.Context, container containerd.Container) error { 73 task, err := container.Task(ctx, nil) 74 if err == nil { 75 wait, err := task.Wait(ctx) 76 if err != nil { 77 if _, derr := task.Delete(ctx); derr == nil { 78 return nil 79 } 80 return err 81 } 82 if err := task.Kill(ctx, syscall.SIGKILL, containerd.WithKillAll); err != nil { 83 if _, derr := task.Delete(ctx); derr == nil { 84 return nil 85 } 86 return err 87 } 88 <-wait 89 if _, err := task.Delete(ctx); err != nil { 90 return err 91 } 92 } 93 return nil 94 }