Skip to content

Commit

Permalink
Merge pull request #1743 from goodrain/merageto2306
Browse files Browse the repository at this point in the history
perf: openapi into main
  • Loading branch information
yangkaa authored Aug 23, 2023
2 parents cfbc59a + b859881 commit dfea24e
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 25 deletions.
1 change: 1 addition & 0 deletions api/api/api_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ type TenantInterface interface {
type HelmInterface interface {
CheckHelmApp(w http.ResponseWriter, r *http.Request)
GetChartInformation(w http.ResponseWriter, r *http.Request)
GetYamlByChart(w http.ResponseWriter, r *http.Request)
}

// ServiceInterface ServiceInterface
Expand Down
1 change: 1 addition & 0 deletions api/api_routers/version2/v2Routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (v2 *V2) helmRouter() chi.Router {
r := chi.NewRouter()
r.Get("/check_helm_app", controller.GetManager().CheckHelmApp)
r.Get("/get_chart_information", controller.GetManager().GetChartInformation)
r.Get("/get_chart_yaml", controller.GetManager().GetYamlByChart)
return r
}

Expand Down
2 changes: 1 addition & 1 deletion api/controller/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (a *AppStruct) ImportID(w http.ResponseWriter, r *http.Request) {
continue
}
ex := filepath.Ext(dir.Name())
if ex != ".zip" && ex != ".tar.gz" && ex != ".gz" {
if ex != ".zip" && ex != ".tar.gz" && ex != ".gz" && ex != ".tgz" {
continue
}
appArr = append(appArr, dir.Name())
Expand Down
21 changes: 21 additions & 0 deletions api/controller/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,24 @@ func (t *HelmStruct) GetChartInformation(w http.ResponseWriter, r *http.Request)
}
httputil.ReturnSuccess(r, w, chartVersion)
}

