Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/node_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func nbcToAKSNodeConfigV1(nbc *datamodel.NodeBootstrappingConfiguration) *aksnod
"testdomain456.com": {
QueryLogging: "Log",
Protocol: "PreferUDP",
ForwardDestination: "ClusterCoreDNS",
ForwardDestination: "VnetDNS",
ForwardPolicy: "Random",
MaxConcurrent: to.Ptr(int32(1000)),
CacheDurationInSeconds: to.Ptr(int32(3600)),
Expand Down
3 changes: 1 addition & 2 deletions e2e/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"time"

"github.com/Azure/agentbaker/e2e/config"
"github.com/Azure/agentbaker/e2e/toolkit"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -99,7 +98,7 @@ func ValidateCommonLinux(ctx context.Context, s *Scenario) {
}

// localdns is not supported on scriptless, privatekube and VHDUbuntu2204Gen2ContainerdAirgappedK8sNotCached.
if s.Tags.Scriptless != true && s.VHD != config.VHDUbuntu2204Gen2ContainerdPrivateKubePkg && s.VHD != config.VHDUbuntu2204Gen2ContainerdAirgappedK8sNotCached && !s.VHD.UnsupportedLocalDns {
if !s.VHD.UnsupportedLocalDns {
ValidateLocalDNSService(ctx, s, "enabled")
ValidateLocalDNSResolution(ctx, s, "169.254.10.10")
}
Expand Down
11 changes: 10 additions & 1 deletion parts/linux/cloud-init/artifacts/cse_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1145,12 +1145,21 @@ LOCALDNS_SLICEFILE="/etc/systemd/system/localdns.slice"
# It creates the localdns corefile and slicefile, then enables and starts localdns.
# In this function, generated base64 encoded localdns corefile is decoded and written to the corefile path.
# This function also creates the localdns slice file with memory and cpu limits, that will be used by localdns systemd unit.
shouldEnableLocalDns() {
enableLocalDNSForScriptless() {
mkdir -p "$(dirname "${LOCALDNS_COREFILE}")"
touch "${LOCALDNS_COREFILE}"
chmod 0644 "${LOCALDNS_COREFILE}"
echo "${LOCALDNS_GENERATED_COREFILE}" | base64 -d > "${LOCALDNS_COREFILE}" || exit $ERR_LOCALDNS_FAIL

# Create environment file for corefile regeneration.
# This file will be referenced by localdns.service using EnvironmentFile directive.
LOCALDNS_ENV_FILE="/etc/localdns/environment"
mkdir -p "$(dirname "${LOCALDNS_ENV_FILE}")"
cat > "${LOCALDNS_ENV_FILE}" <<EOF
LOCALDNS_BASE64_ENCODED_COREFILE=${LOCALDNS_GENERATED_COREFILE}
EOF
chmod 0644 "${LOCALDNS_ENV_FILE}"

mkdir -p "$(dirname "${LOCALDNS_SLICEFILE}")"
touch "${LOCALDNS_SLICEFILE}"
chmod 0644 "${LOCALDNS_SLICEFILE}"
Expand Down
2 changes: 1 addition & 1 deletion parts/linux/cloud-init/artifacts/cse_main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ EOF

# This is to enable localdns using scriptless.
if [ "${SHOULD_ENABLE_LOCALDNS}" = "true" ]; then
logs_to_events "AKS.CSE.shouldEnableLocalDns" shouldEnableLocalDns || exit $ERR_LOCALDNS_FAIL
logs_to_events "AKS.CSE.enableLocalDNSForScriptless" enableLocalDNSForScriptless || exit $ERR_LOCALDNS_FAIL
fi

if [ "${ID}" != "mariner" ] && [ "${ID}" != "azurelinux" ]; then
Expand Down
3 changes: 2 additions & 1 deletion parts/linux/cloud-init/artifacts/localdns.service
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Restart=on-failure
KillMode=mixed
TimeoutStopSec=30
Slice=localdns.slice
EnvironmentFile=-/etc/localdns/environment
ExecStart=/opt/azure/containers/localdns/localdns.sh

[Install]
WantedBy=multi-user.target
WantedBy=multi-user.target
36 changes: 35 additions & 1 deletion parts/linux/cloud-init/artifacts/localdns.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,18 @@ verify_localdns_corefile() {
return 1
fi

# Check if corefile exists, is not empty, else attempt to regenerate it.
if [ ! -f "${LOCALDNS_CORE_FILE}" ] || [ ! -s "${LOCALDNS_CORE_FILE}" ]; then
echo "Localdns corefile either does not exist or is empty at ${LOCALDNS_CORE_FILE}."
return 1

echo "Attempting to regenerate localdns corefile..."
if regenerate_localdns_corefile; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will localdns restart automatically when corefile is deleted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, localdns systemd unit will not restart automatically when the corefile get deleted. If the corefile gets deleted for any reason, and if watchdog restarts systemd unit inside the node, then localdns systemd unit will never come up. So this PR will handle this case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, if the corefile is deleted, and the systemd unit is not restarted, will it still functioning?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that is correct and that is what I have noticed on the node.

echo "Localdns corefile regenerated successfully."
return 0
else
echo "Failed to regenerate localdns corefile."
return 1
fi
fi
return 0
}
Expand Down Expand Up @@ -115,6 +124,31 @@ verify_localdns_binary() {
return 0
}

# Regenerate the localdns corefile from base64 encoded content.
# This is used when the corefile goes missing.
regenerate_localdns_corefile() {
if [ -z "${LOCALDNS_BASE64_ENCODED_COREFILE:-}" ]; then
echo "LOCALDNS_BASE64_ENCODED_COREFILE is not set. Cannot regenerate corefile."
return 1
fi
echo "Regenerating localdns corefile at ${LOCALDNS_CORE_FILE}"

mkdir -p "$(dirname "${LOCALDNS_CORE_FILE}")"
# Decode base64 corefile content and write to corefile.
if ! echo "${LOCALDNS_BASE64_ENCODED_COREFILE}" | base64 -d > "${LOCALDNS_CORE_FILE}"; then
echo "Failed to decode and write corefile."
return 1
fi

chmod 0644 "${LOCALDNS_CORE_FILE}" || {
echo "Failed to set permissions on ${LOCALDNS_CORE_FILE}"
return 1
}

echo "Successfully regenerated localdns corefile."
return 0
}

# Replace AzureDNSIP in corefile with VNET DNS ServerIPs if necessary.
replace_azurednsip_in_corefile() {
if [ -z "${RESOLV_CONF:-}" ]; then
Expand Down
4 changes: 2 additions & 2 deletions pkg/agent/testdata/AKSUbuntu2204+China/CustomData

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/AKSUbuntu2204+Containerd+CDI/CustomData

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/AKSUbuntu2204+Containerd+MIG/CustomData

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/AKSUbuntu2204+CustomCloud/CustomData

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/AKSUbuntu2204+OutboundTypeNil/CustomData

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/AKSUbuntu2204+SSHStatusOff/CustomData

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/AKSUbuntu2204+SSHStatusOn/CustomData

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/AKSUbuntu2204+SecurityProfile/CustomData

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/AKSUbuntu2204+cgroupv2/CustomData

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/AKSUbuntu2404+NetworkPolicy/CustomData

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/AKSUbuntu2404+Teleport/CustomData

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/AzureLinuxV2+Kata/CustomData

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/AzureLinuxV3+Kata/CustomData

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/CustomizedImage/CustomData

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/CustomizedImageKata/CustomData

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/CustomizedImageLinuxGuard/CustomData

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/agent/testdata/Flatcar+CustomCloud+USSec/CustomData

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/agent/testdata/Flatcar+CustomCloud/CustomData

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/Flatcar+CustomCloud/CustomData.inner

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/agent/testdata/Flatcar/CustomData

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/Flatcar/CustomData.inner

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/MarinerV2+CustomCloud+USNat/CustomData

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/MarinerV2+CustomCloud+USSec/CustomData

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/MarinerV2+CustomCloud/CustomData

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/agent/testdata/MarinerV2+Kata/CustomData

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions spec/parts/linux/cloud-init/artifacts/cse_config_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -548,15 +548,15 @@ Describe 'cse_config.sh'

# Success case.
It 'should enable localdns successfully'
When call shouldEnableLocalDns
When call enableLocalDNSForScriptless
The status should be success
The output should include "localdns should be enabled."
The output should include "Enable localdns succeeded."
End

# Corefile file creation.
It 'should create localdns.corefile with correct data'
When call shouldEnableLocalDns
When call enableLocalDNSForScriptless
The status should be success
The output should include "localdns should be enabled."
The path "$LOCALDNS_COREFILE" should be file
Expand All @@ -568,7 +568,7 @@ Describe 'cse_config.sh'
# Corefile already exists (idempotency).
It 'should overwrite existing localdns.corefile'
echo "wrong data" > "$LOCALDNS_COREFILE"
When call shouldEnableLocalDns
When call enableLocalDNSForScriptless
The status should be success
The path "$LOCALDNS_COREFILE" should be file
The contents of file "$LOCALDNS_COREFILE" should include "localdns corefile"
Expand All @@ -578,7 +578,7 @@ Describe 'cse_config.sh'

# Slice file creation.
It 'should create localdns.slice with correct CPU and Memory limits'
When call shouldEnableLocalDns
When call enableLocalDNSForScriptless
The status should be success
The output should include "localdns should be enabled."
The path "$LOCALDNS_SLICEFILE" should be file
Expand Down
73 changes: 69 additions & 4 deletions spec/parts/linux/cloud-init/artifacts/localdns_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ Describe 'localdns.sh'
# These functions are defined in parts/linux/cloud-init/artifacts/localdns.sh file.
#------------------------------------------------------------------------------------------------------------------------------------
Describe 'verify_localdns_files'
check_file_permissions() {
local file=$1
local expected_perms=$2
local actual_perms=$(stat -c '%a' "$file" 2>/dev/null)
[ "$actual_perms" = "$expected_perms" ]
}

setup() {
Include "./parts/linux/cloud-init/artifacts/localdns.sh"

Expand All @@ -18,8 +25,12 @@ Describe 'localdns.sh'
LOCALDNS_CORE_FILE="${LOCALDNS_SCRIPT_PATH}/localdns.corefile"
UPDATED_LOCALDNS_CORE_FILE="${LOCALDNS_SCRIPT_PATH}/updated.localdns.corefile"
mkdir -p "$LOCALDNS_SCRIPT_PATH"
echo "forward . 168.63.129.16" >> "$LOCALDNS_CORE_FILE"
echo "forward . 168.63.129.16" >> "$UPDATED_LOCALDNS_CORE_FILE"
echo ".:5353 {" >> "$LOCALDNS_CORE_FILE"
echo " forward . 168.63.129.16" >> "$LOCALDNS_CORE_FILE"
echo "}" >> "$LOCALDNS_CORE_FILE"
echo ".:5353 {" >> "$UPDATED_LOCALDNS_CORE_FILE"
echo " forward . 168.63.129.16" >> "$UPDATED_LOCALDNS_CORE_FILE"
echo "}" >> "$UPDATED_LOCALDNS_CORE_FILE"

LOCALDNS_SLICE_PATH="${TEST_DIR}/etc/systemd/system"
LOCALDNS_SLICE_FILE="${LOCALDNS_SLICE_PATH}/localdns.slice"
Expand Down Expand Up @@ -54,24 +65,78 @@ EOF
}
BeforeEach 'setup'
AfterEach 'cleanup'
#------------------------ regenerate_localdns_corefile ---------------------------------------------
It 'should regenerate corefile successfully when LOCALDNS_BASE64_ENCODED_COREFILE is set'
rm -f "$LOCALDNS_CORE_FILE"
LOCALDNS_BASE64_ENCODED_COREFILE=$(echo ".:5353 {
forward . 168.63.129.16
}" | base64)
When run regenerate_localdns_corefile
The status should be success
The stdout should include "Regenerating localdns corefile at $LOCALDNS_CORE_FILE"
The stdout should include "Successfully regenerated localdns corefile."
The path "$LOCALDNS_CORE_FILE" should be file
End

It 'should fail to regenerate when LOCALDNS_BASE64_ENCODED_COREFILE is not set'
rm -f "$LOCALDNS_CORE_FILE"
unset LOCALDNS_BASE64_ENCODED_COREFILE
When run regenerate_localdns_corefile
The status should be failure
The stdout should include "LOCALDNS_BASE64_ENCODED_COREFILE is not set. Cannot regenerate corefile."
End

It 'should set correct permissions on regenerated corefile'
rm -f "$LOCALDNS_CORE_FILE"
LOCALDNS_BASE64_ENCODED_COREFILE=$(echo ".:5353 {
forward . 168.63.129.16
}" | base64)
When run regenerate_localdns_corefile
The status should be success
The stdout should include "Successfully regenerated localdns corefile."
The path "$LOCALDNS_CORE_FILE" should be file
End

#------------------------ verify_localdns_corefile -------------------------------------------------
It 'should return success if localdns corefile exists and is not empty'
When run verify_localdns_corefile
The status should be success
End

It 'should return failure if localdns corefile does not exist'
It 'should succeed without regeneration if corefile exists and is not empty'
echo ".:5353 {
forward . 168.63.129.16
}" > "$LOCALDNS_CORE_FILE"
When run verify_localdns_corefile
The status should be success
End

It 'should regenerate and succeed if corefile is missing and LOCALDNS_BASE64_ENCODED_COREFILE is set'
rm -f "$LOCALDNS_CORE_FILE"
LOCALDNS_BASE64_ENCODED_COREFILE=$(echo ".:5353 {
forward . 168.63.129.16
}" | base64)
When run verify_localdns_corefile
The status should be success
The stdout should include "Attempting to regenerate localdns corefile..."
The stdout should include "Localdns corefile regenerated successfully."
End

It 'should return failure if localdns corefile does not exist and regeneration fails'
rm -r "$LOCALDNS_CORE_FILE"
When run verify_localdns_corefile
The status should be failure
The stdout should include "Localdns corefile either does not exist or is empty at $LOCALDNS_CORE_FILE."
The stdout should include "Attempting to regenerate localdns corefile..."
The stdout should include "LOCALDNS_BASE64_ENCODED_COREFILE is not set. Cannot regenerate corefile."
End

It 'should return failure if localdns corefile is empty'
It 'should return failure if localdns corefile is empty and regeneration fails'
> "$LOCALDNS_CORE_FILE"
When run verify_localdns_corefile
The status should be failure
The stdout should include "Localdns corefile either does not exist or is empty at $LOCALDNS_CORE_FILE."
The stdout should include "Attempting to regenerate localdns corefile..."
End

It 'should return failure if LOCALDNS_CORE_FILE is unset'
Expand Down
Loading