Skip to content

Commit ba2c539

Browse files
authored
Merge pull request #3088 from sedefsavas/capd-webhook
✨Add CAPD webhooks
2 parents fa96673 + f610abd commit ba2c539

19 files changed

+215
-31
lines changed

docs/book/src/developer/guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ and
133133

134134
```
135135
$EDITOR config/manager/manager_image_patch.yaml
136-
$EDITOR test/infrastructure/docker/config/default/manager_image_patch.yaml
136+
$EDITOR test/infrastructure/docker/config/manager/manager_image_patch.yaml
137137
```
138138

139139
In both cases, change the `- image:` url to the digest URL mentioned above:

test/infrastructure/docker/Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ generate-manifests: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc.
122122
paths=./controllers/... \
123123
crd:crdVersions=v1 \
124124
rbac:roleName=manager-role \
125-
output:crd:dir=./config/crd/bases
125+
output:crd:dir=./config/crd/bases \
126+
output:webhook:dir=./config/webhook \
127+
webhook
126128

127129
.PHONY: modules
128130
modules: ## Runs go mod to ensure modules are up to date.
@@ -172,12 +174,12 @@ docker-push-manifest: ## Push the fat manifest docker image.
172174
.PHONY: set-manifest-image
173175
set-manifest-image:
174176
$(info Updating kustomize image patch file for manager resource)
175-
sed -i'' -e 's@image: .*@image: '"${MANIFEST_IMG}:$(MANIFEST_TAG)"'@' ./config/default/manager_image_patch.yaml
177+
sed -i'' -e 's@image: .*@image: '"${MANIFEST_IMG}:$(MANIFEST_TAG)"'@' ./config/manager/manager_image_patch.yaml
176178

177179
.PHONY: set-manifest-pull-policy
178180
set-manifest-pull-policy:
179181
$(info Updating kustomize pull policy file for manager resource)
180-
sed -i'' -e 's@imagePullPolicy: .*@imagePullPolicy: '"$(PULL_POLICY)"'@' ./config/default/manager_pull_policy.yaml
182+
sed -i'' -e 's@imagePullPolicy: .*@imagePullPolicy: '"$(PULL_POLICY)"'@' ./config/manager/manager_pull_policy.yaml
181183

182184
## --------------------------------------
183185
## Release
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2020 The Kubernetes 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 v1alpha3
18+
19+
import (
20+
"errors"
21+
"reflect"
22+
23+
runtime "k8s.io/apimachinery/pkg/runtime"
24+
ctrl "sigs.k8s.io/controller-runtime"
25+
"sigs.k8s.io/controller-runtime/pkg/webhook"
26+
)
27+
28+
func (m *DockerMachineTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
29+
return ctrl.NewWebhookManagedBy(mgr).
30+
For(m).
31+
Complete()
32+
}
33+
34+
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha3-dockermachinetemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=dockermachinetemplates,versions=v1alpha3,name=validation.dockermachinetemplate.infrastructure.cluster.x-k8s.io,sideEffects=None
35+
36+
var _ webhook.Validator = &DockerMachineTemplate{}
37+
38+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
39+
func (m *DockerMachineTemplate) ValidateCreate() error {
40+
return nil
41+
}
42+
43+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
44+
func (m *DockerMachineTemplate) ValidateUpdate(old runtime.Object) error {
45+
oldCRS := old.(*DockerMachineTemplate)
46+
if !reflect.DeepEqual(m.Spec, oldCRS.Spec) {
47+
return errors.New("DockerMachineTemplateSpec is immutable")
48+
}
49+
return nil
50+
}
51+
52+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
53+
func (m *DockerMachineTemplate) ValidateDelete() error {
54+
return nil
55+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright 2020 The Kubernetes 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 v1alpha3
18+
19+
import (
20+
"testing"
21+
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
)
24+
25+
func TestDockerMachineTemplateInvalid(t *testing.T) {
26+
oldTemplate := DockerMachineTemplate{
27+
ObjectMeta: metav1.ObjectMeta{},
28+
Spec: DockerMachineTemplateSpec{
29+
Template: DockerMachineTemplateResource{},
30+
},
31+
}
32+
33+
newTemplate := oldTemplate.DeepCopy()
34+
newTemplate.Spec.Template.Spec.ExtraMounts = append(newTemplate.Spec.Template.Spec.ExtraMounts, []Mount{{ContainerPath: "/var/run/docker.sock", HostPath: "/var/run/docker.sock"}}...)
35+
36+
tests := []struct {
37+
name string
38+
newTemplate *DockerMachineTemplate
39+
oldTemplate *DockerMachineTemplate
40+
wantError bool
41+
}{
42+
{
43+
name: "return no error if no modification",
44+
newTemplate: newTemplate,
45+
oldTemplate: newTemplate,
46+
wantError: false,
47+
},
48+
{
49+
name: "don't allow modification",
50+
newTemplate: newTemplate,
51+
oldTemplate: &oldTemplate,
52+
wantError: true,
53+
},
54+
}
55+
for _, tt := range tests {
56+
t.Run(tt.name, func(t *testing.T) {
57+
err := tt.newTemplate.ValidateUpdate(tt.oldTemplate)
58+
if (err != nil) != tt.wantError {
59+
t.Errorf("unexpected result - wanted %+v, got %+v", tt.wantError, err)
60+
}
61+
})
62+
}
63+
}

test/infrastructure/docker/api/v1alpha3/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.

test/infrastructure/docker/config/certmanager/certificate.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
# The following manifests contain a self-signed issuer CR and a certificate CR.
22
# More document can be found at https://docs.cert-manager.io
3-
apiVersion: cert-manager.io/v1alpha1
3+
apiVersion: cert-manager.io/v1alpha2
44
kind: Issuer
55
metadata:
66
name: selfsigned-issuer
77
namespace: system
88
spec:
99
selfSigned: {}
1010
---
11-
apiVersion: cert-manager.io/v1alpha1
11+
apiVersion: cert-manager.io/v1alpha2
1212
kind: Certificate
1313
metadata:
1414
name: serving-cert # this name should match the one appeared in kustomizeconfig.yaml
1515
namespace: system
1616
spec:
1717
# $(SERVICE_NAME) and $(SERVICE_NAMESPACE) will be substituted by kustomize
18-
commonName: $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc
1918
dnsNames:
20-
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc.cluster.local
19+
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc
20+
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc.cluster.local
2121
issuerRef:
2222
kind: Issuer
2323
name: selfsigned-issuer

test/infrastructure/docker/config/default/kustomization.yaml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ apiVersion: kustomize.config.k8s.io/v1beta1
22
kind: Kustomization
33
namespace: capd-system
44

5-
patchesStrategicMerge:
6-
- manager_image_patch.yaml
7-
- manager_auth_proxy_patch.yaml
8-
95
resources:
10-
- namespace.yaml
11-
- ../rbac
12-
- ../manager
6+
- namespace.yaml
7+
8+
bases:
9+
- ../rbac

test/infrastructure/docker/config/kustomization.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ commonLabels:
66
resources:
77
- crd
88
- default
9+
- webhook

test/infrastructure/docker/config/manager/kustomization.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ apiVersion: kustomize.config.k8s.io/v1beta1
22
kind: Kustomization
33
resources:
44
- manager.yaml
5+
6+
patchesStrategicMerge:
7+
- manager_image_patch.yaml
8+
- manager_auth_proxy_patch.yaml

0 commit comments

Comments
 (0)