From 8251519278edc2366ab42fb5d7458f730755514f Mon Sep 17 00:00:00 2001 From: Alexander Chernov Date: Tue, 5 Jul 2022 17:21:53 +0100 Subject: [PATCH] feat: added environment setup. --- README.md | 2 ++ api/v1beta1/runner_types.go | 4 ++++ api/v1beta1/zz_generated.deepcopy.go | 5 +++++ config/crd/bases/gitlab.k8s.alekc.dev_runners.yaml | 6 ++++++ controllers/runner_controller_test.go | 12 ++++++++++++ internal/generate/config.go | 5 +++-- 6 files changed, 32 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2388d2a..3a8d1a2 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,8 @@ kind: Runner metadata: name: runner-sample spec: + environment: + - "bar=foo" concurrent: 1 log_level: info gitlab_instance_url: https://gitlab.com diff --git a/api/v1beta1/runner_types.go b/api/v1beta1/runner_types.go index 3e7272c..6792376 100644 --- a/api/v1beta1/runner_types.go +++ b/api/v1beta1/runner_types.go @@ -45,6 +45,10 @@ type RunnerSpec struct { CheckInterval int `json:"check_interval,omitempty"` ExecutorConfig KubernetesConfig `json:"executor_config,omitempty"` + + // +kubebuilder:validation:Optional + // Environment contains custom environment variables injected to build environment + Environment []string `json:"environment,omitempty"` } // RunnerStatus defines the observed state of Runner diff --git a/api/v1beta1/zz_generated.deepcopy.go b/api/v1beta1/zz_generated.deepcopy.go index 586a1ef..b86840a 100644 --- a/api/v1beta1/zz_generated.deepcopy.go +++ b/api/v1beta1/zz_generated.deepcopy.go @@ -1092,6 +1092,11 @@ func (in *RunnerSpec) DeepCopyInto(out *RunnerSpec) { *out = *in in.RegistrationConfig.DeepCopyInto(&out.RegistrationConfig) in.ExecutorConfig.DeepCopyInto(&out.ExecutorConfig) + if in.Environment != nil { + in, out := &in.Environment, &out.Environment + *out = make([]string, len(*in)) + copy(*out, *in) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RunnerSpec. diff --git a/config/crd/bases/gitlab.k8s.alekc.dev_runners.yaml b/config/crd/bases/gitlab.k8s.alekc.dev_runners.yaml index 03c08fc..d31ddfa 100644 --- a/config/crd/bases/gitlab.k8s.alekc.dev_runners.yaml +++ b/config/crd/bases/gitlab.k8s.alekc.dev_runners.yaml @@ -42,6 +42,12 @@ spec: concurrent: minimum: 1 type: integer + environment: + description: Environment contains custom environment variables injected + to build environment + items: + type: string + type: array executor_config: properties: affinity: diff --git a/controllers/runner_controller_test.go b/controllers/runner_controller_test.go index 5d32203..7adc480 100644 --- a/controllers/runner_controller_test.go +++ b/controllers/runner_controller_test.go @@ -134,6 +134,7 @@ var _ = Describe("Runner controller", func() { // tc.CheckRunner(createdRunner) }, + table.Entry("Should support setting of env var for build env", caseEnvironmentIsSpecified), table.Entry("Should have created a different registration on tag update", caseTagsChanged), table.Entry("Should have created a different registration on registration token update", caseRegistrationTokenChanged), table.Entry("Should have updated runner status with auth token", caseTestAuthToken), @@ -211,6 +212,17 @@ func caseCheckDeployment(tc *testCase) { } } +func caseEnvironmentIsSpecified(tc *testCase) { + ctx := context.Background() + tc.Runner.Spec.Environment = []string{"foo=bar"} + tc.CheckRunner = func(runner *v1beta1.Runner) { + var deployment appsv1.Deployment + Eventually(func() bool { + return k8sClient.Get(ctx, nameSpacedDependencyName(runner), &deployment) == nil + }, timeout, interval).Should(BeTrue()) + } +} + // caseTagsChanged deals with situation when we change tags for an existing runner func caseTagsChanged(tc *testCase) { ctx := context.Background() diff --git a/internal/generate/config.go b/internal/generate/config.go index bf82cfa..1f7ef97 100644 --- a/internal/generate/config.go +++ b/internal/generate/config.go @@ -21,8 +21,9 @@ func ConfigText(runnerObject *v1beta1.Runner) (gitlabConfig, configHashKey strin URL: runnerObject.Spec.GitlabInstanceURL, }, RunnerSettings: config.RunnerSettings{ - Executor: "kubernetes", - Kubernetes: &runnerObject.Spec.ExecutorConfig, + Environment: runnerObject.Spec.Environment, + Executor: "kubernetes", + Kubernetes: &runnerObject.Spec.ExecutorConfig, }, } // set the namespace to the same one as the runner object if not declared otherwise