Skip to content

Commit 47d1be0

Browse files
authored
🌱 Make output-for-watch.sh work, if called from a different directory. (#1710)
1 parent 060ab54 commit 47d1be0

File tree

7 files changed

+158
-22
lines changed

7 files changed

+158
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,4 @@ watchall-output*
8484
/conditions*.txt
8585

8686
/.config
87+
/.kube

docs/caph/04-developers/01-development-guide.md

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,32 @@ This ensures the following:
2323
- helmfile
2424
- kind (required)
2525
- kubectl (required)
26-
- tilt (required)
27-
- hcloud
26+
- tilt
27+
- hcloud cli-tool.
2828

2929
## Preparing Hetzner project
3030

31-
For more information, please see [here](/docs/caph/01-getting-started/03-preparation.md).
31+
For more information, please see the [Hetzner project preparation](/docs/caph/01-getting-started/03-preparation.md) guide.
3232

33-
## Setting Tilt up
33+
## Tilt vs pushing development container
3434

35-
You need to create a `.envrc` file and specify the values you need. After the `.envrc` is loaded, invoke `direnv allow` to load the environment variables in your current shell session.
35+
You can use [Tilt](https://tilt.dev/) or a script like
36+
[update-operator-dev-deployment.sh](/hack/update-operator-dev-deployment.sh) to install your changed
37+
code in the management cluster.
3638

37-
The complete reference can be found [here](/docs/caph/04-developers/02-tilt.md).
39+
We do not update the Tilt configuration regularly. The script may be an easier solution.
40+
41+
We recommend creating a `.envrc` file and specifying the values you need. After the `.envrc` is loaded
42+
([direnv.net](https://direnv.net/)), invoke `direnv allow` to load the environment variables in your
43+
current shell session.
44+
45+
The complete reference can be found in the [Reference of Tilt](/docs/caph/04-developers/02-tilt.md) documentation.
3846

3947
## Developing with Tilt
4048

4149
![tilt](https://syself.com/images/tilt.png)
4250

43-
Provider Integration development requires a lot of iteration, and the “build, tag, push, update deployment” workflow can be very tedious. Tilt makes this process much simpler by watching for updates and automatically building and deploying them. To build a kind cluster and to start Tilt, run:
51+
To build a kind cluster and to start Tilt, run:
4452

4553
```shell
4654
make tilt-up
@@ -68,6 +76,25 @@ To delete the registry, use `make delete-registry`. Use `make delete-mgt-cluster
6876

6977
If you have any trouble finding the right command, you can run the `make help` command to get a list of all available make targets.
7078

79+
## Troubleshooting
80+
81+
If you want to have a better overview about what is going on in your management cluster, then you can use the
82+
following tools.
83+
84+
```console
85+
watch ./hack/output-for-watch.sh
86+
```
87+
88+
This script continuously shows the most important resources (capi machines, infra machines, ...)
89+
and logs of caph and capi. Run this with your management cluster kubeconfig active.
90+
91+
```console
92+
go run github.com/guettli/check-conditions@latest all
93+
```
94+
95+
[check-conditions](https://github.com/guettli/check-conditions) shows all unhealthy conditions of
96+
the current cluster. You can use it in both the management and workload clusters.
97+
7198
## Submitting PRs and testing
7299

73100
Pull requests and issues are highly encouraged! For more information, please have a look at the [Contribution Guidelines](https://github.com/syself/cluster-api-provider-hetzner/blob/main/CONTRIBUTING.md)

docs/caph/04-developers/02-tilt.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ sidebar: Reference of Tilt
55
description: Full list of available Tilt configuration values and their description.
66
---
77

8-
```
8+
We do not update the Tilt configuration regularly. The script
9+
([update-operator-dev-deployment.sh](/hack/update-operator-dev-deployment.sh)) may be an easier
10+
solution.
11+
12+
```json
913
"allowed_contexts": [
1014
"kind-caph",
1115
],

hack/get-leading-pod.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2023 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Bash Strict Mode: https://github.com/guettli/bash-strict-mode
18+
trap 'echo -e "\n🤷 🚨 🔥 Warning: A command has failed. Exiting the script. Line was ($0:$LINENO): $(sed -n "${LINENO}p" "$0" 2>/dev/null || true) 🔥 🚨 🤷 "; exit 3' ERR
19+
set -Eeuo pipefail
20+
21+
if [[ $# -eq 0 ]] || [[ $# -gt 2 ]] || [[ "$1" == -* ]]; then
22+
echo "Usage: $0 <deployment-name> [<namespace>]" >&2
23+
exit 1
24+
fi
25+
26+
dep="$1"
27+
28+
ns="${2:-}"
29+
30+
hack_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
31+
32+
if [[ -z $ns ]]; then
33+
ns=$("$hack_dir/get-namespace-of-deployment.sh" "$dep")
34+
fi
35+
36+
leases=$(kubectl get leases -n "$ns" -o yaml |
37+
yq ".items[] | .spec.holderIdentity" | { grep -P "^${dep}-[^-]+-[^-]+_" || true; })
38+
39+
if [[ -z $leases ]]; then
40+
echo "Error: failed to find a lease for deployment $dep in namespace $ns"
41+
exit 1
42+
fi
43+
44+
if [ "$(echo "$leases" | wc -l)" -gt 1 ]; then
45+
echo "Error: Multiple leases found for deployment '$dep'" >&2
46+
exit 1
47+
fi
48+
49+
echo "$leases" | cut -d_ -f1
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2023 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Bash Strict Mode: https://github.com/guettli/bash-strict-mode
18+
trap 'echo -e "\n🤷 🚨 🔥 Warning: A command has failed. Exiting the script. Line was ($0:$LINENO): $(sed -n "${LINENO}p" "$0" 2>/dev/null || true) 🔥 🚨 🤷 "; exit 3' ERR
19+
set -Eeuo pipefail
20+
21+
if [[ $# -ne 1 ]] || [[ "$1" == -* ]]; then
22+
echo "Usage: $0 <deployment-name>" >&2
23+
exit 1
24+
fi
25+
26+
dep="$1"
27+
28+
# Find the namespace (must be exactly one)
29+
ns_candidates="$(kubectl get deploy -A -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.metadata.namespace}{"\n"}{end}' |
30+
awk -v d="$dep" '$1==d{print $2}')"
31+
32+
ns_count="$(printf '%s\n' "$ns_candidates" | sed '/^$/d' | wc -l | tr -d ' ')"
33+
if [ "$ns_count" -eq 0 ]; then
34+
echo "ERROR: Deployment '$dep' not found in any namespace." >&2
35+
exit 1
36+
elif [ "$ns_count" -gt 1 ]; then
37+
echo "ERROR: Deployment '$dep' found in multiple namespaces:" >&2
38+
printf '%s\n' "$ns_candidates" >&2
39+
exit 1
40+
fi
41+
printf '%s\n' "$ns_candidates" | head -n1

hack/output-for-watch.sh

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
#############################################################
18+
# This script creates an overview of the management cluster.
19+
# You can call it once, or continuously like this:
20+
# watch ./hack/output-for-watch.sh
21+
#
22+
# You can call it from a different directory, too:
23+
# ../cluster-api-provider-hetzner/hack/output-for-watch.sh
24+
#############################################################
25+
26+
hack_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
27+
1728
function print_heading() {
1829
blue='\033[0;34m'
1930
nc='\033[0m' # No Color
@@ -47,16 +58,18 @@ kubectl get events -A --sort-by=lastTimestamp | grep -vP 'LeaderElection' | tail
4758

4859
print_heading caph:
4960

50-
./hack/tail-controller-logs.sh
61+
"$hack_dir"/tail-controller-logs.sh
5162

5263
regex='^I\d\d\d\d|\
5364
.*it may have already been deleted|\
5465
.*WARNING: ignoring DaemonSet-managed Pods|\
5566
.*failed to retrieve Spec.ProviderID|\
5667
.*failed to patch Machine default
5768
'
58-
capi_ns=$(kubectl get deployments -A | grep capi-con | cut -d' ' -f1)
59-
capi_logs=$(kubectl logs -n "$capi_ns" deployments/capi-controller-manager --since 10m | grep -vP "$(echo "$regex" | tr -d '\n')" | tail -5)
69+
capi_ns=$("$hack_dir"/get-namespace-of-deployment.sh capi-controller-manager)
70+
capi_pod=$("$hack_dir"/get-leading-pod.sh capi-controller-manager "$capi_ns")
71+
72+
capi_logs=$(kubectl logs -n "$capi_ns" "$capi_pod" --since 10m | grep -vP "$(echo "$regex" | tr -d '\n')" | tail -5)
6073
if [ -n "$capi_logs" ]; then
6174
print_heading capi
6275
echo "$capi_logs"
@@ -89,7 +102,7 @@ fi
89102

90103
echo
91104

92-
./hack/get-kubeconfig-of-workload-cluster.sh
105+
"$hack_dir"/get-kubeconfig-of-workload-cluster.sh
93106

94107
kubeconfig_wl=".workload-cluster-kubeconfig.yaml"
95108

@@ -120,9 +133,9 @@ print_heading "workload-cluster nodes"
120133
KUBECONFIG=$kubeconfig_wl kubectl get nodes -o 'custom-columns=NAME:.metadata.name,STATUS:.status.phase,ROLES:.metadata.labels.kubernetes\.io/role,creationTimestamp:.metadata.creationTimestamp,VERSION:.status.nodeInfo.kubeletVersion,IP:.status.addresses[?(@.type=="ExternalIP")].address'
121134

122135
if [ "$(kubectl get machine | wc -l)" -ne "$(KUBECONFIG="$kubeconfig_wl" kubectl get nodes | wc -l)" ]; then
123-
echo "❌ Number of nodes in wl-cluster does not match number of machines in mgt-cluster"
136+
echo "❌ Number of nodes in workload cluster does not match number of machines in management cluster"
124137
else
125-
echo "👌 number of nodes in wl-cluster is equal to number of machines in mgt-cluster"
138+
echo "👌 number of nodes in workload cluster is equal to number of machines in management cluster"
126139
fi
127140

128141
rows=$(kubectl get hcloudremediation -A 2>/dev/null)

hack/tail-controller-logs.sh

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
# Copyright 2023 The Kubernetes Authors.
44
#
@@ -14,14 +14,15 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
ns=$(kubectl get deployments.apps -A | grep caph-controller-manager | cut -d' ' -f1)
18-
pod=$(kubectl -n "$ns" get pods | grep caph-controller-manager | cut -d' ' -f1)
17+
# Bash Strict Mode: https://github.com/guettli/bash-strict-mode
18+
trap 'echo -e "\n🤷 🚨 🔥 Warning: A command has failed. Exiting the script. Line was ($0:$LINENO): $(sed -n "${LINENO}p" "$0" 2>/dev/null || true) 🔥 🚨 🤷 "; exit 3' ERR
19+
set -Eeuo pipefail
1920

20-
if [ -z "$pod" ]; then
21-
echo "failed to find caph-controller-manager pod"
22-
exit 1
23-
fi
21+
dep="caph-controller-manager"
2422

23+
hack_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
24+
ns=$("$hack_dir"/get-namespace-of-deployment.sh $dep)
25+
pod=$("$hack_dir"/get-leading-pod.sh $dep "$ns")
2526
kubectl -n "$ns" logs "$pod" --tail 200 |
26-
./hack/filter-caph-controller-manager-logs.py - |
27+
"$hack_dir"/filter-caph-controller-manager-logs.py - |
2728
tail -n 10

0 commit comments

Comments
 (0)