From dd48f79eb6bc23528c1f6633adec0fb7c4367db6 Mon Sep 17 00:00:00 2001 From: Guilhem Bonnefille Date: Thu, 6 Oct 2022 11:26:40 +0200 Subject: [PATCH] Initial support for multiple runners Add a `runners:` array. --- api/v1beta1/runner_types.go | 32 +++++++++++++++-- internal/generate/config.go | 59 +++++++++++++++++++++++--------- internal/generate/config_test.go | 53 ++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 19 deletions(-) create mode 100644 internal/generate/config_test.go diff --git a/api/v1beta1/runner_types.go b/api/v1beta1/runner_types.go index 6792376..cca2144 100644 --- a/api/v1beta1/runner_types.go +++ b/api/v1beta1/runner_types.go @@ -29,7 +29,7 @@ import ( // RunnerSpec defines the desired state of Runner type RunnerSpec struct { - RegistrationConfig RegisterNewRunnerOptions `json:"registration_config"` + RegistrationConfig RegisterNewRunnerOptions `json:"registration_config,omitempty"` // +kubebuilder:validation:Optional GitlabInstanceURL string `json:"gitlab_instance_url,omitempty"` @@ -49,6 +49,31 @@ type RunnerSpec struct { // +kubebuilder:validation:Optional // Environment contains custom environment variables injected to build environment Environment []string `json:"environment,omitempty"` + + Runners []RunnerRunner `json:"runners,omitempty"` +} + +type RunnerRunner struct { + RegistrationConfig RegisterNewRunnerOptions `json:"registration_config"` + + // +kubebuilder:validation:Optional + GitlabInstanceURL string `json:"gitlab_instance_url,omitempty"` + ExecutorConfig KubernetesConfig `json:"executor_config,omitempty"` + + // +kubebuilder:validation:Optional + // Environment contains custom environment variables injected to build environment + Environment []string `json:"environment,omitempty"` +} + +type RunnerStatusRunner struct { + // LastRegistrationToken is the last token used for a successful authentication + LastRegistrationToken string `json:"last_registration_token"` + + // LastRegistrationTags are last tags used in successful registration + LastRegistrationTags []string `json:"last_registration_tags"` + + // AuthenticationToken obtained from the gitlab which can be used in runner configuration for authentication + AuthenticationToken string `json:"authentication_token"` } // RunnerStatus defines the observed state of Runner @@ -63,7 +88,10 @@ type RunnerStatus struct { // AuthenticationToken obtained from the gitlab which can be used in runner configuration for authentication AuthenticationToken string `json:"authentication_token"` - ConfigMapVersion string `json:"config_map_version"` + + Runners []RunnerStatusRunner `json:"runners"` + + ConfigMapVersion string `json:"config_map_version"` // Ready indicates that all runner operation has been completed and final object is ready to serve Ready bool `json:"ready"` diff --git a/internal/generate/config.go b/internal/generate/config.go index 1f7ef97..d392fb1 100644 --- a/internal/generate/config.go +++ b/internal/generate/config.go @@ -13,29 +13,54 @@ import ( // ConfigText initialize default config object and returns it as a text func ConfigText(runnerObject *v1beta1.Runner) (gitlabConfig, configHashKey string, err error) { // define sensible config for some configuration values - runnerConfig := &config.RunnerConfig{ - Name: runnerObject.Name, - Limit: 10, - RunnerCredentials: config.RunnerCredentials{ - Token: runnerObject.Status.AuthenticationToken, - URL: runnerObject.Spec.GitlabInstanceURL, - }, - RunnerSettings: config.RunnerSettings{ - 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 - if runnerConfig.RunnerSettings.Kubernetes.Namespace == "" { - runnerConfig.RunnerSettings.Kubernetes.Namespace = runnerObject.Namespace + var runnersConfig []*config.RunnerConfig + if len(runnerObject.Spec.Runners) > 0 { + for _, runner := range runnerObject.Spec.Runners { + runnerConfig := &config.RunnerConfig{ + Name: runnerObject.Name, + Limit: 10, + RunnerCredentials: config.RunnerCredentials{ + Token: runnerObject.Status.AuthenticationToken, // FIXME + URL: runner.GitlabInstanceURL, + }, + RunnerSettings: config.RunnerSettings{ + Environment: runner.Environment, + Executor: "kubernetes", + Kubernetes: &runner.ExecutorConfig, + }, + } + // set the namespace to the same one as the runner object if not declared otherwise + if runnerConfig.RunnerSettings.Kubernetes.Namespace == "" { + runnerConfig.RunnerSettings.Kubernetes.Namespace = runnerObject.Namespace + } + runnersConfig = append(runnersConfig, runnerConfig) + } + } else { + runnerConfig := &config.RunnerConfig{ + Name: runnerObject.Name, + Limit: 10, + RunnerCredentials: config.RunnerCredentials{ + Token: runnerObject.Status.AuthenticationToken, + URL: runnerObject.Spec.GitlabInstanceURL, + }, + RunnerSettings: config.RunnerSettings{ + 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 + if runnerConfig.RunnerSettings.Kubernetes.Namespace == "" { + runnerConfig.RunnerSettings.Kubernetes.Namespace = runnerObject.Namespace + } + runnersConfig = append(runnersConfig, runnerConfig) } rootConfig := &config.Config{ ListenAddress: ":9090", Concurrent: int(math.Max(1, float64(runnerObject.Spec.Concurrent))), CheckInterval: int(math.Max(3, float64(runnerObject.Spec.CheckInterval))), LogLevel: runnerObject.Spec.LogLevel, - Runners: []*config.RunnerConfig{runnerConfig}, + Runners: runnersConfig, } // if not explicit, define the log level diff --git a/internal/generate/config_test.go b/internal/generate/config_test.go new file mode 100644 index 0000000..e90185a --- /dev/null +++ b/internal/generate/config_test.go @@ -0,0 +1,53 @@ +package generate + +import ( + "log" + "testing" + + "gitlab.k8s.alekc.dev/api/v1beta1" + "k8s.io/utils/pointer" +) + +func TestConfigText(t *testing.T) { + runner := &v1beta1.Runner{ + Spec: v1beta1.RunnerSpec{ + RegistrationConfig: v1beta1.RegisterNewRunnerOptions{ + Token: pointer.StringPtr("1"), + }, + GitlabInstanceURL: "https://gitlab.com", + }, + } + text, hash, err := ConfigText(runner) + if err != nil { + t.Error(err) + } + log.Println("Text", text) + log.Println("Hash", hash) +} + +func TestConfigTexts(t *testing.T) { + runner := &v1beta1.Runner{ + Spec: v1beta1.RunnerSpec{ + Runners: []v1beta1.RunnerRunner{ + { + RegistrationConfig: v1beta1.RegisterNewRunnerOptions{ + Token: pointer.StringPtr("1"), + }, + GitlabInstanceURL: "https://gitlab.com", + }, + { + RegistrationConfig: v1beta1.RegisterNewRunnerOptions{ + Token: pointer.StringPtr("2"), + }, + GitlabInstanceURL: "https://gitlab.corp", + }, + }, + }, + } + text, hash, err := ConfigText(runner) + if err != nil { + t.Error(err) + } + log.Println("Text", text) + log.Println("Hash", hash) +}