-
Notifications
You must be signed in to change notification settings - Fork 4
Fix/dns hosts configuration #362
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
Open
sanjujunnuthula
wants to merge
7
commits into
development
Choose a base branch
from
fix/dns-hosts-configuration
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+163
−107
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
52e8521
chore: autoupdating CoreDNS host config while installing pre-requisites
sanjujunnuthula fe33ecc
chore: added dns-host-config file
sanjujunnuthula df1ca10
chore: updated dns-host-config file
sanjujunnuthula 0b4de38
chore: removed Configure CoreDNS for k3s step from github sanity action
sanjujunnuthula c64cff4
chore: update setupguide by removing CoreDNS configuration
d68e26c
chore: update setupguide by removing CoreDNS configuration
6d6db4e
chore: update setupguide by removing CoreDNS configuration
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 |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| #!/bin/bash | ||
|
|
||
| #------------------------------------------------------------------------------ | ||
| # Returns the IPv4 address of the specified hostname from /etc/hosts. | ||
| #------------------------------------------------------------------------------ | ||
| get_ip_from_hosts() { | ||
| local hostname="$1" | ||
| local hosts_file="/etc/hosts" | ||
| local ip | ||
|
|
||
| if [[ ! -r "$hosts_file" ]]; then | ||
| echo "[ERROR] Unable to read ${hosts_file}." >&2 | ||
| return 1 | ||
| fi | ||
|
|
||
| ip=$( | ||
| awk -v host="$hostname" ' | ||
| /^[[:space:]]*#/ { next } | ||
| NF >= 2 && $2 == host { | ||
| print $1 | ||
| exit | ||
| } | ||
| ' "$hosts_file" | ||
| ) | ||
|
|
||
| if [[ -z "$ip" ]]; then | ||
| echo "[ERROR] Host '${hostname}' not found in ${hosts_file}." >&2 | ||
| return 1 | ||
| fi | ||
|
|
||
| if [[ ! "$ip" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then | ||
| echo "[ERROR] Invalid IPv4 address '${ip}' for '${hostname}'." >&2 | ||
| return 1 | ||
| fi | ||
|
|
||
| printf '%s\n' "$ip" | ||
| } | ||
|
|
||
| #------------------------------------------------------------------------------ | ||
| # Adds or updates NodeHosts entries in the CoreDNS ConfigMap. | ||
| #------------------------------------------------------------------------------ | ||
|
|
||
| add_or_update_host() { | ||
| local ip="$1" | ||
| local host="$2" | ||
|
|
||
| if awk -v host="$host" ' | ||
| /^[[:space:]]*#/ { next } | ||
| $2 == host { found=1 } | ||
| END { exit !found } | ||
| ' <<<"$updated"; then | ||
|
|
||
| updated=$( | ||
| awk -v ip="$ip" -v host="$host" ' | ||
| /^[[:space:]]*#/ { | ||
| next | ||
| } | ||
|
|
||
| $2 == host { | ||
| print ip " " host | ||
| next | ||
| } | ||
|
|
||
| { | ||
| } | ||
| ' <<<"$updated" | ||
| ) | ||
|
|
||
| else | ||
| if [[ -n "$updated" ]]; then | ||
| updated+=$'\n' | ||
| fi | ||
| updated+="${ip} ${host}" | ||
| fi | ||
| } | ||
|
|
||
| configure_coredns_hosts() { | ||
| set -euo pipefail | ||
|
|
||
| local namespace="kube-system" | ||
| local configmap="coredns" | ||
|
|
||
| local harbor_host="${EXPOSED_HARBOR_HOST}" | ||
| local symphony_host="${WFM_HOST}" | ||
|
|
||
| local harbor_ip | ||
| local symphony_ip | ||
|
|
||
| local nodehosts | ||
| local updated | ||
|
|
||
| echo "[INFO] Validating dependencies..." | ||
|
|
||
| for cmd in kubectl jq awk sed; do | ||
| if ! command -v "$cmd" >/dev/null 2>&1; then | ||
| echo "[ERROR] Required command '$cmd' is not installed." >&2 | ||
| return 1 | ||
| fi | ||
| done | ||
|
|
||
| echo "[INFO] Reading IP addresses from /etc/hosts..." | ||
|
|
||
| harbor_ip=$(get_ip_from_hosts "$harbor_host") | ||
| symphony_ip=$(get_ip_from_hosts "$symphony_host") | ||
|
|
||
| echo "[INFO] Verifying CoreDNS ConfigMap..." | ||
|
|
||
| if ! kubectl -n "$namespace" get configmap "$configmap" >/dev/null 2>&1; then | ||
| echo "[ERROR] ConfigMap '${configmap}' not found in namespace '${namespace}'." >&2 | ||
| return 1 | ||
| fi | ||
|
|
||
| nodehosts=$( | ||
| kubectl -n "$namespace" get configmap "$configmap" \ | ||
| -o jsonpath='{.data.NodeHosts}' 2>/dev/null || true | ||
| ) | ||
|
|
||
| updated="$nodehosts" | ||
|
|
||
| add_or_update_host "$harbor_ip" "$harbor_host" | ||
| add_or_update_host "$symphony_ip" "$symphony_host" | ||
|
|
||
| updated=$(sed '/^[[:space:]]*$/d' <<<"$updated") | ||
|
|
||
| if [[ "$updated" == "$nodehosts" ]]; then | ||
| echo "[INFO] CoreDNS NodeHosts already up to date." | ||
| return 0 | ||
| fi | ||
|
|
||
| echo "[INFO] Updating CoreDNS ConfigMap..." | ||
|
|
||
| kubectl -n "$namespace" patch configmap "$configmap" \ | ||
| --type merge \ | ||
| --patch "$(cat <<EOF | ||
| { | ||
| "data": { | ||
| "NodeHosts": $(printf '%s' "$updated" | jq -Rs .) | ||
| } | ||
| } | ||
| EOF | ||
| )" | ||
|
|
||
| echo "[INFO] Restarting CoreDNS..." | ||
|
|
||
| kubectl -n "$namespace" rollout restart deployment/coredns | ||
|
|
||
| echo "[INFO] Waiting for CoreDNS rollout to complete..." | ||
|
|
||
| kubectl -n "$namespace" rollout status deployment/coredns --timeout=180s | ||
|
|
||
| echo "[INFO] CoreDNS NodeHosts updated successfully." | ||
| } | ||
|
|
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.