Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 #22

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

V2 #22

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
50 changes: 37 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
.SHELLFLAGS += -e

# Image URL to use all building/pushing image targets
IMG ?= controller:dev

REPOSITORY_ROOT := $(shell git rev-parse --show-toplevel)
BIN_DIR := $(REPOSITORY_ROOT)/bin

CONTROLLER_GEN_VERSION ?= v0.13.0

# Kubernetes env test
ENVTEST_ARCH?=arm64
ENVTEST_KUBERNETES_VERSION?=1.27

# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
CRD_OPTIONS ?= "crd:crdVersions=v1"

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand All @@ -20,6 +30,10 @@ SHELL = /usr/bin/env bash -o pipefail

all: build

tidy: ## Tidy Go modules.
rm -f go.sum; go mod tidy -compat=1.21


##@ General

# The help target prints out all targets with their descriptions organized
Expand Down Expand Up @@ -86,24 +100,34 @@ undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/confi
$(KUSTOMIZE) build config/default | kubectl delete -f -


CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
CONTROLLER_GEN=$(BIN_DIR)/controller-gen
controller-gen: ## Download controller-gen locally if necessary.
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.4.1)
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION))

KUSTOMIZE = $(shell pwd)/bin/kustomize
KUSTOMIZE = $(BIN_DIR)/kustomize
kustomize: ## Download kustomize locally if necessary.
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])
$(call go-install-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])

# go-get-tool will 'go get' any package $2 and install it to $1.
# go-install-tool will 'go install' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-get-tool

define go-install-tool
@[ -f $(1) ] || { \
echo "Installing $(2) in $(1)";\
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
go mod init tmp ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\
rm -rf $$TMP_DIR ;\
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
}
endef

#define go-install-tool
#@[ -f $(1) ] || { \
#echo "Installing $(2) in $(1)";\
#set -e ;\
#TMP_DIR=$$(mktemp -d) ;\
#cd $$TMP_DIR ;\
# go mod init tmp ;\
#echo "Downloading $(2)" ;\
#GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
#rm -rf $$TMP_DIR ;\
#}
#endef
9 changes: 9 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ resources:
defaulting: true
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: k8s.alekc.dev
group: gitlab
kind: MultiRunner
path: gitlab.k8s.alekc.dev/api/v1beta1
version: v1beta1
version: "3"
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ helm install gitlab-runner-operator alekc/gitlab-runner-operator
## Configuration
The CRD Runner is composed by following fields (all of them are optional, except for registration tokens, see examples below):
```yaml
apiVersion: gitlab.k8s.alekc.dev/v1beta1
apiVersion: gitlab.k8s.alekc.dev/v1beta2
kind: Runner
metadata:
name: runner-sample
Expand All @@ -53,7 +53,7 @@ In order to run your runner you will need to obtain a registration token ([see t

In this config you only need to specify the name of your runner (`runner-sample`), registration token (`xxx`), and it's tag (`test-gitlab-runner`)
```yaml
apiVersion: gitlab.k8s.alekc.dev/v1beta1
apiVersion: gitlab.k8s.alekc.dev/v1beta2
kind: Runner
metadata:
name: runner-sample
Expand All @@ -77,7 +77,7 @@ metadata:
name: gitlab-runner-token
type: Opaque
---
apiVersion: gitlab.k8s.alekc.dev/v1beta1
apiVersion: gitlab.k8s.alekc.dev/v1beta2
kind: Runner
metadata:
name: runner-sample
Expand Down Expand Up @@ -117,7 +117,7 @@ data:
bar.txt: |
zzz
---
apiVersion: gitlab.k8s.alekc.dev/v1beta1
apiVersion: gitlab.k8s.alekc.dev/v1beta2
kind: Runner
metadata:
name: runner-sample
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package v1beta1 contains API Schema definitions for the gitlab v1beta1 API group
//+kubebuilder:object:generate=true
//+groupName=gitlab.k8s.alekc.dev
package v1beta1
// Package v1beta2 contains API Schema definitions for the gitlab v1beta2 API group
// +kubebuilder:object:generate=true
// +groupName=gitlab.k8s.alekc.dev
package v1beta2

