-
Notifications
You must be signed in to change notification settings - Fork 58
Refactor startup command to wait for node IP changes #598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
713dc5b
Patch node ip when server pod restarts
galal-hussein 8eab73c
wsl
galal-hussein 3dc3c1d
Refactor startup command and adding safe mode
galal-hussein fd4bf2a
Add date/time logging to the startup script
galal-hussein 46e0e31
fixes
galal-hussein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,99 @@ | ||
| package server | ||
|
|
||
| var singleServerTemplate string = ` | ||
| if [ -d "{{.ETCD_DIR}}" ]; then | ||
| # if directory exists then it means its not an initial run | ||
| /bin/k3s server --cluster-reset --config {{.INIT_CONFIG}} {{.EXTRA_ARGS}} 2>&1 | tee /var/log/k3s.log | ||
| fi | ||
| rm -f /var/lib/rancher/k3s/server/db/reset-flag | ||
| /bin/k3s server --config {{.INIT_CONFIG}} {{.EXTRA_ARGS}} 2>&1 | tee /var/log/k3s.log` | ||
|
|
||
| var HAServerTemplate string = ` | ||
| if [ ${POD_NAME: -1} == 0 ] && [ ! -d "{{.ETCD_DIR}}" ]; then | ||
| var StartupCommand string = ` | ||
| info() | ||
| { | ||
| echo "[INFO] [$(date +"%c")]" "$@" | ||
| } | ||
| fatal() | ||
| { | ||
| echo "[FATAL] [$(date +"%c")] " "$@" >&2 | ||
| exit 1 | ||
| } | ||
| # safe mode function to reset node IP after pod restarts | ||
| safe_mode() { | ||
| CURRENT_IP="" | ||
| if [ -f /var/lib/rancher/k3s/k3k-node-ip ]; then | ||
| CURRENT_IP=$(cat /var/lib/rancher/k3s/k3k-node-ip) | ||
| fi | ||
| if [ -z "$CURRENT_IP" ] || [ "$CURRENT_IP" = "$POD_IP" ] || [ {{.K3K_MODE}} != "virtual" ]; then | ||
| return | ||
| fi | ||
| # skipping if the node is starting for the first time | ||
| if [ -d "{{.ETCD_DIR}}" ]; then | ||
| info "Starting K3s in Safe Mode (Network Policy Disabled) to patch Node IP from ${CURRENT_IP} to ${POD_IP}" | ||
| /bin/k3s server --disable-network-policy --config $1 {{.EXTRA_ARGS}} > /dev/null 2>&1 & | ||
| PID=$! | ||
| # Start the loop to wait for the nodeIP to change | ||
| info "Waiting for Node IP to update to ${POD_IP}." | ||
| count=0 | ||
| until kubectl get nodes -o wide 2>/dev/null | grep -q "${POD_IP}"; do | ||
| if ! kill -0 $PID 2>/dev/null; then | ||
| fatal "safe Mode K3s process died unexpectedly!" | ||
| fi | ||
| sleep 2 | ||
| count=$((count+1)) | ||
| if [ $count -gt 60 ]; then | ||
| fatal "timed out waiting for node to change IP from $CURRENT_IP to $POD_IP" | ||
| fi | ||
| done | ||
| info "Node IP is set to ${POD_IP} successfully. Stopping Safe Mode process..." | ||
| kill $PID | ||
| wait $PID 2>/dev/null || true | ||
| fi | ||
| } | ||
| single_server() { | ||
galal-hussein marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| info "Starting single node setup..." | ||
| # checking for existing data in single server if found we must perform reset | ||
| if [ -d "{{.ETCD_DIR}}" ]; then | ||
| info "Existing data found in single node setup. Performing cluster-reset to ensure quorum..." | ||
| if ! /bin/k3s server --cluster-reset --config {{.INIT_CONFIG}} {{.EXTRA_ARGS}} > /dev/null 2>&1; then | ||
| fatal "cluster reset failed!" | ||
| fi | ||
| info "Cluster reset complete. Removing Reset flag file." | ||
| rm -f /var/lib/rancher/k3s/server/db/reset-flag | ||
| fi | ||
| # entering safe mode to ensure correct NodeIP | ||
| safe_mode {{.INIT_CONFIG}} | ||
| info "Adding pod IP file." | ||
| echo $POD_IP > /var/lib/rancher/k3s/k3k-node-ip | ||
| /bin/k3s server --config {{.INIT_CONFIG}} {{.EXTRA_ARGS}} 2>&1 | tee /var/log/k3s.log | ||
| else | ||
| /bin/k3s server --config {{.SERVER_CONFIG}} {{.EXTRA_ARGS}} 2>&1 | tee /var/log/k3s.log | ||
| } | ||
| ha_server() { | ||
galal-hussein marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| info "Starting pod $POD_NAME in HA node setup" | ||
| if [ ${POD_NAME: -1} == 0 ] && [ ! -d "{{.ETCD_DIR}}" ]; then | ||
| info "Adding pod IP file." | ||
| echo $POD_IP > /var/lib/rancher/k3s/k3k-node-ip | ||
| /bin/k3s server --config {{.INIT_CONFIG}} {{.EXTRA_ARGS}} 2>&1 | tee /var/log/k3s.log | ||
| else | ||
| safe_mode {{.SERVER_CONFIG}} | ||
| info "Adding pod IP file." | ||
| echo $POD_IP > /var/lib/rancher/k3s/k3k-node-ip | ||
| /bin/k3s server --config {{.SERVER_CONFIG}} {{.EXTRA_ARGS}} 2>&1 | tee /var/log/k3s.info | ||
| fi | ||
| } | ||
| if [ {{.ORCHESTRATION_MODE}} == "ha" ]; then | ||
| ha_server | ||
| else | ||
| single_server | ||
| fi` | ||
galal-hussein marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.