Skip to content

Commit 390f2a6

Browse files
authored
Merge pull request #50 from summerwind/runner-validation-webhook
Add validation webhooks
2 parents 1555651 + 55323c3 commit 390f2a6

File tree

10 files changed

+443
-47
lines changed

10 files changed

+443
-47
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@ This controller operates self-hosted runners for GitHub Actions on your Kubernet
66

77
[GitHub Actions](https://github.com/features/actions) is a very useful tool for automating development. GitHub Actions jobs are run in the cloud by default, but you may want to run your jobs in your environment. [Self-hosted runner](https://github.com/actions/runner) can be used for such use cases, but requires the provisioning and configuration of a virtual machine instance. Instead if you already have a Kubernetes cluster, it makes more sense to run the self-hosted runner on top of it.
88

9-
*actions-runner-controller* makes that possible. Just create a *Runner* resource on your Kubernetes, and it will run and operate the self-hosted runner for the specified repository. Combined with Kubernetes RBAC, you can also build simple Self-hosted runners as a Service.
9+
**actions-runner-controller** makes that possible. Just create a *Runner* resource on your Kubernetes, and it will run and operate the self-hosted runner for the specified repository. Combined with Kubernetes RBAC, you can also build simple Self-hosted runners as a Service.
1010

1111
## Installation
1212

13-
First, install *actions-runner-controller* with a manifest file. This will create *actions-runner-system* namespace in your Kubernetes and deploy the required resources.
13+
actions-runner-controller uses [cert-manager](https://cert-manager.io/docs/installation/kubernetes/) for certificate management of Admission Webhook. Make sure you have already installed cert-manager before you install. The installation instructions for cert-manager can be found below.
14+
15+
- [Installing cert-manager on Kubernetes](https://cert-manager.io/docs/installation/kubernetes/)
16+
17+
Install the custom resource and actions-runner-controller itself. This will create actions-runner-system namespace in your Kubernetes and deploy the required resources.
1418

1519
```
1620
$ kubectl apply -f https://github.com/summerwind/actions-runner-controller/releases/latest/download/actions-runner-controller.yaml
1721
```
1822

1923
## Setting up authentication with GitHub API
2024

21-
There are two ways for _actions-runner-controller_ to authenticate with the the GitHub API:
25+
There are two ways for actions-runner-controller to authenticate with the the GitHub API:
2226

2327
1. Using GitHub App.
2428
2. Using Personal Access Token.

api/v1alpha1/runner_types.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20+
"errors"
21+
2022
corev1 "k8s.io/api/core/v1"
2123
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2224
)
@@ -75,6 +77,19 @@ type RunnerSpec struct {
7577
TerminationGracePeriodSeconds *int64 `json:"terminationGracePeriodSeconds,omitempty"`
7678
}
7779

80+
// ValidateRepository validates repository field.
81+
func (rs *RunnerSpec) ValidateRepository() error {
82+
// Organization and repository are both exclusive.
83+
if len(rs.Organization) == 0 && len(rs.Repository) == 0 {
84+
return errors.New("Spec needs organization or repository")
85+
}
86+
if len(rs.Organization) > 0 && len(rs.Repository) > 0 {
87+
return errors.New("Spec cannot have both organization and repository")
88+
}
89+
90+
return nil
91+
}
92+
7893
// RunnerStatus defines the observed state of Runner
7994
type RunnerStatus struct {
8095
Registration RunnerStatusRegistration `json:"registration"`

api/v1alpha1/runner_webhook.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright 2020 The actions-runner-controller authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
apierrors "k8s.io/apimachinery/pkg/api/errors"
21+
"k8s.io/apimachinery/pkg/runtime"
22+
"k8s.io/apimachinery/pkg/util/validation/field"
23+
ctrl "sigs.k8s.io/controller-runtime"
24+
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
25+
"sigs.k8s.io/controller-runtime/pkg/webhook"
26+
)
27+
28+
// log is for logging in this package.
29+
var runnerLog = logf.Log.WithName("runner-resource")
30+
31+
func (r *Runner) SetupWebhookWithManager(mgr ctrl.Manager) error {
32+
return ctrl.NewWebhookManagedBy(mgr).
33+
For(r).
34+
Complete()
35+
}
36+
37+
// +kubebuilder:webhook:path=/mutate-actions-summerwind-dev-v1alpha1-runner,verbs=create;update,mutating=true,failurePolicy=fail,groups=actions.summerwind.dev,resources=runners,versions=v1alpha1,name=mutate.runner.actions.summerwind.dev
38+
39+
var _ webhook.Defaulter = &Runner{}
40+
41+
// Default implements webhook.Defaulter so a webhook will be registered for the type
42+
func (r *Runner) Default() {
43+
// Nothing to do.
44+
}
45+
46+
// +kubebuilder:webhook:path=/validate-actions-summerwind-dev-v1alpha1-runner,verbs=create;update,mutating=false,failurePolicy=fail,groups=actions.summerwind.dev,resources=runners,versions=v1alpha1,name=validate.runner.actions.summerwind.dev
47+
48+
var _ webhook.Validator = &Runner{}
49+
50+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
51+
func (r *Runner) ValidateCreate() error {
52+
runnerLog.Info("validate resource to be created", "name", r.Name)
53+
return r.Validate()
54+
}
55+
56+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
57+
func (r *Runner) ValidateUpdate(old runtime.Object) error {
58+
runnerLog.Info("validate resource to be updated", "name", r.Name)
59+
return r.Validate()
60+
}
61+
62+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
63+
func (r *Runner) ValidateDelete() error {
64+
return nil
65+
}
66+
67+
// Validate validates resource spec.
68+
func (r *Runner) Validate() error {
69+
var (
70+
errList field.ErrorList
71+
err error
72+
)
73+
74+
err = r.Spec.ValidateRepository()
75+
if err != nil {
76+
errList = append(errList, field.Invalid(field.NewPath("spec", "repository"), r.Spec.Repository, err.Error()))
77+
}
78+
79+
if len(errList) > 0 {
80+
return apierrors.NewInvalid(r.GroupVersionKind().GroupKind(), r.Name, errList)
81+
}
82+
83+
return nil
84+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright 2020 The actions-runner-controller authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
apierrors "k8s.io/apimachinery/pkg/api/errors"
21+
"k8s.io/apimachinery/pkg/runtime"
22+
"k8s.io/apimachinery/pkg/util/validation/field"
23+
ctrl "sigs.k8s.io/controller-runtime"
24+
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
25+
"sigs.k8s.io/controller-runtime/pkg/webhook"
26+
)
27+
28+
// log is for logging in this package.
29+
var runenrDeploymentLog = logf.Log.WithName("runnerdeployment-resource")
30+
31+
func (r *RunnerDeployment) SetupWebhookWithManager(mgr ctrl.Manager) error {
32+
return ctrl.NewWebhookManagedBy(mgr).
33+
For(r).
34+
Complete()
35+
}
36+
37+
// +kubebuilder:webhook:path=/mutate-actions-summerwind-dev-v1alpha1-runnerdeployment,verbs=create;update,mutating=true,failurePolicy=fail,groups=actions.summerwind.dev,resources=runnerdeployments,versions=v1alpha1,name=mutate.runnerdeployment.actions.summerwind.dev
38+
39+
var _ webhook.Defaulter = &RunnerDeployment{}
40+
41+
// Default implements webhook.Defaulter so a webhook will be registered for the type
42+
func (r *RunnerDeployment) Default() {
43+
// Nothing to do.
44+
}
45+
46+
// +kubebuilder:webhook:path=/validate-actions-summerwind-dev-v1alpha1-runnerdeployment,verbs=create;update,mutating=false,failurePolicy=fail,groups=actions.summerwind.dev,resources=runnerdeployments,versions=v1alpha1,name=validate.runnerdeployment.actions.summerwind.dev
47+
48+
var _ webhook.Validator = &RunnerDeployment{}
49+
50+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
51+
func (r *RunnerDeployment) ValidateCreate() error {
52+
runenrDeploymentLog.Info("validate resource to be created", "name", r.Name)
53+
return r.Validate()
54+
}
55+
56+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
57+
func (r *RunnerDeployment) ValidateUpdate(old runtime.Object) error {
58+
runenrDeploymentLog.Info("validate resource to be updated", "name", r.Name)
59+
return r.Validate()
60+
}
61+
62+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
63+
func (r *RunnerDeployment) ValidateDelete() error {
64+
return nil
65+
}
66+
67+
// Validate validates resource spec.
68+
func (r *RunnerDeployment) Validate() error {
69+
var (
70+
errList field.ErrorList
71+
err error
72+
)
73+
74+
err = r.Spec.Template.Spec.ValidateRepository()
75+
if err != nil {
76+
errList = append(errList, field.Invalid(field.NewPath("spec", "template", "spec", "repository"), r.Spec.Template.Spec.Repository, err.Error()))
77+
}
78+
79+
if len(errList) > 0 {
80+
return apierrors.NewInvalid(r.GroupVersionKind().GroupKind(), r.Name, errList)
81+
}
82+
83+
return nil
84+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright 2020 The actions-runner-controller authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
apierrors "k8s.io/apimachinery/pkg/api/errors"
21+
"k8s.io/apimachinery/pkg/runtime"
22+
"k8s.io/apimachinery/pkg/util/validation/field"
23+
ctrl "sigs.k8s.io/controller-runtime"
24+
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
25+
"sigs.k8s.io/controller-runtime/pkg/webhook"
26+
)
27+
28+
// log is for logging in this package.
29+
var runnerReplicaSetLog = logf.Log.WithName("runnerreplicaset-resource")
30+
31+
func (r *RunnerReplicaSet) SetupWebhookWithManager(mgr ctrl.Manager) error {
32+
return ctrl.NewWebhookManagedBy(mgr).
33+
For(r).
34+
Complete()
35+
}
36+
37+
// +kubebuilder:webhook:path=/mutate-actions-summerwind-dev-v1alpha1-runnerreplicaset,verbs=create;update,mutating=true,failurePolicy=fail,groups=actions.summerwind.dev,resources=runnerreplicasets,versions=v1alpha1,name=mutate.runnerreplicaset.actions.summerwind.dev
38+
39+
var _ webhook.Defaulter = &RunnerReplicaSet{}
40+
41+
// Default implements webhook.Defaulter so a webhook will be registered for the type
42+
func (r *RunnerReplicaSet) Default() {
43+
// Nothing to do.
44+
}
45+
46+
// +kubebuilder:webhook:path=/validate-actions-summerwind-dev-v1alpha1-runnerreplicaset,verbs=create;update,mutating=false,failurePolicy=fail,groups=actions.summerwind.dev,resources=runnerreplicasets,versions=v1alpha1,name=validate.runnerreplicaset.actions.summerwind.dev
47+
48+
var _ webhook.Validator = &RunnerReplicaSet{}
49+
50+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
51+
func (r *RunnerReplicaSet) ValidateCreate() error {
52+
runnerReplicaSetLog.Info("validate resource to be created", "name", r.Name)
53+
return r.Validate()
54+
}
55+
56+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
57+
func (r *RunnerReplicaSet) ValidateUpdate(old runtime.Object) error {
58+
runnerReplicaSetLog.Info("validate resource to be updated", "name", r.Name)
59+
return r.Validate()
60+
}
61+
62+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
63+
func (r *RunnerReplicaSet) ValidateDelete() error {
64+
return nil
65+
}
66+
67+
// Validate validates resource spec.
68+
func (r *RunnerReplicaSet) Validate() error {
69+
var (
70+
errList field.ErrorList
71+
err error
72+
)
73+
74+
err = r.Spec.Template.Spec.ValidateRepository()
75+
if err != nil {
76+
errList = append(errList, field.Invalid(field.NewPath("spec", "template", "spec", "repository"), r.Spec.Template.Spec.Repository, err.Error()))
77+
}
78+
79+
if len(errList) > 0 {
80+
return apierrors.NewInvalid(r.GroupVersionKind().GroupKind(), r.Name, errList)
81+
}
82+
83+
return nil
84+
}

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/default/kustomization.yaml

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ bases:
1717
- ../rbac
1818
- ../manager
1919
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in crd/kustomization.yaml
20-
#- ../webhook
20+
- ../webhook
2121
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
22-
#- ../certmanager
22+
- ../certmanager
2323
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
2424
#- ../prometheus
2525

@@ -36,39 +36,39 @@ patchesStrategicMerge:
3636
#- manager_prometheus_metrics_patch.yaml
3737

3838
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in crd/kustomization.yaml
39-
#- manager_webhook_patch.yaml
39+
- manager_webhook_patch.yaml
4040

4141
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
4242
# Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.
4343
# 'CERTMANAGER' needs to be enabled to use ca injection
44-
#- webhookcainjection_patch.yaml
44+
- webhookcainjection_patch.yaml
4545

4646
# the following config is for teaching kustomize how to do var substitution
4747
vars:
4848
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix.
49-
#- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR
50-
# objref:
51-
# kind: Certificate
52-
# group: cert-manager.io
53-
# version: v1alpha2
54-
# name: serving-cert # this name should match the one in certificate.yaml
55-
# fieldref:
56-
# fieldpath: metadata.namespace
57-
#- name: CERTIFICATE_NAME
58-
# objref:
59-
# kind: Certificate
60-
# group: cert-manager.io
61-
# version: v1alpha2
62-
# name: serving-cert # this name should match the one in certificate.yaml
63-
#- name: SERVICE_NAMESPACE # namespace of the service
64-
# objref:
65-
# kind: Service
66-
# version: v1
67-
# name: webhook-service
68-
# fieldref:
69-
# fieldpath: metadata.namespace
70-
#- name: SERVICE_NAME
71-
# objref:
72-
# kind: Service
73-
# version: v1
74-
# name: webhook-service
49+
- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR
50+
objref:
51+
kind: Certificate
52+
group: cert-manager.io
53+
version: v1alpha2
54+
name: serving-cert # this name should match the one in certificate.yaml
55+
fieldref:
56+
fieldpath: metadata.namespace
57+
- name: CERTIFICATE_NAME
58+
objref:
59+
kind: Certificate
60+
group: cert-manager.io
61+
version: v1alpha2
62+
name: serving-cert # this name should match the one in certificate.yaml
63+
- name: SERVICE_NAMESPACE # namespace of the service
64+
objref:
65+
kind: Service
66+
version: v1
67+
name: webhook-service
68+
fieldref:
69+
fieldpath: metadata.namespace
70+
- name: SERVICE_NAME
71+
objref:
72+
kind: Service
73+
version: v1
74+
name: webhook-service

0 commit comments

Comments
 (0)