import (
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -26,7 +26,7 @@ import (

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "gitlab.k8s.alekc.dev", Version: "v1beta1"}
GroupVersion = schema.GroupVersion{Group: "gitlab.k8s.alekc.dev", Version: "v1beta2"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
Expand Down
57 changes: 57 additions & 0 deletions api/v1beta2/multirunner_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2021.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta2

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// MultiRunnerSpec defines the desired state of MultiRunner
type MultiRunnerSpec struct {
Foo string `json:"foo,omitempty"`
}

// MultiRunnerStatus defines the observed state of MultiRunner
type MultiRunnerStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// MultiRunner is the Schema for the multirunners API
type MultiRunner struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec MultiRunnerSpec `json:"spec,omitempty"`
Status MultiRunnerStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// MultiRunnerList contains a list of MultiRunner
type MultiRunnerList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []MultiRunner `json:"items"`
}

func init() {
SchemeBuilder.Register(&MultiRunner{}, &MultiRunnerList{})
}
34 changes: 7 additions & 27 deletions api/v1beta1/runner_types.go → api/v1beta2/runner_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1
package v1beta2

import (
"fmt"
Expand All @@ -24,12 +24,13 @@ import (
"k8s.io/utils/pointer"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// RunnerSpec defines the desired state of Runner
type RunnerSpec struct {
RegistrationConfig RegisterNewRunnerOptions `json:"registration_config"`
// +kubebuilder:validation:Optional
Token *string `url:"token" json:"token,omitempty"`

// +kubebuilder:validation:Optional
TokenSecret string `json:"token_secret,omitempty"`

// +kubebuilder:validation:Optional
GitlabInstanceURL string `json:"gitlab_instance_url,omitempty"`
Expand Down Expand Up @@ -59,7 +60,7 @@ type RunnerStatus struct {
LastRegistrationToken string `json:"last_registration_token"`

// LastRegistrationTags are last tags used in successful registration
LastRegistrationTags []string `json:"last_registration_tags"`
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"`
Expand Down Expand Up @@ -369,27 +370,6 @@ type Service struct {
Entrypoint []string `toml:"entrypoint" json:"entrypoint" long:"entrypoint" description:"Command or script that should be executed as the container’s entrypoint. syntax is similar to https://docs.docker.com/engine/reference/builder/#entrypoint"`
}

// RegisterNewRunnerOptions represents the available RegisterNewRunner()
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/runners.html#register-a-new-runner
type RegisterNewRunnerOptions struct {
Token *string `url:"token" json:"token,omitempty"`
TokenSecret string `json:"token_secret,omitempty"`
Description *string `url:"description,omitempty" json:"description,omitempty"`
Info *RegisterNewRunnerInfoOptions `url:"info,omitempty" json:"info,omitempty"`
// Active is deprecated. use paused instead
Active *bool `url:"active,omitempty" json:"active,omitempty"`
Paused *bool `url:"paused,omitempty" json:"paused,omitempty"`
Locked *bool `url:"locked,omitempty" json:"locked,omitempty"`
RunUntagged *bool `url:"run_untagged,omitempty" json:"run_untagged,omitempty"`
TagList []string `url:"tag_list[],omitempty" json:"tag_list,omitempty"`
AccessLevel *string `url:"accessLevel,omitempty" json:"accessLevel,omitempty"`
MaximumTimeout *int `url:"maximum_timeout,omitempty" json:"maximum_timeout,omitempty"`
MaintenanceNote *string `url:"maintenance_note,omitempty" json:"maintenance_note,omitempty"`
}

// RegisterNewRunnerInfoOptions represents the info hashmap parameter in
// RegisterNewRunnerOptions.
//
Expand Down
19 changes: 10 additions & 9 deletions api/v1beta1/runner_webhook.go → api/v1beta2/runner_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1
package v1beta2

import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// log is for logging in this package.
Expand All @@ -34,7 +35,7 @@ func (r *Runner) SetupWebhookWithManager(mgr ctrl.Manager) error {

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!

// +kubebuilder:webhook:path=/mutate-gitlab-k8s-alekc-dev-v1beta1-runner,mutating=true,failurePolicy=fail,sideEffects=None,groups=gitlab.k8s.alekc.dev,resources=runners,verbs=create;update,versions=v1beta1,name=mrunner.kb.io,admissionReviewVersions={v1,v1beta1}
// +kubebuilder:webhook:path=/mutate-gitlab-k8s-alekc-dev-v1beta2-runner,mutating=true,failurePolicy=fail,sideEffects=None,groups=gitlab.k8s.alekc.dev,resources=runners,verbs=create;update,versions=v1beta2,name=mrunner.kb.io,admissionReviewVersions={v1,v1beta2}

var _ webhook.Defaulter = &Runner{}

Expand All @@ -46,31 +47,31 @@ func (r *Runner) Default() {
}

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// +kubebuilder:webhook:path=/validate-gitlab-k8s-alekc-dev-v1beta1-runner,mutating=false,failurePolicy=fail,sideEffects=None,groups=gitlab.k8s.alekc.dev,resources=runners,verbs=create;update,versions=v1beta1,name=vrunner.kb.io,admissionReviewVersions={v1,v1beta1}
// +kubebuilder:webhook:path=/validate-gitlab-k8s-alekc-dev-v1beta2-runner,mutating=false,failurePolicy=fail,sideEffects=None,groups=gitlab.k8s.alekc.dev,resources=runners,verbs=create;update,versions=v1beta2,name=vrunner.kb.io,admissionReviewVersions={v1,v1beta2}

var _ webhook.Validator = &Runner{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *Runner) ValidateCreate() error {
func (r *Runner) ValidateCreate() (admission.Warnings, error) {
// runnerlog.Info("validate create", "name", r.Name)

return nil
return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *Runner) ValidateUpdate(old runtime.Object) error {
func (r *Runner) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
// runnerlog.Info("validate update", "name", r.Name)

// we do not want to permit changing of the name of the runner (or better, we'd rather avoid dealing with cleaning)

// TODO(user): fill in your validation logic upon object update.
return nil
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *Runner) ValidateDelete() error {
func (r *Runner) ValidateDelete() (admission.Warnings, error) {
// runnerlog.Info("validate delete", "name", r.Name)

// TODO(user): fill in your validation logic upon object deletion.
return nil
return nil, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1
package v1beta2

import (
"context"
Expand All @@ -36,7 +36,6 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1beta1
package v1beta2

import (
"time"
Expand Down
Loading
Loading