mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-09-15 18:57:01 +00:00
chore: refactor internal volume names accessors
- getInternalVolumeWorkdir is used instead of rc.jobContainerName() - getInternalVolumeEnv is used insted of rc.jobContainerName() + "-env" - getInternalVolumeNames is used when all internal volumes are needed
This commit is contained in:
parent
8a8e01522d
commit
023427115f
2 changed files with 20 additions and 11 deletions
|
@ -117,10 +117,23 @@ func getDockerDaemonSocketMountPath(daemonPath string) string {
|
||||||
return daemonPath
|
return daemonPath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rc *RunContext) getInternalVolumeNames(ctx context.Context) []string {
|
||||||
|
return []string{
|
||||||
|
rc.getInternalVolumeWorkdir(ctx),
|
||||||
|
rc.getInternalVolumeEnv(ctx),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rc *RunContext) getInternalVolumeWorkdir(ctx context.Context) string {
|
||||||
|
return rc.jobContainerName()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rc *RunContext) getInternalVolumeEnv(ctx context.Context) string {
|
||||||
|
return fmt.Sprintf("%s-env", rc.jobContainerName())
|
||||||
|
}
|
||||||
|
|
||||||
// Returns the binds and mounts for the container, resolving paths as appopriate
|
// Returns the binds and mounts for the container, resolving paths as appopriate
|
||||||
func (rc *RunContext) GetBindsAndMounts(ctx context.Context) ([]string, map[string]string, []string) {
|
func (rc *RunContext) GetBindsAndMounts(ctx context.Context) ([]string, map[string]string, []string) {
|
||||||
name := rc.jobContainerName()
|
|
||||||
|
|
||||||
if rc.Config.ContainerDaemonSocket == "" {
|
if rc.Config.ContainerDaemonSocket == "" {
|
||||||
rc.Config.ContainerDaemonSocket = "/var/run/docker.sock"
|
rc.Config.ContainerDaemonSocket = "/var/run/docker.sock"
|
||||||
}
|
}
|
||||||
|
@ -134,7 +147,7 @@ func (rc *RunContext) GetBindsAndMounts(ctx context.Context) ([]string, map[stri
|
||||||
ext := container.LinuxContainerEnvironmentExtensions{}
|
ext := container.LinuxContainerEnvironmentExtensions{}
|
||||||
|
|
||||||
mounts := map[string]string{
|
mounts := map[string]string{
|
||||||
name + "-env": ext.GetActPath(),
|
rc.getInternalVolumeEnv(ctx): ext.GetActPath(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if job := rc.Run.Job(); job != nil {
|
if job := rc.Run.Job(); job != nil {
|
||||||
|
@ -162,14 +175,10 @@ func (rc *RunContext) GetBindsAndMounts(ctx context.Context) ([]string, map[stri
|
||||||
}
|
}
|
||||||
binds = append(binds, fmt.Sprintf("%s:%s%s", rc.Config.Workdir, ext.ToContainerPath(rc.Config.Workdir), bindModifiers))
|
binds = append(binds, fmt.Sprintf("%s:%s%s", rc.Config.Workdir, ext.ToContainerPath(rc.Config.Workdir), bindModifiers))
|
||||||
} else {
|
} else {
|
||||||
mounts[name] = ext.ToContainerPath(rc.Config.Workdir)
|
mounts[rc.getInternalVolumeWorkdir(ctx)] = ext.ToContainerPath(rc.Config.Workdir)
|
||||||
}
|
}
|
||||||
|
|
||||||
validVolumes := []string{
|
validVolumes := append(rc.getInternalVolumeNames(ctx), getDockerDaemonSocketMountPath(rc.Config.ContainerDaemonSocket))
|
||||||
name,
|
|
||||||
name + "-env",
|
|
||||||
getDockerDaemonSocketMountPath(rc.Config.ContainerDaemonSocket),
|
|
||||||
}
|
|
||||||
validVolumes = append(validVolumes, rc.Config.ValidVolumes...)
|
validVolumes = append(validVolumes, rc.Config.ValidVolumes...)
|
||||||
return binds, mounts, validVolumes
|
return binds, mounts, validVolumes
|
||||||
}
|
}
|
||||||
|
@ -523,7 +532,7 @@ func (rc *RunContext) prepareJobContainer(ctx context.Context) error {
|
||||||
|
|
||||||
if rc.JobContainer != nil {
|
if rc.JobContainer != nil {
|
||||||
return rc.JobContainer.Remove().IfNot(reuseJobContainer).
|
return rc.JobContainer.Remove().IfNot(reuseJobContainer).
|
||||||
Then(container.NewDockerVolumesRemoveExecutor([]string{rc.jobContainerName(), rc.jobContainerName() + "-env"})).IfNot(reuseJobContainer).
|
Then(container.NewDockerVolumesRemoveExecutor(rc.getInternalVolumeNames(ctx))).IfNot(reuseJobContainer).
|
||||||
Then(func(ctx context.Context) error {
|
Then(func(ctx context.Context) error {
|
||||||
if len(rc.ServiceContainers) > 0 {
|
if len(rc.ServiceContainers) > 0 {
|
||||||
logger.Infof("Cleaning up services for job %s", rc.JobName)
|
logger.Infof("Cleaning up services for job %s", rc.JobName)
|
||||||
|
|
|
@ -229,7 +229,7 @@ func TestRunContext_GetBindsAndMounts(t *testing.T) {
|
||||||
}
|
}
|
||||||
assert.Contains(t, gotbind, fullBind)
|
assert.Contains(t, gotbind, fullBind)
|
||||||
} else {
|
} else {
|
||||||
mountkey := testcase.rc.jobContainerName()
|
mountkey := testcase.rc.getInternalVolumeWorkdir(t.Context())
|
||||||
assert.EqualValues(t, testcase.wantmount, gotmount[mountkey])
|
assert.EqualValues(t, testcase.wantmount, gotmount[mountkey])
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue