Skip to content

Commit da0085e

Browse files
dlipovetskyjimmidyson
authored andcommitted
refactor: Derive path of a patch file in a common package
1 parent 55c072e commit da0085e

File tree

7 files changed

+65
-28
lines changed

7 files changed

+65
-28
lines changed

pkg/common/containerd.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2024 Nutanix. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package common
5+
6+
import "path/filepath"
7+
8+
const (
9+
// ConfigDirOnRemote is the directory on the machine where we write CAREN configuration (e.g. scripts, patches
10+
// etc) as files.
11+
// These files are later applied by one or more commands that run on the machine.
12+
ConfigDirOnRemote = "/etc/caren"
13+
14+
// ContainerdScriptsDirOnRemote is the directory where we write scripts that relate to containerd as files.
15+
// It is a subdirectory of the root config directory.
16+
ContainerdScriptsDirOnRemote = ConfigDirOnRemote + "/containerd"
17+
18+
// ContainerdPatchDirOnRemote is the directory where we write containerd configuration patches as files.
19+
// It is a subdirectory of the containerd config directory.
20+
ContainerdPatchDirOnRemote = ConfigDirOnRemote + "/containerd/patches"
21+
)
22+
23+
// ConfigFilePathOnRemote returns the absolute path of a file that CAREN deploys onto the machine.
24+
func ConfigFilePathOnRemote(relativePath string) string {
25+
return filepath.Join(ConfigDirOnRemote, relativePath)
26+
}
27+
28+
// ContainerPathOnRemote returns the absolute path of a script that relates to containerd on the machine.
29+
func ContainerdScriptPathOnRemote(relativePath string) string {
30+
return filepath.Join(ContainerdScriptsDirOnRemote, relativePath)
31+
}
32+
33+
// ContainerdPatchPathOnRemote returns the absolute path of a containerd configuration patch on the machine.
34+
func ContainerdPatchPathOnRemote(relativePath string) string {
35+
return filepath.Join(ContainerdPatchDirOnRemote, relativePath)
36+
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ import (
99
"text/template"
1010

1111
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
12+
13+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/common"
1214
)
1315

1416
const (
15-
tomlMergeImage = "ghcr.io/mesosphere/toml-merge:v0.2.0"
16-
ContainerdPatchesDirOnRemote = "/etc/caren/containerd/patches"
17-
containerdApplyPatchesScriptOnRemote = "/etc/caren/containerd/apply-patches.sh"
17+
tomlMergeImage = "ghcr.io/mesosphere/toml-merge:v0.2.0"
18+
)
19+
20+
var (
21+
containerdApplyPatchesScriptOnRemote = common.ContainerdScriptPathOnRemote("apply-patches.sh")
1822
containerdApplyPatchesScriptOnRemoteCommand = "/bin/bash " + containerdApplyPatchesScriptOnRemote
1923
)
2024

@@ -32,7 +36,7 @@ func generateContainerdApplyPatchesScript() (bootstrapv1.File, string, error) {
3236
PatchDir string
3337
}{
3438
TOMLMergeImage: tomlMergeImage,
35-
PatchDir: ContainerdPatchesDirOnRemote,
39+
PatchDir: common.ContainerdPatchDirOnRemote,
3640
}
3741

3842
var b bytes.Buffer

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var _ = Describe("Generate Containerd apply patches and restart patches", func()
4242
"path", containerdApplyPatchesScriptOnRemote,
4343
),
4444
gomega.HaveKeyWithValue(
45-
"path", ContainerdRestartScriptOnRemote,
45+
"path", containerdRestartScriptOnRemote,
4646
),
4747
),
4848
},
@@ -51,7 +51,7 @@ var _ = Describe("Generate Containerd apply patches and restart patches", func()
5151
Path: "/spec/template/spec/kubeadmConfigSpec/preKubeadmCommands",
5252
ValueMatcher: gomega.HaveExactElements(
5353
containerdApplyPatchesScriptOnRemoteCommand,
54-
ContainerdRestartScriptOnRemoteCommand,
54+
containerdRestartScriptOnRemoteCommand,
5555
),
5656
},
5757
},
@@ -78,7 +78,7 @@ var _ = Describe("Generate Containerd apply patches and restart patches", func()
7878
"path", containerdApplyPatchesScriptOnRemote,
7979
),
8080
gomega.HaveKeyWithValue(
81-
"path", ContainerdRestartScriptOnRemote,
81+
"path", containerdRestartScriptOnRemote,
8282
),
8383
),
8484
},
@@ -87,7 +87,7 @@ var _ = Describe("Generate Containerd apply patches and restart patches", func()
8787
Path: "/spec/template/spec/preKubeadmCommands",
8888
ValueMatcher: gomega.HaveExactElements(
8989
containerdApplyPatchesScriptOnRemoteCommand,
90-
ContainerdRestartScriptOnRemoteCommand,
90+
containerdRestartScriptOnRemoteCommand,
9191
),
9292
},
9393
},

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ package containerdapplypatchesandrestart
55
import (
66
_ "embed"
77

8+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/common"
89
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
910
)
1011

