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 raw api (#715) #719

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
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))
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 just put this in pkg/repo/chart.go, and provide a function like GetChartRawdate() ?

Copy link
Author

Choose a reason for hiding this comment

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

thank you for your review
I tried to wrap a function like you said, but I got an error import cycle not allowed
That would require a bigger change, what's your opinion? whether this larger change is needed

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