Skip to content
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

fix: restart Containerd when setting mirror config #374

Merged
merged 1 commit into from
Feb 16, 2024
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
4 changes: 4 additions & 0 deletions pkg/handlers/generic/mutation/mirrors/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ func generateFilesAndCommands(
}
files = append(files, applyPatchesFile...)
commands = append(commands, applyPatchesCommand)
// generate Containerd restart script and command
restartFile, restartCommand := generateContainerdRestartScript()
files = append(files, restartFile...)
commands = append(commands, restartCommand)

return files, commands, err
}
17 changes: 17 additions & 0 deletions pkg/handlers/generic/mutation/mirrors/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const (
containerdPatchesDirOnRemote = "/etc/containerd/cre.d"
containerdApplyPatchesScriptOnRemote = "/etc/containerd/apply-patches.sh"
containerdApplyPatchesScriptOnRemoteCommand = "/bin/bash " + containerdApplyPatchesScriptOnRemote

containerdRestartScriptOnRemote = "/etc/containerd/restart.sh"
containerdRestartScriptOnRemoteCommand = "/bin/bash " + containerdRestartScriptOnRemote
)

var (
Expand All @@ -47,6 +50,9 @@ var (

//go:embed templates/containerd-apply-patches.sh.gotmpl
containerdApplyConfigPatchesScript []byte

//go:embed templates/containerd-restart.sh
containerdRestartScript []byte
)

type mirrorConfig struct {
Expand Down Expand Up @@ -220,3 +226,14 @@ func generateContainerdApplyPatchesScript() ([]cabpkv1.File, string, error) {
},
}, containerdApplyPatchesScriptOnRemoteCommand, nil
}

//nolint:gocritic // no need for named return values
func generateContainerdRestartScript() ([]cabpkv1.File, string) {
return []cabpkv1.File{
{
Path: containerdRestartScriptOnRemote,
Content: string(containerdRestartScript),
Permissions: "0700",
},
}, containerdRestartScriptOnRemoteCommand
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
systemctl restart containerd

if ! command -v crictl; then
echo "Command crictl is not available, will not wait for Containerd to be running"
exit
fi

SECONDS=0
until crictl info; do
if ((SECONDS > 60)); then
echo "Containerd is not running. Giving up..."
exit 1
fi
echo "Containerd is not running yet. Waiting..."
sleep 5
done
24 changes: 20 additions & 4 deletions pkg/handlers/generic/mutation/mirrors/tests/generate_patches.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,17 @@ func TestGeneratePatches(
gomega.HaveKeyWithValue(
"path", "/etc/containerd/apply-patches.sh",
),
gomega.HaveKeyWithValue(
"path", "/etc/containerd/restart.sh",
),
),
},
{
Operation: "add",
Path: "/spec/template/spec/kubeadmConfigSpec/preKubeadmCommands",
ValueMatcher: gomega.ContainElement(
ValueMatcher: gomega.ContainElements(
"/bin/bash /etc/containerd/apply-patches.sh",
"/bin/bash /etc/containerd/restart.sh",
),
},
},
Expand Down Expand Up @@ -120,13 +124,17 @@ func TestGeneratePatches(
gomega.HaveKeyWithValue(
"path", "/etc/containerd/apply-patches.sh",
),
gomega.HaveKeyWithValue(
"path", "/etc/containerd/restart.sh",
),
),
},
{
Operation: "add",
Path: "/spec/template/spec/kubeadmConfigSpec/preKubeadmCommands",
ValueMatcher: gomega.ContainElement(
ValueMatcher: gomega.ContainElements(
"/bin/bash /etc/containerd/apply-patches.sh",
"/bin/bash /etc/containerd/restart.sh",
),
},
},
Expand Down Expand Up @@ -165,13 +173,17 @@ func TestGeneratePatches(
gomega.HaveKeyWithValue(
"path", "/etc/containerd/apply-patches.sh",
),
gomega.HaveKeyWithValue(
"path", "/etc/containerd/restart.sh",
),
),
},
{
Operation: "add",
Path: "/spec/template/spec/preKubeadmCommands",
ValueMatcher: gomega.ContainElement(
ValueMatcher: gomega.ContainElements(
"/bin/bash /etc/containerd/apply-patches.sh",
"/bin/bash /etc/containerd/restart.sh",
),
},
},
Expand Down Expand Up @@ -218,13 +230,17 @@ func TestGeneratePatches(
gomega.HaveKeyWithValue(
"path", "/etc/containerd/apply-patches.sh",
),
gomega.HaveKeyWithValue(
"path", "/etc/containerd/restart.sh",
),
),
},
{
Operation: "add",
Path: "/spec/template/spec/preKubeadmCommands",
ValueMatcher: gomega.ContainElement(
ValueMatcher: gomega.ContainElements(
"/bin/bash /etc/containerd/apply-patches.sh",
"/bin/bash /etc/containerd/restart.sh",
),
},
},
Expand Down
Loading