Verified Commit 81d8ab96 authored by Steve Azzopardi's avatar Steve Azzopardi Committed by Steve Azzopardi

Refactor k8s executor to use helperimage package

The Kubernetes executor is using the same logic to build the helper
image tag, which is just duplicated from the docker executor, start
using the helperimage to use the same implementation.
parent afa41f0b
......@@ -20,7 +20,7 @@ import (
api "k8s.io/api/core/v1"
"gitlab.com/gitlab-org/gitlab-runner/helpers"
"gitlab.com/gitlab-org/gitlab-runner/helpers/docker"
docker_helpers "gitlab.com/gitlab-org/gitlab-runner/helpers/docker"
"gitlab.com/gitlab-org/gitlab-runner/helpers/ssh"
"gitlab.com/gitlab-org/gitlab-runner/helpers/timeperiod"
)
......@@ -32,8 +32,6 @@ const (
PullPolicyAlways = "always"
PullPolicyNever = "never"
PullPolicyIfNotPresent = "if-not-present"
defaultHelperImage = "gitlab/gitlab-runner-helper"
)
// Get returns one of the predefined values or returns an error if the value can't match the predefined
......@@ -504,19 +502,6 @@ func (c *DockerConfig) GetOomKillDisable() *bool {
return &c.OomKillDisable
}
func (c *KubernetesConfig) GetHelperImage() string {
if len(c.HelperImage) > 0 {
return c.HelperImage
}
rev := REVISION
if rev == "HEAD" {
rev = "latest"
}
return fmt.Sprintf("%s:x86_64-%s", defaultHelperImage, rev)
}
func (c *KubernetesConfig) GetPollAttempts() int {
if c.PollTimeout <= 0 {
c.PollTimeout = KubernetesPollTimeout
......
......@@ -8,19 +8,20 @@ import (
"net/url"
"strings"
"gitlab.com/gitlab-org/gitlab-terminal"
"github.com/sirupsen/logrus"
terminal "gitlab.com/gitlab-org/gitlab-terminal"
"golang.org/x/net/context"
api "k8s.io/api/core/v1"
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"
_ "k8s.io/client-go/plugin/pkg/client/auth" // Register all available authentication methods
restclient "k8s.io/client-go/rest"
"gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/executors"
"gitlab.com/gitlab-org/gitlab-runner/helpers/dns"
"gitlab.com/gitlab-org/gitlab-runner/helpers/docker/helperimage"
"gitlab.com/gitlab-org/gitlab-runner/helpers/featureflags"
terminalsession "gitlab.com/gitlab-org/gitlab-runner/session/terminal"
)
......@@ -61,6 +62,8 @@ type executor struct {
serviceRequests api.ResourceList
helperRequests api.ResourceList
pullPolicy common.KubernetesPullPolicy
helperImageInfo helperimage.Info
}
func (s *executor) setupResources() error {
......@@ -463,7 +466,6 @@ func (s *executor) setupBuildPod() error {
}
buildImage := s.Build.GetAllVariables().ExpandValue(s.options.Image.Name)
helperImage := common.AppVersion.Variables().ExpandValue(s.Config.Kubernetes.GetHelperImage())
pod, err := s.kubeClient.CoreV1().Pods(s.configurationOverwrites.namespace).Create(&api.Pod{
ObjectMeta: metav1.ObjectMeta{
......@@ -481,7 +483,7 @@ func (s *executor) setupBuildPod() error {
Containers: append([]api.Container{
// TODO use the build and helper template here
s.buildContainer("build", buildImage, s.options.Image, s.buildRequests, s.buildLimits, s.BuildShell.DockerCommand...),
s.buildContainer("helper", helperImage, common.Image{}, s.helperRequests, s.helperLimits, s.BuildShell.DockerCommand...),
s.buildContainer("helper", s.getHelperImage(), common.Image{}, s.helperRequests, s.helperLimits, s.BuildShell.DockerCommand...),
}, services...),
TerminationGracePeriodSeconds: &s.Config.Kubernetes.TerminationGracePeriodSeconds,
ImagePullSecrets: imagePullSecrets,
......@@ -497,6 +499,14 @@ func (s *executor) setupBuildPod() error {
return nil
}
func (s *executor) getHelperImage() string {
if len(s.Config.Kubernetes.HelperImage) > 0 {
return common.AppVersion.Variables().ExpandValue(s.Config.Kubernetes.HelperImage)
}
return s.helperImageInfo.String()
}
func (s *executor) runInContainer(ctx context.Context, name string, command []string, script string) <-chan error {
errc := make(chan error, 1)
go func() {
......@@ -670,10 +680,24 @@ func (s *executor) checkDefaults() error {
}
func createFn() common.Executor {
revision := "latest"
if common.REVISION != "HEAD" {
revision = common.REVISION
}
helperImageInfo, err := helperimage.Get(revision, helperimage.Config{
OSType: helperimage.OSTypeLinux,
Architecture: "amd64",
})
if err != nil {
logrus.WithError(err).Fatal("Failed to set up helper image for kubernetes executor")
}
return &executor{
AbstractExecutor: executors.AbstractExecutor{
ExecutorOptions: executorOptions,
},
helperImageInfo: helperImageInfo,
}
}
......
......@@ -30,6 +30,7 @@ import (
"gitlab.com/gitlab-org/gitlab-runner/executors"
"gitlab.com/gitlab-org/gitlab-runner/helpers"
dns_test "gitlab.com/gitlab-org/gitlab-runner/helpers/dns/test"
"gitlab.com/gitlab-org/gitlab-runner/helpers/docker/helperimage"
"gitlab.com/gitlab-org/gitlab-runner/helpers/featureflags"
"gitlab.com/gitlab-org/gitlab-runner/session"
)
......@@ -1460,6 +1461,12 @@ func TestSetupBuildPod(t *testing.T) {
for testName, test := range tests {
t.Run(testName, func(t *testing.T) {
helperImageInfo, err := helperimage.Get("latest", helperimage.Config{
OSType: helperimage.OSTypeLinux,
Architecture: "amd64",
})
require.NoError(t, err)
vars := test.Variables
if vars == nil {
vars = []common.JobVariable{}
......@@ -1488,13 +1495,14 @@ func TestSetupBuildPod(t *testing.T) {
Runner: &test.RunnerConfig,
},
},
helperImageInfo: helperImageInfo,
}
if test.PrepareFn != nil {
test.PrepareFn(t, test, &ex)
}
err := ex.prepareOverwrites(make(common.JobVariables, 0))
err = ex.prepareOverwrites(make(common.JobVariables, 0))
assert.NoError(t, err, "error preparing overwrites")
err = ex.setupBuildPod()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment