Skip to content

Commit

Permalink
harbor -> registry && fix registry update bug
Browse files Browse the repository at this point in the history
  • Loading branch information
yeka committed Nov 11, 2022
1 parent 4515e61 commit b412152
Show file tree
Hide file tree
Showing 28 changed files with 623 additions and 188 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
coverage.data

.DS_Store
core/__debug_bin
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ for example pushing data to cmdb or callback.
prometheus, with grafana, we can draw a straightforward diagram.
* With swagger, apis are supported on swagger.
* Ci depends on tekton, Horizon triggers tekton pipeline,
tekton builds and pushs image to harbor.
tekton builds and pushs image to image registry.
* CD depends on argoCD, argo calls helm.
Helm renders files deploys on kubernetes.
* Horizon defines all kinds of template for different projects,
Expand Down
8 changes: 4 additions & 4 deletions core/controller/cluster/controller_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ func (c *controller) DeleteCluster(ctx context.Context, clusterID uint, hard boo
return
}

// 2. delete rg repository
// 2. delete image
rg, err := c.registryFty.GetRegistryByConfig(newctx, &registry.Config{
Server: regionEntity.Registry.Server,
Token: regionEntity.Registry.Token,
Expand All @@ -784,9 +784,9 @@ func (c *controller) DeleteCluster(ctx context.Context, clusterID uint, hard boo
}

if rg != nil {
if err = rg.DeleteRepository(newctx, application.Name, cluster.Name); err != nil {
// log error, not return here, delete harbor repository failed has no effect
log.Errorf(newctx, "failed to delete harbor repository: %v, err: %v", cluster.Name, err)
if err = rg.DeleteImage(newctx, application.Name, cluster.Name); err != nil {
// log error, not return here, delete image failed has no effect
log.Errorf(newctx, "failed to delete image: %v, err: %v", cluster.Name, err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/controller/cluster/controller_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
groupservice "g.hz.netease.com/horizon/pkg/group/service"
membermodels "g.hz.netease.com/horizon/pkg/member/models"
regionmodels "g.hz.netease.com/horizon/pkg/region/models"
harbordao "g.hz.netease.com/horizon/pkg/registry/dao"
registrydao "g.hz.netease.com/horizon/pkg/registry/dao"
registrymodels "g.hz.netease.com/horizon/pkg/registry/models"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -278,7 +278,7 @@ func testControllerFreeOrDeleteClusterFailed(t *testing.T) {
regionMgr: manager.RegionMgr,
}

id, err := harbordao.NewDAO(db).Create(ctx, &registrymodels.Registry{
id, err := registrydao.NewDAO(db).Create(ctx, &registrymodels.Registry{
Server: "http://127.0.0.1",
})
assert.Nil(t, err)
Expand Down
29 changes: 2 additions & 27 deletions core/controller/cluster/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import (
"g.hz.netease.com/horizon/pkg/param/managerparam"
prmodels "g.hz.netease.com/horizon/pkg/pipelinerun/models"
regionmodels "g.hz.netease.com/horizon/pkg/region/models"
harbordao "g.hz.netease.com/horizon/pkg/registry/dao"
registrydao "g.hz.netease.com/horizon/pkg/registry/dao"
registrymodels "g.hz.netease.com/horizon/pkg/registry/models"
"g.hz.netease.com/horizon/pkg/server/global"
"g.hz.netease.com/horizon/pkg/server/middleware/requestid"
Expand Down Expand Up @@ -520,7 +520,7 @@ func test(t *testing.T) {
envMgr := manager.EnvMgr
regionMgr := manager.RegionMgr
groupMgr := manager.GroupManager
registryDAO := harbordao.NewDAO(db)
registryDAO := registrydao.NewDAO(db)
envRegionMgr := manager.EnvRegionMgr

// init data
Expand Down Expand Up @@ -1160,7 +1160,6 @@ func testV2(t *testing.T) {
regionMgr := manager.RegionMgr
groupMgr := manager.GroupManager
envRegionMgr := manager.EnvRegionMgr
// harborDAO := harbordao.NewDAO(db)

// init data
group, err := groupMgr.Create(ctx, &groupmodels.Group{
Expand Down Expand Up @@ -1198,30 +1197,6 @@ func testV2(t *testing.T) {
})
assert.Nil(t, err)
assert.NotNil(t, tr)
/*
id, err := harborDAO.Create(ctx, &registrymodels.Harbor{
Server: "https://harbor.com",
Token: "xxx",
PreheatPolicyID: 1,
})
assert.Nil(t, err)
assert.NotNil(t, id)
region, err := regionMgr.Create(ctx, &regionmodels.Region{
Name: "hz",
DisplayName: "HZ",
HarborID: id,
})
assert.Nil(t, err)
assert.NotNil(t, region)
er, err := envRegionMgr.CreateEnvironmentRegion(ctx, &envregionmodels.EnvironmentRegion{
EnvironmentName: "test",
RegionName: "hz",
})
assert.Nil(t, err)
assert.NotNil(t, er)
*/

c = &controller{
clusterMgr: manager.ClusterMgr,
Expand Down
42 changes: 10 additions & 32 deletions core/controller/registry/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ import (
var kindCache []string

type Controller interface {
// Create a harbor
// Create a registry
Create(ctx context.Context, request *CreateRegistryRequest) (uint, error)
// ListAll list all harbors
// ListAll list all registries
ListAll(ctx context.Context) (Registries, error)
// UpdateByID update a harbor
// UpdateByID update a registry
UpdateByID(ctx context.Context, id uint, request *UpdateRegistryRequest) error
// DeleteByID delete a harbor by id
// DeleteByID delete a registry by id
DeleteByID(ctx context.Context, id uint) error
GetByID(ctx context.Context, id uint) (*Registry, error)
GetKinds(ctx context.Context) []string
}

func NewController(param *param.Param) Controller {
return &controller{registryManager: param.HarborManager}
return &controller{registryManager: param.RegistryManager}
}

type controller struct {
Expand All @@ -50,16 +50,16 @@ func (c controller) Create(ctx context.Context, request *CreateRegistryRequest)
}

func (c controller) ListAll(ctx context.Context) (Registries, error) {
harbors, err := c.registryManager.ListAll(ctx)
registries, err := c.registryManager.ListAll(ctx)
if err != nil {
return nil, err
}

return ofRegistryModels(harbors), nil
return ofRegistryModels(registries), nil
}

func (c controller) UpdateByID(ctx context.Context, id uint, request *UpdateRegistryRequest) error {
oldRegistry, err := c.registryManager.GetByID(ctx, id)
_, err := c.registryManager.GetByID(ctx, id)
if err != nil {
return err
}
Expand All @@ -72,30 +72,8 @@ func (c controller) UpdateByID(ctx context.Context, id uint, request *UpdateRegi
Path: request.Path,
}

if registry.Name == "" {
registry.Name = oldRegistry.Name
}

if registry.Server == "" {
registry.Server = oldRegistry.Server
}

if registry.Token == "" {
registry.Token = oldRegistry.Token
}

if registry.Kind == "" {
registry.Kind = oldRegistry.Kind
}

if registry.Path == "" {
registry.Path = oldRegistry.Path
}

if request.InsecureSkipTLSVerify != nil {
registry.InsecureSkipTLSVerify = *request.InsecureSkipTLSVerify
} else {
registry.InsecureSkipTLSVerify = oldRegistry.InsecureSkipTLSVerify
}

err = c.registryManager.UpdateByID(ctx, id, registry)
Expand All @@ -116,12 +94,12 @@ func (c controller) DeleteByID(ctx context.Context, id uint) error {
}

func (c controller) GetByID(ctx context.Context, id uint) (*Registry, error) {
harbor, err := c.registryManager.GetByID(ctx, id)
registry, err := c.registryManager.GetByID(ctx, id)
if err != nil {
return nil, err
}

return ofRegistryModel(harbor), nil
return ofRegistryModel(registry), nil
}

func (c controller) GetKinds(ctx context.Context) []string {
Expand Down
6 changes: 3 additions & 3 deletions core/controller/registry/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ type Registry struct {
type Registries []*Registry

func ofRegistryModels(entities []*models.Registry) Registries {
harbors := make([]*Registry, 0)
registries := make([]*Registry, 0)
for _, entity := range entities {
harbors = append(harbors, ofRegistryModel(entity))
registries = append(registries, ofRegistryModel(entity))
}

return harbors
return registries
}

func ofRegistryModel(entity *models.Registry) *Registry {
Expand Down
2 changes: 1 addition & 1 deletion core/controller/template/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Controller interface {
ListTemplateReleaseByTemplateID(ctx context.Context, templateID uint) (Releases, error)
// CreateTemplate creates a template with a release under a group
CreateTemplate(ctx context.Context, groupID uint, request CreateTemplateRequest) (*Template, error)
// CreateRelease downloads template archive and push it to harbor,
// CreateRelease downloads template archive and push it to chatmusuem,
// then creates a template release in database.
CreateRelease(ctx context.Context, templateID uint, request CreateReleaseRequest) (*Release, error)
// GetTemplate gets template by templateID
Expand Down
14 changes: 6 additions & 8 deletions core/errors/horizonerrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ var (
RegionInDB = sourceType{name: "RegionInDB"}
GroupInDB = sourceType{name: "GroupInDB"}
K8SClient = sourceType{name: "K8SClient"}
Harbor = sourceType{name: "Harbor"}
RegistryInDB = sourceType{name: "RegistryInDB"}
Pipelinerun = sourceType{name: "Pipelinerun"}
PipelinerunInTekton = sourceType{name: "PipelinerunInTekton"}
Expand Down Expand Up @@ -59,11 +58,10 @@ var (
TektonClient = sourceType{name: "TektonClient"}
TektonCollector = sourceType{name: "TektonCollector"}

HelmRepo = sourceType{name: "HelmRepo"}
OAuthInDB = sourceType{name: "OauthAppClient"}
TokenInDB = sourceType{name: "TokenInDB"}
HarborChartURL = sourceType{name: "HarborChartURL"}
ChartFile = sourceType{name: "ChartFile"}
HelmRepo = sourceType{name: "HelmRepo"}
OAuthInDB = sourceType{name: "OauthAppClient"}
TokenInDB = sourceType{name: "TokenInDB"}
ChartFile = sourceType{name: "ChartFile"}

// identity provider
Oauth2Token = sourceType{name: "Oauth2Token"}
Expand Down Expand Up @@ -249,8 +247,8 @@ var (
ErrOAuthTokenFormatError = errors.New("Oauth token format error")
ErrOAuthNotGroupOwnerType = errors.New("not group oauth app")

// ErrHarborUsedByRegions used when deleting a harbor that is still used by regions
ErrHarborUsedByRegions = errors.New("cannot delete a harbor when used by regions")
// ErrRegistryUsedByRegions used when deleting a registry that is still used by regions
ErrRegistryUsedByRegions = errors.New("cannot delete a registry when used by regions")

// ErrRegionUsedByClusters used when deleting a region that is still used by clusters
ErrRegionUsedByClusters = errors.New("cannot delete a region when used by clusters")
Expand Down
2 changes: 1 addition & 1 deletion core/http/api/v1/region/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (a *API) DeleteByID(c *gin.Context) {
response.AbortWithRPCError(c, rpcerror.NotFoundError.WithErrMsg(err.Error()))
return
}
if perror.Cause(err) == herrors.ErrHarborUsedByRegions {
if perror.Cause(err) == herrors.ErrRegistryUsedByRegions {
response.AbortWithRPCError(c, rpcerror.BadRequestError.WithErrMsg(err.Error()))
return
}
Expand Down
18 changes: 9 additions & 9 deletions core/http/api/v1/registry/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

const (
// param
_harborIDParam = "harborID"
_registryIDParam = "registryID"
)

type API struct {
Expand All @@ -26,12 +26,12 @@ func NewAPI(ctl registry.Controller) *API {
}

func (a *API) ListAll(c *gin.Context) {
harbors, err := a.registryCtl.ListAll(c)
registries, err := a.registryCtl.ListAll(c)
if err != nil {
response.AbortWithRPCError(c, rpcerror.InternalError.WithErrMsg(err.Error()))
return
}
response.SuccessWithData(c, harbors)
response.SuccessWithData(c, registries)
}

func (a *API) Create(c *gin.Context) {
Expand All @@ -52,7 +52,7 @@ func (a *API) Create(c *gin.Context) {
}

func (a *API) UpdateByID(c *gin.Context) {
idStr := c.Param(_harborIDParam)
idStr := c.Param(_registryIDParam)
id, err := strconv.ParseUint(idStr, 10, 0)
if err != nil {
response.AbortWithRPCError(c, rpcerror.ParamError.WithErrMsg(fmt.Sprintf("invalid id: %s, err: %s",
Expand Down Expand Up @@ -81,7 +81,7 @@ func (a *API) UpdateByID(c *gin.Context) {
}

func (a *API) DeleteByID(c *gin.Context) {
idStr := c.Param(_harborIDParam)
idStr := c.Param(_registryIDParam)
id, err := strconv.ParseUint(idStr, 10, 0)
if err != nil {
response.AbortWithRPCError(c, rpcerror.ParamError.WithErrMsg(fmt.Sprintf("invalid id: %s, err: %s",
Expand All @@ -95,7 +95,7 @@ func (a *API) DeleteByID(c *gin.Context) {
response.AbortWithRPCError(c, rpcerror.NotFoundError.WithErrMsg(err.Error()))
return
}
if perror.Cause(err) == herrors.ErrHarborUsedByRegions {
if perror.Cause(err) == herrors.ErrRegistryUsedByRegions {
response.AbortWithRPCError(c, rpcerror.BadRequestError.WithErrMsg(err.Error()))
return
}
Expand All @@ -107,15 +107,15 @@ func (a *API) DeleteByID(c *gin.Context) {
}

func (a *API) GetByID(c *gin.Context) {
idStr := c.Param(_harborIDParam)
idStr := c.Param(_registryIDParam)
id, err := strconv.ParseUint(idStr, 10, 0)
if err != nil {
response.AbortWithRPCError(c, rpcerror.ParamError.WithErrMsg(fmt.Sprintf("invalid id: %s, err: %s",
idStr, err.Error())))
return
}

harborEntity, err := a.registryCtl.GetByID(c, uint(id))
registry, err := a.registryCtl.GetByID(c, uint(id))
if err != nil {
if _, ok := perror.Cause(err).(*herrors.HorizonErrNotFound); ok {
response.AbortWithRPCError(c, rpcerror.NotFoundError.WithErrMsg(err.Error()))
Expand All @@ -125,7 +125,7 @@ func (a *API) GetByID(c *gin.Context) {
return
}

response.SuccessWithData(c, harborEntity)
response.SuccessWithData(c, registry)
}

func (a *API) GetKinds(c *gin.Context) {
Expand Down
6 changes: 3 additions & 3 deletions core/http/api/v1/registry/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ func RegisterRoutes(engine *gin.Engine, api *API) {
HandlerFunc: api.Create,
}, {
Method: http.MethodGet,
Pattern: fmt.Sprintf("/:%v", _harborIDParam),
Pattern: fmt.Sprintf("/:%v", _registryIDParam),
HandlerFunc: api.GetByID,
}, {
Method: http.MethodPut,
Pattern: fmt.Sprintf("/:%v", _harborIDParam),
Pattern: fmt.Sprintf("/:%v", _registryIDParam),
HandlerFunc: api.UpdateByID,
}, {
Method: http.MethodDelete,
Pattern: fmt.Sprintf("/:%v", _harborIDParam),
Pattern: fmt.Sprintf("/:%v", _registryIDParam),
HandlerFunc: api.DeleteByID,
}, {
Method: http.MethodGet,
Expand Down
Loading

0 comments on commit b412152

Please sign in to comment.