Skip to content

Commit

Permalink
调整helm路由结构 (#564)
Browse files Browse the repository at this point in the history
Co-authored-by: caoyingjunz <[email protected]>
  • Loading branch information
lbemi and caoyingjunz authored Dec 21, 2024
1 parent 4633bef commit 4f7fe61
Show file tree
Hide file tree
Showing 16 changed files with 819 additions and 268 deletions.
10 changes: 0 additions & 10 deletions api/server/router/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,4 @@ func (cr *clusterRouter) initRoutes(httpEngine *gin.Engine) {
indexerRoute.GET("/clusters/:cluster/resources/:resource/namespaces/:namespace", cr.listIndexerResources)
}

// 调用 helm 对象
helmRoute := httpEngine.Group(helmBaseURL)
{
// Helm Release API 列表
helmRoute.POST("/clusters/:cluster/v1/namespaces/:namespace/releases", cr.InstallRelease)
helmRoute.PUT("/clusters/:cluster/v1/namespaces/:namespace/releases", cr.UpgradeRelease)
helmRoute.DELETE("/clusters/:cluster/v1/namespaces/:namespace/releases/:name", cr.UninstallRelease)
helmRoute.GET("/clusters/:cluster/v1/namespaces/:namespace/releases/:name", cr.GetRelease)
helmRoute.GET("/clusters/:cluster/v1/namespaces/:namespace/releases", cr.ListReleases)
}
}
66 changes: 66 additions & 0 deletions api/server/router/helm/helm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2021 The Pixiu Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package helm

import (
"github.com/gin-gonic/gin"

"github.com/caoyingjunz/pixiu/cmd/app/options"
"github.com/caoyingjunz/pixiu/pkg/controller"
)

const (
helmBaseURL = "/pixiu/helms"
)

type helmRouter struct {
c controller.PixiuInterface
}

func NewRouter(o *options.Options) {
hr := &helmRouter{
c: o.Controller,
}
hr.initRoutes(o.HttpEngine)
}

