1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
package kubernetes
import (
"fmt"
api "k8s.io/api/core/v1"
"gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/helpers/container/services"
"gitlab.com/gitlab-org/gitlab-runner/helpers/dns"
)
type invalidHostAliasDNSError struct {
service common.Image
inner error
}
func (e *invalidHostAliasDNSError) Error() string {
return fmt.Sprintf(
"provided host alias %s for service %s is invalid DNS. %s",
e.service.Alias,
e.service.Name,
e.inner,
)
}
func (e *invalidHostAliasDNSError) Is(err error) bool {
_, ok := err.(*invalidHostAliasDNSError)
return ok
}
func createHostAliases(services common.Services, hostAliases []api.HostAlias) ([]api.HostAlias, error) {
servicesHostAlias, err := createServicesHostAlias(services)
if err != nil {
return nil, err
}
// The order that we add host aliases matter here. The host file resolves
// host on a firs-come-first-served basis. We always want to have the
// service host aliases first so it resolves to that ip.
var allHostAliases []api.HostAlias
if servicesHostAlias != nil {
allHostAliases = append(allHostAliases, *servicesHostAlias)
}
allHostAliases = append(allHostAliases, hostAliases...)
return allHostAliases, nil
}
func createServicesHostAlias(srvs common.Services) (*api.HostAlias, error) {
var hostnames []string
for _, srv := range srvs {
// Services with ports are coming from .gitlab-webide.yml
// they are used for ports mapping and their aliases are in no way validated
// so we ignore them. Check out https://gitlab.com/gitlab-org/gitlab-runner/merge_requests/1170
// for details
if len(srv.Ports) > 0 {
continue
}
serviceMeta := services.SplitNameAndVersion(srv.Name)
for _, alias := range serviceMeta.Aliases {
// For backward compatibility reasons a non DNS1123 compliant alias might be generated,
// this will be removed in https://gitlab.com/gitlab-org/gitlab-runner/issues/6100
err := dns.ValidateDNS1123Subdomain(alias)
if err == nil {
hostnames = append(hostnames, alias)
}
}
if srv.Alias == "" {
continue
}
err := dns.ValidateDNS1123Subdomain(srv.Alias)
if err != nil {
return nil, &invalidHostAliasDNSError{service: srv, inner: err}
}
hostnames = append(hostnames, srv.Alias)
}
// no service hostnames to add to aliases
if len(hostnames) == 0 {
return nil, nil
}
return &api.HostAlias{IP: "127.0.0.1", Hostnames: hostnames}, nil
}
|