//GetYamlByChart -
func (t *HelmStruct) GetYamlByChart(w http.ResponseWriter, r *http.Request) {
var yc api_model.GetYamlByChart
if ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &yc, nil); !ok {
return
}
data := map[string]string{"checkAdopt": "true"}
if yc.EventID == "" {
httputil.ReturnError(r, w, 400, "Failed to parse eventID.")
return
}
chartPath := fmt.Sprintf("%s/import/%s/%s", handler.GetAppHandler().GetStaticDir(), yc.EventID, yc.FileName)
yaml, err := handler.GetHelmManager().GetYamlByChart(chartPath, yc.Namespace, yc.Name, yc.Version, []string{})
if err != nil {
data["checkAdopt"] = "false"
data["yaml"] = err.Error()
}
data["yaml"] = yaml
httputil.ReturnSuccess(r, w, data)
}
15 changes: 12 additions & 3 deletions api/handler/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (h *HelmAction) GetChartInformation(chart api_model.ChartInformation) (*[]a

// CheckHelmApp check helm app
func (h *HelmAction) CheckHelmApp(checkHelmApp api_model.CheckHelmApp) (string, error) {
helmAppYaml, err := GetHelmAppYaml(checkHelmApp.Name, checkHelmApp.Chart, checkHelmApp.Version, checkHelmApp.Namespace, checkHelmApp.Overrides)
helmAppYaml, err := GetHelmAppYaml(checkHelmApp.Name, checkHelmApp.Chart, checkHelmApp.Version, checkHelmApp.Namespace, "", checkHelmApp.Overrides)
if err != nil {
return "", errors.Wrap(err, "helm app check failed")
}
Expand All @@ -121,14 +121,14 @@ func (h *HelmAction) AddHelmRepo(helmRepo api_model.CheckHelmApp) error {
}

//GetHelmAppYaml get helm app yaml
func GetHelmAppYaml(name, chart, version, namespace string, overrides []string) (string, error) {
func GetHelmAppYaml(name, chart, version, namespace, chartPath string, overrides []string) (string, error) {
logrus.Info("get into GetHelmAppYaml function")
helmCmd, err := helm.NewHelm(namespace, repoFile, repoCache)
if err != nil {
logrus.Errorf("Failed to create help client:%v", err)
return "", err
}
release, err := helmCmd.Install(name, chart, version, overrides)
release, err := helmCmd.Install(chartPath, name, chart, version, overrides)
if err != nil {
logrus.Errorf("helm --dry-run install failure: %v", err)
return "", err
Expand All @@ -150,3 +150,12 @@ func UpdateRepo(names string) error {
}
return nil
}

// GetYamlByChart get yaml by chart
func (h *HelmAction) GetYamlByChart(chartPath, namespace, name, version string, overrides []string) (string, error) {
helmAppYaml, err := GetHelmAppYaml(name, "", version, namespace, chartPath, overrides)
if err != nil {
return "", errors.Wrap(err, "helm app check failed")
}
return helmAppYaml, nil
}
1 change: 1 addition & 0 deletions api/handler/helmhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type HelmHandler interface {
CheckHelmApp(checkHelmApp api_model.CheckHelmApp) (string, error)
GetChartInformation(chart api_model.ChartInformation) (*[]api_model.HelmChartInformation, *util.APIHandleError)
UpdateHelmRepo(names string) error
GetYamlByChart(chartPath, namespace, name, version string, overrides []string) (string, error)
}
9 changes: 9 additions & 0 deletions api/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,15 @@ type ChartInformation struct {
ChartName string `json:"chart_name"`
}

// GetYamlByChart -
type GetYamlByChart struct {
EventID string `json:"event_id"`
Name string `json:"name"`
FileName string `json:"file_name"`
Version string `json:"version"`
Namespace string `json:"namespace"`
}

const (
//CreateSuccess -
CreateSuccess = 1
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ replace (
github.com/docker/docker => github.com/docker/docker v20.10.2+incompatible
github.com/envoyproxy/go-control-plane => github.com/envoyproxy/go-control-plane v0.9.5
github.com/godbus/dbus => github.com/godbus/dbus/v5 v5.0.4
github.com/goodrain/rainbond-oam => github.com/goodrain/rainbond-oam v0.0.0-20230117125005-98bcc9233a7f
github.com/goodrain/rainbond-oam => github.com/goodrain/rainbond-oam v0.0.0-20230614023629-d4b302e90dfa
github.com/prometheus/common => github.com/prometheus/common v0.15.0
google.golang.org/grpc => google.golang.org/grpc v1.27.1
helm.sh/helm/v3 => helm.sh/helm/v3 v3.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,8 @@ github.com/gomodule/redigo v1.8.2 h1:H5XSIre1MB5NbPYFp+i1NBbb5qN1W8Y8YAQoAYbkm8k
github.com/gomodule/redigo v1.8.2/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0=
github.com/goodrain/gorm-bulk-upsert v1.0.1-0.20210608013724-7e7870d16357 h1:kdSSrpA5yNvgqbfpMlEr8bvQWiLc1Uz9g0vYf3JVT7s=
github.com/goodrain/gorm-bulk-upsert v1.0.1-0.20210608013724-7e7870d16357/go.mod h1:b7/GgeVNbf/SFw4FYIWslxNV5I10C9Mhf/++3jsDk3M=
github.com/goodrain/rainbond-oam v0.0.0-20230117125005-98bcc9233a7f h1:lP3vUduSD4YlyznmmMkeZW4icM4zNZR7daEdXhqw3uk=
github.com/goodrain/rainbond-oam v0.0.0-20230117125005-98bcc9233a7f/go.mod h1:LVLCqKFl0n+huV8Q9f1yFNhcBvF9U4c/q5UQF9nba4A=
github.com/goodrain/rainbond-oam v0.0.0-20230614023629-d4b302e90dfa h1:/fqrFF8oyPUkvb+r8B1sRd0GHfmBkragUBPDv8t74wI=
github.com/goodrain/rainbond-oam v0.0.0-20230614023629-d4b302e90dfa/go.mod h1:LVLCqKFl0n+huV8Q9f1yFNhcBvF9U4c/q5UQF9nba4A=
github.com/goodrain/rainbond-operator v1.3.1-0.20210401055914-f8fe4bf89a21 h1:iCPI96slvJv88iPc1NJW8zhpkiza0kwB0jtsuZIJLRQ=
github.com/goodrain/rainbond-operator v1.3.1-0.20210401055914-f8fe4bf89a21/go.mod h1:jcQfNoxO67nkLalCmgihYrdWF82TKyuPW032tgGdqVY=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
Expand Down
22 changes: 13 additions & 9 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ func (h *Helm) UpdateRepo(names string) error {

// PreInstall -
func (h *Helm) PreInstall(name, chart, version string) error {
_, err := h.install(name, chart, version, nil, true, ioutil.Discard)
_, err := h.install(name, chart, version, "", nil, true, ioutil.Discard)
return err
}

// Install -
func (h *Helm) Install(name, chart, version string, overrides []string) (*release.Release, error) {
release, err := h.install(name, chart, version, overrides, true, ioutil.Discard)
func (h *Helm) Install(chartPath, name, chart, version string, overrides []string) (*release.Release, error) {
release, err := h.install(name, chart, version, chartPath, overrides, true, ioutil.Discard)
return release, err
}

Expand Down Expand Up @@ -171,20 +171,24 @@ func (h *Helm) getDigest(chart, version string) (string, error) {
return "", errors.New(fmt.Sprintf("chart(%s) version(%s) not found", chart, version))
}

func (h *Helm) install(name, chart, version string, overrides []string, dryRun bool, out io.Writer) (*release.Release, error) {
func (h *Helm) install(name, chart, version, chartPath string, overrides []string, dryRun bool, out io.Writer) (*release.Release, error) {
client := action.NewInstall(h.cfg)
client.ReleaseName = name
client.Namespace = h.namespace
client.Version = version
client.DryRun = dryRun
//client.IsUpgrade = true
client.ClientOnly = true

cp, err := h.locateChart(chart, version)
if err != nil {
return nil, err
var cp string
if chartPath != ""{
cp = chartPath
} else {
res, err := h.locateChart(chart, version)
if err != nil {
return nil, err
}
cp = res
}

logrus.Debugf("CHART PATH: %s\n", cp)

p := getter.All(h.settings)
Expand Down
16 changes: 8 additions & 8 deletions worker/appm/volume/other.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ func (v *OtherVolume) CreateVolume(define *Define) error {
return "linux"
}(),
}
v.as.SetClaim(claim) // store claim to appService
v.as.SetClaim(claim) // store claim to appService
statefulset := v.as.GetStatefulSet() //有状态组件
vo := corev1.Volume{Name: volumeMountName}
vo.PersistentVolumeClaim = &corev1.PersistentVolumeClaimVolumeSource{ClaimName: claim.GetName(), ReadOnly: volumeReadOnly}
define.volumes = append(define.volumes, vo)
if shareFile {
v.as.SetClaimManually(claim)
if statefulset != nil {
statefulset.Spec.VolumeClaimTemplates = append(statefulset.Spec.VolumeClaimTemplates, *claim)
logrus.Debugf("stateset.Spec.VolumeClaimTemplates: %+v", statefulset.Spec.VolumeClaimTemplates)
} else {
statefulset := v.as.GetStatefulSet() //有状态组件
if statefulset != nil {
statefulset.Spec.VolumeClaimTemplates = append(statefulset.Spec.VolumeClaimTemplates, *claim)
logrus.Debugf("stateset.Spec.VolumeClaimTemplates: %+v", statefulset.Spec.VolumeClaimTemplates)
if shareFile {
v.as.SetClaimManually(claim)
}
define.volumes = append(define.volumes, vo)
}

vm := corev1.VolumeMount{
Expand Down
2 changes: 1 addition & 1 deletion worker/master/controller/helmapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func (a *App) installOrUpdate() error {

if errors.Is(err, driver.ErrReleaseNotFound) {
logrus.Debugf("name: %s; namespace: %s; chart: %s; install helm app", a.name, a.namespace, a.Chart())
if _, err := a.helmCmd.Install(a.name, a.Chart(), a.version, a.overrides); err != nil {
if _, err := a.helmCmd.Install("", a.name, a.Chart(), a.version, a.overrides); err != nil {
return err
}

Expand Down

0 comments on commit dfea24e

Please sign in to comment.