func (hr *helmRouter) initRoutes(httpEngine *gin.Engine) {

helmRoute := httpEngine.Group(helmBaseURL)
{
// helm Repository
helmRoute.POST("/repositories", hr.createRepository)
helmRoute.PUT("/repositories/:id", hr.updateRepository)
helmRoute.DELETE("/repositories/:id", hr.deleteRepository)
helmRoute.GET("/repositories/:id", hr.getRepository)
helmRoute.GET("/repositories", hr.listRepositories)

helmRoute.GET("/repositories/:id/charts", hr.getRepoCharts)
helmRoute.GET("/repositories/charts", hr.getRepoChartsByURL)
helmRoute.GET("/repositories/values", hr.getChartValues)

// Helm Release
helmRoute.POST("/clusters/:cluster/namespaces/:namespace/releases", hr.InstallRelease)
helmRoute.PUT("/clusters/:cluster/namespaces/:namespace/releases", hr.UpgradeRelease)
helmRoute.DELETE("/clusters/:cluster/namespaces/:namespace/releases/:name", hr.UninstallRelease)
helmRoute.GET("/clusters/:cluster/namespaces/:namespace/releases/:name", hr.GetRelease)
helmRoute.GET("/clusters/:cluster/namespaces/:namespace/releases", hr.ListReleases)

helmRoute.GET("/clusters/:cluster/namespaces/:namespace/releases/:name/history", hr.GetReleaseHistory)
helmRoute.POST("/clusters/:cluster/namespaces/:namespace/releases/:name/rollback", hr.RollbackRelease)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package cluster
package helm

import (
"github.com/gin-gonic/gin"
Expand All @@ -23,10 +23,10 @@ import (
"github.com/caoyingjunz/pixiu/pkg/types"
)

// GetRelease retrieves a release by its name from the specified namespace and cluster
// GetRelease retrieves a release by its name in the specified namespace and cluster
//
// @Summary get a release by name
// @Description retrieves a release from the specified namespace and cluster using the provided name
// @Summary get a release
// @Description retrieves a release from the specified namespace and cluster
// @Tags helm
// @Accept json
// @Produce json
Expand All @@ -37,8 +37,8 @@ import (
// @Failure 400 {object} httputils.Response
// @Failure 404 {object} httputils.Response
// @Failure 500 {object} httputils.Response
// @Router /helm/release/{cluster}/{namespace}/{name} [get]
func (cr *clusterRouter) GetRelease(c *gin.Context) {
// @Router /helm/releases/{cluster}/{namespace}/{name} [get]
func (hr *helmRouter) GetRelease(c *gin.Context) {
r := httputils.NewResponse()
var (
err error
Expand All @@ -49,18 +49,17 @@ func (cr *clusterRouter) GetRelease(c *gin.Context) {
return
}

if r.Result, err = cr.c.Cluster().Helm(helmMeta.Cluster).Releases(helmMeta.Namespace).GetRelease(c, helmMeta.Name); err != nil {
if r.Result, err = hr.c.Helm().Release(helmMeta.Cluster, helmMeta.Namespace).Get(c, helmMeta.Name); err != nil {
httputils.SetFailed(c, r, err)
return
}

httputils.SetSuccess(c, r)
}

// ListReleases retrieves a list of all releases in the specified namespace and cluster
// ListReleases lists all releases in the specified namespace and cluster
//
// @Summary list all releases
// @Description retrieves a list of all releases from the specified namespace and cluster
// @Summary list releases
// @Description lists all releases in the specified namespace and cluster
// @Tags helm
// @Accept json
// @Produce json
Expand All @@ -71,7 +70,7 @@ func (cr *clusterRouter) GetRelease(c *gin.Context) {
// @Failure 404 {object} httputils.Response
// @Failure 500 {object} httputils.Response
// @Router /helm/releases/{cluster}/{namespace} [get]
func (cr *clusterRouter) ListReleases(c *gin.Context) {
func (hr *helmRouter) ListReleases(c *gin.Context) {
r := httputils.NewResponse()
var (
err error
Expand All @@ -82,7 +81,7 @@ func (cr *clusterRouter) ListReleases(c *gin.Context) {
return
}

if r.Result, err = cr.c.Cluster().Helm(helmMeta.Cluster).Releases(helmMeta.Namespace).ListRelease(c); err != nil {
if r.Result, err = hr.c.Helm().Release(helmMeta.Cluster, helmMeta.Namespace).List(c); err != nil {
httputils.SetFailed(c, r, err)
return
}
Expand All @@ -92,8 +91,8 @@ func (cr *clusterRouter) ListReleases(c *gin.Context) {

// InstallRelease installs a new release in the specified namespace and cluster
//
// @Summary install a new release
// @Description installs a new release into the specified Kubernetes namespace and cluster
// @Summary install a release
// @Description installs a release in the specified Kubernetes namespace and cluster
// @Tags helm
// @Accept json
// @Produce json
Expand All @@ -102,10 +101,9 @@ func (cr *clusterRouter) ListReleases(c *gin.Context) {
// @Param body body types.ReleaseForm true "Release information"
// @Success 200 {object} httputils.Response
// @Failure 400 {object} httputils.Response
// @Failure 404 {object} httputils.Response
// @Failure 500 {object} httputils.Response
// @Router /helm/releases/{cluster}/{namespace} [post]
func (cr *clusterRouter) InstallRelease(c *gin.Context) {
func (hr *helmRouter) InstallRelease(c *gin.Context) {
r := httputils.NewResponse()
var (
err error
Expand All @@ -117,7 +115,7 @@ func (cr *clusterRouter) InstallRelease(c *gin.Context) {
return
}

if r.Result, err = cr.c.Cluster().Helm(helmMeta.Cluster).Releases(helmMeta.Namespace).InstallRelease(c, &releaseOpt); err != nil {
if r.Result, err = hr.c.Helm().Release(helmMeta.Cluster, helmMeta.Namespace).Install(c, &releaseOpt); err != nil {
httputils.SetFailed(c, r, err)
return
}
Expand All @@ -128,7 +126,7 @@ func (cr *clusterRouter) InstallRelease(c *gin.Context) {
// UninstallRelease uninstalls a release from the specified namespace and cluster
//
// @Summary uninstall a release
// @Description uninstalls a release from the specified namespace and cluster
// @Description uninstalls a release from the specified Kubernetes namespace and cluster
// @Tags helm
// @Accept json
// @Produce json
Expand All @@ -140,7 +138,7 @@ func (cr *clusterRouter) InstallRelease(c *gin.Context) {
// @Failure 404 {object} httputils.Response
// @Failure 500 {object} httputils.Response
// @Router /helm/releases/{cluster}/{namespace}/{name} [delete]
func (cr *clusterRouter) UninstallRelease(c *gin.Context) {
func (hr *helmRouter) UninstallRelease(c *gin.Context) {
r := httputils.NewResponse()
var (
err error
Expand All @@ -151,7 +149,7 @@ func (cr *clusterRouter) UninstallRelease(c *gin.Context) {
return
}

if r.Result, err = cr.c.Cluster().Helm(helmMeta.Cluster).Releases(helmMeta.Namespace).UninstallRelease(c, helmMeta.Name); err != nil {
if r.Result, err = hr.c.Helm().Release(helmMeta.Cluster, helmMeta.Namespace).Uninstall(c, helmMeta.Name); err != nil {
httputils.SetFailed(c, r, err)
return
}
Expand All @@ -162,7 +160,7 @@ func (cr *clusterRouter) UninstallRelease(c *gin.Context) {
// UpgradeRelease upgrades a release in the specified namespace and cluster
//
// @Summary upgrade a release
// @Description upgrades a release in the specified namespace and cluster
// @Description upgrades a release in the specified Kubernetes namespace and cluster
// @Tags helm
// @Accept json
// @Produce json
Expand All @@ -172,10 +170,9 @@ func (cr *clusterRouter) UninstallRelease(c *gin.Context) {
// @Param body body types.ReleaseForm true "Release information"
// @Success 200 {object} httputils.Response
// @Failure 400 {object} httputils.Response
// @Failure 404 {object} httputils.Response
// @Failure 500 {object} httputils.Response
// @Router /helm/releases/{cluster}/{namespace}/{name} [put]
func (cr *clusterRouter) UpgradeRelease(c *gin.Context) {
func (hr *helmRouter) UpgradeRelease(c *gin.Context) {
r := httputils.NewResponse()
var (
err error
Expand All @@ -187,7 +184,7 @@ func (cr *clusterRouter) UpgradeRelease(c *gin.Context) {
return
}

if r.Result, err = cr.c.Cluster().Helm(helmMeta.Cluster).Releases(helmMeta.Namespace).UpgradeRelease(c, &releaseOpt); err != nil {
if r.Result, err = hr.c.Helm().Release(helmMeta.Cluster, helmMeta.Namespace).Upgrade(c, &releaseOpt); err != nil {
httputils.SetFailed(c, r, err)
return
}
Expand All @@ -210,7 +207,7 @@ func (cr *clusterRouter) UpgradeRelease(c *gin.Context) {
// @Failure 404 {object} httputils.Response
// @Failure 500 {object} httputils.Response
// @Router /helm/releases/history/{cluster}/{namespace}/{name} [get]
func (cr *clusterRouter) GetReleaseHistory(c *gin.Context) {
func (hr *helmRouter) GetReleaseHistory(c *gin.Context) {
r := httputils.NewResponse()
var (
err error
Expand All @@ -221,31 +218,31 @@ func (cr *clusterRouter) GetReleaseHistory(c *gin.Context) {
return
}

if r.Result, err = cr.c.Cluster().Helm(helmMeta.Cluster).Releases(helmMeta.Namespace).GetReleaseHistory(c, helmMeta.Name); err != nil {
if r.Result, err = hr.c.Helm().Release(helmMeta.Cluster, helmMeta.Namespace).History(c, helmMeta.Name); err != nil {
httputils.SetFailed(c, r, err)
return
}

httputils.SetSuccess(c, r)
}

// RollbackRelease rolls back a release in the specified namespace and cluster to a specific revision
// RollbackRelease rolls back a release in the specified namespace and cluster to the specified revision
//
// @Summary rollback a release
// @Description rolls back a release from the specified Kubernetes namespace and cluster to a specific revision
// @Description rolls back a release from the specified Kubernetes namespace and cluster to the specified revision
// @Tags helm
// @Accept json
// @Produce json
// @Param cluster path string true "Kubernetes cluster name"
// @Param namespace path string true "Kubernetes namespace"
// @Param name path string true "Release name"
// @Param version query int true "Release version"
// @Param version query int true "Release revision"
// @Success 200 {object} httputils.Response
// @Failure 400 {object} httputils.Response
// @Failure 404 {object} httputils.Response
// @Failure 500 {object} httputils.Response
// @Router /helm/releases/rollback/{cluster}/{namespace}/{name} [post]
func (cr *clusterRouter) RollbackRelease(c *gin.Context) {
func (hr *helmRouter) RollbackRelease(c *gin.Context) {
r := httputils.NewResponse()
var (
err error
Expand All @@ -257,7 +254,7 @@ func (cr *clusterRouter) RollbackRelease(c *gin.Context) {
return
}

if err = cr.c.Cluster().Helm(helmMeta.Cluster).Releases(helmMeta.Namespace).RollbackRelease(c, helmMeta.Name, reverionMeta.Version); err != nil {
if err = hr.c.Helm().Release(helmMeta.Cluster, helmMeta.Namespace).Rollback(c, helmMeta.Name, reverionMeta.Version); err != nil {
httputils.SetFailed(c, r, err)
return
}
Expand Down
Loading

0 comments on commit 4f7fe61

Please sign in to comment.