Commit a4c8dc8b authored by Edmund Ochieng's avatar Edmund Ochieng

allow operator to configure global runner options

parent be2f8444
......@@ -29,10 +29,19 @@ type RunnerSpec struct {
//Name of secret containing the runner-registration-token key used to register the runner
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Registration Token",xDescriptors="urn:alm:descriptor:com.tectonic.ui:selector:core:v1:Secret"
RegistrationToken string `json:"token,omitempty"`
// List of comma separated tags to be applied to the runner
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Tags",xDescriptors="urn:alm:descriptor:com.tectonic.ui:text"
Tags string `json:"tags,omitempty"`
// Concurrent limits the number of jobs globally that can run concurrently
// +operator-sdk:csv:customresourcedefinitions:type=status,displayName="Concurrent",xDescriptors="urn:alm:descriptor:com.tectonic.ui:text"
Concurrent *int32 `json:"concurrent,omitempty"`
// CheckInterval defines the number of seconds between checks for new jobs
// +operator-sdk:csv:customresourcedefinitions:type=status,displayName="Check Interval",xDescriptors="urn:alm:descriptor:com.tectonic.ui:text"
CheckInterval *int32 `json:"interval,omitempty"`
// Cache defines an S3 compatible object store
Cache *RunnerCacheSpec `json:"cache,omitempty"`
}
......
......@@ -454,6 +454,16 @@ func (in *RunnerList) DeepCopyObject() runtime.Object {
func (in *RunnerSpec) DeepCopyInto(out *RunnerSpec) {
*out = *in
out.Gitlab = in.Gitlab
if in.Concurrent != nil {
in, out := &in.Concurrent, &out.Concurrent
*out = new(int32)
**out = **in
}
if in.CheckInterval != nil {
in, out := &in.CheckInterval, &out.CheckInterval
*out = new(int32)
**out = **in
}
if in.Cache != nil {
in, out := &in.Cache, &out.Cache
*out = new(RunnerCacheSpec)
......
......@@ -52,6 +52,10 @@ spec:
description: S3 cache server URL
type: string
type: object
concurrent:
description: Concurrent limits the number of jobs globally that can run concurrently
format: int32
type: integer
gitlab:
description: GitlabResource represents a Gitlab custom resource. Should only be used to reference Gitlab instance created by the operator
properties:
......@@ -62,6 +66,10 @@ spec:
description: URL of GitLab instance
type: string
type: object
interval:
description: CheckInterval defines the number of seconds between checks for new jobs
format: int32
type: integer
tags:
description: List of comma separated tags to be applied to the runner
type: string
......
......@@ -384,6 +384,14 @@ spec:
- patch
- update
- watch
- apiGroups:
- apps.gitlab.com
resources:
- runners/finalizers
verbs:
- delete
- patch
- update
- apiGroups:
- apps.gitlab.com
resources:
......
......@@ -58,6 +58,11 @@ spec:
description: S3 cache server URL
type: string
type: object
concurrent:
description: Concurrent limits the number of jobs globally that can
run concurrently
format: int32
type: integer
gitlab:
description: GitlabResource represents a Gitlab custom resource. Should
only be used to reference Gitlab instance created by the operator
......@@ -69,6 +74,11 @@ spec:
description: URL of GitLab instance
type: string
type: object
interval:
description: CheckInterval defines the number of seconds between checks
for new jobs
format: int32
type: integer
tags:
description: List of comma separated tags to be applied to the runner
type: string
......
package runner
import (
"bytes"
"fmt"
"text/template"
gitlabv1beta1 "gitlab.com/gitlab-org/gl-openshift/gitlab-operator/api/v1beta1"
gitlabutils "gitlab.com/gitlab-org/gl-openshift/gitlab-operator/controllers/utils"
corev1 "k8s.io/api/core/v1"
)
// Config struct holds the values used to
// configure Runner Global options
type Config struct {
Concurrent int32
CheckInterval int32
}
func userOptions(cr *gitlabv1beta1.Runner) Config {
options := Config{Concurrent: 10, CheckInterval: 30}
if cr.Spec.Concurrent != nil {
options.Concurrent = *cr.Spec.Concurrent
}
if cr.Spec.CheckInterval != nil {
options.CheckInterval = *cr.Spec.CheckInterval
}
fmt.Printf("%+v", options)
return options
}
// GetConfigMap returns the runner configmap object
func GetConfigMap(cr *gitlabv1beta1.Runner) *corev1.ConfigMap {
labels := gitlabutils.Label(cr.Name, "runner", gitlabutils.RunnerType)
var gitlabURL string
configToml := gitlabutils.ReadConfig("/templates/gitlab-runner/config.toml")
var configToml bytes.Buffer
configTemplate := template.Must(template.ParseFiles("/templates/gitlab-runner/config.toml"))
configTemplate.Execute(&configToml, userOptions(cr))
entrypointScript := gitlabutils.ReadConfig("/templates/gitlab-runner/entrypoint.sh")
configureScript := gitlabutils.ReadConfig("/templates/gitlab-runner/configure.sh")
registrationScript := gitlabutils.ReadConfig("/templates/gitlab-runner/registration.sh")
......@@ -36,7 +65,7 @@ func GetConfigMap(cr *gitlabv1beta1.Runner) *corev1.ConfigMap {
runnerConfigMap := gitlabutils.GenericConfigMap(labels["app.kubernetes.io/instance"]+"-config", cr.Namespace, labels)
runnerConfigMap.Data = map[string]string{
"ci_server_url": gitlabURL,
"config.toml": configToml,
"config.toml": configToml.String(),
"entrypoint": entrypointScript,
"check-live": aliveScript,
"register-runner": registrationScript,
......
......@@ -26,6 +26,7 @@ import (
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
monitoringv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
gitlabv1beta1 "gitlab.com/gitlab-org/gl-openshift/gitlab-operator/api/v1beta1"
......@@ -109,13 +110,17 @@ func (r *RunnerReconciler) reconcileSecrets(ctx context.Context, cr *gitlabv1bet
err = r.Get(ctx, types.NamespacedName{Name: tokens.Name, Namespace: cr.Namespace}, found)
if err != nil {
if errors.IsNotFound(err) {
if err := controllerutil.SetControllerReference(cr, tokens, r.Scheme); err != nil {
return err
}
return r.Create(ctx, tokens)
}
return err
}
if reflect.DeepEqual(tokens.Data, found.Data) {
if !reflect.DeepEqual(tokens.Data, found.Data) {
found.Data = tokens.Data
return r.Update(ctx, found)
}
......@@ -130,13 +135,17 @@ func (r *RunnerReconciler) reconcileConfigMaps(ctx context.Context, cr *gitlabv1
err := r.Get(ctx, types.NamespacedName{Name: configs.Name, Namespace: cr.Namespace}, found)
if err != nil {
if errors.IsNotFound(err) {
if err := controllerutil.SetControllerReference(cr, configs, r.Scheme); err != nil {
return err
}
return r.Create(ctx, configs)
}
return err
}
if reflect.DeepEqual(configs.Data, found.Data) {
if !reflect.DeepEqual(configs.Data, found.Data) {
found.Data = configs.Data
return r.Update(ctx, found)
}
......@@ -151,13 +160,17 @@ func (r *RunnerReconciler) reconcileDeployments(ctx context.Context, cr *gitlabv
err := r.Get(ctx, types.NamespacedName{Name: runner.Name, Namespace: cr.Namespace}, found)
if err != nil {
if errors.IsNotFound(err) {
if err := controllerutil.SetControllerReference(cr, runner, r.Scheme); err != nil {
return err
}
return r.Create(ctx, runner)
}
return err
}
if reflect.DeepEqual(runner.Spec, found.Spec) {
if !reflect.DeepEqual(runner.Spec, found.Spec) {
found.Spec = runner.Spec
return r.Update(ctx, found)
}
......@@ -216,15 +229,20 @@ func (r *RunnerReconciler) reconcileMetrics(ctx context.Context, cr *gitlabv1bet
err := r.Get(ctx, types.NamespacedName{Name: svc.Name, Namespace: cr.Namespace}, found)
if err != nil {
if errors.IsNotFound(err) {
if err := controllerutil.SetControllerReference(cr, svc, r.Scheme); err != nil {
return err
}
return r.Create(ctx, svc)
}
return err
}
if reflect.DeepEqual(svc.Spec, found.Spec) {
found.Spec = svc.Spec
return r.Update(ctx, found)
if !reflect.DeepEqual(svc.Spec, found.Spec) {
// besides ClusterIP, not much is expected to change
// return r.Update(ctx, found)
return nil
}
return nil
......@@ -239,13 +257,17 @@ func (r *RunnerReconciler) reconcileServiceMonitor(ctx context.Context, cr *gitl
err := r.Get(ctx, types.NamespacedName{Name: sm.Name, Namespace: cr.Namespace}, found)
if err != nil {
if errors.IsNotFound(err) {
if err := controllerutil.SetControllerReference(cr, sm, r.Scheme); err != nil {
return err
}
return r.Create(ctx, sm)
}
return err
}
if reflect.DeepEqual(sm.Spec, found.Spec) {
if !reflect.DeepEqual(sm.Spec, found.Spec) {
found.Spec = sm.Spec
return r.Update(ctx, found)
}
......@@ -253,3 +275,8 @@ func (r *RunnerReconciler) reconcileServiceMonitor(ctx context.Context, cr *gitl
return nil
}
func (r *RunnerReconciler) validateRegistrationSecret(ctx context.Context, cr *gitlabv1beta1.Runner) error {
return nil
}
concurrent = 10
check_interval = 30
concurrent = {{ .Concurrent }}
check_interval = {{ .CheckInterval }}
log_level = "info"
listen_address = '[::]:9252'
\ No newline at end of file
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