mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-09-15 18:57:01 +00:00
fix: sanitize network aliases to be valid DNS names (#190)
- s/[^A-Z0-9-]/_/g - add a log line in case the name is sanitized Closes forgejo/runner#226 --- It is breaking because it will fail jobs that rely on service names that contain characters that are sanitized Reviewed-on: https://code.forgejo.org/forgejo/act/pulls/190 Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Co-authored-by: Earl Warren <contact@earl-warren.org> Co-committed-by: Earl Warren <contact@earl-warren.org>
This commit is contained in:
parent
4a8d6556c7
commit
2be7a6f1a5
2 changed files with 21 additions and 2 deletions
|
@ -402,6 +402,17 @@ func (rc *RunContext) getNetworkName(_ context.Context) (networkName string, cre
|
||||||
return networkName, createAndDeleteNetwork
|
return networkName, createAndDeleteNetwork
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var sanitizeNetworkAliasRegex = regexp.MustCompile("[^A-Z0-9-]")
|
||||||
|
|
||||||
|
func sanitizeNetworkAlias(ctx context.Context, original string) string {
|
||||||
|
sanitized := sanitizeNetworkAliasRegex.ReplaceAllString(strings.ToUpper(original), "_")
|
||||||
|
if sanitized != original {
|
||||||
|
logger := common.Logger(ctx)
|
||||||
|
logger.Infof("The network alias is %s (sanitized version of %s)", original, sanitized)
|
||||||
|
}
|
||||||
|
return sanitized
|
||||||
|
}
|
||||||
|
|
||||||
func (rc *RunContext) prepareJobContainer(ctx context.Context) error {
|
func (rc *RunContext) prepareJobContainer(ctx context.Context) error {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
image := rc.platformImage(ctx)
|
image := rc.platformImage(ctx)
|
||||||
|
@ -492,7 +503,7 @@ func (rc *RunContext) prepareJobContainer(ctx context.Context) error {
|
||||||
Platform: rc.Config.ContainerArchitecture,
|
Platform: rc.Config.ContainerArchitecture,
|
||||||
AutoRemove: rc.Config.AutoRemove,
|
AutoRemove: rc.Config.AutoRemove,
|
||||||
NetworkMode: networkName,
|
NetworkMode: networkName,
|
||||||
NetworkAliases: []string{serviceID},
|
NetworkAliases: []string{sanitizeNetworkAlias(ctx, serviceID)},
|
||||||
ExposedPorts: exposedPorts,
|
ExposedPorts: exposedPorts,
|
||||||
PortBindings: portBindings,
|
PortBindings: portBindings,
|
||||||
ValidVolumes: rc.Config.ValidVolumes,
|
ValidVolumes: rc.Config.ValidVolumes,
|
||||||
|
@ -547,7 +558,7 @@ func (rc *RunContext) prepareJobContainer(ctx context.Context) error {
|
||||||
ToolCache: rc.getToolCache(ctx),
|
ToolCache: rc.getToolCache(ctx),
|
||||||
Mounts: mounts,
|
Mounts: mounts,
|
||||||
NetworkMode: networkName,
|
NetworkMode: networkName,
|
||||||
NetworkAliases: []string{rc.Name},
|
NetworkAliases: []string{sanitizeNetworkAlias(ctx, rc.Name)},
|
||||||
Binds: binds,
|
Binds: binds,
|
||||||
Stdout: logWriter,
|
Stdout: logWriter,
|
||||||
Stderr: logWriter,
|
Stderr: logWriter,
|
||||||
|
|
|
@ -711,6 +711,14 @@ func Test_createSimpleContainerName(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSanitizeNetworkAlias(t *testing.T) {
|
||||||
|
same := "SAME"
|
||||||
|
assert.Equal(t, same, sanitizeNetworkAlias(context.Background(), same))
|
||||||
|
original := "OR.IGIN'A-L"
|
||||||
|
sanitized := "OR_IGIN_A-L"
|
||||||
|
assert.Equal(t, sanitized, sanitizeNetworkAlias(context.Background(), original))
|
||||||
|
}
|
||||||
|
|
||||||
func TestPrepareJobContainer(t *testing.T) {
|
func TestPrepareJobContainer(t *testing.T) {
|
||||||
yaml := `
|
yaml := `
|
||||||
on:
|
on:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue