executor: add config option to define the init image

* Add an executor config option to define the init image to use.
This commit is contained in:
Simone Gotti 2021-05-25 10:27:04 +02:00
parent a7f55f5344
commit 21a63af6f5
7 changed files with 48 additions and 16 deletions

View File

@ -109,6 +109,8 @@ type Executor struct {
Driver Driver `yaml:"driver"` Driver Driver `yaml:"driver"`
InitImage InitImage `yaml:"initImage"`
Labels map[string]string `yaml:"labels"` Labels map[string]string `yaml:"labels"`
// ActiveTasksLimit is the max number of concurrent active tasks // ActiveTasksLimit is the max number of concurrent active tasks
ActiveTasksLimit int `yaml:"active_tasks_limit"` ActiveTasksLimit int `yaml:"active_tasks_limit"`
@ -116,6 +118,10 @@ type Executor struct {
AllowPrivilegedContainers bool `yaml:"allowPrivilegedContainers"` AllowPrivilegedContainers bool `yaml:"allowPrivilegedContainers"`
} }
type InitImage struct {
Image string `yaml:"image"`
}
type Configstore struct { type Configstore struct {
Debug bool `yaml:"debug"` Debug bool `yaml:"debug"`
@ -228,6 +234,9 @@ var defaultConfig = Config{
RunWorkspaceExpireInterval: 7 * 24 * time.Hour, RunWorkspaceExpireInterval: 7 * 24 * time.Hour,
}, },
Executor: Executor{ Executor: Executor{
InitImage: InitImage{
Image: "busybox",
},
ActiveTasksLimit: 2, ActiveTasksLimit: 2,
}, },
} }
@ -263,6 +272,14 @@ func validateWeb(w *Web) error {
return nil return nil
} }
func validateInitImage(i *InitImage) error {
if i.Image == "" {
return errors.Errorf("image is empty")
}
return nil
}
func Validate(c *Config, componentsNames []string) error { func Validate(c *Config, componentsNames []string) error {
// Global // Global
if len(c.ID) > maxIDLength { if len(c.ID) > maxIDLength {
@ -331,6 +348,10 @@ func Validate(c *Config, componentsNames []string) error {
default: default:
return errors.Errorf("executor driver type %q unknown", c.Executor.Driver.Type) return errors.Errorf("executor driver type %q unknown", c.Executor.Driver.Type)
} }
if err := validateInitImage(&c.Executor.InitImage); err != nil {
return errors.Errorf("executor initImage configuration error: %w", err)
}
} }
// Scheduler // Scheduler

View File

@ -48,11 +48,12 @@ type DockerDriver struct {
log *zap.SugaredLogger log *zap.SugaredLogger
client *client.Client client *client.Client
toolboxPath string toolboxPath string
initImage string
executorID string executorID string
arch types.Arch arch types.Arch
} }
func NewDockerDriver(logger *zap.Logger, executorID, toolboxPath string) (*DockerDriver, error) { func NewDockerDriver(logger *zap.Logger, executorID, toolboxPath, initImage string) (*DockerDriver, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion("1.26")) cli, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion("1.26"))
if err != nil { if err != nil {
return nil, err return nil, err
@ -62,6 +63,7 @@ func NewDockerDriver(logger *zap.Logger, executorID, toolboxPath string) (*Docke
log: logger.Sugar(), log: logger.Sugar(),
client: cli, client: cli,
toolboxPath: toolboxPath, toolboxPath: toolboxPath,
initImage: initImage,
executorID: executorID, executorID: executorID,
arch: types.ArchFromString(runtime.GOARCH), arch: types.ArchFromString(runtime.GOARCH),
}, nil }, nil
@ -72,7 +74,7 @@ func (d *DockerDriver) Setup(ctx context.Context) error {
} }
func (d *DockerDriver) createToolboxVolume(ctx context.Context, podID string) (*dockertypes.Volume, error) { func (d *DockerDriver) createToolboxVolume(ctx context.Context, podID string) (*dockertypes.Volume, error) {
reader, err := d.client.ImagePull(ctx, "busybox", dockertypes.ImagePullOptions{}) reader, err := d.client.ImagePull(ctx, d.initImage, dockertypes.ImagePullOptions{})
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -95,7 +97,7 @@ func (d *DockerDriver) createToolboxVolume(ctx context.Context, podID string) (*
resp, err := d.client.ContainerCreate(ctx, &container.Config{ resp, err := d.client.ContainerCreate(ctx, &container.Config{
Entrypoint: []string{"cat"}, Entrypoint: []string{"cat"},
Image: "busybox", Image: d.initImage,
Tty: true, Tty: true,
}, &container.HostConfig{ }, &container.HostConfig{
Binds: []string{fmt.Sprintf("%s:%s", toolboxVol.Name, "/tmp/agola")}, Binds: []string{fmt.Sprintf("%s:%s", toolboxVol.Name, "/tmp/agola")},

View File

@ -42,7 +42,9 @@ func TestDockerPod(t *testing.T) {
logger := zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel)) logger := zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel))
d, err := NewDockerDriver(logger, "executorid01", toolboxPath) initImage := "busybox"
d, err := NewDockerDriver(logger, "executorid01", toolboxPath, initImage)
if err != nil { if err != nil {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }

View File

@ -74,6 +74,7 @@ type K8sDriver struct {
restconfig *restclient.Config restconfig *restclient.Config
client *kubernetes.Clientset client *kubernetes.Clientset
toolboxPath string toolboxPath string
initImage string
namespace string namespace string
executorID string executorID string
executorsGroupID string executorsGroupID string
@ -95,7 +96,7 @@ type K8sPod struct {
initVolumeDir string initVolumeDir string
} }
func NewK8sDriver(logger *zap.Logger, executorID, toolboxPath string) (*K8sDriver, error) { func NewK8sDriver(logger *zap.Logger, executorID, toolboxPath, initImage string) (*K8sDriver, error) {
kubeClientConfig := NewKubeClientConfig("", "", "") kubeClientConfig := NewKubeClientConfig("", "", "")
kubecfg, err := kubeClientConfig.ClientConfig() kubecfg, err := kubeClientConfig.ClientConfig()
if err != nil { if err != nil {
@ -112,13 +113,14 @@ func NewK8sDriver(logger *zap.Logger, executorID, toolboxPath string) (*K8sDrive
} }
d := &K8sDriver{ d := &K8sDriver{
log: logger.Sugar(), log: logger.Sugar(),
restconfig: kubecfg, restconfig: kubecfg,
client: kubecli, client: kubecli,
toolboxPath: toolboxPath, toolboxPath: toolboxPath,
namespace: namespace, initImage: initImage,
executorID: executorID, namespace: namespace,
k8sLabelArch: corev1.LabelArchStable, executorID: executorID,
k8sLabelArch: corev1.LabelArchStable,
} }
serverVersion, err := d.client.Discovery().ServerVersion() serverVersion, err := d.client.Discovery().ServerVersion()
@ -361,7 +363,7 @@ func (d *K8sDriver) NewPod(ctx context.Context, podConfig *PodConfig, out io.Wri
InitContainers: []corev1.Container{ InitContainers: []corev1.Container{
{ {
Name: "initcontainer", Name: "initcontainer",
Image: "busybox", Image: d.initImage,
// wait for a file named /tmp/done and then exit // wait for a file named /tmp/done and then exit
Command: []string{"/bin/sh", "-c", "while true; do if [[ -f /tmp/done ]]; then exit; fi; sleep 1; done"}, Command: []string{"/bin/sh", "-c", "while true; do if [[ -f /tmp/done ]]; then exit; fi; sleep 1; done"},
Stdin: true, Stdin: true,

View File

@ -41,7 +41,9 @@ func TestK8sPod(t *testing.T) {
logger := zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel)) logger := zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel))
d, err := NewK8sDriver(logger, "executorid01", toolboxPath) initImage := "busybox"
d, err := NewK8sDriver(logger, "executorid01", toolboxPath, initImage)
if err != nil { if err != nil {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }

View File

@ -1426,12 +1426,12 @@ func NewExecutor(ctx context.Context, l *zap.Logger, c *config.Executor) (*Execu
var d driver.Driver var d driver.Driver
switch c.Driver.Type { switch c.Driver.Type {
case config.DriverTypeDocker: case config.DriverTypeDocker:
d, err = driver.NewDockerDriver(logger, e.id, e.c.ToolboxPath) d, err = driver.NewDockerDriver(logger, e.id, e.c.ToolboxPath, e.c.InitImage.Image)
if err != nil { if err != nil {
return nil, errors.Errorf("failed to create docker driver: %w", err) return nil, errors.Errorf("failed to create docker driver: %w", err)
} }
case config.DriverTypeK8s: case config.DriverTypeK8s:
d, err = driver.NewK8sDriver(logger, e.id, c.ToolboxPath) d, err = driver.NewK8sDriver(logger, e.id, c.ToolboxPath, e.c.InitImage.Image)
if err != nil { if err != nil {
return nil, errors.Errorf("failed to create kubernetes driver: %w", err) return nil, errors.Errorf("failed to create kubernetes driver: %w", err)
} }

View File

@ -264,6 +264,9 @@ func setup(ctx context.Context, t *testing.T, dir string) (*testutil.TestEmbedde
}, },
Labels: map[string]string{}, Labels: map[string]string{},
ActiveTasksLimit: 2, ActiveTasksLimit: 2,
InitImage: config.InitImage{
Image: "busybox",
},
}, },
Configstore: config.Configstore{ Configstore: config.Configstore{
Debug: false, Debug: false,