Skip to content

Commit f84e493

Browse files
committed
Reset SONiC switches to their unprovisioned state on cell deletion
Signed-off-by: Andreas Fritzler <andreas.fritzler@sap.com>
1 parent ef8626c commit f84e493

5 files changed

Lines changed: 113 additions & 9 deletions

File tree

‎.dockerignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010
# Re-include Go module files
1111
!go.mod
1212
!go.sum
13+
14+
# Re-include assets embedded via go:embed
15+
!sonic/templates/**

‎sonic/script.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ func (r *ScriptRuntime) ApplyCell(ctx context.Context, node string, cfg *cellrun
6363
return runScript(ctx, script)
6464
}
6565

66-
// TODO: reset the configuration of the device. Runs the (currently noop) reset script.
6766
func (r *ScriptRuntime) DeleteCell(ctx context.Context, node string) error {
67+
if !fileExists(doneFlagFile) {
68+
return fmt.Errorf("cell %s %w", node, cellruntime.ErrNotFound)
69+
}
6870
script, err := r.renderReset(node)
6971
if err != nil {
7072
return err

‎sonic/script_test.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ func TestRenderReset(t *testing.T) {
126126
if err != nil {
127127
t.Fatalf("rendering reset: %v", err)
128128
}
129+
if strings.Contains(string(got), "{{") {
130+
t.Error("rendered script still contains a template action")
131+
}
129132
compareGolden(t, "reset.golden.sh", got)
130133
}
131134

‎sonic/templates/reset.sh‎

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,61 @@
44
set -e
55

66
# When run inside a container, re-exec in the host namespaces so the
7-
# reset calls below act on the host.
7+
# config/systemctl/reboot calls below act on the host.
88
if [ -z "$FABRIC_ON_HOST" ]; then
99
export FABRIC_ON_HOST=1
1010
exec nsenter --target 1 --mount --net --pid bash "$0"
1111
fi
1212

1313
# Reset script for node {{ .Node }}, rendered by the fabric cell runtime.
14-
# TODO: reset the configuration of the device to a clean state.
15-
echo "Resetting the device is not implemented yet"
16-
exit 0
14+
# Undoes what the cell script applied: server VLANs, loopback IPs, port
15+
# state and the FRR/BGP config. Everything is read from the running
16+
# CONFIG_DB, so this also works on a partially provisioned device (failed
17+
# cells). Deliberately kept: hostname, management config, link-local and
18+
# the physical port parameters (mtu/fec/speed).
19+
20+
CONFIG_DB=/etc/sonic/config_db.json
21+
22+
# Remove the loopback addresses handed down by the cell
23+
for ENTRY in $(jq -r '.LOOPBACK_INTERFACE // {} | keys[] | select(contains("|"))' "$CONFIG_DB"); do
24+
config interface ip remove "${ENTRY%%|*}" "${ENTRY#*|}"
25+
done
26+
27+
# Remove the VLANs the cell's server peers created, in dependency order:
28+
# DHCP relay, interface IPs, members, then the VLAN itself
29+
for VLAN in $(jq -r '.VLAN // {} | keys[]' "$CONFIG_DB"); do
30+
VLAN_ID="${VLAN#Vlan}"
31+
for SERVER in $(jq -r --arg v "$VLAN" '.DHCP_RELAY[$v] // {} | .dhcpv6_servers // .dhcp_servers // [] | .[]' "$CONFIG_DB"); do
32+
config vlan dhcp_relay del "$VLAN_ID" "$SERVER"
33+
done
34+
for ENTRY in $(jq -r --arg v "$VLAN" '.VLAN_INTERFACE // {} | keys[] | select(startswith($v + "|"))' "$CONFIG_DB"); do
35+
config interface ip remove "$VLAN" "${ENTRY#*|}"
36+
done
37+
for MEMBER in $(jq -r --arg v "$VLAN" '.VLAN_MEMBER // {} | keys[] | select(startswith($v + "|"))' "$CONFIG_DB"); do
38+
config vlan member del "$VLAN_ID" "${MEMBER#*|}"
39+
done
40+
config vlan del "$VLAN_ID"
41+
done
42+
43+
# Shut down every front panel port the cell script started up
44+
for PORT in $(jq -r '.PORT // {} | to_entries[] | select(.value.admin_status == "up") | .key' "$CONFIG_DB"); do
45+
config interface shutdown "$PORT"
46+
done
47+
48+
config save -y
49+
50+
# Return FRR to an empty config: no BGP peering
51+
cat <<EOF >/etc/sonic/frr/frr.conf
52+
frr version 8.1
53+
frr defaults traditional
54+
hostname $(hostname)
55+
log syslog informational
56+
service integrated-vtysh-config
57+
EOF
58+
59+
systemctl restart bgp
60+
61+
# Mark the device as unprovisioned, let the fabriclet expire the cell
62+
rm -f /etc/sonic/fabric-cell.done
63+
64+
reboot

‎sonic/testdata/reset.golden.sh‎

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,61 @@
44
set -e
55

66
# When run inside a container, re-exec in the host namespaces so the
7-
# reset calls below act on the host.
7+
# config/systemctl/reboot calls below act on the host.
88
if [ -z "$FABRIC_ON_HOST" ]; then
99
export FABRIC_ON_HOST=1
1010
exec nsenter --target 1 --mount --net --pid bash "$0"
1111
fi
1212

1313
# Reset script for node swi1-ab-42, rendered by the fabric cell runtime.
14-
# TODO: reset the configuration of the device to a clean state.
15-
echo "Resetting the device is not implemented yet"
16-
exit 0
14+
# Undoes what the cell script applied: server VLANs, loopback IPs, port
15+
# state and the FRR/BGP config. Everything is read from the running
16+
# CONFIG_DB, so this also works on a partially provisioned device (failed
17+
# cells). Deliberately kept: hostname, management config, link-local and
18+
# the physical port parameters (mtu/fec/speed).
19+
20+
CONFIG_DB=/etc/sonic/config_db.json
21+
22+
# Remove the loopback addresses handed down by the cell
23+
for ENTRY in $(jq -r '.LOOPBACK_INTERFACE // {} | keys[] | select(contains("|"))' "$CONFIG_DB"); do
24+
config interface ip remove "${ENTRY%%|*}" "${ENTRY#*|}"
25+
done
26+
27+
# Remove the VLANs the cell's server peers created, in dependency order:
28+
# DHCP relay, interface IPs, members, then the VLAN itself
29+
for VLAN in $(jq -r '.VLAN // {} | keys[]' "$CONFIG_DB"); do
30+
VLAN_ID="${VLAN#Vlan}"
31+
for SERVER in $(jq -r --arg v "$VLAN" '.DHCP_RELAY[$v] // {} | .dhcpv6_servers // .dhcp_servers // [] | .[]' "$CONFIG_DB"); do
32+
config vlan dhcp_relay del "$VLAN_ID" "$SERVER"
33+
done
34+
for ENTRY in $(jq -r --arg v "$VLAN" '.VLAN_INTERFACE // {} | keys[] | select(startswith($v + "|"))' "$CONFIG_DB"); do
35+
config interface ip remove "$VLAN" "${ENTRY#*|}"
36+
done
37+
for MEMBER in $(jq -r --arg v "$VLAN" '.VLAN_MEMBER // {} | keys[] | select(startswith($v + "|"))' "$CONFIG_DB"); do
38+
config vlan member del "$VLAN_ID" "${MEMBER#*|}"
39+
done
40+
config vlan del "$VLAN_ID"
41+
done
42+
43+
# Shut down every front panel port the cell script started up
44+
for PORT in $(jq -r '.PORT // {} | to_entries[] | select(.value.admin_status == "up") | .key' "$CONFIG_DB"); do
45+
config interface shutdown "$PORT"
46+
done
47+
48+
config save -y
49+
50+
# Return FRR to an empty config: no BGP peering
51+
cat <<EOF >/etc/sonic/frr/frr.conf
52+
frr version 8.1
53+
frr defaults traditional
54+
hostname $(hostname)
55+
log syslog informational
56+
service integrated-vtysh-config
57+
EOF
58+
59+
systemctl restart bgp
60+
61+
# Mark the device as unprovisioned, let the fabriclet expire the cell
62+
rm -f /etc/sonic/fabric-cell.done
63+
64+
reboot

0 commit comments

Comments
 (0)