Skip to content

Commit c1e1c79

Browse files
committed
add documentation on running e2e test locally
- update Makefile targets for local e2e - Add script on peering vnets
1 parent d188352 commit c1e1c79

File tree

8 files changed

+716
-65
lines changed

8 files changed

+716
-65
lines changed

Makefile

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ E2E_CONF_FILE_ENVSUBST := $(ROOT_DIR)/test/e2e/config/azure-dev-envsubst.yaml
186186
SKIP_CLEANUP ?= false
187187
AZWI_SKIP_CLEANUP ?= false
188188
SKIP_LOG_COLLECTION ?= false
189+
MGMT_CLUSTER_TYPE ?= kind
189190
# @sonasingh46: Skip creating mgmt cluster for ci as workload identity needs kind cluster
190191
# to be created with extra mounts for key pairs which is not yet supported
191192
# by existing e2e framework. A mgmt cluster(kind) is created as part of e2e suite
@@ -317,8 +318,12 @@ install-tools: $(ENVSUBST) $(KUSTOMIZE) $(KUBECTL) $(HELM) $(GINKGO) $(KIND) $(A
317318

318319
.PHONY: create-management-cluster
319320
create-management-cluster: $(KUSTOMIZE) $(ENVSUBST) $(KUBECTL) $(KIND) ## Create a management cluster.
320-
# Create kind management cluster.
321-
$(MAKE) kind-create
321+
# Create management cluster based on type
322+
@if [ "$(MGMT_CLUSTER_TYPE)" = "aks" ]; then \
323+
$(MAKE) aks-create; \
324+
else \
325+
$(MAKE) kind-create; \
326+
fi
322327

323328
# Install cert manager and wait for availability
324329
./hack/install-cert-manager.sh
@@ -334,7 +339,9 @@ create-management-cluster: $(KUSTOMIZE) $(ENVSUBST) $(KUBECTL) $(KIND) ## Create
334339
timeout --foreground 300 bash -c "until curl --retry $(CURL_RETRIES) -sSL https://github.com/kubernetes-sigs/cluster-api-addon-provider-helm/releases/download/v0.2.5/addon-components.yaml | $(ENVSUBST) | $(KUBECTL) apply -f -; do sleep 5; done"
335340

336341
# Deploy CAPZ
337-
$(KIND) load docker-image $(CONTROLLER_IMG)-$(ARCH):$(TAG) --name=$(KIND_CLUSTER_NAME)
342+
if [ "$(MGMT_CLUSTER_TYPE)" != "aks" ]; then \
343+
$(KIND) load docker-image $(CONTROLLER_IMG)-$(ARCH):$(TAG) --name=$(KIND_CLUSTER_NAME); \
344+
fi
338345
timeout --foreground 300 bash -c "until $(KUSTOMIZE) build config/default | $(ENVSUBST) | $(KUBECTL) apply -f - --server-side=true; do sleep 5; done"
339346

340347
# Wait for CAPI deployments
@@ -360,7 +367,10 @@ create-management-cluster: $(KUSTOMIZE) $(ENVSUBST) $(KUBECTL) $(KIND) ## Create
360367
timeout --foreground 300 bash -c "until $(KUBECTL) get clusters -A; do sleep 3; done"
361368
timeout --foreground 300 bash -c "until $(KUBECTL) get azureclusters -A; do sleep 3; done"
362369
timeout --foreground 300 bash -c "until $(KUBECTL) get kubeadmcontrolplanes -A; do sleep 3; done"
363-
@echo 'Set kubectl context to the kind management cluster by running "$(KUBECTL) config set-context kind-$(KIND_CLUSTER_NAME)"'
370+
371+
@if [ "$(MGMT_CLUSTER_TYPE)" != "aks" ]; then \
372+
echo 'Set kubectl context to the kind management cluster by running "$(KUBECTL) config set-context kind-$(KIND_CLUSTER_NAME)"'; \
373+
fi
364374

365375
.PHONY: create-workload-cluster
366376
create-workload-cluster: $(ENVSUBST) $(KUBECTL) ## Create a workload cluster.
@@ -726,6 +736,24 @@ test-cover: test ## Run tests with code coverage and generate reports.
726736
kind-create-bootstrap: $(KUBECTL) ## Create capz kind bootstrap cluster.
727737
KIND_CLUSTER_NAME=capz-e2e ./scripts/kind-with-registry.sh
728738

739+
.PHONY: create-bootstrap
740+
create-bootstrap: $(KUBECTL) ## Create bootstrap cluster (AKS or KIND) for CAPZ testing. Default is KIND.
741+
@echo "Creating bootstrap cluster with type: $(MGMT_CLUSTER_TYPE)"
742+
@if [ "$(MGMT_CLUSTER_TYPE)" == "aks" ]; then \
743+
if [ -z "$(AZURE_SUBSCRIPTION_ID)" ]; then \
744+
echo "Error: AZURE_SUBSCRIPTION_ID is required for AKS bootstrap cluster" >&2; \
745+
exit 1; \
746+
fi; \
747+
MGMT_CLUSTER_NAME="$${MGMT_CLUSTER_NAME:-capz-e2e-$(shell date +%s)}" \
748+
AKS_RESOURCE_GROUP="$${AKS_RESOURCE_GROUP:-$$MGMT_CLUSTER_NAME}" \
749+
AKS_MGMT_VNET_NAME="$${AKS_MGMT_VNET_NAME:-$$MGMT_CLUSTER_NAME-vnet}" \
750+
AKS_MGMT_SUBNET_NAME="$${AKS_MGMT_SUBNET_NAME:-$$MGMT_CLUSTER_NAME-subnet}" \
751+
./scripts/aks-as-mgmt.sh || { echo "Failed to create AKS bootstrap cluster" >&2; exit 1; }; \
752+
else \
753+
KIND_CLUSTER_NAME=capz-e2e ./scripts/kind-with-registry.sh || { echo "Failed to create KIND bootstrap cluster" >&2; exit 1; }; \
754+
fi
755+
@echo "Bootstrap cluster created successfully"
756+
729757
.PHONY: cleanup-workload-identity
730758
cleanup-workload-identity: ## Cleanup CI workload-identity infra
731759
@if ! [ "$(AZWI_SKIP_CLEANUP)" == "true" ]; then \

docs/book/src/developers/development.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,8 @@ To run E2E locally, set `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`, `AZURE_SUBSCRI
507507
./scripts/ci-e2e.sh
508508
```
509509

510+
Note: Users that have a restrictive environment and want to leverage API Server ILB in their flavors and want to run e2e tests locally, refer to detailed explanation on achieving so in [running e2e tests locally leveraging apiserver ilb solution](./tilt-with-aks-as-mgmt-ilb.md#running-e2e-tests-locally-using-api-server-ilbs-networking-solution)
511+
510512
You can optionally set the following variables:
511513

512514
| Variable | Description | Default |

docs/book/src/developers/tilt-with-aks-as-mgmt-ilb.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,154 @@ Once the tilt UI is up and running
124124
- Flavors that leverage internal load balancer are:
125125
- `apiserver-ilb`
126126
- `windows-apiserver-ilb`
127+
128+
### Running e2e tests locally using API Server ILB's networking solution
129+
130+
Building upon the [challenges and solutions](#challenges-and-solutions) from above, running an e2e test locally in a restricted environment calls from some workarounds to the prow templates and the e2e test itself.
131+
132+
1. We need the apiserver ilb with private endpoints, opinionated CIDRs to the workload cluster's VNet & Subnets, and pre-kubeadm commands updating the etc/hosts of the worker nodes of the workload cluster.
133+
134+
2. Once the template has been modified to be run in local environment using AKS as management cluster, we need to be able to peer the vnets, create private DNS zone for the FQDN of the workload cluster and re-enable blocked NSG ports.
135+
136+
**Note:**
137+
138+
- Following guidance is only for testing, and not a recommendation for any production environment.
139+
140+
- The below steps for self-managed templates only. Does not apply to AKS workload clusters.
141+
142+
- The below steps are for running the tests in a intuned device. If you are going to run the local tests from a dev machine in Azure, you have to first follow below steps and then proceed ahead.
143+
1. Create a managed Identity
144+
2. Assign that managed identity a contributor role to your subscription
145+
3. Set `AZURE_CLIENT_ID_USER_ASSIGNED_IDENTITY`, `AZURE_OBJECT_ID_USER_ASSIGNED_IDENTITY`, `AZURE_USER_ASSIGNED_IDENTITY_RESOURCE_ID` to the user assigned managed identity.
146+
147+
#### Update prow template with apiserver ILB networking solution
148+
149+
There are three sections of a prow template that needs an update.
150+
151+
1. AzureCluster
152+
- `/spec/networkSpec/apiServerLB`
153+
- Add FrontendIP
154+
- Add an associated private IP to be leveraged by an intern ILB
155+
- `/spec/networkSpec/vnet/cidrBlocks`
156+
- Add VNet CIDR
157+
- `/spec/networkSpec/subnets/0/cidrBlocks`
158+
- Add Subnet CIDR for the control plane
159+
- `/spec/networkSpec/subnets/1/cidrBlocks`
160+
- Add Subnet CIDR for the worker node
161+
2. `KubeadmConfigTemplate` - linux node; Identifiable by `name: .*-md-0`
162+
- `/spec/template/spec/preKubeadmCommands/0`
163+
- Add prekubeadm command updating the `/etc/hosts` of the worker node of the type linux.
164+
3. `KubeadmConfigTemplate` - windows node; Identifiable by `name: .*-md-win`
165+
- `/spec/template/spec/preKubeadmCommands/0`
166+
- Add prekubeadm command updating the `/etc/hosts` of the worker node of the type windows.
167+
168+
A Sample kustomize command updating a prow template via its kustomization.yaml is pasted below.
169+
170+
```yaml
171+
- target:
172+
kind: AzureCluster
173+
patch: |-
174+
- op: add
175+
path: /spec/networkSpec/apiServerLB
176+
value:
177+
frontendIPs:
178+
- name: ${CLUSTER_NAME}-api-lb
179+
publicIP:
180+
dnsName: ${CLUSTER_NAME}-${APISERVER_LB_DNS_SUFFIX}.${AZURE_LOCATION}.cloudapp.azure.com
181+
name: ${CLUSTER_NAME}-api-lb
182+
- name: ${CLUSTER_NAME}-internal-lb-private-ip
183+
privateIP: ${AZURE_INTERNAL_LB_PRIVATE_IP}
184+
- target:
185+
kind: AzureCluster
186+
patch: |-
187+
- op: add
188+
path: /spec/networkSpec/vnet/cidrBlocks
189+
value: []
190+
- op: add
191+
path: /spec/networkSpec/vnet/cidrBlocks/-
192+
value: ${AZURE_VNET_CIDR}
193+
- target:
194+
kind: AzureCluster
195+
patch: |-
196+
- op: add
197+
path: /spec/networkSpec/subnets/0/cidrBlocks
198+
value: []
199+
- op: add
200+
path: /spec/networkSpec/subnets/0/cidrBlocks/-
201+
value: ${AZURE_CP_SUBNET_CIDR}
202+
- target:
203+
kind: AzureCluster
204+
patch: |-
205+
- op: add
206+
path: /spec/networkSpec/subnets/1/cidrBlocks
207+
value: []
208+
- op: add
209+
path: /spec/networkSpec/subnets/1/cidrBlocks/-
210+
value: ${AZURE_NODE_SUBNET_CIDR}
211+
- target:
212+
kind: KubeadmConfigTemplate
213+
name: .*-md-0
214+
patch: |-
215+
- op: add
216+
path: /spec/template/spec/preKubeadmCommands
217+
value: []
218+
- op: add
219+
path: /spec/template/spec/preKubeadmCommands/-
220+
value: echo '${AZURE_INTERNAL_LB_PRIVATE_IP} ${CLUSTER_NAME}-${APISERVER_LB_DNS_SUFFIX}.${AZURE_LOCATION}.cloudapp.azure.com' >> /etc/hosts
221+
- target:
222+
kind: KubeadmConfigTemplate
223+
name: .*-md-win
224+
patch: |-
225+
- op: add
226+
path: /spec/template/spec/preKubeadmCommands/-
227+
value:
228+
powershell -Command "Add-Content -Path 'C:\\Windows\\System32\\drivers\\etc\\hosts' -Value '${AZURE_INTERNAL_LB_PRIVATE_IP} ${CLUSTER_NAME}-${APISERVER_LB_DNS_SUFFIX}.${AZURE_LOCATION}.cloudapp.azure.com'"
229+
```
230+
231+
#### Peer Vnets of the management cluster and the workload cluster
232+
233+
Peering VNets, creation of private DNS zone with the FQDN of the workload cluster and updating of NSGs of management and workload cluster can be achieved by `peer-vnets.sh` present in `scripts/peer-vnets.sh`
234+
235+
This script, `scripts/peer-vnets.sh`, should be run after triggering the test run locally and in a separate terminal.
236+
237+
#### Running the test locally
238+
239+
We recommend running the test individually while debugging the test failure. This implies that `GINKGO_FOCUS` as unique as possible. So for instance if you want to run `periodic-cluster-api-provider-azure-e2e-main`'s "With 3 control-plane nodes and 2 Linux and 2 Windows worker nodes" test,
240+
241+
1. We first need to add the following environment variables to the test itself. For example:
242+
243+
```go
244+
Expect(os.Setenv("EXP_APISERVER_ILB", "true")).To(Succeed())
245+
Expect(os.Setenv("AZURE_INTERNAL_LB_PRIVATE_IP", "10.0.0.101")).To(Succeed())
246+
Expect(os.Setenv("AZURE_VNET_CIDR", "10.0.0.0/8")).To(Succeed())
247+
Expect(os.Setenv("AZURE_CP_SUBNET_CIDR", "10.0.0.0/16")).To(Succeed())
248+
Expect(os.Setenv("AZURE_NODE_SUBNET_CIDR", "10.1.0.0/16")).To(Succeed())
249+
```
250+
251+
The above lines should be added before the `clusterctl.ApplyClusterTemplateAndWait()` is invoked.
252+
253+
254+
2. Open the terminal and run the below command:
255+
256+
```bash
257+
GINKGO_FOCUS="With 3 control-plane nodes and 2 Linux and 2 Windows worker nodes" USE_LOCAL_KIND_REGISTRY=false SKIP_CLEANUP="true" SKIP_LOG_COLLECTION="true" REGISTRY="<>" MGMT_CLUSTER_TYPE="aks" EXP_APISERVER_ILB=true AZURE_LOCATION="<>" ARCH="amd64" scripts/ci-e2e.sh
258+
```
259+
260+
**Note:**
261+
262+
- Set `MGMT_CLUSTER_TYPE` to `"aks"` to leverage `AKS` as the management cluster.
263+
- Set `EXP_APISERVER_ILB` to `true` to enable the API Server ILB feature gate.
264+
- Set `AZURE_CLIENT_ID_USER_ASSIGNED_IDENTITY`, `AZURE_OBJECT_ID_USER_ASSIGNED_IDENTITY` and `AZURE_USER_ASSIGNED_IDENTITY_RESOURCE_ID` to use the user assigned managed identity instead of the AKS created managed identity.
265+
266+
3. In a new terminal, wait for AzureClusters to be created by the above command. Check it using `kubectl get AzureClusters -A`. Note that this command will fail or will not output anything unless the above command, `GINKGO_FOCUS...`, has deployed the worker template and initiated workload cluster creation.
267+
268+
Once the worker cluster has been created, `export` the `CLUSTER_NAME` and `CLUSTER_NAMESPACE`.
269+
It is recommended that `AZURE_INTERNAL_LB_PRIVATE_IP` is set an IP of `10.0.0.x`, say `10.0.0.101`, to avoid any test updates.
270+
271+
Then open a new terminal at the root of the cluster api provider azure repo and run the below command.
272+
273+
```bash
274+
AZURE_INTERNAL_LB_PRIVATE_IP="<IP of your choice>" CLUSTER_NAME="<e2e workload cluster name>" CLUSTER_NAMESPACE="<e2e cluster namespace>" ./scripts/peer-vnets.sh ./tilt-settings.yaml
275+
```
276+
277+
You will see that the test progresses in the first terminal window that invoked `GINKGO_FOCUS=....`

e2e.mk

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
##@ E2E Testing:
77
.PHONY: test-e2e-run
8-
test-e2e-run: generate-e2e-templates install-tools kind-create-bootstrap ## Run e2e tests.
9-
@$(ENVSUBST) < $(E2E_CONF_FILE) > $(E2E_CONF_FILE_ENVSUBST) && \
8+
test-e2e-run: generate-e2e-templates install-tools create-bootstrap ## Run e2e tests.
9+
if [ "$(MGMT_CLUSTER_TYPE)" == "aks" ]; then \
10+
source ./scripts/peer-vnets.sh && source_tilt_settings tilt-settings.yaml; \
11+
fi; \
12+
$(ENVSUBST) < $(E2E_CONF_FILE) > $(E2E_CONF_FILE_ENVSUBST) && \
1013
if [ -z "${AZURE_CLIENT_ID_USER_ASSIGNED_IDENTITY}" ]; then \
1114
export AZURE_CLIENT_ID_USER_ASSIGNED_IDENTITY=$(shell cat $(AZURE_IDENTITY_ID_FILEPATH)); \
1215
fi; \

0 commit comments

Comments
 (0)