containerd/runtime/restart/monitor/monitor.go
Ye Sijun 35994a475c
fix monitor restart
Signed-off-by: Ye Sijun <junnplus@gmail.com>
2022-04-20 21:44:29 +08:00

266 lines
7.3 KiB
Go

/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package monitor
import (
"context"
"fmt"
"strconv"
"sync"
"time"
"github.com/containerd/containerd"
containers "github.com/containerd/containerd/api/services/containers/v1"
diff "github.com/containerd/containerd/api/services/diff/v1"
images "github.com/containerd/containerd/api/services/images/v1"
namespacesapi "github.com/containerd/containerd/api/services/namespaces/v1"
tasks "github.com/containerd/containerd/api/services/tasks/v1"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/leases"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/plugin"
"github.com/containerd/containerd/runtime/restart"
"github.com/containerd/containerd/services"
"github.com/containerd/containerd/snapshots"
"github.com/sirupsen/logrus"
)
type duration struct {
time.Duration
}
func (d *duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
func (d duration) MarshalText() ([]byte, error) {
return []byte(d.Duration.String()), nil
}
// Config for the restart monitor
type Config struct {
// Interval for how long to wait to check for state changes
Interval duration `toml:"interval"`
}
func init() {
plugin.Register(&plugin.Registration{
Type: plugin.InternalPlugin,
Requires: []plugin.Type{
plugin.EventPlugin,
plugin.ServicePlugin,
},
ID: "restart",
Config: &Config{
Interval: duration{
Duration: 10 * time.Second,
},
},
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
}
client, err := containerd.New("", containerd.WithServices(opts...))
if err != nil {
return nil, err
}
m := &monitor{
client: client,
}
go m.run(ic.Config.(*Config).Interval.Duration)
return m, nil
},
})
}
// getServicesOpts get service options from plugin context.
func getServicesOpts(ic *plugin.InitContext) ([]containerd.ServicesOpt, error) {
var opts []containerd.ServicesOpt
for t, fn := range map[plugin.Type]func(interface{}) containerd.ServicesOpt{
plugin.EventPlugin: func(i interface{}) containerd.ServicesOpt {
return containerd.WithEventService(i.(containerd.EventService))
},
plugin.LeasePlugin: func(i interface{}) containerd.ServicesOpt {
return containerd.WithLeasesService(i.(leases.Manager))
},
} {
i, err := ic.Get(t)
if err != nil {
return nil, fmt.Errorf("failed to get %q plugin: %w", t, err)
}
opts = append(opts, fn(i))
}
plugins, err := ic.GetByType(plugin.ServicePlugin)
if err != nil {
return nil, fmt.Errorf("failed to get service plugin: %w", err)
}
for s, fn := range map[string]func(interface{}) containerd.ServicesOpt{
services.ContentService: func(s interface{}) containerd.ServicesOpt {
return containerd.WithContentStore(s.(content.Store))
},
services.ImagesService: func(s interface{}) containerd.ServicesOpt {
return containerd.WithImageClient(s.(images.ImagesClient))
},
services.SnapshotsService: func(s interface{}) containerd.ServicesOpt {
return containerd.WithSnapshotters(s.(map[string]snapshots.Snapshotter))
},
services.ContainersService: func(s interface{}) containerd.ServicesOpt {
return containerd.WithContainerClient(s.(containers.ContainersClient))
},
services.TasksService: func(s interface{}) containerd.ServicesOpt {
return containerd.WithTaskClient(s.(tasks.TasksClient))
},
services.DiffService: func(s interface{}) containerd.ServicesOpt {
return containerd.WithDiffClient(s.(diff.DiffClient))
},
services.NamespacesService: func(s interface{}) containerd.ServicesOpt {
return containerd.WithNamespaceClient(s.(namespacesapi.NamespacesClient))
},
} {
p := plugins[s]
if p == nil {
return nil, fmt.Errorf("service %q not found", s)
}
i, err := p.Instance()
if err != nil {
return nil, fmt.Errorf("failed to get instance of service %q: %w", s, err)
}
if i == nil {
return nil, fmt.Errorf("instance of service %q not found", s)
}
opts = append(opts, fn(i))
}
return opts, nil
}
type change interface {
apply(context.Context, *containerd.Client) error
}
type monitor struct {
client *containerd.Client
}
func (m *monitor) run(interval time.Duration) {
if interval == 0 {
interval = 10 * time.Second
}
for {
if err := m.reconcile(context.Background()); err != nil {
logrus.WithError(err).Error("reconcile")
}
time.Sleep(interval)
}
}
func (m *monitor) reconcile(ctx context.Context) error {
ns, err := m.client.NamespaceService().List(ctx)
if err != nil {
return err
}
var wgNSLoop sync.WaitGroup
for _, name := range ns {
name := name
wgNSLoop.Add(1)
go func() {
defer wgNSLoop.Done()
ctx := namespaces.WithNamespace(ctx, name)
changes, err := m.monitor(ctx)
if err != nil {
logrus.WithError(err).Error("monitor for changes")
return
}
var wgChangesLoop sync.WaitGroup
for _, c := range changes {
c := c
wgChangesLoop.Add(1)
go func() {
defer wgChangesLoop.Done()
if err := c.apply(ctx, m.client); err != nil {
logrus.WithError(err).Error("apply change")
}
}()
}
wgChangesLoop.Wait()
}()
}
wgNSLoop.Wait()
return nil
}
func (m *monitor) monitor(ctx context.Context) ([]change, error) {
containers, err := m.client.Containers(ctx, fmt.Sprintf("labels.%q", restart.StatusLabel))
if err != nil {
return nil, err
}
var changes []change
for _, c := range containers {
var (
task containerd.Task
status containerd.Status
err error
)
labels, err := c.Labels(ctx)
if err != nil {
return nil, err
}
desiredStatus := containerd.ProcessStatus(labels[restart.StatusLabel])
if task, err = c.Task(ctx, nil); err == nil {
if status, err = task.Status(ctx); err == nil {
if desiredStatus == status.Status {
continue
}
}
}
// Task or Status return error, only desired to running
if err != nil {
logrus.WithError(err).Error("monitor")
if desiredStatus == containerd.Stopped {
continue
}
}
// Known issue:
// The status may be empty when task failed but was deleted,
// which will result in an `on-failure` restart policy reconcile error.
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{
container: c,
})
}
}
return changes, nil
}