Merge pull request #112172 from chendave/remove_tmp

kubeadm: Add the option to cleanup the `tmp` directory
This commit is contained in:
Kubernetes Prow Robot 2022-09-02 02:54:05 -07:00 committed by GitHub
commit 1913c6be35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 1 deletions

View File

@ -148,4 +148,7 @@ const (
// Print the addon manifests to STDOUT instead of installing them. // Print the addon manifests to STDOUT instead of installing them.
PrintManifest = "print-manifest" PrintManifest = "print-manifest"
// CleanupTmpDir flag indicates whether reset will cleanup the tmp dir
CleanupTmpDir = "cleanup-tmp-dir"
) )

View File

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path"
"path/filepath" "path/filepath"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -47,6 +48,7 @@ func NewCleanupNodePhase() workflow.Phase {
InheritFlags: []string{ InheritFlags: []string{
options.CertificatesDir, options.CertificatesDir,
options.NodeCRISocket, options.NodeCRISocket,
options.CleanupTmpDir,
}, },
} }
} }
@ -103,7 +105,12 @@ func runCleanupNode(c workflow.RunData) error {
if certsDir != kubeadmapiv1.DefaultCertificatesDir { if certsDir != kubeadmapiv1.DefaultCertificatesDir {
klog.Warningf("[reset] WARNING: Cleaning a non-default certificates directory: %q\n", certsDir) klog.Warningf("[reset] WARNING: Cleaning a non-default certificates directory: %q\n", certsDir)
} }
dirsToClean = append(dirsToClean, certsDir) dirsToClean = append(dirsToClean, certsDir)
if r.CleanupTmpDir() {
tempDir := path.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.TempDirForKubeadm)
dirsToClean = append(dirsToClean, tempDir)
}
resetConfigDir(kubeadmconstants.KubernetesDir, dirsToClean, r.DryRun()) resetConfigDir(kubeadmconstants.KubernetesDir, dirsToClean, r.DryRun())
if r.Cfg() != nil && features.Enabled(r.Cfg().FeatureGates, features.RootlessControlPlane) { if r.Cfg() != nil && features.Enabled(r.Cfg().FeatureGates, features.RootlessControlPlane) {

View File

@ -142,6 +142,17 @@ func TestConfigDirCleaner(t *testing.T) {
"test-path", "test-path",
}, },
}, },
"cleanup temp directory": {
setupDirs: []string{
"tmp",
},
setupFiles: []string{
"tmp/kubeadm-init-dryrun2845575027",
},
verifyExists: []string{
"tmp",
},
},
} }
for name, test := range tests { for name, test := range tests {
@ -176,6 +187,7 @@ func TestConfigDirCleaner(t *testing.T) {
dirsToClean := []string{ dirsToClean := []string{
filepath.Join(tmpDir, test.resetDir), filepath.Join(tmpDir, test.resetDir),
filepath.Join(tmpDir, kubeadmconstants.ManifestsSubDirName), filepath.Join(tmpDir, kubeadmconstants.ManifestsSubDirName),
filepath.Join(tmpDir, kubeadmconstants.TempDirForKubeadm),
} }
resetConfigDir(tmpDir, dirsToClean, false) resetConfigDir(tmpDir, dirsToClean, false)
@ -185,6 +197,7 @@ func TestConfigDirCleaner(t *testing.T) {
assertNotExists(t, filepath.Join(tmpDir, kubeadmconstants.KubeletKubeConfigFileName)) assertNotExists(t, filepath.Join(tmpDir, kubeadmconstants.KubeletKubeConfigFileName))
assertDirEmpty(t, filepath.Join(tmpDir, "manifests")) assertDirEmpty(t, filepath.Join(tmpDir, "manifests"))
assertDirEmpty(t, filepath.Join(tmpDir, "pki")) assertDirEmpty(t, filepath.Join(tmpDir, "pki"))
assertDirEmpty(t, filepath.Join(tmpDir, "tmp"))
// Verify the files as requested by the test: // Verify the files as requested by the test:
for _, path := range test.verifyExists { for _, path := range test.verifyExists {

View File

@ -36,4 +36,5 @@ type resetData interface {
Client() clientset.Interface Client() clientset.Interface
CertificatesDir() string CertificatesDir() string
CRISocketPath() string CRISocketPath() string
CleanupTmpDir() bool
} }

View File

@ -39,3 +39,4 @@ func (t *testData) DryRun() bool { return false }
func (t *testData) Client() clientset.Interface { return nil } func (t *testData) Client() clientset.Interface { return nil }
func (t *testData) CertificatesDir() string { return "" } func (t *testData) CertificatesDir() string { return "" }
func (t *testData) CRISocketPath() string { return "" } func (t *testData) CRISocketPath() string { return "" }
func (t *testData) CleanupTmpDir() bool { return false }

View File

@ -19,6 +19,7 @@ package cmd
import ( import (
"fmt" "fmt"
"io" "io"
"path"
"github.com/lithammer/dedent" "github.com/lithammer/dedent"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -65,6 +66,7 @@ type resetOptions struct {
ignorePreflightErrors []string ignorePreflightErrors []string
kubeconfigPath string kubeconfigPath string
dryRun bool dryRun bool
cleanupTmpDir bool
} }
// resetData defines all the runtime information used when running the kubeadm reset workflow; // resetData defines all the runtime information used when running the kubeadm reset workflow;
@ -79,6 +81,7 @@ type resetData struct {
outputWriter io.Writer outputWriter io.Writer
cfg *kubeadmapi.InitConfiguration cfg *kubeadmapi.InitConfiguration
dryRun bool dryRun bool
cleanupTmpDir bool
} }
// newResetOptions returns a struct ready for being used for creating cmd join flags. // newResetOptions returns a struct ready for being used for creating cmd join flags.
@ -87,6 +90,7 @@ func newResetOptions() *resetOptions {
certificatesDir: kubeadmapiv1.DefaultCertificatesDir, certificatesDir: kubeadmapiv1.DefaultCertificatesDir,
forceReset: false, forceReset: false,
kubeconfigPath: kubeadmconstants.GetAdminKubeConfigPath(), kubeconfigPath: kubeadmconstants.GetAdminKubeConfigPath(),
cleanupTmpDir: false,
} }
} }
@ -136,6 +140,7 @@ func newResetData(cmd *cobra.Command, options *resetOptions, in io.Reader, out i
outputWriter: out, outputWriter: out,
cfg: cfg, cfg: cfg,
dryRun: options.dryRun, dryRun: options.dryRun,
cleanupTmpDir: options.cleanupTmpDir,
}, nil }, nil
} }
@ -160,6 +165,10 @@ func AddResetFlags(flagSet *flag.FlagSet, resetOptions *resetOptions) {
&resetOptions.dryRun, options.DryRun, resetOptions.dryRun, &resetOptions.dryRun, options.DryRun, resetOptions.dryRun,
"Don't apply any changes; just output what would be done.", "Don't apply any changes; just output what would be done.",
) )
flagSet.BoolVar(
&resetOptions.cleanupTmpDir, options.CleanupTmpDir, resetOptions.cleanupTmpDir,
fmt.Sprintf("Cleanup the %q directory", path.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.TempDirForKubeadm)),
)
options.AddKubeConfigFlag(flagSet, &resetOptions.kubeconfigPath) options.AddKubeConfigFlag(flagSet, &resetOptions.kubeconfigPath)
options.AddIgnorePreflightErrorsFlag(flagSet, &resetOptions.ignorePreflightErrors) options.AddIgnorePreflightErrorsFlag(flagSet, &resetOptions.ignorePreflightErrors)
@ -215,11 +224,16 @@ func (r *resetData) Cfg() *kubeadmapi.InitConfiguration {
return r.cfg return r.cfg
} }
// DryRun returns the DryRun flag. // DryRun returns the dryRun flag.
func (r *resetData) DryRun() bool { func (r *resetData) DryRun() bool {
return r.dryRun return r.dryRun
} }
// CleanupTmpDir returns the cleanupTmpDir flag.
func (r *resetData) CleanupTmpDir() bool {
return r.cleanupTmpDir
}
// CertificatesDir returns the CertificatesDir. // CertificatesDir returns the CertificatesDir.
func (r *resetData) CertificatesDir() string { func (r *resetData) CertificatesDir() string {
return r.certificatesDir return r.certificatesDir