Implement temp file cleanup for chartify and helmfile temp files
Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com>
This commit is contained in:
parent
3ce5abcc4f
commit
53ee477e20
|
|
@ -68,6 +68,14 @@ func NewRootCmd(globalConfig *config.GlobalOptions) (*cobra.Command, error) {
|
|||
}
|
||||
logger = helmexec.NewLogger(logOut, logLevel)
|
||||
globalConfig.SetLogger(logger)
|
||||
|
||||
// Clean up old temporary files if enabled
|
||||
if globalConfig.CleanupTempFiles {
|
||||
if err := app.CleanupTempFiles(logger); err != nil {
|
||||
logger.Warnf("Failed to cleanup temporary files: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
|
@ -141,6 +149,7 @@ The name of a release can be used as a label: "--selector name=myrelease"`)
|
|||
fs.BoolVar(&globalOptions.EnableLiveOutput, "enable-live-output", globalOptions.EnableLiveOutput, `Show live output from the Helm binary Stdout/Stderr into Helmfile own Stdout/Stderr.
|
||||
It only applies for the Helm CLI commands, Stdout/Stderr for Hooks are still displayed only when it's execution finishes.`)
|
||||
fs.BoolVarP(&globalOptions.Interactive, "interactive", "i", false, "Request confirmation before attempting to modify clusters")
|
||||
fs.BoolVar(&globalOptions.CleanupTempFiles, "cleanup-temp-files", true, "Clean up temporary files and directories from previous runs on startup")
|
||||
// avoid 'pflag: help requested' error (#251)
|
||||
fs.BoolP("help", "h", false, "help for helmfile")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2270,3 +2270,37 @@ func GetArgs(args string, state *state.HelmState) []string {
|
|||
|
||||
return state.HelmDefaults.Args
|
||||
}
|
||||
|
||||
// CleanupTempFiles removes leftover temporary files and directories from previous runs
|
||||
func CleanupTempFiles(logger *zap.SugaredLogger) error {
|
||||
if logger == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
tmpDir := os.TempDir()
|
||||
entries, err := os.ReadDir(tmpDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("reading temp directory %s: %v", tmpDir, err)
|
||||
}
|
||||
|
||||
var cleanupCount int
|
||||
for _, entry := range entries {
|
||||
name := entry.Name()
|
||||
// Clean up old helmfile and chartify temporary files/directories
|
||||
if strings.HasPrefix(name, "helmfile") || strings.HasPrefix(name, "chartify") {
|
||||
fullPath := filepath.Join(tmpDir, name)
|
||||
if err := os.RemoveAll(fullPath); err != nil {
|
||||
logger.Warnf("Failed to remove temporary file/directory %s: %v", fullPath, err)
|
||||
} else {
|
||||
logger.Debugf("Cleaned up temporary file/directory: %s", fullPath)
|
||||
cleanupCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if cleanupCount > 0 {
|
||||
logger.Debugf("Cleaned up %d temporary files/directories", cleanupCount)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -70,6 +70,8 @@ type GlobalOptions struct {
|
|||
Args string
|
||||
// LogOutput is the writer to use for writing logs.
|
||||
LogOutput io.Writer
|
||||
// CleanupTempFiles enables cleanup of old temporary files on startup.
|
||||
CleanupTempFiles bool
|
||||
}
|
||||
|
||||
// Logger returns the logger to use.
|
||||
|
|
|
|||
|
|
@ -251,6 +251,7 @@ func (st *HelmState) appendShowOnlyFlags(flags []string, showOnly []string) []st
|
|||
type Chartify struct {
|
||||
Opts *chartify.ChartifyOpts
|
||||
Clean func()
|
||||
AddToCleanup func(string) // Add function to track additional files/dirs for cleanup
|
||||
}
|
||||
|
||||
func (st *HelmState) downloadChartWithGoGetter(r *ReleaseSpec) (string, error) {
|
||||
|
|
@ -312,6 +313,13 @@ func (st *HelmState) PrepareChartify(helm helmexec.Interface, release *ReleaseSp
|
|||
st.removeFiles(filesNeedCleaning)
|
||||
}
|
||||
|
||||
// Add function to track additional files/directories for cleanup
|
||||
c.AddToCleanup = func(path string) {
|
||||
filesNeedCleaning = append(filesNeedCleaning, path)
|
||||
}
|
||||
|
||||
c.Clean = clean
|
||||
|
||||
var shouldRun bool
|
||||
|
||||
dir := chart
|
||||
|
|
|
|||
|
|
@ -1342,6 +1342,10 @@ func (st *HelmState) PrepareCharts(helm helmexec.Interface, dir string, concurre
|
|||
return
|
||||
} else {
|
||||
chartPath = out
|
||||
// Track the chartify output directory for cleanup
|
||||
if chartification != nil && chartification.AddToCleanup != nil {
|
||||
chartification.AddToCleanup(out)
|
||||
}
|
||||
}
|
||||
|
||||
// Skip `helm dep build` and `helm dep up` altogether when the chart is from remote or the dep is
|
||||
|
|
|
|||
Loading…
Reference in New Issue