Fix chartify temp directory cleanup - track output directories for proper cleanup
Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com>
This commit is contained in:
parent
53ee477e20
commit
e06c185667
|
|
@ -68,14 +68,6 @@ func NewRootCmd(globalConfig *config.GlobalOptions) (*cobra.Command, error) {
|
||||||
}
|
}
|
||||||
logger = helmexec.NewLogger(logOut, logLevel)
|
logger = helmexec.NewLogger(logOut, logLevel)
|
||||||
globalConfig.SetLogger(logger)
|
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
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
@ -149,7 +141,6 @@ 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.
|
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.`)
|
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.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)
|
// avoid 'pflag: help requested' error (#251)
|
||||||
fs.BoolP("help", "h", false, "help for helmfile")
|
fs.BoolP("help", "h", false, "help for helmfile")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2270,37 +2270,3 @@ func GetArgs(args string, state *state.HelmState) []string {
|
||||||
|
|
||||||
return state.HelmDefaults.Args
|
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,8 +70,6 @@ type GlobalOptions struct {
|
||||||
Args string
|
Args string
|
||||||
// LogOutput is the writer to use for writing logs.
|
// LogOutput is the writer to use for writing logs.
|
||||||
LogOutput io.Writer
|
LogOutput io.Writer
|
||||||
// CleanupTempFiles enables cleanup of old temporary files on startup.
|
|
||||||
CleanupTempFiles bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logger returns the logger to use.
|
// Logger returns the logger to use.
|
||||||
|
|
|
||||||
|
|
@ -251,7 +251,7 @@ func (st *HelmState) appendShowOnlyFlags(flags []string, showOnly []string) []st
|
||||||
type Chartify struct {
|
type Chartify struct {
|
||||||
Opts *chartify.ChartifyOpts
|
Opts *chartify.ChartifyOpts
|
||||||
Clean func()
|
Clean func()
|
||||||
AddToCleanup func(string) // Add function to track additional files/dirs for cleanup
|
AddToCleanup func(string) // Add a path to be cleaned up
|
||||||
}
|
}
|
||||||
|
|
||||||
func (st *HelmState) downloadChartWithGoGetter(r *ReleaseSpec) (string, error) {
|
func (st *HelmState) downloadChartWithGoGetter(r *ReleaseSpec) (string, error) {
|
||||||
|
|
@ -297,6 +297,16 @@ func (st *HelmState) goGetterChart(chart, dir, cacheDir string, force bool) (str
|
||||||
}
|
}
|
||||||
|
|
||||||
func (st *HelmState) PrepareChartify(helm helmexec.Interface, release *ReleaseSpec, chart string, workerIndex int) (*Chartify, func(), error) {
|
func (st *HelmState) PrepareChartify(helm helmexec.Interface, release *ReleaseSpec, chart string, workerIndex int) (*Chartify, func(), error) {
|
||||||
|
var filesNeedCleaning []string
|
||||||
|
|
||||||
|
clean := func() {
|
||||||
|
st.removeFiles(filesNeedCleaning)
|
||||||
|
}
|
||||||
|
|
||||||
|
addToCleanup := func(path string) {
|
||||||
|
filesNeedCleaning = append(filesNeedCleaning, path)
|
||||||
|
}
|
||||||
|
|
||||||
c := &Chartify{
|
c := &Chartify{
|
||||||
Opts: &chartify.ChartifyOpts{
|
Opts: &chartify.ChartifyOpts{
|
||||||
WorkaroundOutputDirIssue: true,
|
WorkaroundOutputDirIssue: true,
|
||||||
|
|
@ -305,21 +315,9 @@ func (st *HelmState) PrepareChartify(helm helmexec.Interface, release *ReleaseSp
|
||||||
Namespace: release.Namespace,
|
Namespace: release.Namespace,
|
||||||
ID: ReleaseToID(release),
|
ID: ReleaseToID(release),
|
||||||
},
|
},
|
||||||
|
AddToCleanup: addToCleanup,
|
||||||
}
|
}
|
||||||
|
|
||||||
var filesNeedCleaning []string
|
|
||||||
|
|
||||||
clean := func() {
|
|
||||||
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
|
var shouldRun bool
|
||||||
|
|
||||||
dir := chart
|
dir := chart
|
||||||
|
|
@ -421,9 +419,11 @@ func (st *HelmState) PrepareChartify(helm helmexec.Interface, release *ReleaseSp
|
||||||
c.Opts.SetFlags = setFlags
|
c.Opts.SetFlags = setFlags
|
||||||
c.Opts.TemplateData = st.newReleaseTemplateData(release)
|
c.Opts.TemplateData = st.newReleaseTemplateData(release)
|
||||||
c.Opts.TemplateFuncs = st.newReleaseTemplateFuncMap(dir)
|
c.Opts.TemplateFuncs = st.newReleaseTemplateFuncMap(dir)
|
||||||
|
c.Clean = clean
|
||||||
|
|
||||||
return c, clean, nil
|
return c, clean, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.Clean = clean
|
||||||
return nil, clean, nil
|
return nil, clean, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue