Skip to content
Snippets Groups Projects
Commit 4ef4d802 authored by Kamil Trzciński's avatar Kamil Trzciński
Browse files

Merge branch '3883-fix-dns-1123-with-docker-machine' into 'master'

Make new runner tokens compatible with docker-machine executor

Closes #3883

See merge request gitlab-org/gitlab-runner!1144
parents dd021465 7ae0170a
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"time" "time"
"gitlab.com/gitlab-org/gitlab-runner/common" "gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/helpers/dns"
) )
func machineFormat(runner string, template string) string { func machineFormat(runner string, template string) string {
...@@ -17,7 +18,7 @@ func machineFormat(runner string, template string) string { ...@@ -17,7 +18,7 @@ func machineFormat(runner string, template string) string {
} }
func machineFilter(config *common.RunnerConfig) string { func machineFilter(config *common.RunnerConfig) string {
return machineFormat(config.ShortDescription(), config.Machine.MachineName) return machineFormat(dns.MakeRFC1123Compatible(config.ShortDescription()), config.Machine.MachineName)
} }
func matchesMachineFilter(name, filter string) bool { func matchesMachineFilter(name, filter string) bool {
......
package machine package machine
import ( import (
"testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/gitlab-runner/common" "gitlab.com/gitlab-org/gitlab-runner/common"
"testing" dns_test "gitlab.com/gitlab-org/gitlab-runner/helpers/dns/test"
) )
func TestMachineNewName(t *testing.T) { func TestNewMachineName(t *testing.T) {
testCases := map[string]struct {
token string
}{
"DNS-1123 compatible token": {
token: "token-of",
},
"non DNS-1123 compatible token": {
token: "ToK3_?OF",
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
config := &common.RunnerConfig{
RunnerCredentials: common.RunnerCredentials{
Token: testCase.token,
},
RunnerSettings: common.RunnerSettings{
Machine: &common.DockerMachine{
MachineName: "test-machine-%s",
},
},
}
name := newMachineName(config)
dns_test.AssertRFC1123Compatibility(t, name)
})
}
}
func TestNewMachineNameIsUnique(t *testing.T) {
config := &common.RunnerConfig{ config := &common.RunnerConfig{
RunnerSettings: common.RunnerSettings{ RunnerSettings: common.RunnerSettings{
Machine: &common.DockerMachine{ Machine: &common.DockerMachine{
......
...@@ -20,6 +20,7 @@ import ( ...@@ -20,6 +20,7 @@ import (
"gitlab.com/gitlab-org/gitlab-runner/common" "gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/executors" "gitlab.com/gitlab-org/gitlab-runner/executors"
"gitlab.com/gitlab-org/gitlab-runner/helpers/dns"
terminalsession "gitlab.com/gitlab-org/gitlab-runner/session/terminal" terminalsession "gitlab.com/gitlab-org/gitlab-runner/session/terminal"
) )
...@@ -394,7 +395,7 @@ type dockerConfigEntry struct { ...@@ -394,7 +395,7 @@ type dockerConfigEntry struct {
} }
func (s *executor) projectUniqueName() string { func (s *executor) projectUniqueName() string {
return makeDNS1123Compatible(s.Build.ProjectUniqueName()) return dns.MakeRFC1123Compatible(s.Build.ProjectUniqueName())
} }
func (s *executor) setupCredentials() error { func (s *executor) setupCredentials() error {
......
...@@ -20,7 +20,6 @@ import ( ...@@ -20,7 +20,6 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-runner/session"
api "k8s.io/api/core/v1" api "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
...@@ -30,6 +29,8 @@ import ( ...@@ -30,6 +29,8 @@ import (
"gitlab.com/gitlab-org/gitlab-runner/common" "gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/executors" "gitlab.com/gitlab-org/gitlab-runner/executors"
"gitlab.com/gitlab-org/gitlab-runner/helpers" "gitlab.com/gitlab-org/gitlab-runner/helpers"
dns_test "gitlab.com/gitlab-org/gitlab-runner/helpers/dns/test"
"gitlab.com/gitlab-org/gitlab-runner/session"
) )
var ( var (
...@@ -940,7 +941,7 @@ func TestSetupCredentials(t *testing.T) { ...@@ -940,7 +941,7 @@ func TestSetupCredentials(t *testing.T) {
}, },
}, },
VerifyFn: func(t *testing.T, test testDef, secret *api.Secret) { VerifyFn: func(t *testing.T, test testDef, secret *api.Secret) {
assertDNS1123Compatibility(t, secret.GetGenerateName()) dns_test.AssertRFC1123Compatibility(t, secret.GetGenerateName())
}, },
}, },
} }
...@@ -1451,7 +1452,7 @@ func TestSetupBuildPod(t *testing.T) { ...@@ -1451,7 +1452,7 @@ func TestSetupBuildPod(t *testing.T) {
}, },
}, },
VerifyFn: func(t *testing.T, test setupBuildPodTestDef, pod *api.Pod) { VerifyFn: func(t *testing.T, test setupBuildPodTestDef, pod *api.Pod) {
assertDNS1123Compatibility(t, pod.GetGenerateName()) dns_test.AssertRFC1123Compatibility(t, pod.GetGenerateName())
}, },
}, },
} }
......
...@@ -5,8 +5,6 @@ import ( ...@@ -5,8 +5,6 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"regexp"
"strings"
"time" "time"
"golang.org/x/net/context" "golang.org/x/net/context"
...@@ -20,12 +18,6 @@ import ( ...@@ -20,12 +18,6 @@ import (
"gitlab.com/gitlab-org/gitlab-runner/common" "gitlab.com/gitlab-org/gitlab-runner/common"
) )
const (
DNS1123NameMaximumLength = 63
DNS1123NotAllowedCharacters = "[^-a-z0-9]"
DNS1123NotAllowedStartCharacters = "^[^a-z0-9]+"
)
type kubeConfigProvider func() (*restclient.Config, error) type kubeConfigProvider func() (*restclient.Config, error)
var ( var (
...@@ -264,19 +256,3 @@ func buildVariables(bv common.JobVariables) []api.EnvVar { ...@@ -264,19 +256,3 @@ func buildVariables(bv common.JobVariables) []api.EnvVar {
} }
return e return e
} }
func makeDNS1123Compatible(name string) string {
name = strings.ToLower(name)
nameNotAllowedChars := regexp.MustCompile(DNS1123NotAllowedCharacters)
name = nameNotAllowedChars.ReplaceAllString(name, "")
nameNotAllowedStartChars := regexp.MustCompile(DNS1123NotAllowedStartCharacters)
name = nameNotAllowedStartChars.ReplaceAllString(name, "")
if len(name) > DNS1123NameMaximumLength {
name = name[0:DNS1123NameMaximumLength]
}
return name
}
...@@ -6,7 +6,6 @@ import ( ...@@ -6,7 +6,6 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"regexp"
"strings" "strings"
"testing" "testing"
...@@ -401,31 +400,3 @@ func testVersionAndCodec() (version string, codec runtime.Codec) { ...@@ -401,31 +400,3 @@ func testVersionAndCodec() (version string, codec runtime.Codec) {
return return
} }
func assertDNS1123Compatibility(t *testing.T, name string) {
dns1123MaxLength := 63
dns1123FormatRegexp := regexp.MustCompile("^[a-z0-9]([-a-z0-9]*[a-z0-9])?$")
assert.True(t, len(name) <= dns1123MaxLength, "Name length needs to be shorter than %d", dns1123MaxLength)
assert.Regexp(t, dns1123FormatRegexp, name, "Name needs to be in DNS-1123 allowed format")
}
func TestMakeDNS1123Compatible(t *testing.T) {
examples := []struct {
name string
expected string
}{
{name: "tOk3_?ofTHE-Runner", expected: "tok3ofthe-runner"},
{name: "----tOk3_?ofTHE-Runner", expected: "tok3ofthe-runner"},
{name: "very-long-token-----------------------------------------------end", expected: "very-long-token-----------------------------------------------e"},
}
for _, example := range examples {
t.Run(example.name, func(t *testing.T) {
name := makeDNS1123Compatible(example.name)
assert.Equal(t, example.expected, name)
assertDNS1123Compatibility(t, name)
})
}
}
package test
import (
"regexp"
"testing"
"github.com/stretchr/testify/assert"
)
func AssertRFC1123Compatibility(t *testing.T, name string) {
dns1123MaxLength := 63
dns1123FormatRegexp := regexp.MustCompile("^[a-z0-9]([-a-z0-9]*[a-z0-9])?$")
assert.True(t, len(name) <= dns1123MaxLength, "Name length needs to be shorter than %d", dns1123MaxLength)
assert.Regexp(t, dns1123FormatRegexp, name, "Name needs to be in RFC-1123 allowed format")
}
package dns
import (
"regexp"
"strings"
)
const (
RFC1123NameMaximumLength = 63
RFC1123NotAllowedCharacters = "[^-a-z0-9]"
RFC1123NotAllowedStartCharacters = "^[^a-z0-9]+"
)
func MakeRFC1123Compatible(name string) string {
name = strings.ToLower(name)
nameNotAllowedChars := regexp.MustCompile(RFC1123NotAllowedCharacters)
name = nameNotAllowedChars.ReplaceAllString(name, "")
nameNotAllowedStartChars := regexp.MustCompile(RFC1123NotAllowedStartCharacters)
name = nameNotAllowedStartChars.ReplaceAllString(name, "")
if len(name) > RFC1123NameMaximumLength {
name = name[0:RFC1123NameMaximumLength]
}
return name
}
package dns
import (
"testing"
"github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/gitlab-runner/helpers/dns/test"
)
func TestMakeRFC1123Compatible(t *testing.T) {
examples := []struct {
name string
expected string
}{
{name: "tOk3_?ofTHE-Runner", expected: "tok3ofthe-runner"},
{name: "----tOk3_?ofTHE-Runner", expected: "tok3ofthe-runner"},
{name: "very-long-token-----------------------------------------------end", expected: "very-long-token-----------------------------------------------e"},
}
for _, example := range examples {
t.Run(example.name, func(t *testing.T) {
name := MakeRFC1123Compatible(example.name)
assert.Equal(t, example.expected, name)
test.AssertRFC1123Compatibility(t, name)
})
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment