diff --git a/toolkit/tools/pkg/imagecustomizerlib/customizepackages.go b/toolkit/tools/pkg/imagecustomizerlib/customizepackages.go index 63466ba5422..3673c1dd7b2 100644 --- a/toolkit/tools/pkg/imagecustomizerlib/customizepackages.go +++ b/toolkit/tools/pkg/imagecustomizerlib/customizepackages.go @@ -82,6 +82,13 @@ func addRemoveAndUpdatePackages(buildDir string, baseConfigPath string, config * } } + if needRpmsSources { + err = cleanTdnfCache(imageChroot) + if err != nil { + return err + } + } + return nil } @@ -224,3 +231,21 @@ func isPackageInstalled(imageChroot *safechroot.Chroot, packageName string) bool } return true } + +func cleanTdnfCache(imageChroot *safechroot.Chroot) error { + logger.Log.Infof("Cleaning up RPM cache") + // Run all cleanup tasks inside the chroot environment + return imageChroot.UnsafeRun(func() error { + tdnfArgs := []string{ + "-v", "clean", "all", + } + err := shell.NewExecBuilder("tdnf", tdnfArgs...). + LogLevel(logrus.TraceLevel, logrus.DebugLevel). + ErrorStderrLines(1). + Execute() + if err != nil { + return fmt.Errorf("Failed to clean tdnf cache: %w", err) + } + return nil + }) +} diff --git a/toolkit/tools/pkg/imagecustomizerlib/customizepackages_test.go b/toolkit/tools/pkg/imagecustomizerlib/customizepackages_test.go index 4b27103ed8c..1770413a7c9 100644 --- a/toolkit/tools/pkg/imagecustomizerlib/customizepackages_test.go +++ b/toolkit/tools/pkg/imagecustomizerlib/customizepackages_test.go @@ -5,6 +5,7 @@ package imagecustomizerlib import ( "fmt" + "io/fs" "os" "path/filepath" "strings" @@ -185,6 +186,9 @@ func TestCustomizeImagePackagesUpdate(t *testing.T) { } defer imageConnection.Close() + // Ensure tdnf cache was cleaned. + ensureTdnfCacheCleanup(t, imageConnection, "/var/cache/tdnf") + // Ensure packages were installed. ensureFilesExist(t, imageConnection, "/usr/bin/jq", @@ -211,3 +215,33 @@ func TestCustomizeImagePackagesDiskSpace(t *testing.T) { assert.ErrorContains(t, err, "failed to customize raw image") assert.ErrorContains(t, err, "failed to install package (gcc)") } + +func ensureTdnfCacheCleanup(t *testing.T, imageConnection *ImageConnection, dirPath string) { + // Array to capture all the files of the provided root directory + var existingFiles []string + + // Start the directory walk from the initial dirPath and collect all existing files + fullPath := filepath.Join(imageConnection.Chroot().RootDir(), dirPath) + err := filepath.WalkDir(fullPath, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return fmt.Errorf("Failed to access path (%s): %w", path, err) + } + // Ignore files in the local-repo folder if the base image version is 2.0 + if !(strings.Contains(path, "local-repo") && baseImageVersionDefault == "2.0") { + fileInfo, err := os.Stat(path) + if err != nil { + return fmt.Errorf("failed to get file info for %s: %w", path, err) + } + if !fileInfo.IsDir() { + // Append the file to the existingFiles array + existingFiles = append(existingFiles, path) + } + } + return nil + }) + + assert.NoError(t, err) + + // Ensure the cache has been cleaned up + assert.Equal(t, 0, len(existingFiles), "Expected no file data in cache, but got %d files", len(existingFiles)) +}