Merge pull request #6744 from Junnplus/restart-policy
Add restart policy for enhanced restart manager
This commit is contained in:
@@ -20,10 +20,12 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"github.com/containerd/containerd"
|
||||
"github.com/containerd/containerd/cio"
|
||||
"github.com/containerd/containerd/runtime/restart"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -38,6 +40,7 @@ func (s *stopChange) apply(ctx context.Context, client *containerd.Client) error
|
||||
type startChange struct {
|
||||
container containerd.Container
|
||||
logURI string
|
||||
count int
|
||||
|
||||
// Deprecated(in release 1.5): but recognized now, prefer to use logURI
|
||||
logPath string
|
||||
@@ -61,6 +64,15 @@ func (s *startChange) apply(ctx context.Context, client *containerd.Client) erro
|
||||
s.logPath, s.logURI)
|
||||
}
|
||||
|
||||
if s.count > 0 {
|
||||
labels := map[string]string{
|
||||
restart.CountLabel: strconv.Itoa(s.count),
|
||||
}
|
||||
opt := containerd.WithAdditionalContainerLabels(labels)
|
||||
if err := s.container.Update(ctx, containerd.UpdateContainerOpts(opt)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
killTask(ctx, s.container)
|
||||
task, err := s.container.NewTask(ctx, log)
|
||||
if err != nil {
|
||||
|
||||
@@ -19,6 +19,7 @@ package monitor
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -72,6 +73,7 @@ func init() {
|
||||
},
|
||||
},
|
||||
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
|
||||
ic.Meta.Capabilities = []string{"no", "always", "on-failure", "unless-stopped"}
|
||||
opts, err := getServicesOpts(ic)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -217,15 +219,29 @@ func (m *monitor) monitor(ctx context.Context) ([]change, error) {
|
||||
return nil, err
|
||||
}
|
||||
desiredStatus := containerd.ProcessStatus(labels[restart.StatusLabel])
|
||||
if m.isSameStatus(ctx, desiredStatus, c) {
|
||||
task, err := c.Task(ctx, nil)
|
||||
if err != nil && desiredStatus == containerd.Stopped {
|
||||
continue
|
||||
}
|
||||
status, err := task.Status(ctx)
|
||||
if err != nil && desiredStatus == containerd.Stopped {
|
||||
continue
|
||||
}
|
||||
if desiredStatus == status.Status {
|
||||
continue
|
||||
}
|
||||
|
||||
switch desiredStatus {
|
||||
case containerd.Running:
|
||||
if !restart.Reconcile(status, labels) {
|
||||
continue
|
||||
}
|
||||
restartCount, _ := strconv.Atoi(labels[restart.CountLabel])
|
||||
changes = append(changes, &startChange{
|
||||
container: c,
|
||||
logPath: labels[restart.LogPathLabel],
|
||||
logURI: labels[restart.LogURILabel],
|
||||
count: restartCount + 1,
|
||||
})
|
||||
case containerd.Stopped:
|
||||
changes = append(changes, &stopChange{
|
||||
@@ -235,15 +251,3 @@ func (m *monitor) monitor(ctx context.Context) ([]change, error) {
|
||||
}
|
||||
return changes, nil
|
||||
}
|
||||
|
||||
func (m *monitor) isSameStatus(ctx context.Context, desired containerd.ProcessStatus, container containerd.Container) bool {
|
||||
task, err := container.Task(ctx, nil)
|
||||
if err != nil {
|
||||
return desired == containerd.Stopped
|
||||
}
|
||||
state, err := task.Status(ctx)
|
||||
if err != nil {
|
||||
return desired == containerd.Stopped
|
||||
}
|
||||
return desired == state.Status
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user