2018-07-02 13:34:04 +02:00
|
|
|
package chartserver
|
|
|
|
|
|
|
|
import (
|
2018-07-06 14:53:13 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-07-02 13:34:04 +02:00
|
|
|
"net/http"
|
2018-07-06 14:53:13 +02:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ghodss/yaml"
|
|
|
|
helm_repo "k8s.io/helm/pkg/repo"
|
2018-07-02 13:34:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
//ManipulationHandler includes all the handler methods for the purpose of manipulating the
|
|
|
|
//chart repository
|
|
|
|
type ManipulationHandler struct {
|
|
|
|
//Proxy used to to transfer the traffic of requests
|
|
|
|
//It's mainly used to talk to the backend chart server
|
2018-07-03 15:36:20 +02:00
|
|
|
trafficProxy *ProxyEngine
|
2018-07-02 13:34:04 +02:00
|
|
|
|
|
|
|
//Parse and process the chart version to provide required info data
|
|
|
|
chartOperator *ChartOperator
|
2018-07-06 14:53:13 +02:00
|
|
|
|
|
|
|
//HTTP client used to call the realted APIs of the backend chart repositories
|
|
|
|
apiClient *ChartClient
|
|
|
|
|
|
|
|
//Point to the url of the backend server
|
|
|
|
backendServerAddress *url.URL
|
2018-07-02 13:34:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//ListCharts lists all the charts under the specified namespace
|
2018-07-03 15:36:20 +02:00
|
|
|
func (mh *ManipulationHandler) ListCharts(w http.ResponseWriter, req *http.Request) {
|
2018-07-06 14:53:13 +02:00
|
|
|
rootURL := strings.TrimSuffix(mh.backendServerAddress.String(), "/")
|
|
|
|
fullURL := fmt.Sprintf("%s%s", rootURL, req.RequestURI)
|
|
|
|
|
|
|
|
content, err := mh.apiClient.GetContent(fullURL)
|
|
|
|
if err != nil {
|
|
|
|
writeInternalError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
chartList, err := mh.chartOperator.GetChartList(content)
|
|
|
|
if err != nil {
|
|
|
|
writeInternalError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonData, err := json.Marshal(chartList)
|
|
|
|
if err != nil {
|
|
|
|
writeInternalError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeJSONData(w, jsonData)
|
2018-07-03 15:36:20 +02:00
|
|
|
}
|
2018-07-02 13:34:04 +02:00
|
|
|
|
|
|
|
//GetChart returns all the chart versions under the specified chart
|
2018-07-03 15:36:20 +02:00
|
|
|
func (mh *ManipulationHandler) GetChart(w http.ResponseWriter, req *http.Request) {
|
|
|
|
mh.trafficProxy.ServeHTTP(w, req)
|
|
|
|
}
|
2018-07-02 13:34:04 +02:00
|
|
|
|
|
|
|
//GetChartVersion get the specified version for one chart
|
|
|
|
//This handler should return the details of the chart version,
|
|
|
|
//maybe including metadata,dependencies and values etc.
|
2018-07-03 15:36:20 +02:00
|
|
|
func (mh *ManipulationHandler) GetChartVersion(w http.ResponseWriter, req *http.Request) {
|
2018-07-06 14:53:13 +02:00
|
|
|
chartV, err := mh.getChartVersion(req.RequestURI)
|
|
|
|
if err != nil {
|
|
|
|
writeInternalError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO:
|
|
|
|
namespace := "repo2"
|
|
|
|
content, err := mh.getChartVersionContent(namespace, chartV.URLs[0])
|
|
|
|
if err != nil {
|
|
|
|
writeInternalError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//Process bytes and get more details of chart version
|
|
|
|
chartDetails, err := mh.chartOperator.GetChartDetails(content)
|
|
|
|
if err != nil {
|
|
|
|
writeInternalError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
chartDetails.Metadata = chartV
|
|
|
|
|
|
|
|
bytes, err := json.Marshal(chartDetails)
|
|
|
|
if err != nil {
|
|
|
|
writeInternalError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeJSONData(w, bytes)
|
2018-07-03 15:36:20 +02:00
|
|
|
}
|
2018-07-02 13:34:04 +02:00
|
|
|
|
|
|
|
//UploadChartVersion will save the new version of the chart to the backend storage
|
2018-07-03 15:36:20 +02:00
|
|
|
func (mh *ManipulationHandler) UploadChartVersion(w http.ResponseWriter, req *http.Request) {
|
|
|
|
mh.trafficProxy.ServeHTTP(w, req)
|
|
|
|
}
|
2018-07-02 13:34:04 +02:00
|
|
|
|
|
|
|
//UploadProvenanceFile will save the provenance file of the chart to the backend storage
|
2018-07-03 15:36:20 +02:00
|
|
|
func (mh *ManipulationHandler) UploadProvenanceFile(w http.ResponseWriter, req *http.Request) {
|
|
|
|
mh.trafficProxy.ServeHTTP(w, req)
|
|
|
|
}
|
2018-07-02 13:34:04 +02:00
|
|
|
|
|
|
|
//DeleteChartVersion will delete the specified version of the chart
|
2018-07-03 15:36:20 +02:00
|
|
|
func (mh *ManipulationHandler) DeleteChartVersion(w http.ResponseWriter, req *http.Request) {
|
|
|
|
mh.trafficProxy.ServeHTTP(w, req)
|
|
|
|
}
|
2018-07-06 14:53:13 +02:00
|
|
|
|
|
|
|
//Get the basic metadata of chart version
|
|
|
|
func (mh *ManipulationHandler) getChartVersion(path string) (*helm_repo.ChartVersion, error) {
|
|
|
|
rootURL := strings.TrimSuffix(mh.backendServerAddress.String(), "/")
|
|
|
|
fullURL := fmt.Sprintf("%s%s", rootURL, path)
|
|
|
|
|
|
|
|
content, err := mh.apiClient.GetContent(fullURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
chartVersion := &helm_repo.ChartVersion{}
|
|
|
|
if err := yaml.Unmarshal(content, chartVersion); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return chartVersion, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get the content bytes of the chart version
|
|
|
|
func (mh *ManipulationHandler) getChartVersionContent(namespace string, path string) ([]byte, error) {
|
|
|
|
rootURL := strings.TrimSuffix(mh.backendServerAddress.String(), "/")
|
|
|
|
fullPath := fmt.Sprintf("%s/%s/%s", rootURL, namespace, path)
|
|
|
|
|
|
|
|
return mh.apiClient.GetContent(fullPath)
|
|
|
|
}
|