Skip to content

Commit

Permalink
Feat: support get secret from project config for helm chart (#698)
Browse files Browse the repository at this point in the history
* Feat: support get secret from project config for helm chart

Signed-off-by: caiqi <caiqi_yewu@cmss.chinamobile.com>

* Feat: support get secret from project config for helm chart

Signed-off-by: caiqi <caiqi_yewu@cmss.chinamobile.com>

* Feat: support get secret from project config for helm chart

Signed-off-by: caiqi <caiqi_yewu@cmss.chinamobile.com>

* Feat: support get secret from project config for helm chart

Signed-off-by: caiqi <caiqi_yewu@cmss.chinamobile.com>

* Feat: support get secret from project config for helm chart

Signed-off-by: caiqi <caiqi_yewu@cmss.chinamobile.com>

* Feat: support get secret from project config for helm chart

Signed-off-by: caiqi <caiqi_yewu@cmss.chinamobile.com>

* Feat: support get secret from project config for helm chart

Signed-off-by: caiqi <caiqi_yewu@cmss.chinamobile.com>

---------

Signed-off-by: caiqi <caiqi_yewu@cmss.chinamobile.com>
  • Loading branch information
caiqi1111 committed Mar 30, 2023
1 parent ce2a5f8 commit 4ca9085
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 103 deletions.
180 changes: 108 additions & 72 deletions docs/apidoc/swagger.json

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions packages/velaux-ui/src/api/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function getChartValueFiles(params: {
secretName?: string;
chart: string;
version: string;
project?: string
}) {
const url = baseURL + repository + '/chart/values';
return get(url, {
Expand All @@ -35,14 +36,15 @@ export function getChartValueFiles(params: {
secretName: params.secretName,
chart: params.chart,
version: params.version,
project: params.project
},
}).then((res) => res);
}

export function getCharts(params: { url: string; repoType: string; secretName?: string }) {
export function getCharts(params: { url: string; repoType: string; secretName?: string; project?: string }) {
const url = baseURL + repository + '/charts';
return get(url, {
params: { repoUrl: params.url, repoType: params.repoType, secretName: params.secretName },
params: { repoUrl: params.url, repoType: params.repoType, secretName: params.secretName, project: params.project },
}).then((res) => res);
}

Expand All @@ -51,6 +53,7 @@ export function getChartVersions(params: {
repoType: string;
chart: string;
secretName?: string;
project?: string
}) {
const url = baseURL + repository + '/chart/versions';
return get(url, {
Expand All @@ -59,6 +62,7 @@ export function getChartVersions(params: {
repoType: params.repoType,
secretName: params.secretName,
chart: params.chart,
project: params.project
},
}).then((res) => res);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/velaux-ui/src/extends/HelmChartSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Props = {
url: string;
repoType: string;
};
project?: string;
repo?: HelmRepo;
};

Expand Down Expand Up @@ -44,14 +45,15 @@ class HelmChartSelect extends Component<Props, State> {
}

loadCharts = () => {
const { project } = this.props;
const { helm, repo } = this.props;
if (helm?.url && (!this.state.loading || this.state.helm?.url != helm.url)) {
// Reset chart value
if (this.state.helm) {
this.props.onChange('');
}
this.setState({ loading: true, helm: helm });
getCharts({ url: helm.url, repoType: helm.repoType, secretName: repo?.secretName }).then(
getCharts({ url: helm.url, repoType: helm.repoType, secretName: repo?.secretName, project: project }).then(
(re: string[]) => {
this.setState({ charts: re && Array.isArray(re) ? re : [], loading: false, helm: helm });
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Props = {
repoType: string;
chart: string;
};
project?: string;
repo?: HelmRepo;
};

Expand Down Expand Up @@ -46,6 +47,7 @@ class HelmChartVersionSelect extends Component<Props, State> {
}

loadChartVersions = () => {
const { project } = this.props;
const { helm, repo } = this.props;
if (
helm?.url &&
Expand All @@ -63,6 +65,7 @@ class HelmChartVersionSelect extends Component<Props, State> {
repoType: helm.repoType,
chart: helm.chart,
secretName: repo?.secretName,
project: project
}).then((re: { versions: ChartVersion[] }) => {
if (re) {
this.setState({ versions: re.versions || [], loading: false, helm: helm });
Expand Down
4 changes: 3 additions & 1 deletion packages/velaux-ui/src/extends/HelmValues/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Props = {
chart: string;
version: string;
};
project?: string;
repo?: HelmRepo;
};

Expand Down Expand Up @@ -85,9 +86,10 @@ class HelmValues extends Component<Props, State> {
}

loadChartValues = () => {
const { project } = this.props;
const { helm, repo } = this.props;
if (helm?.chart && helm.version && helm.url) {
getChartValueFiles({ ...helm, secretName: repo?.secretName }).then(
getChartValueFiles({ ...helm, secretName: repo?.secretName, project: project }).then(
(re: Record<string, string>) => {
if (re) {
try {
Expand Down
53 changes: 37 additions & 16 deletions pkg/server/domain/service/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func NewHelmService() HelmService {

// HelmService responsible handle helm related interface
type HelmService interface {
ListChartNames(ctx context.Context, url string, secretName string, skipCache bool) ([]string, error)
ListChartVersions(ctx context.Context, url string, chartName string, secretName string, skipCache bool) (repo.ChartVersions, error)
ListChartValuesFiles(ctx context.Context, url string, chartName string, version string, secretName string, repoType string, skipCache bool) (map[string]string, error)
ListChartNames(ctx context.Context, url string, secretName string, projectName string, skipCache bool) ([]string, error)
ListChartVersions(ctx context.Context, url string, chartName string, secretName string, projectName string, skipCache bool) (repo.ChartVersions, error)
ListChartValuesFiles(ctx context.Context, url string, chartName string, version string, secretName string, projectName string, repoType string, skipCache bool) (map[string]string, error)
ListChartRepo(ctx context.Context, projectName string) (*v1.ChartRepoResponseList, error)
GetChartValues(ctx context.Context, repoURL string, chartName string, version string, secretName string, repoType string, skipCache bool) (map[string]interface{}, error)
GetChartValues(ctx context.Context, repoURL string, chartName string, version string, secretName string, projectName string, repoType string, skipCache bool) (map[string]interface{}, error)
}

type defaultHelmImpl struct {
Expand All @@ -57,16 +57,37 @@ type defaultHelmImpl struct {
ConfigService ConfigService `inject:""`
}

func (d defaultHelmImpl) ListChartNames(ctx context.Context, repoURL string, secretName string, skipCache bool) ([]string, error) {
func (d defaultHelmImpl) GetHelmHTTPOption(ctx context.Context, secretName string, project string) (*common.HTTPOption, error) {
var opts *common.HTTPOption
var err error
if project != "" {
config, err := d.ConfigService.GetConfig(ctx, project, secretName)
if err != nil {
return nil, bcode.ErrRepoBasicAuth
}
opts, err = helm.SetHTTPOption(ctx, d.K8sClient, types2.NamespacedName{Namespace: config.Namespace, Name: secretName})
if err != nil {
return nil, bcode.ErrRepoBasicAuth
}
} else {
opts, err = helm.SetHTTPOption(ctx, d.K8sClient, types2.NamespacedName{Namespace: types.DefaultKubeVelaNS, Name: secretName})
if err != nil {
return nil, bcode.ErrRepoBasicAuth
}
}
return opts, nil
}

func (d defaultHelmImpl) ListChartNames(ctx context.Context, repoURL string, secretName string, projectName string, skipCache bool) ([]string, error) {
if !utils.IsValidURL(repoURL) {
return nil, bcode.ErrRepoInvalidURL
}
var opts *common.HTTPOption
var err error
if len(secretName) != 0 {
opts, err = helm.SetHTTPOption(ctx, d.K8sClient, types2.NamespacedName{Namespace: types.DefaultKubeVelaNS, Name: secretName})
opts, err = d.GetHelmHTTPOption(ctx, secretName, projectName)
if err != nil {
return nil, bcode.ErrRepoBasicAuth
return nil, err
}
}
charts, err := d.helper.ListChartsFromRepo(repoURL, skipCache, opts)
Expand All @@ -77,16 +98,16 @@ func (d defaultHelmImpl) ListChartNames(ctx context.Context, repoURL string, sec
return charts, nil
}

func (d defaultHelmImpl) ListChartVersions(ctx context.Context, repoURL string, chartName string, secretName string, skipCache bool) (repo.ChartVersions, error) {
func (d defaultHelmImpl) ListChartVersions(ctx context.Context, repoURL string, chartName string, secretName string, projectName string, skipCache bool) (repo.ChartVersions, error) {
if !utils.IsValidURL(repoURL) {
return nil, bcode.ErrRepoInvalidURL
}
var opts *common.HTTPOption
var err error
if len(secretName) != 0 {
opts, err = helm.SetHTTPOption(ctx, d.K8sClient, types2.NamespacedName{Namespace: types.DefaultKubeVelaNS, Name: secretName})
opts, err = d.GetHelmHTTPOption(ctx, secretName, projectName)
if err != nil {
return nil, bcode.ErrRepoBasicAuth
return nil, err
}
}
chartVersions, err := d.helper.ListVersions(repoURL, chartName, skipCache, opts)
Expand All @@ -101,16 +122,16 @@ func (d defaultHelmImpl) ListChartVersions(ctx context.Context, repoURL string,
return chartVersions, nil
}

func (d defaultHelmImpl) ListChartValuesFiles(ctx context.Context, repoURL string, chartName string, version string, secretName string, repoType string, skipCache bool) (map[string]string, error) {
func (d defaultHelmImpl) ListChartValuesFiles(ctx context.Context, repoURL string, chartName string, version string, secretName string, projectName string, repoType string, skipCache bool) (map[string]string, error) {
if !utils.IsValidURL(repoURL) {
return nil, bcode.ErrRepoInvalidURL
}
var opts *common.HTTPOption
var err error
if len(secretName) != 0 {
opts, err = helm.SetHTTPOption(ctx, d.K8sClient, types2.NamespacedName{Namespace: types.DefaultKubeVelaNS, Name: secretName})
opts, err = d.GetHelmHTTPOption(ctx, secretName, projectName)
if err != nil {
return nil, bcode.ErrRepoBasicAuth
return nil, err
}
}
v, err := d.helper.GetValuesFromChart(repoURL, chartName, version, skipCache, repoType, opts)
Expand All @@ -121,16 +142,16 @@ func (d defaultHelmImpl) ListChartValuesFiles(ctx context.Context, repoURL strin
return v.Data, nil
}

func (d defaultHelmImpl) GetChartValues(ctx context.Context, repoURL string, chartName string, version string, secretName string, repoType string, skipCache bool) (map[string]interface{}, error) {
func (d defaultHelmImpl) GetChartValues(ctx context.Context, repoURL string, chartName string, version string, secretName string, projectName string, repoType string, skipCache bool) (map[string]interface{}, error) {
if !utils.IsValidURL(repoURL) {
return nil, bcode.ErrRepoInvalidURL
}
var opts *common.HTTPOption
var err error
if len(secretName) != 0 {
opts, err = helm.SetHTTPOption(ctx, d.K8sClient, types2.NamespacedName{Namespace: types.DefaultKubeVelaNS, Name: secretName})
opts, err = d.GetHelmHTTPOption(ctx, secretName, projectName)
if err != nil {
return nil, bcode.ErrRepoBasicAuth
return nil, err
}
}
v, err := d.helper.GetValuesFromChart(repoURL, chartName, version, skipCache, repoType, opts)
Expand Down
12 changes: 6 additions & 6 deletions pkg/server/domain/service/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,17 @@ var _ = Describe("test helm usecasae", func() {

u, _, err := NewTestHelmService()
Expect(err).Should(BeNil())
charts, err := u.ListChartNames(ctx, mockServer.URL, "repo-secret", false)
charts, err := u.ListChartNames(ctx, mockServer.URL, "repo-secret", "", false)
Expect(err).Should(BeNil())
Expect(len(charts)).Should(BeEquivalentTo(1))
Expect(charts[0]).Should(BeEquivalentTo("mysql"))

versions, err := u.ListChartVersions(ctx, mockServer.URL, "mysql", "repo-secret", false)
versions, err := u.ListChartVersions(ctx, mockServer.URL, "mysql", "repo-secret", "", false)
Expect(err).Should(BeNil())
Expect(len(versions)).Should(BeEquivalentTo(1))
Expect(versions[0].Version).Should(BeEquivalentTo("8.8.23"))

values, err := u.ListChartValuesFiles(ctx, mockServer.URL, "mysql", "8.8.23", "repo-secret", "helm", false)
values, err := u.ListChartValuesFiles(ctx, mockServer.URL, "mysql", "8.8.23", "repo-secret", "", "helm", false)
Expect(err).Should(BeNil())
Expect(values).ShouldNot(BeNil())
Expect(len(values)).ShouldNot(BeEquivalentTo(0))
Expand All @@ -223,13 +223,13 @@ var _ = Describe("test helm usecasae", func() {
It("coverage not secret notExist error", func() {
u, _, err := NewTestHelmService()
Expect(err).Should(BeNil())
_, err = u.ListChartNames(ctx, "http://127.0.0.1:8080", "repo-secret-notExist", false)
_, err = u.ListChartNames(ctx, "http://127.0.0.1:8080", "repo-secret-notExist", "", false)
Expect(err).ShouldNot(BeNil())

_, err = u.ListChartVersions(ctx, "http://127.0.0.1:8080", "mysql", "repo-secret-notExist", false)
_, err = u.ListChartVersions(ctx, "http://127.0.0.1:8080", "mysql", "repo-secret-notExist", "", false)
Expect(err).ShouldNot(BeNil())

_, err = u.ListChartValuesFiles(ctx, "http://127.0.0.1:8080", "mysql", "8.8.23", "repo-secret-notExist", "helm", false)
_, err = u.ListChartValuesFiles(ctx, "http://127.0.0.1:8080", "mysql", "8.8.23", "repo-secret-notExist", "", "helm", false)
Expect(err).ShouldNot(BeNil())
})
})
Expand Down
20 changes: 15 additions & 5 deletions pkg/server/interfaces/api/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (h repository) GetWebServiceRoute() *restful.WebService {
ws.Route(ws.GET("/charts").To(h.listCharts).
Doc("list charts").
Metadata(restfulspec.KeyOpenAPITags, tags).
Param(ws.QueryParameter("project", "the config project").DataType("string").Required(true)).
Param(ws.QueryParameter("repoUrl", "helm repository url").DataType("string")).
Param(ws.QueryParameter("secretName", "secret of the repo").DataType("string")).
Returns(200, "OK", []string{}).
Expand All @@ -73,6 +74,7 @@ func (h repository) GetWebServiceRoute() *restful.WebService {
ws.Route(ws.GET("/chart/versions").To(h.listVersionsFromQuery).
Doc("list versions").
Metadata(restfulspec.KeyOpenAPITags, tags).
Param(ws.QueryParameter("project", "the config project").DataType("string").Required(true)).
Param(ws.QueryParameter("chart", "helm chart").DataType("string").Required(true)).
Param(ws.QueryParameter("repoUrl", "helm repository url").DataType("string").Required(true)).
Param(ws.QueryParameter("secretName", "secret of the repo").DataType("string")).
Expand All @@ -83,6 +85,7 @@ func (h repository) GetWebServiceRoute() *restful.WebService {
ws.Route(ws.GET("/charts/{chart}/versions").To(h.listChartVersions).
Doc("list versions").Deprecate().
Metadata(restfulspec.KeyOpenAPITags, tags).
Param(ws.QueryParameter("project", "the config project").DataType("string").Required(true)).
Param(ws.PathParameter("chart", "identifier of the helm chart").DataType("string").Required(true)).
Param(ws.QueryParameter("repoUrl", "helm repository url").DataType("string")).
Param(ws.QueryParameter("secretName", "secret of the repo").DataType("string")).
Expand All @@ -94,6 +97,7 @@ func (h repository) GetWebServiceRoute() *restful.WebService {
ws.Route(ws.GET("/chart/values").To(h.chartValues).
Doc("get chart value").
Metadata(restfulspec.KeyOpenAPITags, tags).
Param(ws.QueryParameter("project", "the config project").DataType("string").Required(true)).
Param(ws.QueryParameter("chart", "helm chart").DataType("string").Required(true)).
Param(ws.QueryParameter("version", "helm chart version").DataType("string").Required(true)).
Param(ws.QueryParameter("repoUrl", "helm repository url").DataType("string").Required(true)).
Expand All @@ -106,6 +110,7 @@ func (h repository) GetWebServiceRoute() *restful.WebService {
ws.Route(ws.GET("/charts/{chart}/versions/{version}/values").To(h.getChartValues).
Doc("get chart value").Deprecate().
Metadata(restfulspec.KeyOpenAPITags, tags).
Param(ws.QueryParameter("project", "the config project").DataType("string").Required(true)).
Param(ws.PathParameter("chart", "identifier of the helm chart").DataType("string").Required(true)).
Param(ws.PathParameter("version", "version of the helm chart").DataType("string").Required(true)).
Param(ws.QueryParameter("repoUrl", "helm repository url").DataType("string")).
Expand Down Expand Up @@ -141,12 +146,13 @@ func (h repository) GetWebServiceRoute() *restful.WebService {
func (h repository) listCharts(req *restful.Request, res *restful.Response) {
url := utils.Sanitize(req.QueryParameter("repoUrl"))
secName := utils.Sanitize(req.QueryParameter("secretName"))
projectName := req.QueryParameter("project")
skipCache, err := isSkipCache(req)
if err != nil {
bcode.ReturnError(req, res, bcode.ErrSkipCacheParameter)
return
}
charts, err := h.HelmService.ListChartNames(req.Request.Context(), url, secName, skipCache)
charts, err := h.HelmService.ListChartNames(req.Request.Context(), url, secName, projectName, skipCache)
if err != nil {
bcode.ReturnError(req, res, err)
return
Expand All @@ -162,13 +168,14 @@ func (h repository) listVersionsFromQuery(req *restful.Request, res *restful.Res
url := req.QueryParameter("repoUrl")
chartName := req.QueryParameter("chart")
secName := req.QueryParameter("secretName")
projectName := req.QueryParameter("project")
skipCache, err := isSkipCache(req)
if err != nil {
bcode.ReturnError(req, res, bcode.ErrSkipCacheParameter)
return
}

versions, err := h.HelmService.ListChartVersions(req.Request.Context(), url, chartName, secName, skipCache)
versions, err := h.HelmService.ListChartVersions(req.Request.Context(), url, chartName, secName, projectName, skipCache)
if err != nil {
bcode.ReturnError(req, res, err)
return
Expand All @@ -184,14 +191,15 @@ func (h repository) getChartValues(req *restful.Request, res *restful.Response)
url := req.QueryParameter("repoUrl")
secName := req.QueryParameter("secretName")
chartName := req.PathParameter("chart")
projectName := req.QueryParameter("project")
version := req.PathParameter("version")
skipCache, err := isSkipCache(req)
if err != nil {
bcode.ReturnError(req, res, bcode.ErrSkipCacheParameter)
return
}

values, err := h.HelmService.GetChartValues(req.Request.Context(), url, chartName, version, secName, "helm", skipCache)
values, err := h.HelmService.GetChartValues(req.Request.Context(), url, chartName, version, secName, projectName, "helm", skipCache)
if err != nil {
bcode.ReturnError(req, res, err)
return
Expand All @@ -207,12 +215,13 @@ func (h repository) listChartVersions(req *restful.Request, res *restful.Respons
url := req.QueryParameter("repoUrl")
chartName := req.PathParameter("chart")
secName := req.QueryParameter("secretName")
projectName := req.QueryParameter("project")
skipCache, err := isSkipCache(req)
if err != nil {
bcode.ReturnError(req, res, bcode.ErrSkipCacheParameter)
return
}
versions, err := h.HelmService.ListChartVersions(req.Request.Context(), url, chartName, secName, skipCache)
versions, err := h.HelmService.ListChartVersions(req.Request.Context(), url, chartName, secName, projectName, skipCache)
if err != nil {
bcode.ReturnError(req, res, err)
return
Expand All @@ -228,6 +237,7 @@ func (h repository) chartValues(req *restful.Request, res *restful.Response) {
url := req.QueryParameter("repoUrl")
secName := req.QueryParameter("secretName")
chartName := req.QueryParameter("chart")
projectName := req.QueryParameter("project")
version := req.QueryParameter("version")
repoType := req.QueryParameter("repoType")
skipCache, err := isSkipCache(req)
Expand All @@ -236,7 +246,7 @@ func (h repository) chartValues(req *restful.Request, res *restful.Response) {
return
}

values, err := h.HelmService.ListChartValuesFiles(req.Request.Context(), url, chartName, version, secName, repoType, skipCache)
values, err := h.HelmService.ListChartValuesFiles(req.Request.Context(), url, chartName, version, secName, projectName, repoType, skipCache)
if err != nil {
bcode.ReturnError(req, res, err)
return
Expand Down

0 comments on commit 4ca9085

Please sign in to comment.