2020-04-01 09:55:42 -05:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
|
|
|
|
https://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.
|
|
|
|
*/
|
|
|
|
|
2019-08-12 08:43:06 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2021-02-07 23:37:37 +00:00
|
|
|
const (
|
2021-06-03 22:56:27 -05:00
|
|
|
// DriverDocker indicates that docker driver should be used in container constructor
|
|
|
|
DriverDocker = "docker"
|
2021-02-07 23:37:37 +00:00
|
|
|
)
|
|
|
|
|
2020-10-21 12:34:34 -05:00
|
|
|
// Status type provides container status
|
|
|
|
type Status string
|
|
|
|
|
|
|
|
// State provides information about the Container
|
|
|
|
type State struct {
|
|
|
|
// ExitCode: returns the container's exit code. Zero means exited normally, otherwise errored
|
|
|
|
ExitCode int
|
|
|
|
// Status: String representation of the container state.
|
|
|
|
Status Status
|
|
|
|
}
|
|
|
|
|
2020-02-05 16:06:35 -06:00
|
|
|
// Container interface abstraction for container.
|
2019-08-12 08:43:06 +00:00
|
|
|
// Particular implementation depends on container runtime environment (CRE). Interface
|
|
|
|
// defines methods that must be implemented for CRE (e.g. docker, containerd or CRI-O)
|
|
|
|
type Container interface {
|
|
|
|
ImagePull() error
|
2021-02-04 20:50:35 +00:00
|
|
|
RunCommand(RunCommandOptions) error
|
2021-02-07 23:37:37 +00:00
|
|
|
GetContainerLogs(GetLogOptions) (io.ReadCloser, error)
|
2020-10-21 12:34:34 -05:00
|
|
|
InspectContainer() (State, error)
|
|
|
|
WaitUntilFinished() error
|
2019-08-12 08:43:06 +00:00
|
|
|
RmContainer() error
|
2020-02-28 10:31:45 -05:00
|
|
|
GetID() string
|
2019-08-12 08:43:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 20:50:35 +00:00
|
|
|
// RunCommandOptions options for RunCommand
|
|
|
|
type RunCommandOptions struct {
|
2021-02-07 23:37:37 +00:00
|
|
|
Privileged bool
|
2021-04-02 21:34:43 +00:00
|
|
|
HostNetwork bool
|
2021-02-07 23:37:37 +00:00
|
|
|
|
|
|
|
Cmd []string
|
|
|
|
EnvVars []string
|
|
|
|
Binds []string
|
2021-02-04 21:10:07 +00:00
|
|
|
|
2021-02-07 23:37:37 +00:00
|
|
|
Mounts []Mount
|
|
|
|
Input io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mount describes mount settings
|
|
|
|
type Mount struct {
|
|
|
|
ReadOnly bool
|
|
|
|
Type string
|
|
|
|
Dst string
|
|
|
|
Src string
|
|
|
|
}
|
2021-02-04 21:10:07 +00:00
|
|
|
|
2021-02-07 23:37:37 +00:00
|
|
|
// GetLogOptions options for getting logs
|
|
|
|
// If both Stderr and Stdout are specified the logs will contain both stderr and stdout
|
|
|
|
type GetLogOptions struct {
|
|
|
|
Stderr bool
|
|
|
|
Stdout bool
|
|
|
|
Follow bool
|
2021-02-04 20:50:35 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 08:43:06 +00:00
|
|
|
// NewContainer returns instance of Container interface implemented by particular driver
|
2020-02-05 16:06:35 -06:00
|
|
|
// Returned instance type (i.e. implementation) depends on driver specified via function
|
2019-08-12 08:43:06 +00:00
|
|
|
// arguments (e.g. "docker").
|
|
|
|
// Supported drivers:
|
|
|
|
// * docker
|
2020-10-27 15:21:42 -05:00
|
|
|
func NewContainer(ctx context.Context, driver string, url string) (Container, error) {
|
2019-08-12 08:43:06 +00:00
|
|
|
switch driver {
|
2020-04-20 20:38:19 -07:00
|
|
|
case "":
|
|
|
|
return nil, ErrNoContainerDriver{}
|
2021-06-03 22:56:27 -05:00
|
|
|
case DriverDocker:
|
2019-08-12 08:43:06 +00:00
|
|
|
cli, err := NewDockerClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewDockerContainer(ctx, url, cli)
|
|
|
|
default:
|
|
|
|
return nil, ErrContainerDrvNotSupported{Driver: driver}
|
|
|
|
}
|
|
|
|
}
|