Skip to content

Commit

Permalink
support raw api (#715)
Browse files Browse the repository at this point in the history
Signed-off-by: Allen <[email protected]>
  • Loading branch information
allenhaozi committed Sep 8, 2023
1 parent 26e2e63 commit f0f5a74
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Powered by some great Go technology:
- `GET /api/charts/<name>/<version>` - describe a chart version
- `GET /api/charts/<name>/<version>/templates` - get chart template
- `GET /api/charts/<name>/<version>/values` - get chart values
- `GET /api/charts/<name>/<version>/raw` - get all the files of chart
- `HEAD /api/charts/<name>` - check if chart exists (any versions)
- `HEAD /api/charts/<name>/<version>` - check if chart version exists

Expand Down
51 changes: 40 additions & 11 deletions pkg/chartmuseum/server/multitenant/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@ import (
"time"

cm_storage "github.com/chartmuseum/storage"

"github.com/gin-gonic/gin"
"go.uber.org/zap"
cm_logger "helm.sh/chartmuseum/pkg/chartmuseum/logger"
cm_repo "helm.sh/chartmuseum/pkg/repo"

"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
helm_repo "helm.sh/helm/v3/pkg/repo"

"github.com/gin-gonic/gin"

"go.uber.org/zap"
)

var (
Expand Down Expand Up @@ -157,6 +153,7 @@ func (server *MultiTenantServer) getStorageObjectRequestHandler(c *gin.Context)
}
c.Data(200, storageObject.ContentType, storageObject.Content)
}

func (server *MultiTenantServer) getStorageObjectTemplateRequestHandler(c *gin.Context) {
repo := c.Param("repo")
name := c.Param("name")
Expand Down Expand Up @@ -186,6 +183,34 @@ func (server *MultiTenantServer) getStorageObjectTemplateRequestHandler(c *gin.C
})
}

func (server *MultiTenantServer) getStorageObjectRawRequestHandler(c *gin.Context) {
repo := c.Param("repo")
name := c.Param("name")
version := c.Param("version")

log := server.Logger.ContextLoggingFn(c)

fileName, err := server.getChartFileName(log, repo, name, version)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Message})
return
}

storageObject, err := server.getStorageObject(log, repo, fileName)
if err != nil {
c.JSON(err.Status, gin.H{"error": err.Message})
return
}
chrt, err1 := loader.LoadArchive(bytes.NewReader(storageObject.Content))
if err1 != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err1})
return
}
c.JSON(200, map[string]interface{}{
"raw": chrt.Raw,
})
}

func (server *MultiTenantServer) getStorageObjectValuesRequestHandler(c *gin.Context) {
repo := c.Param("repo")
name := c.Param("name")
Expand Down Expand Up @@ -222,6 +247,7 @@ func (server *MultiTenantServer) getStorageObjectValuesRequestHandler(c *gin.Con
}
c.Data(200, "application/yaml", data)
}

func (server *MultiTenantServer) getAllChartsRequestHandler(c *gin.Context) {
repo := c.Param("repo")
offset := 0
Expand Down Expand Up @@ -368,7 +394,8 @@ func (server *MultiTenantServer) postPackageRequestHandler(c *gin.Context) {
chart, chartErr := cm_repo.ChartVersionFromStorageObject(cm_storage.Object{
Path: pathutil.Join(repo, filename),
Content: content,
LastModified: time.Now()})
LastModified: time.Now(),
})
if chartErr != nil {
log(cm_logger.ErrorLevel, "cannot get chart from content", zap.Error(chartErr), zap.Binary("content", content))
}
Expand Down Expand Up @@ -430,9 +457,10 @@ func (server *MultiTenantServer) postPackageAndProvenanceRequestHandler(c *gin.C
if len(c.Errors) > 0 {
return // this is a "request too large"
}
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf(
"no package or provenance file found in form fields %s and %s",
server.ChartPostFormFieldName, server.ProvPostFormFieldName),
c.JSON(http.StatusBadRequest, gin.H{
"error": fmt.Sprintf(
"no package or provenance file found in form fields %s and %s",
server.ChartPostFormFieldName, server.ProvPostFormFieldName),
})
return
}
Expand Down Expand Up @@ -466,7 +494,8 @@ func (server *MultiTenantServer) postPackageAndProvenanceRequestHandler(c *gin.C
chart, chartErr := cm_repo.ChartVersionFromStorageObject(cm_storage.Object{
Path: path,
Content: chartContent,
LastModified: time.Now()})
LastModified: time.Now(),
})
if chartErr != nil {
log(cm_logger.ErrorLevel, "cannot get chart from content", zap.Error(err), zap.Binary("content", chartContent))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/chartmuseum/server/multitenant/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package multitenant

import (
cm_auth "github.com/chartmuseum/auth"

cm_router "helm.sh/chartmuseum/pkg/chartmuseum/router"
)

Expand Down Expand Up @@ -49,6 +48,7 @@ func (s *MultiTenantServer) Routes() []*cm_router.Route {
{Method: "GET", Path: "/api/:repo/charts/:name/:version", Handler: s.getChartVersionRequestHandler, Action: cm_auth.PullAction},
{Method: "GET", Path: "/api/:repo/charts/:name/:version/templates", Handler: s.getStorageObjectTemplateRequestHandler, Action: cm_auth.PullAction},
{Method: "GET", Path: "/api/:repo/charts/:name/:version/values", Handler: s.getStorageObjectValuesRequestHandler, Action: cm_auth.PullAction},
{Method: "GET", Path: "/api/:repo/charts/:name/:version/raw", Handler: s.getStorageObjectRawRequestHandler, Action: cm_auth.PullAction},
{Method: "POST", Path: "/api/:repo/charts", Handler: s.postRequestHandler, Action: cm_auth.PushAction},
{Method: "POST", Path: "/api/:repo/prov", Handler: s.postProvenanceFileRequestHandler, Action: cm_auth.PushAction},
}
Expand Down

0 comments on commit f0f5a74

Please sign in to comment.