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

support for delete all version of a specific chart #642

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 21 additions & 0 deletions pkg/chartmuseum/server/multitenant/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ func (server *MultiTenantServer) deleteChartVersion(log cm_logger.LoggingFn, rep
return nil
}

func (server *MultiTenantServer) deleteChart(log cm_logger.LoggingFn, repo string, name string) *HTTPError {
log(cm_logger.DebugLevel, "Deleting chart from storage",
"chart", name,
)

packages, err := server.StorageBackend.ListObjects(repo)
if err != nil {
return &HTTPError{http.StatusNotFound, err.Error()}
}
for _, obj := range packages {
if strings.HasPrefix(obj.Path, fmt.Sprintf("%s-", name)) {
err = server.StorageBackend.DeleteObject(obj.Path)
if err != nil {
return &HTTPError{http.StatusNotFound, err.Error()}
}
}
}

return nil
}

func (server *MultiTenantServer) getChartFileName(log cm_logger.LoggingFn, repo string, name string, version string) (string, *HTTPError) {
chartVersion, err := server.getChartVersion(log, repo, name, version)
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions pkg/chartmuseum/server/multitenant/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ type (
)

const (
updateChart operationType = 0
addChart operationType = 1
deleteChart operationType = 2
updateChart operationType = 0
addChart operationType = 1
deleteChart operationType = 2
deleteAllChart operationType = 3
)

var (
Expand Down Expand Up @@ -536,6 +537,8 @@ func (server *MultiTenantServer) startEventListener() {
index.AddEntry(e.ChartVersion)
case deleteChart:
index.RemoveEntry(e.ChartVersion)
case deleteAllChart:
index.RemoveAllEntry(e.ChartVersion)
default:
log(cm_logger.ErrorLevel, "Invalid operation type", zap.String("repo", repo),
"operation_type", e.OpType)
Expand Down
18 changes: 18 additions & 0 deletions pkg/chartmuseum/server/multitenant/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,24 @@ func (server *MultiTenantServer) deleteChartVersionRequestHandler(c *gin.Context
c.JSON(200, objectDeletedResponse)
}

func (server *MultiTenantServer) deleteChartRequestHandler(c *gin.Context) {
repo := c.Param("repo")
name := c.Param("name")
log := server.Logger.ContextLoggingFn(c)
err := server.deleteChart(log, repo, name)
if err != nil {
c.JSON(err.Status, gin.H{"error": err.Message})
return
}

server.emitEvent(c, repo, deleteAllChart, &helm_repo.ChartVersion{
Metadata: &chart.Metadata{
Name: name,
},
})
c.JSON(200, objectDeletedResponse)
}

func (server *MultiTenantServer) postRequestHandler(c *gin.Context) {
if c.ContentType() == "multipart/form-data" {
server.postPackageAndProvenanceRequestHandler(c) // new route handling form-based chart and/or prov files
Expand Down
1 change: 1 addition & 0 deletions pkg/chartmuseum/server/multitenant/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (s *MultiTenantServer) Routes() []*cm_router.Route {
}

if s.APIEnabled && !s.DisableDelete {
routes = append(routes, &cm_router.Route{Method: "DELETE", Path: "/api/:repo/charts/:name/*", Handler: s.deleteChartRequestHandler, Action: cm_auth.PushAction})
routes = append(routes, &cm_router.Route{Method: "DELETE", Path: "/api/:repo/charts/:name/:version", Handler: s.deleteChartVersionRequestHandler, Action: cm_auth.PushAction})
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/chartmuseum/server/multitenant/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,10 @@ func (suite *MultiTenantServerTestSuite) testAllRoutes(repo string, depth int) {
res = suite.doRequest(stype, "DELETE", fmt.Sprintf("%s/charts/mychart/0.1.0", apiPrefix), nil, "")
suite.Equal(200, res.Status(), fmt.Sprintf("200 DELETE %s/charts/mychart/0.1.0", apiPrefix))

// DELETE /api/:repo/charts/:name/*
res = suite.doRequest(stype, "DELETE", fmt.Sprintf("%s/charts/mychart/*", apiPrefix), nil, "")
suite.Equal(200, res.Status(), fmt.Sprintf("200 DELETE %s/charts/mychart/*", apiPrefix))

res = suite.doRequest(stype, "DELETE", fmt.Sprintf("%s/charts/mychart/0.1.0", apiPrefix), nil, "")
suite.Equal(404, res.Status(), fmt.Sprintf("200 DELETE %s/charts/mychart/0.1.0", apiPrefix))

Expand Down
7 changes: 7 additions & 0 deletions pkg/repo/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ func (index *Index) RemoveEntry(chartVersion *helm_repo.ChartVersion) {
}
}

// RemoveAllEntry removes all versions of a chart from index
func (index *Index) RemoveAllEntry(chartVersion *helm_repo.ChartVersion) {
if _, ok := index.Entries[chartVersion.Name]; ok {
delete(index.Entries, chartVersion.Name)
}
}

// AddEntry adds a chart version to index
func (index *Index) AddEntry(chartVersion *helm_repo.ChartVersion) {
if _, ok := index.Entries[chartVersion.Name]; !ok {
Expand Down