11-
const (
12-
ContainerdRestartScriptOnRemote = "/etc/containerd/restart.sh"
13-
ContainerdRestartScriptOnRemoteCommand = "/bin/bash " + ContainerdRestartScriptOnRemote
12+
var (
13+
containerdRestartScriptOnRemote = common.ContainerdScriptPathOnRemote("restart.sh")
14+
containerdRestartScriptOnRemoteCommand = "/bin/bash " + containerdRestartScriptOnRemote
1415
)
1516

1617
//go:embed templates/containerd-restart.sh
@@ -19,9 +20,9 @@ var containerdRestartScript []byte
1920
//nolint:gocritic // no need for named return values
2021
func generateContainerdRestartScript() (bootstrapv1.File, string) {
2122
return bootstrapv1.File{
22-
Path: ContainerdRestartScriptOnRemote,
23+
Path: containerdRestartScriptOnRemote,
2324
Content: string(containerdRestartScript),
2425
Permissions: "0700",
2526
},
26-
ContainerdRestartScriptOnRemoteCommand
27+
containerdRestartScriptOnRemoteCommand
2728
}

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,16 @@ package containerdmetrics
44

55
import (
66
_ "embed"
7-
"path"
87

98
cabpkv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
10-
)
119

12-
const (
13-
// TODO Factor out this constant to a common package.
14-
containerdPatchesDirOnRemote = "/etc/caren/containerd"
10+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/common"
1511
)
1612

1713
var (
1814
//go:embed files/metrics-config.toml
1915
metricsConfigDropIn []byte
20-
metricsConfigDropInFileOnRemote = path.Join(
21-
containerdPatchesDirOnRemote,
22-
"metrics-config.toml",
23-
)
16+
metricsConfigDropInFileOnRemote = common.ContainerdPatchPathOnRemote("metrics-config.toml")
2417
)
2518

2619
func generateMetricsConfigDropIn() cabpkv1.File {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ import (
1010
"text/template"
1111

1212
cabpkv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
13+
14+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/common"
1315
)
1416

15-
const (
16-
//nolint:gosec // Does not contain hard coded credentials.
17-
installKubeletCredentialProvidersScriptOnRemote = "/etc/caren/install-kubelet-credential-providers.sh"
17+
var (
18+
installKubeletCredentialProvidersScriptOnRemote = common.ConfigFilePathOnRemote(
19+
"install-kubelet-credential-providers.sh")
1820

1921
installKubeletCredentialProvidersScriptOnRemoteCommand = "/bin/bash " + installKubeletCredentialProvidersScriptOnRemote
22+
)
2023

24+
const (
2125
//nolint:gosec // Does not contain hard coded credentials.
2226
dynamicCredentialProviderImage = "ghcr.io/mesosphere/dynamic-credential-provider:v0.5.0"
2327

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ 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"
20+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/common"
2121
)
2222

2323
const (
@@ -36,8 +36,7 @@ var (
3636

3737
//go:embed templates/containerd-registry-config-drop-in.toml
3838
containerdRegistryConfigDropIn []byte
39-
containerdRegistryConfigDropInFileOnRemote = path.Join(
40-
containerdapplypatchesandrestart.ContainerdPatchesDirOnRemote,
39+
containerdRegistryConfigDropInFileOnRemote = common.ContainerdPatchPathOnRemote(
4140
"registry-config.toml",
4241
)
4342
)

0 commit comments

Comments
 (0)