-
Notifications
You must be signed in to change notification settings - Fork 461
[fcos] GCP: add a script and a service which ensure GCP routes are set correctly #1442
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 all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| filesystem: "root" | ||
| mode: 0744 | ||
| path: "/usr/local/bin/gcp-routes.sh" | ||
| contents: | ||
| inline: | | ||
| #!/bin/bash | ||
|
|
||
| declare -A routes | ||
| curler() { | ||
| curl --silent -L -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/${1}" | ||
| } | ||
|
|
||
| get_ifname() { | ||
| sysfs_path="/sys/class/net" | ||
| for dev in $(find ${sysfs_path} -maxdepth 1 -mindepth 1); | ||
| do | ||
| local mac=$(<${dev}/address); | ||
| local name="$(basename ${dev})" | ||
| if [ "${mac}" == "${1}" ]; | ||
| then | ||
| echo "${name}" | ||
| return; | ||
| fi | ||
| done | ||
| } | ||
|
|
||
| set_routes() { | ||
| local dev="${1}" | ||
| read -a dev_routes <<< "${routes[$dev]}" | ||
| for cur_route in $(ip route show dev ${dev} table local proto 66 | awk '{print$2}'); | ||
| do | ||
| if [[ ! "${dev_routes[@]}" =~ "${cur_route}" ]]; | ||
| then | ||
| echo "Removing stale forwarded IP ${cur_route}/32" | ||
| ip route del ${cur_route}/32 dev ${dev} table local proto 66 | ||
| fi | ||
| done | ||
| for route in ${dev_routes[@]} | ||
| do | ||
| ip route replace to local ${route} dev $dev proto 66 | ||
| done | ||
| unset dev_routes | ||
| } | ||
|
|
||
| del_routes() { | ||
| local dev="${1}" | ||
| read -a dev_routes <<< "${routes[$dev]}" | ||
| for cur_route in $(ip route show dev ${dev} table local proto 66 | awk '{print$2}'); | ||
| do | ||
| if [[ "${dev_routes[@]}" =~ "${cur_route}" ]]; | ||
| then | ||
| echo "Removing forwarded IP ${cur_route}/32" | ||
| ip route del ${cur_route}/32 dev ${dev} table local proto 66 | ||
| fi | ||
| done | ||
| unset dev_routes | ||
| } | ||
|
|
||
| run() { | ||
| net_path="network-interfaces/" | ||
| for vif in $(curler ${net_path}); do | ||
| hw_addr=$(curler "${net_path}${vif}mac") | ||
| fwip_path="${net_path}${vif}forwarded-ips/" | ||
| dev_name="$(get_ifname ${hw_addr})" | ||
| for level in $(curler ${fwip_path}) | ||
| do | ||
| for fwip in $(curler ${fwip_path}${level}) | ||
| do | ||
| echo "Processing route for NIC ${vif}${hw_addr} as ${dev_name} for ${fwip}" | ||
| routes[$dev_name]+="${fwip} " | ||
| done | ||
| done | ||
| $"${1}" ${dev_name} | ||
|
|
||
| routes[$dev_name]="" | ||
| unset hw_addr | ||
| unset fwip_path | ||
| unset dev_name | ||
| done | ||
| } | ||
|
|
||
| case "$1" in | ||
| start) | ||
| while :; do | ||
| run set_routes | ||
| sleep 30 | ||
| done | ||
| ;; | ||
| cleanup) | ||
| run del_routes | ||
| ;; | ||
| *) | ||
| echo $"Usage: $0 {start|cleanup}" | ||
| exit 1 | ||
|
|
||
| esac |
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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| name: "gcp-routes.service" | ||
| enabled: true | ||
| contents: | | ||
| [Unit] | ||
| Description=Update GCP routes for forwarded IPs. | ||
| ConditionKernelCommandLine=|ignition.platform.id=gce | ||
| ConditionKernelCommandLine=|ignition.platform.id=gcp | ||
| After=network.target | ||
|
|
||
| [Service] | ||
| Type=simple | ||
| ExecStart=/bin/bash /usr/local/bin/gcp-routes.sh start | ||
| ExecStopPost=/bin/bash /usr/local/bin/gcp-routes.sh cleanup | ||
| User=root | ||
| RestartSec=30 | ||
| Restart=always | ||
|
|
||
| [Install] | ||
| WantedBy=multi-user.target | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it recommended to use
network.targetornetwork-online.targetin Fedora?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/ covers this...I think
network-online.targetis more right but the script retries anyways.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since we already have
network.targetin vsphere UPI scripts I'll keepnetwork.target- and replace these later