Fix: Storage leak in Builder and Fetcher (#2979)

* Fix storage leak in builder
```
builder pod keeps old src and deployment packages irrespective of build status.
delete src package after every build request is completed.
delete deployment package after package is uploaded.
```
* Optimized src/deploy cleanup pkg code
* Fix high severity security issue
* Add a test for builder's Clean API
* Resolve review comments

---------

Signed-off-by: Md Soharab Ansari <soharab.ansari@infracloud.io>
This commit is contained in:
soharab-ic
2024-07-15 18:28:42 +05:30
committed by GitHub
parent 35276a503b
commit 8de5a5b0f3
7 changed files with 168 additions and 0 deletions
+29
View File
@@ -267,3 +267,32 @@ func FindFreePort() (int, error) {
return port, nil
}
// DeleteOldPackages deletes src and built deployment packages from builder's storage.
// The function also verifies that sharedVolumePath for builder and fetcher containers
// is /packages. A source_package contains a directory and a .tmp file while a deployment
// package contains a directory and a .zip file.
func DeleteOldPackages(pkgPath, pkgType string) error {
sharedVolumePath := "/packages"
if !strings.HasPrefix(pkgPath, sharedVolumePath) {
return fmt.Errorf("invalid shared volume path: %s", pkgPath)
}
var file string
if pkgType == "DEPLOY_PKG" {
file = pkgPath + ".zip"
} else if pkgType == "SRC_PKG" {
file = pkgPath + ".tmp"
}
err := os.RemoveAll(pkgPath)
if err != nil {
return err
}
err = os.Remove(file)
if err != nil {
return err
}
return nil
}