Skip to content
Snippets Groups Projects
Commit abc1f12d authored by Tomasz Maczukin's avatar Tomasz Maczukin
Browse files

Merge branch 'k8s-pod-labels' into 'master'

Add PodLabels field to Kubernetes config structure

See merge request !558
parents e92b4833 6cc22ef0
No related branches found
No related tags found
No related merge requests found
......@@ -146,6 +146,7 @@ type KubernetesConfig struct {
TerminationGracePeriodSeconds int64 `toml:"terminationGracePeriodSeconds,omitzero" json:"terminationGracePeriodSeconds" long:"terminationGracePeriodSeconds" env:"KUBERNETES_TERMINATIONGRACEPERIODSECONDS" description:"Duration after the processes running in the pod are sent a termination signal and the time when the processes are forcibly halted with a kill signal."`
PollInterval int `toml:"poll_interval,omitzero" json:"poll_interval" long:"poll-interval" env:"KUBERNETES_POLL_INTERVAL" description:"How frequently, in seconds, the runner will poll the Kubernetes pod it has just created to check its status"`
PollTimeout int `toml:"poll_timeout,omitzero" json:"poll_timeout" long:"poll-timeout" env:"KUBERNETES_POLL_TIMEOUT" description:"The total amount of time, in seconds, that needs to pass before the runner will timeout attempting to connect to the pod it has just created (useful for queueing more builds that the cluster can handle at a time)"`
PodLabels map[string]string `toml:"pod_labels,omitempty" json:"pod_labels" long:"pod-labels" description:"A toml table/json object of key-value. Value is expected to be a string. When set, this will create pods with the given pod labels. Environment variables will be substituted for values here."`
}
type RunnerCredentials struct {
......
......@@ -72,6 +72,7 @@ The following keywords help to define the behaviour of the Runner within Kuberne
- `terminationGracePeriodSeconds`: Duration after the processes running in the pod are sent a termination signal and the time when the processes are forcibly halted with a kill signal
- `poll_interval`: How frequently, in seconds, the runner will poll the Kubernetes pod it has just created to check its status. [Default: 3]
- `poll_timeout`: The amount of time, in seconds, that needs to pass before the runner will timeout attempting to connect to the container it has just created (useful for queueing more builds that the cluster can handle at a time) [Default: 180]
- `pod_labels`: A set of labels to be added to each build pod created by the runner. The value of these can include environment variables for expansion.
### Overwriting Kubernetes Namespace
......
......@@ -244,6 +244,10 @@ func (s *executor) setupBuildPod() error {
resolvedImage := s.Build.GetAllVariables().ExpandValue(image)
services[i] = s.buildContainer(fmt.Sprintf("svc-%d", i), resolvedImage, s.serviceRequests, s.serviceLimits)
}
labels := make(map[string]string)
for k, v := range s.Build.Runner.Kubernetes.PodLabels {
labels[k] = s.Build.Variables.ExpandValue(v)
}
var imagePullSecrets []api.LocalObjectReference
for _, imagePullSecret := range s.Config.Kubernetes.ImagePullSecrets {
......@@ -260,6 +264,7 @@ func (s *executor) setupBuildPod() error {
ObjectMeta: api.ObjectMeta{
GenerateName: s.Build.ProjectUniqueName(),
Namespace: s.Config.Kubernetes.Namespace,
Labels: labels,
},
Spec: api.PodSpec{
Volumes: []api.Volume{
......
......@@ -517,6 +517,7 @@ func TestSetupBuildPod(t *testing.T) {
RunnerConfig common.RunnerConfig
PrepareFn func(*testing.T, testDef, *executor)
VerifyFn func(*testing.T, testDef, *api.Pod)
Variables []common.JobVariable
}
tests := []testDef{
{
......@@ -606,6 +607,30 @@ func TestSetupBuildPod(t *testing.T) {
}
},
},
{
RunnerConfig: common.RunnerConfig{
RunnerSettings: common.RunnerSettings{
Kubernetes: &common.KubernetesConfig{
Namespace: "default",
PodLabels: map[string]string{
"test": "label",
"another": "label",
"var": "$test",
},
},
},
},
VerifyFn: func(t *testing.T, test testDef, pod *api.Pod) {
assert.Equal(t, map[string]string{
"test": "label",
"another": "label",
"var": "sometestvar",
}, pod.ObjectMeta.Labels)
},
Variables: []common.JobVariable{
{Key: "test", Value: "sometestvar"},
},
},
}
executed := false
......@@ -648,6 +673,10 @@ func TestSetupBuildPod(t *testing.T) {
}
c.Client = fakeClient.Client
vars := test.Variables
if vars == nil {
vars = []common.JobVariable{}
}
ex := executor{
kubeClient: c,
options: &kubernetesOptions{},
......@@ -656,9 +685,9 @@ func TestSetupBuildPod(t *testing.T) {
BuildShell: &common.ShellConfiguration{},
Build: &common.Build{
JobResponse: common.JobResponse{
Variables: []common.JobVariable{},
Variables: vars,
},
Runner: &common.RunnerConfig{},
Runner: &test.RunnerConfig,
},
},
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment