Skip to content

Commit 180e556

Browse files
committed
Merge branch 'bugfix/fix-exec-when-containerd-restart' into q/133.0
2 parents 92c3d3b + 9ba7b4d commit 180e556

File tree

10 files changed

+35
-100
lines changed

10 files changed

+35
-100
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
and Loki image version to [3.6.5](https://github.com/grafana/loki/releases/tag/v3.6.5)
3131
(PR[#4792](https://github.com/scality/metalk8s/pull/4792))
3232

33+
### Bug Fixes
34+
35+
- Fix a bug where part of the upgrade process would silently be skipped
36+
if the containerd socket is lost (crictl exec would exit with code 0)
37+
(PR[#4802](https://github.com/scality/metalk8s/pull/4802))
38+
3339
## Release 132.0.2 (in development)
3440

3541
## Release 132.0.1

salt/_modules/cri.py

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -99,48 +99,6 @@ def pull_image(image):
9999
return ret
100100

101101

102-
def execute(name, command, *args):
103-
"""
104-
Run a command in a container.
105-
106-
.. note::
107-
108-
This uses the :command:`crictl` command, which should be configured
109-
correctly on the system, e.g. in :file:`/etc/crictl.yaml`.
110-
111-
name
112-
Name of the target container
113-
command
114-
Command to run
115-
args
116-
Command parameters
117-
"""
118-
log.info('Retrieving ID of container "%s"', name)
119-
out = __salt__["cmd.run_all"](
120-
f'crictl ps -q --label io.kubernetes.container.name="{name}"'
121-
)
122-
123-
if out["retcode"] != 0:
124-
log.error('Failed to find container "%s"', name)
125-
return None
126-
127-
container_id = out["stdout"]
128-
if not container_id:
129-
log.error('Container "%s" does not exists', name)
130-
return None
131-
132-
cmd_opts = f"{command} {' '.join(args)}"
133-
134-
log.info('Executing command "%s"', cmd_opts)
135-
out = __salt__["cmd.run_all"](f"crictl exec {container_id} {cmd_opts}")
136-
137-
if out["retcode"] != 0:
138-
log.error('Failed run command "%s"', cmd_opts)
139-
return None
140-
141-
return out["stdout"]
142-
143-
144102
def wait_container(name, state, timeout=60, delay=5):
145103
"""
146104
Wait for a container to be in given state.

salt/tests/unit/modules/test_cri.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -157,40 +157,6 @@ def test_pull_image(self, retcode, stdout, result):
157157
self.assertEqual(cri.pull_image("my-images"), result)
158158
mock_cmd.assert_called_once_with('crictl pull "my-images"')
159159

160-
@parameterized.expand(
161-
[
162-
(0, "292c3b07b", 0, "All ok", "All ok"),
163-
(1, "292c3b07b", 0, "All ok", None),
164-
(0, "", 0, "All ok", None),
165-
(0, "292c3b07b", 1, "All not ok", None),
166-
(0, "292c3b07b", 0, "", ""),
167-
]
168-
)
169-
def test_execute(self, ret_ps, stdout_ps, ret_exec, stdout_exec, result):
170-
"""
171-
Tests the return of `execute` function
172-
"""
173-
cmd_ps = utils.cmd_output(retcode=ret_ps, stdout=stdout_ps)
174-
cmd_exec = utils.cmd_output(retcode=ret_exec, stdout=stdout_exec)
175-
176-
def _cmd_run_all_mock(cmd):
177-
if "crictl ps" in cmd:
178-
return cmd_ps
179-
elif "crictl exec" in cmd:
180-
return cmd_exec
181-
return None
182-
183-
mock_cmd = MagicMock(side_effect=_cmd_run_all_mock)
184-
with patch.dict(cri.__salt__, {"cmd.run_all": mock_cmd}):
185-
self.assertEqual(cri.execute("my_cont", "my command"), result)
186-
mock_cmd.assert_any_call(
187-
'crictl ps -q --label io.kubernetes.container.name="my_cont"'
188-
)
189-
if ret_ps == 0 and stdout_ps:
190-
mock_cmd.assert_called_with(
191-
"crictl exec {} my command ".format(stdout_ps)
192-
)
193-
194160
@parameterized.expand(
195161
[
196162
# Success: Found one container

scripts/backup.sh.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ backup_etcd() {
127127
# etcd 3.4+ defaults to API v3, so ETCDCTL_API=3 is no longer required.
128128
# In distroless images, the root filesystem is read-only, so we save the
129129
# snapshot to /var/lib/etcd which is a writable mounted volume.
130-
crictl exec -i "$etcd_container" \
130+
"${EXEC_CONTAINER_COMMAND[@]}" "$etcd_container" \
131131
etcdctl \
132132
--endpoints=https://127.0.0.1:2379 \
133133
--cert=/etc/kubernetes/pki/etcd/salt-master-etcd-client.crt \
@@ -167,7 +167,7 @@ EOF
167167
}
168168

169169
replicate_archives() {
170-
salt_master_exec=(crictl exec -i "$(get_salt_container)")
170+
salt_master_exec=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
171171

172172
"${salt_master_exec[@]}" salt-run --state-output=mixed state.orchestrate \
173173
metalk8s.orchestrate.backup.replication \

scripts/bootstrap.sh.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ orchestrate_bootstrap() {
9595
pillarenv=metalk8s-@@VERSION \
9696
pillar="${pillar[*]}"
9797

98-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
98+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
9999

100100
run "Syncing Utility modules on Salt master" \
101101
"${SALT_MASTER_CALL[@]}" salt-run --state-output=mixed saltutil.sync_utils \

scripts/common.sh.in

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ SYSTEMCTL=${SYSTEMCTL:-$(command -v systemctl)}
1515
PYTHON=${PYTHON:-$(command -v python3 || command -v python || true)}
1616
LOGFILE_MAX_ROTATIONS=10
1717

18+
# NOTE: We do not use kubectl nor crictl to execute commands in containers,
19+
# since some commands may restart kube-apiserver and/or containerd and/or kubelet.
20+
# That's why we use runc directly to execute commands in containers.
21+
EXEC_CONTAINER_COMMAND=(runc --root=/run/containerd/runc/k8s.io exec)
22+
1823

1924
determine_os() {
2025
# We rely on /etc/os-release to discover the OS because its present on all
@@ -355,7 +360,7 @@ get_salt_minion_ids() {
355360

356361
(
357362
set -o pipefail
358-
retry 5 10 crictl exec -i "$salt_container" \
363+
retry 5 10 "${EXEC_CONTAINER_COMMAND[@]}" "$salt_container" \
359364
salt \* grains.get id --out txt | \
360365
cut -d ' ' -f 2
361366
)

scripts/downgrade.sh.in

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ cleanup() {
8989
trap cleanup EXIT
9090

9191
precheck_downgrade () {
92-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
92+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
9393
"${SALT_MASTER_CALL[@]}" salt-run saltutil.sync_all \
9494
saltenv="$SALTENV"
9595
# Due to a bug in salt do not "raises" when we run from CLI
@@ -102,7 +102,7 @@ precheck_downgrade () {
102102
}
103103

104104
launch_pre_downgrade () {
105-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
105+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
106106
"${SALT_MASTER_CALL[@]}" salt-run saltutil.sync_all \
107107
saltenv="$SALTENV"
108108

@@ -112,14 +112,14 @@ launch_pre_downgrade () {
112112
}
113113

114114
launch_post_downgrade () {
115-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
115+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
116116
"${SALT_MASTER_CALL[@]}" salt-run state.orchestrate \
117117
metalk8s.orchestrate.downgrade.post \
118118
saltenv="$SALTENV"
119119
}
120120

121121
launch_downgrade () {
122-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
122+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
123123
# NOTE: We want to use `$SALTENV` version for the runners
124124
"${SALT_MASTER_CALL[@]}" salt-run saltutil.sync_all \
125125
extmod_whitelist="{'runners': []}" \
@@ -146,7 +146,7 @@ downgrade_bootstrap () {
146146
repo_endpoint="$($SALT_CALL pillar.get metalk8s:endpoints:repositories --out txt \
147147
| cut -d' ' -f2- )"
148148

149-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
149+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
150150
"${SALT_MASTER_CALL[@]}" salt-run state.orchestrate \
151151
metalk8s.orchestrate.bootstrap.pre-downgrade \
152152
saltenv="$SALTENV" || return 1
@@ -159,7 +159,7 @@ downgrade_bootstrap () {
159159

160160
# patch the kube-system namespace annotation with <destination-version> input
161161
patch_kubesystem_namespace() {
162-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
162+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
163163

164164
"${SALT_MASTER_CALL[@]}" salt-run saltutil.sync_all \
165165
saltenv="$SALTENV"

scripts/restore.sh.in

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ configure_salt_master() {
182182
saltenv=metalk8s-@@VERSION \
183183
pillar="${pillar[*]}"
184184

185-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
185+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
186186

187187
"${SALT_MASTER_CALL[@]}" salt-run saltutil.sync_all \
188188
saltenv=metalk8s-@@VERSION
@@ -237,7 +237,7 @@ push_cas() {
237237
}
238238

239239
mark_control_plane() {
240-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
240+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
241241

242242
local -r bootstrap_id=$(
243243
${SALT_CALL} --local --out txt grains.get id \
@@ -252,7 +252,7 @@ mark_control_plane() {
252252
}
253253

254254
reconfigure_nodes() {
255-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
255+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
256256
local -r non_bootstrap=$(
257257
${SALT_CALL} --out=txt slsutil.renderer \
258258
string="{{ pillar.metalk8s.nodes.keys() | difference(salt.metalk8s.minions_by_role('bootstrap')) | join(',') }}" \
@@ -290,7 +290,7 @@ reconfigure_nodes() {
290290
}
291291

292292
highstate_bootstrap() {
293-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
293+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
294294
local -r bootstrap_id=$(
295295
${SALT_CALL} --local --out txt grains.get id \
296296
| awk '/^local\: /{ print $2 }'
@@ -306,7 +306,7 @@ highstate_bootstrap() {
306306
}
307307

308308
reconfigure_k8s_obj() {
309-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
309+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
310310

311311
"${SALT_MASTER_CALL[@]}" salt-run --state-output=mixed state.orchestrate \
312312
metalk8s.addons.nginx-ingress-control-plane.deployed \

scripts/solutions.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ salt_minion_exec() {
241241
salt_master_exec() {
242242
SALTENV=${SALTENV:-$(get_salt_env)}
243243
SALT_MASTER_CONTAINER_ID=${SALT_MASTER_CONTAINER_ID:-$(get_salt_container)}
244-
crictl exec -i "$SALT_MASTER_CONTAINER_ID" "$@" saltenv="$SALTENV"
244+
"${EXEC_CONTAINER_COMMAND[@]}" "$SALT_MASTER_CONTAINER_ID" "$@" saltenv="$SALTENV"
245245
}
246246

247247
activate_solution() {

scripts/upgrade.sh.in

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ upgrade_bootstrap () {
8080
"$SALT_CALL" saltutil.sync_all saltenv="$SALTENV"
8181
"$SALT_CALL" saltutil.refresh_grains
8282

83-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
83+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
8484
"${SALT_MASTER_CALL[@]}" salt-run state.orchestrate \
8585
metalk8s.orchestrate.bootstrap.pre-upgrade \
8686
saltenv="$SALTENV"
@@ -93,7 +93,7 @@ upgrade_bootstrap () {
9393
}
9494

9595
launch_pre_upgrade () {
96-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
96+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
9797
"${SALT_MASTER_CALL[@]}" salt-run saltutil.sync_all \
9898
saltenv="$SALTENV"
9999

@@ -103,14 +103,14 @@ launch_pre_upgrade () {
103103
}
104104

105105
launch_post_upgrade () {
106-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
106+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
107107
"${SALT_MASTER_CALL[@]}" salt-run state.orchestrate \
108108
metalk8s.orchestrate.upgrade.post \
109109
saltenv="$SALTENV"
110110
}
111111

112112
upgrade_etcd () {
113-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
113+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
114114
"${SALT_MASTER_CALL[@]}" salt-run saltutil.sync_all \
115115
saltenv="$SALTENV"
116116

@@ -125,7 +125,7 @@ upgrade_etcd () {
125125
}
126126

127127
upgrade_apiservers () {
128-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
128+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
129129
"${SALT_MASTER_CALL[@]}" salt-run state.orchestrate \
130130
metalk8s.orchestrate.apiserver saltenv="$SALTENV"
131131
}
@@ -166,14 +166,14 @@ upgrade_local_engines () {
166166
}
167167

168168
upgrade_nodes () {
169-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
169+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
170170
"${SALT_MASTER_CALL[@]}" salt-run state.orchestrate \
171171
metalk8s.orchestrate.upgrade saltenv="$SALTENV" \
172172
pillar="{'orchestrate': {'drain_timeout': $DRAIN_TIMEOUT}}"
173173
}
174174

175175
precheck_upgrade() {
176-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
176+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
177177
"${SALT_MASTER_CALL[@]}" salt-run saltutil.sync_all \
178178
saltenv="$SALTENV"
179179
# Due to a bug in salt do not "raises" when we run from CLI
@@ -186,7 +186,7 @@ precheck_upgrade() {
186186

187187
# patch the kube-system namespace annotation with <destination-version> input
188188
patch_kubesystem_namespace() {
189-
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
189+
SALT_MASTER_CALL=("${EXEC_CONTAINER_COMMAND[@]}" "$(get_salt_container)")
190190

191191
"${SALT_MASTER_CALL[@]}" salt-run saltutil.sync_all \
192192
saltenv="$SALTENV"

0 commit comments

Comments
 (0)