github.com/demonoid81/containerd@v1.3.4/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/containers"
    37  )
    38  
    39  const (
    40  	// StatusLabel sets the restart status label for a container
    41  	StatusLabel = "containerd.io/restart.status"
    42  	// LogPathLabel sets the restart log path label for a container
    43  	LogPathLabel = "containerd.io/restart.logpath"
    44  )
    45  
    46  // WithLogPath sets the log path for a container
    47  func WithLogPath(path string) func(context.Context, *containerd.Client, *containers.Container) error {
    48  	return func(_ context.Context, _ *containerd.Client, c *containers.Container) error {
    49  		ensureLabels(c)
    50  		c.Labels[LogPathLabel] = path
    51  		return nil
    52  	}
    53  }
    54  
    55  // WithStatus sets the status for a container
    56  func WithStatus(status containerd.ProcessStatus) func(context.Context, *containerd.Client, *containers.Container) error {
    57  	return func(_ context.Context, _ *containerd.Client, c *containers.Container) error {
    58  		ensureLabels(c)
    59  		c.Labels[StatusLabel] = string(status)
    60  		return nil
    61  	}
    62  }
    63  
    64  // WithNoRestarts clears any restart information from the container
    65  func WithNoRestarts(_ context.Context, _ *containerd.Client, c *containers.Container) error {
    66  	if c.Labels == nil {
    67  		return nil
    68  	}
    69  	delete(c.Labels, StatusLabel)
    70  	delete(c.Labels, LogPathLabel)
    71  	return nil
    72  }
    73  
    74  func ensureLabels(c *containers.Container) {
    75  	if c.Labels == nil {
    76  		c.Labels = make(map[string]string)
    77  	}
    78  }