Skip to content

Commit 8fd7707

Browse files
committed
Facilitate and document local test
- Add some Makefile variables for more granular construction of the build image URI to facilitate simpler overriding for personal builds. - Document the process to build, tag, push, and deploy a personal build to a personal cluster.
1 parent a324838 commit 8fd7707

File tree

2 files changed

+135
-2
lines changed

2 files changed

+135
-2
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ GIT_HASH := $(shell git rev-parse --short=7 HEAD)
55
IMAGETAG ?= ${GIT_HASH}
66

77
BASE_IMG ?= managed-cluster-validating-webhooks
8-
IMG ?= quay.io/app-sre/${BASE_IMG}
8+
IMG_REGISTRY ?= quay.io
9+
IMG_ORG ?= app-sre
10+
IMG ?= $(IMG_REGISTRY)/$(IMG_ORG)/${BASE_IMG}
911

1012
# nb: registry.svc.ci.openshift.org/openshift/release:golang-1.14 doesn't work for this
1113
SYNCSET_GENERATOR_IMAGE := quay.io/app-sre/golang:1.14
@@ -105,6 +107,7 @@ skopeo-push:
105107
.PHONY: push-base
106108
push-base: build/Dockerfile
107109
$(CONTAINER_ENGINE) push $(IMG):$(IMAGETAG)
110+
$(CONTAINER_ENGINE) push $(IMG):latest
108111

109112
coverage: coverage.txt
110113
coverage.txt: vet $(GO_SOURCES)

README.md

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22

33
A framework supporting validating webhooks for OpenShift.
44

