| 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 analyzer |
| 18 |
|
| 19 |
import ( |
| 20 |
"time" |
| 21 |
|
| 22 |
"github.com/containerd/containerd" |
| 23 |
"github.com/containerd/containerd/oci" |
| 24 |
) |
| 25 |
|
| 26 |
type analyzerOpts struct { |
| 27 |
period time.Duration |
| 28 |
waitOnSignal bool |
| 29 |
snapshotter string |
| 30 |
specOpts SpecOpts |
| 31 |
terminal bool |
| 32 |
stdin bool |
| 33 |
waitLineOut string |
| 34 |
} |
| 35 |
|
| 36 |
// Option is runtime configuration of analyzer container |
| 37 |
type Option func(opts *analyzerOpts) |
| 38 |
|
| 39 |
// SpecOpts returns runtime configuration based on the passed image and rootfs path. |
| 40 |
type SpecOpts func(image containerd.Image, rootfs string) (opts []oci.SpecOpts, done func() error, err error) |
| 41 |
|
| 42 |
// WithSpecOpts is the runtime configuration |
| 43 |
func WithSpecOpts(specOpts SpecOpts) Option { |
| 44 |
return func(opts *analyzerOpts) { |
| 45 |
opts.specOpts = specOpts |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
// WithTerminal enable terminal for the container. This must be specified with WithStdin(). |
| 50 |
func WithTerminal() Option { |
| 51 |
return func(opts *analyzerOpts) { |
| 52 |
opts.terminal = true |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// WithStdin attaches stdin to the container |
| 57 |
func WithStdin() Option { |
| 58 |
return func(opts *analyzerOpts) { |
| 59 |
opts.stdin = true |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// WithPeriod is the period to run runtime |
| 64 |
func WithPeriod(period time.Duration) Option { |
| 65 |
return func(opts *analyzerOpts) { |
| 66 |
opts.period = period |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// WithWaitOnSignal disables timeout |
| 71 |
func WithWaitOnSignal() Option { |
| 72 |
return func(opts *analyzerOpts) { |
| 73 |
opts.waitOnSignal = true |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// WithSnapshotter is the snapshotter to use |
| 78 |
func WithSnapshotter(snapshotter string) Option { |
| 79 |
return func(opts *analyzerOpts) { |
| 80 |
opts.snapshotter = snapshotter |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// WithWaitLineOut specifies a substring of a stdout line to be waited. |
| 85 |
// When this line is detected, the container will be killed. |
| 86 |
func WithWaitLineOut(s string) Option { |
| 87 |
return func(opts *analyzerOpts) { |
| 88 |
opts.waitLineOut = s |
| 89 |
} |
| 90 |
} |