Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions api/v1beta1/runner_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
Expand All @@ -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"`
Expand Down
59 changes: 42 additions & 17 deletions internal/generate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions internal/generate/config_test.go
Original file line number Diff line number Diff line change
@@ -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)
}