Skip to content

Commit

Permalink
Remove DNSIP assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
sachinkumarsingh092 committed Jul 24, 2023
1 parent 67fdfa9 commit e484bb7
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 75 deletions.
58 changes: 0 additions & 58 deletions addons/common/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,61 +31,3 @@ use_addon_manifest() {
use_manifest_result="$?"
rm "${tmp_manifest}"
}

# Function to convert an IP address to a decimal number
ip_to_decimal() {
local ip="$1"
echo $(( $(echo "$ip" | awk -F'.' '{print ($1*256^3)+($2*256^2)+($3*256^1)+$4}') ))
}

# Function to convert a decimal number to an IP address
decimal_to_ip() {
local decimal="$1"
echo "$(( (decimal >> 24) & 255 )).$(( (decimal >> 16) & 255 )).$(( (decimal >> 8) & 255 )).$(( decimal & 255 ))"
}

# Function to validate if an IP address is within the CIDR range
ip_in_cidr() {
local ip="$1"
local cidr="$2"

local cidr_ip=$(echo "$cidr" | cut -d'/' -f1)
local cidr_mask=$(echo "$cidr" | cut -d'/' -f2)

local ip_decimal=$(ip_to_decimal "$ip")
local cidr_ip_decimal=$(ip_to_decimal "$cidr_ip")

local bitmask=$((2**(32-cidr_mask)-1))
local network_ip_decimal=$((cidr_ip_decimal & ~bitmask))

local cidr_start_decimal=$((network_ip_decimal + 1))
local cidr_end_decimal=$((network_ip_decimal + bitmask - 1))

[ $ip_decimal -ge $cidr_start_decimal ] && [ $ip_decimal -le $cidr_end_decimal ]
}

# Function to choose a random valid IP address from the CIDR range
choose_random_ip() {
local cidr="$1"
local ip
local count=0
local max_attempts=100

local cidr_ip=$(echo "$cidr" | cut -d'/' -f1)
local base_ip="${cidr_ip%.*}."

while [ "$count" -lt "$max_attempts" ]; do
ip=$(shuf -i 1-254 -n 1)
chosen_ip="${base_ip}$ip"

if ip_in_cidr "$chosen_ip" "$cidr"; then
echo "$chosen_ip"
return 0
fi

((count++))
done

echo "Failed to find a valid IP address after $max_attempts attempts."
return 1
}
1 change: 0 additions & 1 deletion addons/dns/coredns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ metadata:
spec:
selector:
k8s-app: kube-dns
clusterIP: $DNSIP
ports:
- name: dns
port: 53
Expand Down
20 changes: 4 additions & 16 deletions addons/dns/enable
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,6 @@ else
echo "Will use ${nameserver_str} as upstream nameservers"
fi

if [ -e "${SNAP_DATA}/args/cni-env" ]; then
source "${SNAP_DATA}/args/cni-env"
fi
# Choose a random IP address from the service cidr.
DNSIP=$(choose_random_ip "$IPv4_SERVICE_CIDR" || true)
echo "Will use ${DNSIP} as dns service ip address"

DNSIPARG=$2
if [[ -n "$DNSIPARG" ]]; then
DNSIP="$DNSIPARG";
echo "Will use ${DNSIPARG} for dns service ip address"
echo "Make sure this address is in the service cidr!"
fi

echo "Applying manifest"
ALLOWESCALATION="false"
if grep -e ubuntu /proc/version | grep 16.04 &> /dev/null; then
Expand All @@ -70,18 +56,20 @@ fi
declare -A map
map[\$ALLOWESCALATION]="$ALLOWESCALATION"
map[\$NAMESERVERS]="$nameserver_str"
map[\$DNSIP]="$DNSIP"
use_addon_manifest dns/coredns apply "$(declare -p map)"
sleep 5

# Get the DNS IP assigned to the dns service
DNSIP=$($KUBECTL get svc -n kube-system kube-dns -o jsonpath={.spec.clusterIP})

needs_restart=false
if ! grep -q -- --cluster-domain=cluster.local "${SNAP_DATA}/args/kubelet"; then
needs_restart=true
elif ! grep -q -- "--cluster-dns=$DNSIP" "${SNAP_DATA}/args/kubelet"; then
needs_restart=true
fi

#TODO(kjackal): do not hardcode the info below. Get it from the yaml
echo "Setting dns ip address to $DNSIP"
refresh_opt_in_config "cluster-domain" "cluster.local" kubelet
refresh_opt_in_config "cluster-dns" "$DNSIP" kubelet

Expand Down

0 comments on commit e484bb7

Please sign in to comment.