github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/runtime/restart/restart.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 restart enables containers to have labels added and monitored to 18 // keep the container's task running if it is killed. 19 // 20 // Setting the StatusLabel on a container instructs the restart monitor to keep 21 // that container's task in a specific status. 22 // Setting the LogPathLabel on a container will setup the task's IO to be redirected 23 // to a log file when running a task within the restart manager. 24 // 25 // The restart labels can be cleared off of a container using the WithNoRestarts Opt. 26 // 27 // The restart monitor has one option in the containerd config under the [plugins.restart] 28 // section. `interval = "10s" sets the reconcile interval that the restart monitor checks 29 // for task state and reconciles the desired status for that task. 30 package restart 31 32 import ( 33 "context" 34 35 "github.com/containerd/containerd" 36 "github.com/containerd/containerd/cio" 37 "github.com/containerd/containerd/containers" 38 ) 39 40 const ( 41 // StatusLabel sets the restart status label for a container 42 StatusLabel = "containerd.io/restart.status" 43 // LogURILabel sets the restart log uri label for a container 44 LogURILabel = "containerd.io/restart.loguri" 45 46 // LogPathLabel sets the restart log path label for a container 47 // 48 // Deprecated(in release 1.5): use LogURILabel 49 LogPathLabel = "containerd.io/restart.logpath" 50 ) 51 52 // WithBinaryLogURI sets the binary-type log uri for a container. 53 func WithBinaryLogURI(binary string, args map[string]string) func(context.Context, *containerd.Client, *containers.Container) error { 54 return func(_ context.Context, _ *containerd.Client, c *containers.Container) error { 55 uri, err := cio.LogURIGenerator("binary", binary, args) 56 if err != nil { 57 return err 58 } 59 60 ensureLabels(c) 61 c.Labels[LogURILabel] = uri.String() 62 return nil 63 } 64 } 65 66 // WithFileLogURI sets the file-type log uri for a container. 67 func WithFileLogURI(path string) func(context.Context, *containerd.Client, *containers.Container) error { 68 return func(_ context.Context, _ *containerd.Client, c *containers.Container) error { 69 uri, err := cio.LogURIGenerator("file", path, nil) 70 if err != nil { 71 return err 72 } 73 74 ensureLabels(c) 75 c.Labels[LogURILabel] = uri.String() 76 return nil 77 } 78 } 79 80 // WithLogPath sets the log path for a container 81 // 82 // Deprecated(in release 1.5): use WithFileLogURI. 83 func WithLogPath(path string) func(context.Context, *containerd.Client, *containers.Container) error { 84 return func(_ context.Context, _ *containerd.Client, c *containers.Container) error { 85 ensureLabels(c) 86 c.Labels[LogPathLabel] = path 87 return nil 88 } 89 } 90 91 // WithStatus sets the status for a container 92 func WithStatus(status containerd.ProcessStatus) func(context.Context, *containerd.Client, *containers.Container) error { 93 return func(_ context.Context, _ *containerd.Client, c *containers.Container) error { 94 ensureLabels(c) 95 c.Labels[StatusLabel] = string(status) 96 return nil 97 } 98 } 99 100 // WithNoRestarts clears any restart information from the container 101 func WithNoRestarts(_ context.Context, _ *containerd.Client, c *containers.Container) error { 102 if c.Labels == nil { 103 return nil 104 } 105 delete(c.Labels, StatusLabel) 106 delete(c.Labels, LogPathLabel) 107 delete(c.Labels, LogURILabel) 108 return nil 109 } 110 111 func ensureLabels(c *containers.Container) { 112 if c.Labels == nil { 113 c.Labels = make(map[string]string) 114 } 115 }