Kill kills a list of containers
(ctx context.Context, client *containerd.Client, reqs []string, options types.ContainerKillOptions)
| 46 | |
| 47 | // Kill kills a list of containers |
| 48 | func Kill(ctx context.Context, client *containerd.Client, reqs []string, options types.ContainerKillOptions) error { |
| 49 | if !strings.HasPrefix(options.KillSignal, "SIG") { |
| 50 | options.KillSignal = "SIG" + options.KillSignal |
| 51 | } |
| 52 | |
| 53 | parsedSignal, err := signal.ParseSignal(options.KillSignal) |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | |
| 58 | walker := &containerwalker.ContainerWalker{ |
| 59 | Client: client, |
| 60 | OnFound: func(ctx context.Context, found containerwalker.Found) error { |
| 61 | if found.MatchCount > 1 { |
| 62 | return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req) |
| 63 | } |
| 64 | if err := cleanupNetwork(ctx, found.Container, options.GOptions); err != nil { |
| 65 | return fmt.Errorf("unable to cleanup network for container: %s, %q", found.Req, err) |
| 66 | } |
| 67 | if err := killContainer(ctx, found.Container, parsedSignal); err != nil { |
| 68 | if errdefs.IsNotFound(err) { |
| 69 | fmt.Fprintf(options.Stderr, "No such container: %s\n", found.Req) |
| 70 | os.Exit(1) |
| 71 | } |
| 72 | return err |
| 73 | } |
| 74 | _, err := fmt.Fprintln(options.Stdout, found.Container.ID()) |
| 75 | return err |
| 76 | }, |
| 77 | } |
| 78 | |
| 79 | return walker.WalkAll(ctx, reqs, true) |
| 80 | } |
| 81 | |
| 82 | func killContainer(ctx context.Context, container containerd.Container, signal syscall.Signal) (err error) { |
| 83 | defer func() { |
no test coverage detected
searching dependent graphs…