Skip to content
Snippets Groups Projects
Commit fa285e87 authored by Rodrigo Chacon's avatar Rodrigo Chacon Committed by Lars Seipel
Browse files

executors/kubernetes: map service name as pod HostAlias

parent 951ca1bd
No related branches found
No related tags found
No related merge requests found
......@@ -14,6 +14,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
// Register all available authentication methods
_ "k8s.io/client-go/plugin/pkg/client/auth"
restclient "k8s.io/client-go/rest"
......@@ -437,9 +438,18 @@ func (s *executor) setupCredentials() error {
func (s *executor) setupBuildPod() error {
services := make([]api.Container, len(s.options.Services))
servicesHostAlias := api.HostAlias{IP: "127.0.0.1"}
for i, service := range s.options.Services {
resolvedImage := s.Build.GetAllVariables().ExpandValue(service.Name)
services[i] = s.buildContainer(fmt.Sprintf("svc-%d", i), resolvedImage, service, s.serviceRequests, s.serviceLimits)
_, _, _, aliases := common.SplitServiceAndVersion(service.Name)
for _, a := range aliases {
servicesHostAlias.Hostnames = append(servicesHostAlias.Hostnames, a)
}
if service.Alias != "" {
servicesHostAlias.Hostnames = append(servicesHostAlias.Hostnames, service.Alias)
}
}
labels := make(map[string]string)
......@@ -484,6 +494,7 @@ func (s *executor) setupBuildPod() error {
}, services...),
TerminationGracePeriodSeconds: &s.Config.Kubernetes.TerminationGracePeriodSeconds,
ImagePullSecrets: imagePullSecrets,
HostAliases: []api.HostAlias{servicesHostAlias},
},
})
......
......@@ -1316,6 +1316,69 @@ func TestSetupBuildPod(t *testing.T) {
assert.Equal(t, []string{"application", "--debug"}, pod.Spec.Containers[3].Args)
},
},
"supports services as host aliases": {
RunnerConfig: common.RunnerConfig{
RunnerSettings: common.RunnerSettings{
Kubernetes: &common.KubernetesConfig{
Namespace: "default",
HelperImage: "custom/helper-image",
},
},
},
Options: &kubernetesOptions{
Image: common.Image{
Name: "test-image",
Entrypoint: []string{"/init", "run"},
},
Services: common.Services{
{
Name: "test-service",
Alias: "svc-alias",
},
{
Name: "docker:dind",
},
},
},
VerifyFn: func(t *testing.T, test setupBuildPodTestDef, pod *api.Pod) {
require.Len(t, pod.Spec.Containers, 4)
expectedHostnames := []string{
"test-service",
"svc-alias",
"docker",
}
assert.Equal(t, "127.0.0.1", pod.Spec.HostAliases[0].IP)
assert.Equal(t, expectedHostnames, pod.Spec.HostAliases[0].Hostnames)
},
},
"supports services as kubernetes host aliases with simple docker image syntax": {
RunnerConfig: common.RunnerConfig{
RunnerSettings: common.RunnerSettings{
Kubernetes: &common.KubernetesConfig{
Namespace: "default",
HelperImage: "custom/helper-image",
},
},
},
Options: &kubernetesOptions{
Image: common.Image{
Name: "test-image",
Entrypoint: []string{"/init", "run"},
},
Services: common.Services{
{
Name: "docker:dind",
},
},
},
VerifyFn: func(t *testing.T, test setupBuildPodTestDef, pod *api.Pod) {
require.Len(t, pod.Spec.Containers, 3)
assert.Equal(t, "127.0.0.1", pod.Spec.HostAliases[0].IP)
assert.Equal(t, []string{"docker"}, pod.Spec.HostAliases[0].Hostnames)
},
},
// TODO: Remove the mention of Feature Flag in 12.0, make it the only proper test case.
"properly sets command (entrypoint) and args when FF_K8S_USE_ENTRYPOINT_OVER_COMMAND is on": {
RunnerConfig: common.RunnerConfig{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment