Skip to content

Commit 55c072e

Browse files
dlipovetskyjimmidyson
authored andcommitted
refactor: Write configuration under /etc/caren
Previously, we wrote some configuration to /etc/cre. Now that the project name is CAREN, we should use /etc/caren. We also wrote containerd configuration to /etc/containerd/cre.d, but this configuration is not read by containerd directly. Instead, it is read by a script that merges it to the primary containerd configuration. For that reason, this configuration belongs under /etc/caren.
1 parent 2deeee9 commit 55c072e

File tree

9 files changed

+66
-67
lines changed

9 files changed

+66
-67
lines changed

pkg/handlers/generic/mutation/containerdapplypatchesandrestart/apply_patches.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,47 @@
33
package containerdapplypatchesandrestart
44

55
import (
6+
"bytes"
67
_ "embed"
8+
"fmt"
9+
"text/template"
710

811
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
912
)
1013

1114
const (
12-
ContainerdRestartScriptOnRemote = "/etc/containerd/restart.sh"
13-
ContainerdRestartScriptOnRemoteCommand = "/bin/bash " + ContainerdRestartScriptOnRemote
15+
tomlMergeImage = "ghcr.io/mesosphere/toml-merge:v0.2.0"
16+
ContainerdPatchesDirOnRemote = "/etc/caren/containerd/patches"
17+
containerdApplyPatchesScriptOnRemote = "/etc/caren/containerd/apply-patches.sh"
18+
containerdApplyPatchesScriptOnRemoteCommand = "/bin/bash " + containerdApplyPatchesScriptOnRemote
1419
)
1520

16-
//go:embed templates/containerd-restart.sh
17-
var containerdRestartScript []byte
21+
//go:embed templates/containerd-apply-patches.sh.gotmpl
22+
var containerdApplyConfigPatchesScript []byte
23+
24+
func generateContainerdApplyPatchesScript() (bootstrapv1.File, string, error) {
25+
t, err := template.New("").Parse(string(containerdApplyConfigPatchesScript))
26+
if err != nil {
27+
return bootstrapv1.File{}, "", fmt.Errorf("failed to parse go template: %w", err)
28+
}
29+
30+
templateInput := struct {
31+
TOMLMergeImage string
32+
PatchDir string
33+
}{
34+
TOMLMergeImage: tomlMergeImage,
35+
PatchDir: ContainerdPatchesDirOnRemote,
36+
}
37+
38+
var b bytes.Buffer
39+
err = t.Execute(&b, templateInput)
40+
if err != nil {
41+
return bootstrapv1.File{}, "", fmt.Errorf("failed executing template: %w", err)
42+
}
1843

19-
//nolint:gocritic // no need for named return values
20-
func generateContainerdRestartScript() (bootstrapv1.File, string) {
2144
return bootstrapv1.File{
22-
Path: ContainerdRestartScriptOnRemote,
23-
Content: string(containerdRestartScript),
24-
Permissions: "0700",
25-
},
26-
ContainerdRestartScriptOnRemoteCommand
45+
Path: containerdApplyPatchesScriptOnRemote,
46+
Content: b.String(),
47+
Permissions: "0700",
48+
}, containerdApplyPatchesScriptOnRemoteCommand, nil
2749
}