5+
- [Managed Cluster Validating Webhooks](#managed-cluster-validating-webhooks)
6+
- [Updating SelectorSyncSet Template](#updating-selectorsyncset-template)
7+
- [Development](#development)
8+
- [Adding New Webhooks](#adding-new-webhooks)
9+
- [Helper Utils](#helper-utils)
10+
- [Is The Request Valid and Authorized](#is-the-request-valid-and-authorized)
11+
- [Building a Response](#building-a-response)
12+
- [Sending Responses](#sending-responses)
13+
- [Writing Unit Tests](#writing-unit-tests)
14+
- [Local Live Testing](#local-live-testing)
15+
- [Create a Repository](#create-a-repository)
16+
- [Build and Push the Image](#build-and-push-the-image)
17+
- [Pare Down Your Daemonset (Optional)](#pare-down-your-daemonset-optional)
18+
- [Deploy the Image](#deploy-the-image)
19+
- [Test Your Changes](#test-your-changes)
20+
- [End to End Testing](#end-to-end-testing)
21+
- [Disabling Webhooks](#disabling-webhooks)
22+
- [Removing a Webhook](#removing-a-webhook)
23+
524
## Updating SelectorSyncSet Template
625

726
Ensure the git branch is current and run `make syncset`. The updated Template will be [build/selectorsyncset.yaml](build/selectorsyncset.yaml) by default.
@@ -104,7 +123,7 @@ It is important to retain the UID from the incoming request with the outgoing re
104123

105124
Once a [response is built](#building-a-response), it must be sent back to the HTTP client. This is done by returning the `admissionctl.Response` in the `Authorized` method. This structure can be [built up with the helpers mentioned above](#building-a-response).
106125

107-
### Writing Tests
126+
### Writing Unit Tests
108127

109128
Unit tests are important to ensure that webhooks behave as expected, and to help with that process, there is a [testutils](pkg/testutils/testutils.go) helper package designed to unify several processes. The package exports:
110129

@@ -117,6 +136,117 @@ The first function, `CanCanNot`, is very simple and designed to make test failur
117136

118137
The three helper functions are intended to provide for more integration style tests than true unit tests, as they assist in turning a specific set of test criteria a JSON representation and sending via `net/http/httptest` to the webhook's `Authorized`. When using `testutils.SendHTTPRequest`, the response is a `Response` object that can be used in the test suite to access the result of the webhook.
119138

139+
### Local Live Testing
140+
141+
Build and test your changes against your own cluster.
142+
Here is a [recorded demo](https://drive.google.com/file/d/1UaMz-siFDRaSKPVKxjLOneqGgbBuwpBF/view) of this process.
143+
144+
#### Create a Repository
145+
146+
Make sure you have a repository to host the image you are going to build.
147+
It must be **public** so your cluster is able to download the image.
148+
For subsequent steps, you will need the name of the registry, the organization, and the repository.
149+
For example, in the image URI `quay.io/my-user/managed-cluster-validating-webhooks:latest`:
150+
- `quay.io` is the *registry*
151+
- `my-user` is the *organization*
152+
- `managed-cluster-validating-webhooks` is the *repository*
153+
- `latest` is the *image tag*
154+
155+
#### Build and Push the Image
156+
157+
Use `make build-base` to build and tag the image; and `make push-base` to push it.
158+
In order to use your personal repository, you can override any of the components of this URI by setting the following variables:
159+
- `IMG_REGISTRY` overrides the *registry* (default: `quay.io`)
160+
- `IMG_ORG` overrides the *organization* (default: `app-sre`)
161+
- `BASE_IMG` overrides the *repository name* (default: `managed-cluster-validating-webhooks`)
162+
- `IMAGETAG` overrides the *image tag*. (By default this is the current commit hash of your local clone of the git repository; but `make build-base` will also always tag `latest`)
163+
164+
For example, to build, tag, and push `quay.io/my-user/managed-cluster-validating-webhooks:latest`, you can run:
165+
166+
```
167+
make IMG_ORG=my-user build-base push-base
168+
```
169+
170+
#### Pare Down Your Daemonset (Optional)
171+
172+
**Note:** Editing the daemonset requires elevated privileges.
173+
174+
By default, the image will run on all master nodes.
175+
For testing purposes, you may find it easier to run on only one node.
176+
To facilitate this:
177+
1. Pick a master node to use. Any one will do. For example:
178+
```
179+
$ oc get node | grep master | head -1
180+
ip-10-0-147-186.ec2.internal Ready master 3h44m v1.19.0+f173eb4
181+
```
182+
2. Give the node a unique label. For example:
183+
```
184+
$ oc label node ip-10-0-147-186.ec2.internal mcvw-test=true
185+
node/ip-10-0-147-186.ec2.internal labeled
186+
```
187+
3. Edit the `validation-webhook` daemonset's `.spec.template.spec.affinity.nodeAffinity`, replacing the `matchExpressions` entry for `node-role.kubernetes.io/master` to match your label instead.
188+
For example, to match the label from step 2, run:
189+
```
190+
oc edit -n openshift-validation-webhook daemonset validation-webhook
191+
```
192+
and replace
193+
```
194+
nodeAffinity:
195+
requiredDuringSchedulingIgnoredDuringExecution:
196+
nodeSelectorTerms:
197+
- matchExpressions:
198+
- key: node-role.kubernetes.io/master
199+
operator: In
200+
values:
201+
- ""
202+
```
203+
with
204+
```
205+
nodeAffinity:
206+
requiredDuringSchedulingIgnoredDuringExecution:
207+
nodeSelectorTerms:
208+
- matchExpressions:
209+
- key: mcvw-test # <== your label's key
210+
operator: In
211+
values:
212+
- "true" # <== your label's value
213+
```
214+
215+
Once these changes are saved, you should see `validation-webhook-*` pods cycle.
216+
When they have settled, you can confirm that only one pod is running and that it is running on your desired node.
217+
For example:
218+
219+
```
220+
$ oc describe pod -n openshift-validation-webhook | grep ^Node:
221+
Node: ip-10-0-147-186.ec2.internal/10.0.147.186
222+
```
223+
224+
#### Deploy the Image
225+
226+
**Note:** Editing the daemonset requires elevated privileges.
227+
228+
Edit the `validation-webhook` daemonset's `.spec.template.spec.containers[0].image`, replacing it with the URI of the image you built and pushed [above](#build-and-push-the-image).
229+
For example, if you created `quay.io/my-user/managed-cluster-validating-webhooks:latest`, replace
230+
```
231+
image: quay.io/app-sre/managed-cluster-validating-webhooks:a324838
232+
```
233+
with
234+
```
235+
image: quay.io/my-user/managed-cluster-validating-webhooks:latest
236+
```
237+
238+
Once these changes are saved, you should see `validation-webhook-*` pods cycle.
239+
When they have settled, you can confirm that the pod(s) are running with your image.
240+
For example:
241+
```
242+
$ oc describe pod -n openshift-validation-webhook | grep '^ *Image:'
243+
Image: quay.io/my-user/managed-cluster-validating-webhooks:latest
244+
```
245+
246+
#### Test Your Changes
247+
248+
Now that your cluster is running your modified code, you can do whatever is necessary to validate your changes.
249+
120250
### End to End Testing
121251

122252
End to End testing is managed by the [osde2e repo](https://github.com/openshift/osde2e/)

0 commit comments

Comments
 (0)