Skip to content

Commit

Permalink
refactor: Derive path of a patch file in a common package
Browse files Browse the repository at this point in the history
  • Loading branch information
dlipovetsky authored and jimmidyson committed May 14, 2024
1 parent 55c072e commit da0085e
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 28 deletions.
36 changes: 36 additions & 0 deletions pkg/common/containerd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 Nutanix. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package common

import "path/filepath"

const (
// ConfigDirOnRemote is the directory on the machine where we write CAREN configuration (e.g. scripts, patches
// etc) as files.
// These files are later applied by one or more commands that run on the machine.
ConfigDirOnRemote = "/etc/caren"

// ContainerdScriptsDirOnRemote is the directory where we write scripts that relate to containerd as files.
// It is a subdirectory of the root config directory.
ContainerdScriptsDirOnRemote = ConfigDirOnRemote + "/containerd"

// ContainerdPatchDirOnRemote is the directory where we write containerd configuration patches as files.
// It is a subdirectory of the containerd config directory.
ContainerdPatchDirOnRemote = ConfigDirOnRemote + "/containerd/patches"
)

// ConfigFilePathOnRemote returns the absolute path of a file that CAREN deploys onto the machine.
func ConfigFilePathOnRemote(relativePath string) string {
return filepath.Join(ConfigDirOnRemote, relativePath)
}

// ContainerPathOnRemote returns the absolute path of a script that relates to containerd on the machine.
func ContainerdScriptPathOnRemote(relativePath string) string {
return filepath.Join(ContainerdScriptsDirOnRemote, relativePath)
}

// ContainerdPatchPathOnRemote returns the absolute path of a containerd configuration patch on the machine.
func ContainerdPatchPathOnRemote(relativePath string) string {
return filepath.Join(ContainerdPatchDirOnRemote, relativePath)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import (
"text/template"

bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"

"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/common"
)

const (
tomlMergeImage = "ghcr.io/mesosphere/toml-merge:v0.2.0"
ContainerdPatchesDirOnRemote = "/etc/caren/containerd/patches"
containerdApplyPatchesScriptOnRemote = "/etc/caren/containerd/apply-patches.sh"
tomlMergeImage = "ghcr.io/mesosphere/toml-merge:v0.2.0"
)

var (
containerdApplyPatchesScriptOnRemote = common.ContainerdScriptPathOnRemote("apply-patches.sh")
containerdApplyPatchesScriptOnRemoteCommand = "/bin/bash " + containerdApplyPatchesScriptOnRemote
)

Expand All @@ -32,7 +36,7 @@ func generateContainerdApplyPatchesScript() (bootstrapv1.File, string, error) {
PatchDir string
}{
TOMLMergeImage: tomlMergeImage,
PatchDir: ContainerdPatchesDirOnRemote,
PatchDir: common.ContainerdPatchDirOnRemote,
}

var b bytes.Buffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var _ = Describe("Generate Containerd apply patches and restart patches", func()
"path", containerdApplyPatchesScriptOnRemote,
),
gomega.HaveKeyWithValue(
"path", ContainerdRestartScriptOnRemote,
"path", containerdRestartScriptOnRemote,
),
),
},
Expand All @@ -51,7 +51,7 @@ var _ = Describe("Generate Containerd apply patches and restart patches", func()
Path: "/spec/template/spec/kubeadmConfigSpec/preKubeadmCommands",
ValueMatcher: gomega.HaveExactElements(
containerdApplyPatchesScriptOnRemoteCommand,
ContainerdRestartScriptOnRemoteCommand,
containerdRestartScriptOnRemoteCommand,
),
},
},
Expand All @@ -78,7 +78,7 @@ var _ = Describe("Generate Containerd apply patches and restart patches", func()
"path", containerdApplyPatchesScriptOnRemote,
),
gomega.HaveKeyWithValue(
"path", ContainerdRestartScriptOnRemote,
"path", containerdRestartScriptOnRemote,
),
),
},
Expand All @@ -87,7 +87,7 @@ var _ = Describe("Generate Containerd apply patches and restart patches", func()
Path: "/spec/template/spec/preKubeadmCommands",
ValueMatcher: gomega.HaveExactElements(
containerdApplyPatchesScriptOnRemoteCommand,
ContainerdRestartScriptOnRemoteCommand,
containerdRestartScriptOnRemoteCommand,
),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ package containerdapplypatchesandrestart
import (
_ "embed"

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

const (
ContainerdRestartScriptOnRemote = "/etc/containerd/restart.sh"
ContainerdRestartScriptOnRemoteCommand = "/bin/bash " + ContainerdRestartScriptOnRemote
var (
containerdRestartScriptOnRemote = common.ContainerdScriptPathOnRemote("restart.sh")
containerdRestartScriptOnRemoteCommand = "/bin/bash " + containerdRestartScriptOnRemote
)

//go:embed templates/containerd-restart.sh
Expand All @@ -19,9 +20,9 @@ var containerdRestartScript []byte
//nolint:gocritic // no need for named return values
func generateContainerdRestartScript() (bootstrapv1.File, string) {
return bootstrapv1.File{
Path: ContainerdRestartScriptOnRemote,
Path: containerdRestartScriptOnRemote,
Content: string(containerdRestartScript),
Permissions: "0700",
},
ContainerdRestartScriptOnRemoteCommand
containerdRestartScriptOnRemoteCommand
}
11 changes: 2 additions & 9 deletions pkg/handlers/generic/mutation/containerdmetrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,16 @@ package containerdmetrics

import (
_ "embed"
"path"

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

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

var (
//go:embed files/metrics-config.toml
metricsConfigDropIn []byte
metricsConfigDropInFileOnRemote = path.Join(
containerdPatchesDirOnRemote,
"metrics-config.toml",
)
metricsConfigDropInFileOnRemote = common.ContainerdPatchPathOnRemote("metrics-config.toml")
)

func generateMetricsConfigDropIn() cabpkv1.File {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ import (
"text/template"

cabpkv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"

"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/common"
)

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

installKubeletCredentialProvidersScriptOnRemoteCommand = "/bin/bash " + installKubeletCredentialProvidersScriptOnRemote
)

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

Expand Down
5 changes: 2 additions & 3 deletions pkg/handlers/generic/mutation/mirrors/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"

"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/containerdapplypatchesandrestart"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/common"
)

const (
Expand All @@ -36,8 +36,7 @@ var (

//go:embed templates/containerd-registry-config-drop-in.toml
containerdRegistryConfigDropIn []byte
containerdRegistryConfigDropInFileOnRemote = path.Join(
containerdapplypatchesandrestart.ContainerdPatchesDirOnRemote,
containerdRegistryConfigDropInFileOnRemote = common.ContainerdPatchPathOnRemote(
"registry-config.toml",
)
)
Expand Down

0 comments on commit da0085e

Please sign in to comment.