pkg/handlers/generic/mutation/containerdapplypatchesandrestart/inject_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ var _ = Describe("Generate Containerd apply patches and restart patches", func()
109109

110110
func Test_generateContainerdApplyPatchesScript(t *testing.T) {
111111
wantFile := bootstrapv1.File{
112-
Path: "/etc/containerd/apply-patches.sh",
112+
Path: "/etc/caren/containerd/apply-patches.sh",
113113
Owner: "",
114114
Permissions: "0700",
115115
Encoding: "",
@@ -126,7 +126,7 @@ IFS=$'\n\t'
126126
# using -e does not work with globs.
127127
# See https://github.com/koalaman/shellcheck/wiki/SC2144 for an explanation of the following loop.
128128
patches_exist=false
129-
for file in "/etc/containerd/cre.d"/*.toml; do
129+
for file in "/etc/caren/containerd/patches"/*.toml; do
130130
if [ -e "${file}" ]; then
131131
patches_exist=true
132132
fi
@@ -135,7 +135,7 @@ for file in "/etc/containerd/cre.d"/*.toml; do
135135
done
136136
137137
if [ "${patches_exist}" = false ]; then
138-
echo "No TOML files found in the patch directory: /etc/containerd/cre.d - nothing to do"
138+
echo "No TOML files found in the patch directory: /etc/caren/containerd/patches - nothing to do"
139139
exit 0
140140
fi
141141
@@ -158,10 +158,10 @@ readonly tmp_ctr_mount_dir="$(mktemp -d)"
158158
159159
# Mount the toml-merge image filesystem and run the toml-merge binary to merge the TOML files.
160160
ctr --namespace k8s.io images mount "${TOML_MERGE_IMAGE}" "${tmp_ctr_mount_dir}"
161-
"${tmp_ctr_mount_dir}/usr/local/bin/toml-merge" -i --patch-file "/etc/containerd/cre.d/*.toml" /etc/containerd/config.toml
161+
"${tmp_ctr_mount_dir}/usr/local/bin/toml-merge" -i --patch-file "/etc/caren/containerd/patches/*.toml" /etc/containerd/config.toml
162162
`,
163163
}
164-
wantCmd := "/bin/bash /etc/containerd/apply-patches.sh"
164+
wantCmd := "/bin/bash /etc/caren/containerd/apply-patches.sh"
165165
file, cmd, _ := generateContainerdApplyPatchesScript()
166166
assert.Equal(t, wantFile, file)
167167
assert.Equal(t, wantCmd, cmd)

pkg/handlers/generic/mutation/containerdapplypatchesandrestart/restart.go

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,25 @@
33
package containerdapplypatchesandrestart
44

55
import (
6-
"bytes"
76
_ "embed"
8-
"fmt"
9-
"text/template"
107

118
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
129
)
1310

1411
const (
15-
tomlMergeImage = "ghcr.io/mesosphere/toml-merge:v0.2.0"
16-
containerdPatchesDirOnRemote = "/etc/containerd/cre.d"
17-
containerdApplyPatchesScriptOnRemote = "/etc/containerd/apply-patches.sh"
18-
containerdApplyPatchesScriptOnRemoteCommand = "/bin/bash " + containerdApplyPatchesScriptOnRemote
12+
ContainerdRestartScriptOnRemote = "/etc/containerd/restart.sh"
13+
ContainerdRestartScriptOnRemoteCommand = "/bin/bash " + ContainerdRestartScriptOnRemote
1914
)
2015

21-
//go:embed templates/containerd-apply-patches.sh.gotmpl
22-
var containerdApplyConfigPatchesScript []byte
23-
24-
func generateContainerdApplyPatchesScript() (bootstrapv1.File, string, error) {
25-
t, err := template.New("").Parse(string(containerdApplyConfigPatchesScript))
26-
if err != nil {
27-
return bootstrapv1.File{}, "", fmt.Errorf("failed to parse go template: %w", err)
28-
}
29-
30-
templateInput := struct {
31-
TOMLMergeImage string
32-
PatchDir string
33-
}{
34-
TOMLMergeImage: tomlMergeImage,
35-
PatchDir: containerdPatchesDirOnRemote,
36-
}
37-
38-
var b bytes.Buffer
39-
err = t.Execute(&b, templateInput)
40-
if err != nil {
41-
return bootstrapv1.File{}, "", fmt.Errorf("failed executing template: %w", err)
42-
}
16+
//go:embed templates/containerd-restart.sh
17+
var containerdRestartScript []byte
4318

19+
//nolint:gocritic // no need for named return values
20+
func generateContainerdRestartScript() (bootstrapv1.File, string) {
4421
return bootstrapv1.File{
45-
Path: containerdApplyPatchesScriptOnRemote,
46-
Content: b.String(),
47-
Permissions: "0700",
48-
}, containerdApplyPatchesScriptOnRemoteCommand, nil
22+
Path: ContainerdRestartScriptOnRemote,
23+
Content: string(containerdRestartScript),
24+
Permissions: "0700",
25+
},
26+
ContainerdRestartScriptOnRemoteCommand
4927
}

pkg/handlers/generic/mutation/containerdmetrics/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
const (
1313
// TODO Factor out this constant to a common package.
14-
containerdPatchesDirOnRemote = "/etc/containerd/cre.d"
14+
containerdPatchesDirOnRemote = "/etc/caren/containerd"
1515
)
1616

1717
var (

pkg/handlers/generic/mutation/imageregistries/credentials/credential_provider_install_files.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
const (
1616
//nolint:gosec // Does not contain hard coded credentials.
17-
installKubeletCredentialProvidersScriptOnRemote = "/etc/cre/install-kubelet-credential-providers.sh"
17+
installKubeletCredentialProvidersScriptOnRemote = "/etc/caren/install-kubelet-credential-providers.sh"
1818

1919
installKubeletCredentialProvidersScriptOnRemoteCommand = "/bin/bash " + installKubeletCredentialProvidersScriptOnRemote
2020

pkg/handlers/generic/mutation/imageregistries/credentials/inject_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ var _ = Describe("Generate Image registry patches", func() {
161161
Path: "/spec/template/spec/kubeadmConfigSpec/files",
162162
ValueMatcher: gomega.ContainElements(
163163
gomega.HaveKeyWithValue(
164-
"path", "/etc/cre/install-kubelet-credential-providers.sh",
164+
"path", "/etc/caren/install-kubelet-credential-providers.sh",
165165
),
166166
gomega.HaveKeyWithValue(
167167
"path", "/etc/kubernetes/image-credential-provider-config.yaml",
@@ -175,7 +175,7 @@ var _ = Describe("Generate Image registry patches", func() {
175175
Operation: "add",
176176
Path: "/spec/template/spec/kubeadmConfigSpec/preKubeadmCommands",
177177
ValueMatcher: gomega.ContainElement(
178-
"/bin/bash /etc/cre/install-kubelet-credential-providers.sh",
178+
"/bin/bash /etc/caren/install-kubelet-credential-providers.sh",
179179
),
180180
},
181181
{
@@ -222,7 +222,7 @@ var _ = Describe("Generate Image registry patches", func() {
222222
Path: "/spec/template/spec/kubeadmConfigSpec/files",
223223
ValueMatcher: gomega.ContainElements(
224224
gomega.HaveKeyWithValue(
225-
"path", "/etc/cre/install-kubelet-credential-providers.sh",
225+
"path", "/etc/caren/install-kubelet-credential-providers.sh",
226226
),
227227
gomega.HaveKeyWithValue(
228228
"path", "/etc/kubernetes/image-credential-provider-config.yaml",
@@ -239,7 +239,7 @@ var _ = Describe("Generate Image registry patches", func() {
239239
Operation: "add",
240240
Path: "/spec/template/spec/kubeadmConfigSpec/preKubeadmCommands",
241241
ValueMatcher: gomega.ContainElement(
242-
"/bin/bash /etc/cre/install-kubelet-credential-providers.sh",
242+
"/bin/bash /etc/caren/install-kubelet-credential-providers.sh",
243243
),
244244
},
245245
{
@@ -286,7 +286,7 @@ var _ = Describe("Generate Image registry patches", func() {
286286
Path: "/spec/template/spec/files",
287287
ValueMatcher: gomega.ContainElements(
288288
gomega.HaveKeyWithValue(
289-
"path", "/etc/cre/install-kubelet-credential-providers.sh",
289+
"path", "/etc/caren/install-kubelet-credential-providers.sh",
290290
),
291291
gomega.HaveKeyWithValue(
292292
"path", "/etc/kubernetes/image-credential-provider-config.yaml",
@@ -300,7 +300,7 @@ var _ = Describe("Generate Image registry patches", func() {
300300
Operation: "add",
301301
Path: "/spec/template/spec/preKubeadmCommands",
302302
ValueMatcher: gomega.ContainElement(
303-
"/bin/bash /etc/cre/install-kubelet-credential-providers.sh",
303+
"/bin/bash /etc/caren/install-kubelet-credential-providers.sh",
304304
),
305305
},
306306
{
@@ -344,7 +344,7 @@ var _ = Describe("Generate Image registry patches", func() {
344344
Path: "/spec/template/spec/files",
345345
ValueMatcher: gomega.ContainElements(
346346
gomega.HaveKeyWithValue(
347-
"path", "/etc/cre/install-kubelet-credential-providers.sh",
347+
"path", "/etc/caren/install-kubelet-credential-providers.sh",
348348
),
349349
gomega.HaveKeyWithValue(
350350
"path", "/etc/kubernetes/image-credential-provider-config.yaml",
@@ -361,7 +361,7 @@ var _ = Describe("Generate Image registry patches", func() {
361361
Operation: "add",
362362
Path: "/spec/template/spec/preKubeadmCommands",
363363
ValueMatcher: gomega.ContainElement(
364-
"/bin/bash /etc/cre/install-kubelet-credential-providers.sh",
364+
"/bin/bash /etc/caren/install-kubelet-credential-providers.sh",
365365
),
366366
},
367367
{

pkg/handlers/generic/mutation/mirrors/inject_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ var _ = Describe("Generate Global mirror patches", func() {
7070
"path", "/etc/containerd/certs.d/_default/hosts.toml",
7171
),
7272
gomega.HaveKeyWithValue(
73-
"path", "/etc/containerd/cre.d/registry-config.toml",
73+
"path", "/etc/caren/containerd/patches/registry-config.toml",
7474
),
7575
),
7676
},
@@ -105,7 +105,7 @@ var _ = Describe("Generate Global mirror patches", func() {
105105
"path", "/etc/certs/mirror.pem",
106106
),
107107
gomega.HaveKeyWithValue(
108-
"path", "/etc/containerd/cre.d/registry-config.toml",
108+
"path", "/etc/caren/containerd/patches/registry-config.toml",
109109
),
110110
),
111111
},
@@ -140,7 +140,7 @@ var _ = Describe("Generate Global mirror patches", func() {
140140
"path", "/etc/containerd/certs.d/_default/hosts.toml",
141141
),
142142
gomega.HaveKeyWithValue(
143-
"path", "/etc/containerd/cre.d/registry-config.toml",
143+
"path", "/etc/caren/containerd/patches/registry-config.toml",
144144
),
145145
),
146146
},
@@ -183,7 +183,7 @@ var _ = Describe("Generate Global mirror patches", func() {
183183
"path", "/etc/certs/mirror.pem",
184184
),
185185
gomega.HaveKeyWithValue(
186-
"path", "/etc/containerd/cre.d/registry-config.toml",
186+
"path", "/etc/caren/containerd/patches/registry-config.toml",
187187
),
188188
),
189189
},

pkg/handlers/generic/mutation/mirrors/mirror.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ import (
1717
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1818

1919
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
20+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/containerdapplypatchesandrestart"
2021
)
2122

2223
const (
2324
mirrorCACertPathOnRemote = "/etc/certs/mirror.pem"
2425
defaultRegistryMirrorConfigPathOnRemote = "/etc/containerd/certs.d/_default/hosts.toml"
2526
secretKeyForMirrorCACert = "ca.crt"
26-
27-
containerdPatchesDirOnRemote = "/etc/containerd/cre.d"
2827
)
2928

3029
var (
@@ -38,7 +37,7 @@ var (
3837
//go:embed templates/containerd-registry-config-drop-in.toml
3938
containerdRegistryConfigDropIn []byte
4039
containerdRegistryConfigDropInFileOnRemote = path.Join(
41-
containerdPatchesDirOnRemote,
40+
containerdapplypatchesandrestart.ContainerdPatchesDirOnRemote,
4241
"registry-config.toml",
4342
)
4443
)

pkg/handlers/generic/mutation/mirrors/mirror_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func Test_generateMirrorCACertFile(t *testing.T) {
161161
func Test_generateContainerdRegistryConfigDropInFile(t *testing.T) {
162162
want := []cabpkv1.File{
163163
{
164-
Path: "/etc/containerd/cre.d/registry-config.toml",
164+
Path: "/etc/caren/containerd/patches/registry-config.toml",
165165
Owner: "",
166166
Permissions: "0600",
167167
Encoding: "",

0 commit comments

Comments
 (0)