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

feature add chart template and value api #551

Merged
merged 6 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Powered by some great Go technology:
- `GET /api/charts` - list all charts
- `GET /api/charts/<name>` - list all versions of a chart
- `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
- `HEAD /api/charts/<name>` - check if chart exists (any versions)
- `HEAD /api/charts/<name>/<version>` - check if chart version exists

Expand Down
15 changes: 15 additions & 0 deletions pkg/chartmuseum/server/multitenant/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ func (server *MultiTenantServer) deleteChartVersion(log cm_logger.LoggingFn, rep
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 {
return "", err
}
if len(chartVersion.URLs) == 0 {
return "", &HTTPError{http.StatusNotFound, "chart filename not found"}
}
split := strings.Split(chartVersion.URLs[0], "/")
if len(split) < 2 {
return "", &HTTPError{http.StatusNotFound, "chart filename not found"}
}
return split[1], nil
}

func (server *MultiTenantServer) uploadChartPackage(log cm_logger.LoggingFn, repo string, content []byte, force bool) (string, *HTTPError) {
var filename string

Expand Down
64 changes: 64 additions & 0 deletions pkg/chartmuseum/server/multitenant/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package multitenant
import (
"bytes"
"fmt"
"helm.sh/helm/v3/pkg/chart/loader"
"io"
"net/http"
pathutil "path"
Expand Down Expand Up @@ -116,7 +117,70 @@ 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")
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{}{
"templates": chrt.Templates,
"values": chrt.Values,
})
}

func (server *MultiTenantServer) getStorageObjectValuesRequestHandler(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
}
var data []byte
for _, file := range chrt.Raw {
if file.Name == "values.yaml" {
data = file.Data
}
}
if data == nil {
c.JSON(http.StatusNotFound, gin.H{"error": "values.yaml not found"})
return
}
c.Data(200, "application/yaml", data)
}
func (server *MultiTenantServer) getAllChartsRequestHandler(c *gin.Context) {
repo := c.Param("repo")
offset := 0
Expand Down
2 changes: 2 additions & 0 deletions pkg/chartmuseum/server/multitenant/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func (s *MultiTenantServer) Routes() []*cm_router.Route {
{"GET", "/api/:repo/charts/:name", s.getChartRequestHandler, cm_auth.PullAction},
{"HEAD", "/api/:repo/charts/:name/:version", s.headChartVersionRequestHandler, cm_auth.PullAction},
{"GET", "/api/:repo/charts/:name/:version", s.getChartVersionRequestHandler, cm_auth.PullAction},
{"GET", "/api/:repo/charts/:name/:version/templates", s.getStorageObjectTemplateRequestHandler, cm_auth.PullAction},
{"GET", "/api/:repo/charts/:name/:version/values", s.getStorageObjectValuesRequestHandler, cm_auth.PullAction},
{"POST", "/api/:repo/charts", s.postRequestHandler, cm_auth.PushAction},
{"POST", "/api/:repo/prov", s.postProvenanceFileRequestHandler, cm_auth.PushAction},
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/chartmuseum/server/multitenant/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,32 @@ func (suite *MultiTenantServerTestSuite) testAllRoutes(repo string, depth int) {
res = suite.doRequest(stype, "GET", fmt.Sprintf("%s/charts/fakechart/0.1.0", apiPrefix), nil, "")
suite.Equal(404, res.Status(), fmt.Sprintf("200 GET %s/charts/fakechart/0.1.0", apiPrefix))

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

res = suite.doRequest(stype, "GET", fmt.Sprintf("%s/charts/mychart/latest/templates", apiPrefix), nil, "")
suite.Equal(200, res.Status(), fmt.Sprintf("200 GET %s/charts/mychart/latest/templates", apiPrefix))

res = suite.doRequest(stype, "GET", fmt.Sprintf("%s/charts/mychart/0.1.1/templates", apiPrefix), nil, "")
suite.Equal(404, res.Status(), fmt.Sprintf("404 GET %s/charts/mychart/0.1.1/templates", apiPrefix))

res = suite.doRequest(stype, "GET", fmt.Sprintf("%s/charts/fakechart/0.1.0/templates", apiPrefix), nil, "")
suite.Equal(404, res.Status(), fmt.Sprintf("404 GET %s/charts/fakechart/0.1.0/templates", apiPrefix))

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

res = suite.doRequest(stype, "GET", fmt.Sprintf("%s/charts/mychart/latest/values", apiPrefix), nil, "")
suite.Equal(200, res.Status(), fmt.Sprintf("200 GET %s/charts/mychart/latest/values", apiPrefix))

res = suite.doRequest(stype, "GET", fmt.Sprintf("%s/charts/mychart/0.1.1/values", apiPrefix), nil, "")
suite.Equal(404, res.Status(), fmt.Sprintf("404 GET %s/charts/mychart/0.1.1/values", apiPrefix))

res = suite.doRequest(stype, "GET", fmt.Sprintf("%s/charts/fakechart/0.1.0/values", apiPrefix), nil, "")
suite.Equal(404, res.Status(), fmt.Sprintf("404 GET %s/charts/fakechart/0.1.0/values", apiPrefix))

// HEAD /api/:repo/charts/:name/:version
res = suite.doRequest(stype, "HEAD", fmt.Sprintf("%s/charts/mychart/0.1.0", apiPrefix), nil, "")
suite.Equal(200, res.Status(), fmt.Sprintf("200 HEAD %s/charts/mychart/0.1.0", apiPrefix))
Expand Down
Empty file.