Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete outdated helm charts when pulling new helm charts #486

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion cmd/local-artifact-mirror/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,40 @@ var helmChartsCommand = &cli.Command{
dst := defaults.EmbeddedClusterChartsSubDir()
src := filepath.Join(location, HelmChartsArtifactName)
logrus.Infof("uncompressing %s", src)
if err := tgzutils.Decompress(src, dst); err != nil {
newFiles, err := tgzutils.Decompress(src, dst)
if err != nil {
return fmt.Errorf("unable to uncompress helm charts: %w", err)
}

// check which files are present in the directory but not newFiles, and remove them
files, err := os.ReadDir(dst)
if err != nil {
return fmt.Errorf("unable to read directory: %w", err)
}
for _, f := range files {
if !contains(newFiles, f.Name()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use https://pkg.go.dev/slices#Contains instead ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

fp := filepath.Join(dst, f.Name())
logrus.Infof("removing %s", fp)
if err := os.RemoveAll(fp); err != nil {
return fmt.Errorf("unable to remove file: %w", err)
}
}
}

logrus.Infof("helm charts materialized under %s", dst)
return nil
},
}

func contains(haystack []string, needle string) bool {
for _, h := range haystack {
if h == needle {
return true
}
}
return false
}

// binariesCommands pulls the binary artifact from the registry running in the cluster and stores
// it locally. This command is used during cluster upgrades when we want to fetch the most up to
// date binaries. The binaries is stored in the /usr/local/bin directory and they overwrite the
Expand Down
23 changes: 13 additions & 10 deletions pkg/tgzutils/tgz.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,55 @@ import (
)

// Decompress decompresses a .tgz file into a directory.
func Decompress(tgz, dst string) error {
func Decompress(tgz, dst string) ([]string, error) {
fp, err := os.Open(tgz)
if err != nil {
return fmt.Errorf("unable to open tgz file: %v", err)
return nil, fmt.Errorf("unable to open tgz file: %v", err)
}
defer fp.Close()

gzreader, err := gzip.NewReader(fp)
if err != nil {
return fmt.Errorf("unable to create gzip reader: %v", err)
return nil, fmt.Errorf("unable to create gzip reader: %v", err)
}

filenames := []string{}

tarreader := tar.NewReader(gzreader)
for {
header, err := tarreader.Next()
if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("unable to read tar header: %v", err)
return nil, fmt.Errorf("unable to read tar header: %v", err)
}

switch header.Typeflag {
case tar.TypeDir:
mode := os.FileMode(header.Mode)
dst := filepath.Join(dst, header.Name)
if err := os.Mkdir(dst, mode); err != nil {
return fmt.Errorf("unable to create directory: %v", err)
return nil, fmt.Errorf("unable to create directory: %v", err)
}
case tar.TypeReg:
mode := os.FileMode(header.Mode)
dst := filepath.Join(dst, header.Name)
opts := os.O_CREATE | os.O_WRONLY | os.O_TRUNC
outfp, err := os.OpenFile(dst, opts, mode)
if err != nil {
return fmt.Errorf("unable to create file: %v", err)
return nil, fmt.Errorf("unable to create file: %v", err)
}
if _, err := io.Copy(outfp, tarreader); err != nil {
return fmt.Errorf("unable to write file: %v", err)
return nil, fmt.Errorf("unable to write file: %v", err)
}
outfp.Close()
if err := os.Chmod(dst, os.FileMode(header.Mode)); err != nil {
return fmt.Errorf("unable to chmod file: %v", err)
return nil, fmt.Errorf("unable to chmod file: %v", err)
}
filenames = append(filenames, dst)
default:
return fmt.Errorf("unknown type: %v", header.Typeflag)
return nil, fmt.Errorf("unknown type: %v", header.Typeflag)
}
}
return nil
return filenames, nil
}
Loading