Commit ebcdf469 authored by Georgi N. Georgiev's avatar Georgi N. Georgiev
Browse files

Merge branch 'ak/label-cache-init-container' into 'master'

Add labels to cache-init docker container

Closes #25481

See merge request gitlab-org/gitlab-runner!2412
parents 6c788d94 c2840dc5
......@@ -172,7 +172,7 @@ func (m *manager) createHostBasedCacheVolume(destination string) error {
func (m *manager) createCacheVolume(ctx context.Context, destination string) (string, error) {
destination, err := m.absolutePath(destination)
if err != nil {
return "", fmt.Errorf("defining absolute path:%w", err)
return "", fmt.Errorf("defining absolute path: %w", err)
}
err = m.managedVolumes.Add(destination)
......@@ -192,7 +192,7 @@ func (m *manager) createCacheVolume(ctx context.Context, destination string) (st
}
if m.permissionSetter != nil {
err = m.permissionSetter.Set(ctx, v.Name)
err = m.permissionSetter.Set(ctx, v.Name, m.labeler.Labels(map[string]string{"type": "cache-init"}))
if err != nil {
return "", fmt.Errorf("set volume permissions: %w", err)
}
......
......@@ -38,13 +38,13 @@ func NewDockerLinuxSetter(c docker.Client, logger logrus.FieldLogger, helperImag
// 755, so everyone can read from it but only root can write to it. This
// prevents images that don't have root user to fail to write to mounted
// volumes.
func (d *dockerLinuxSetter) Set(ctx context.Context, volumeName string) error {
func (d *dockerLinuxSetter) Set(ctx context.Context, volumeName string, labels map[string]string) error {
d.logger = d.logger.WithFields(logrus.Fields{
"volume_name": volumeName,
"context": "set_volume_permission",
})
containerID, err := d.createContainer(ctx, volumeName)
containerID, err := d.createContainer(ctx, volumeName, labels)
if err != nil {
return fmt.Errorf("create permission container for volume %q: %w", volumeName, err)
}
......@@ -66,12 +66,17 @@ func (d *dockerLinuxSetter) Set(ctx context.Context, volumeName string) error {
return nil
}
func (d *dockerLinuxSetter) createContainer(ctx context.Context, volumeName string) (string, error) {
func (d *dockerLinuxSetter) createContainer(
ctx context.Context,
volumeName string,
labels map[string]string,
) (string, error) {
volumeBinding := fmt.Sprintf("%s:%s", volumeName, dstMount)
config := &container.Config{
Image: d.helperImage.ID,
Cmd: []string{"gitlab-runner-helper", "cache-init", dstMount},
Image: d.helperImage.ID,
Cmd: []string{"gitlab-runner-helper", "cache-init", dstMount},
Labels: labels,
}
hostConfig := &container.HostConfig{
......
......@@ -29,7 +29,7 @@ func TestDockerLinuxSetter(t *testing.T) {
volumeName, setter, client, cleanup := setupTestDockerLinuxSetter(t)
defer cleanup()
err := setter.Set(context.Background(), volumeName)
err := setter.Set(context.Background(), volumeName, map[string]string{})
assert.NoError(t, err)
config := &container.Config{
......
......@@ -11,6 +11,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-runner/executors/docker/internal/wait"
"gitlab.com/gitlab-org/gitlab-runner/helpers/docker"
......@@ -37,6 +38,8 @@ func TestDockerLinuxSetter_Set(t *testing.T) {
containerCmdMatcher := mock.MatchedBy(func(cfg *container.Config) bool {
assert.Equal(t, helperImageID, cfg.Image)
assert.Len(t, cfg.Cmd, 3)
require.Contains(t, cfg.Labels, "foo")
assert.Equal(t, "bar", cfg.Labels["foo"])
return true
})
......@@ -153,7 +156,7 @@ func TestDockerLinuxSetter_Set(t *testing.T) {
},
}
err := setter.Set(context.Background(), volume)
err := setter.Set(context.Background(), volume, map[string]string{"foo": "bar"})
assert.True(t, errors.Is(err, tt.expectedErr), "expected err %T, but got %T", tt.expectedErr, err)
})
}
......
......@@ -13,13 +13,13 @@ type MockSetter struct {
mock.Mock
}
// Set provides a mock function with given fields: ctx, volumeName
func (_m *MockSetter) Set(ctx context.Context, volumeName string) error {
ret := _m.Called(ctx, volumeName)
// Set provides a mock function with given fields: ctx, volumeName, labels
func (_m *MockSetter) Set(ctx context.Context, volumeName string, labels map[string]string) error {
ret := _m.Called(ctx, volumeName, labels)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, string) error); ok {
r0 = rf(ctx, volumeName)
if rf, ok := ret.Get(0).(func(context.Context, string, map[string]string) error); ok {
r0 = rf(ctx, volumeName, labels)
} else {
r0 = ret.Error(0)
}
......
......@@ -3,5 +3,5 @@ package permission
import "context"
type Setter interface {
Set(ctx context.Context, volumeName string) error
Set(ctx context.Context, volumeName string, labels map[string]string) error
}
......@@ -16,6 +16,6 @@ func NewDockerWindowsSetter() Setter {
}
// Set noop
func (d dockerWindowsSetter) Set(_ context.Context, _ string) error {
func (d dockerWindowsSetter) Set(_ context.Context, _ string, _ map[string]string) error {
return nil
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment