Skip to content

Commit

Permalink
Merge pull request #159 from kilosonc/feat/metatag
Browse files Browse the repository at this point in the history
feat: support tag for application
  • Loading branch information
kilosonc authored May 24, 2023
2 parents 420e4ba + 691a2ea commit 17edf24
Show file tree
Hide file tree
Showing 20 changed files with 415 additions and 187 deletions.
55 changes: 54 additions & 1 deletion core/controller/application/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import (
pipelinemanager "github.com/horizoncd/horizon/pkg/pipelinerun/pipeline/manager"
pipelinemodels "github.com/horizoncd/horizon/pkg/pipelinerun/pipeline/models"
regionmodels "github.com/horizoncd/horizon/pkg/region/models"
tagmanager "github.com/horizoncd/horizon/pkg/tag/manager"
tagmodels "github.com/horizoncd/horizon/pkg/tag/models"
trmanager "github.com/horizoncd/horizon/pkg/templaterelease/manager"
templateschema "github.com/horizoncd/horizon/pkg/templaterelease/schema"
usersvc "github.com/horizoncd/horizon/pkg/user/service"
Expand Down Expand Up @@ -93,6 +95,7 @@ type controller struct {
userSvc usersvc.Service
memberManager member.Manager
eventMgr eventmanager.Manager
tagMgr tagmanager.Manager
applicationRegionMgr applicationregionmanager.Manager
pipelinemanager pipelinemanager.Manager
buildSchema *build.Schema
Expand All @@ -113,6 +116,7 @@ func NewController(param *param.Param) Controller {
userSvc: param.UserSvc,
memberManager: param.MemberManager,
eventMgr: param.EventManager,
tagMgr: param.TagManager,
applicationRegionMgr: param.ApplicationRegionManager,
pipelinemanager: param.PipelineMgr,
buildSchema: param.BuildSchema,
Expand Down Expand Up @@ -149,7 +153,14 @@ func (c *controller) GetApplication(ctx context.Context, id uint) (_ *GetApplica
return nil, err
}
fullPath := fmt.Sprintf("%v/%v", group.FullPath, app.Name)
return ofApplicationModel(app, fullPath, trs, pipelineJSONBlob, applicationJSONBlob), nil

// 5. get tags
tags, err := c.tagMgr.ListByResourceTypeID(ctx, common.ResourceApplication, app.ID)
if err != nil {
return nil, err
}

return ofApplicationModel(app, fullPath, trs, pipelineJSONBlob, applicationJSONBlob, tags...), nil
}

func (c *controller) GetApplicationV2(ctx context.Context, id uint) (_ *GetApplicationResponseV2, err error) {
Expand All @@ -174,6 +185,12 @@ func (c *controller) GetApplicationV2(ctx context.Context, id uint) (_ *GetAppli
}
fullPath := fmt.Sprintf("%v/%v", group.FullPath, app.Name)

// 4. get tags
tags, err := c.tagMgr.ListByResourceTypeID(ctx, common.ResourceApplication, app.ID)
if err != nil {
return nil, err
}

resp := &GetApplicationResponseV2{
ID: id,
Name: app.Name,
Expand All @@ -186,6 +203,7 @@ func (c *controller) GetApplicationV2(ctx context.Context, id uint) (_ *GetAppli
return codemodels.NewGit(app.GitURL, app.GitSubfolder, app.GitRefType, app.GitRef)
}(),
BuildConfig: applicationRepo.BuildConf,
Tags: tagmodels.Tags(tags).IntoTagsBasic(),
TemplateInfo: func() *codemodels.TemplateInfo {
if app.Template == "" {
return nil
Expand Down Expand Up @@ -277,6 +295,14 @@ func (c *controller) CreateApplication(ctx context.Context, groupID uint,
return nil, err
}

// 7. create tags
if request.Tags != nil {
err = c.tagMgr.UpsertByResourceTypeID(ctx, common.ResourceApplication, applicationModel.ID, request.Tags)
if err != nil {
return nil, err
}
}

ret := ofApplicationModel(applicationModel, fullPath, trs,
request.TemplateInput.Pipeline, request.TemplateInput.Application)

Expand Down Expand Up @@ -384,6 +410,13 @@ func (c *controller) CreateApplicationV2(ctx context.Context, groupID uint,
return nil, err
}

if request.Tags != nil {
err = c.tagMgr.UpsertByResourceTypeID(ctx, common.ResourceApplication, applicationDBModel.ID, request.Tags)
if err != nil {
return nil, err
}
}

ret := &CreateApplicationResponseV2{
ID: applicationDBModel.ID,
Name: request.Name,
Expand Down Expand Up @@ -481,6 +514,14 @@ func (c *controller) UpdateApplication(ctx context.Context, id uint,
return nil, err
}

// 8. update tags
if request.Tags != nil {
err = c.tagMgr.UpsertByResourceTypeID(ctx, common.ResourceApplication, applicationModel.ID, request.Tags)
if err != nil {
return nil, err
}
}

return ofApplicationModel(applicationModel, fullPath, trs,
request.TemplateInput.Pipeline, request.TemplateInput.Application), nil
}
Expand Down Expand Up @@ -525,6 +566,18 @@ func (c *controller) UpdateApplicationV2(ctx context.Context, id uint,
// 4. update application in db
applicationModel := request.UpdateToApplicationModel(appExistsInDB)
_, err = c.applicationMgr.UpdateByID(ctx, id, applicationModel)
if err != nil {
return err
}

// 5. update tags
if request.Tags != nil {
err = c.tagMgr.UpsertByResourceTypeID(ctx, common.ResourceApplication, id, request.Tags)
if err != nil {
return err
}
}

return err
}

Expand Down
6 changes: 6 additions & 0 deletions core/controller/application/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
membermodels "github.com/horizoncd/horizon/pkg/member/models"
"github.com/horizoncd/horizon/pkg/param/managerparam"
regionmodels "github.com/horizoncd/horizon/pkg/region/models"
tagmodels "github.com/horizoncd/horizon/pkg/tag/models"
tmodels "github.com/horizoncd/horizon/pkg/template/models"
trmodels "github.com/horizoncd/horizon/pkg/templaterelease/models"
trschema "github.com/horizoncd/horizon/pkg/templaterelease/schema"
Expand Down Expand Up @@ -286,6 +287,9 @@ func TestMain(m *testing.M) {
if err := db.AutoMigrate(&membermodels.Member{}); err != nil {
panic(err)
}
if err := db.AutoMigrate(&tagmodels.Tag{}); err != nil {
panic(err)
}
if err := db.AutoMigrate(&eventmodels.Event{}); err != nil {
panic(err)
}
Expand Down Expand Up @@ -351,6 +355,7 @@ func Test(t *testing.T) {
c = &controller{
applicationGitRepo: applicationGitRepo,
templateSchemaGetter: templateSchemaGetter,
tagMgr: manager.TagManager,
applicationMgr: manager.ApplicationManager,
groupMgr: manager.GroupManager,
groupSvc: groupservice.NewService(manager),
Expand Down Expand Up @@ -506,6 +511,7 @@ func TestV2(t *testing.T) {
applicationGitRepo: applicationGitRepo,
templateSchemaGetter: templateSchemaGetter,
applicationMgr: manager.ApplicationManager,
tagMgr: manager.TagManager,
groupMgr: manager.GroupManager,
groupSvc: groupservice.NewService(manager),
templateReleaseMgr: manager.TemplateReleaseManager,
Expand Down
19 changes: 12 additions & 7 deletions core/controller/application/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ import (

"github.com/horizoncd/horizon/pkg/application/models"
codemodels "github.com/horizoncd/horizon/pkg/cluster/code"
tagmodels "github.com/horizoncd/horizon/pkg/tag/models"
trmodels "github.com/horizoncd/horizon/pkg/templaterelease/models"
)

// Base holds the parameters which can be updated of an application
type Base struct {
Description string `json:"description"`
Priority string `json:"priority"`
Template *Template `json:"template"`
Git *codemodels.Git `json:"git"`
TemplateInput *TemplateInput `json:"templateInput"`
Description string `json:"description"`
Priority string `json:"priority"`
Tags tagmodels.TagsBasic `json:"tags,omitempty"`
Template *Template `json:"template"`
Git *codemodels.Git `json:"git"`
TemplateInput *TemplateInput `json:"templateInput"`
}

type TemplateInput struct {
Expand Down Expand Up @@ -129,14 +131,16 @@ func (m *UpdateApplicationRequest) toApplicationModel(appExistsInDB *models.Appl

// ofApplicationModel transfer models.Application, templateInput, pipelineInput to GetApplicationResponse
func ofApplicationModel(app *models.Application, fullPath string, trs []*trmodels.TemplateRelease,
pipelineJSONBlob, applicationJSONBlob map[string]interface{}) *GetApplicationResponse {
pipelineJSONBlob, applicationJSONBlob map[string]interface{}, tags ...*tagmodels.Tag) *GetApplicationResponse {
var recommendedRelease string
for _, tr := range trs {
if *tr.Recommended {
recommendedRelease = tr.Name
}
}

tagsBasic := tagmodels.Tags(tags).IntoTagsBasic()

resp := &GetApplicationResponse{
CreateApplicationRequest: CreateApplicationRequest{
Base: Base{
Expand All @@ -147,7 +151,8 @@ func ofApplicationModel(app *models.Application, fullPath string, trs []*trmodel
Release: app.TemplateRelease,
RecommendedRelease: recommendedRelease,
},
Git: codemodels.NewGit(app.GitURL, app.GitSubfolder, app.GitRefType, app.GitRef),
Tags: tagsBasic,
Git: codemodels.NewGit(app.GitURL, app.GitSubfolder, app.GitRefType, app.GitRef),
TemplateInput: &TemplateInput{
Application: applicationJSONBlob,
Pipeline: pipelineJSONBlob,
Expand Down
13 changes: 8 additions & 5 deletions core/controller/application/v2models.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ import (

"github.com/horizoncd/horizon/pkg/application/models"
codemodels "github.com/horizoncd/horizon/pkg/cluster/code"
tagmodels "github.com/horizoncd/horizon/pkg/tag/models"
)

type GetApplicationResponseV2 struct {
ID uint `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Priority string `json:"priority"`
Git *codemodels.Git `json:"git"`
ID uint `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Priority string `json:"priority"`
Tags tagmodels.TagsBasic `json:"tags,omitempty"`
Git *codemodels.Git `json:"git"`

BuildConfig map[string]interface{} `json:"buildConfig"`
TemplateInfo *codemodels.TemplateInfo `json:"templateInfo"`
Expand All @@ -45,6 +47,7 @@ type CreateOrUpdateApplicationRequestV2 struct {
Name string `json:"name"`
Description string `json:"description"`
Priority *string `json:"priority"`
Tags tagmodels.TagsBasic `json:"tags,omitempty"`
Git *codemodels.Git `json:"git"`
BuildConfig map[string]interface{} `json:"buildConfig"`
TemplateInfo *codemodels.TemplateInfo `json:"templateInfo"`
Expand Down
24 changes: 9 additions & 15 deletions core/controller/applicationregion/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,30 +81,24 @@ func Test(t *testing.T) {
})
assert.Nil(t, err)
assert.NotNil(t, r3)
err = manager.TagManager.UpsertByResourceTypeID(ctx, common.ResourceRegion, r1.ID, []*tagmodels.Tag{
err = manager.TagManager.UpsertByResourceTypeID(ctx, common.ResourceRegion, r1.ID, []*tagmodels.TagBasic{
{
ResourceID: r1.ID,
ResourceType: common.ResourceRegion,
Key: "a",
Value: "1",
Key: "a",
Value: "1",
},
})
assert.Nil(t, err)
err = manager.TagManager.UpsertByResourceTypeID(ctx, common.ResourceRegion, r1.ID, []*tagmodels.Tag{
err = manager.TagManager.UpsertByResourceTypeID(ctx, common.ResourceRegion, r2.ID, []*tagmodels.TagBasic{
{
ResourceID: r2.ID,
ResourceType: common.ResourceRegion,
Key: "a",
Value: "1",
Key: "a",
Value: "1",
},
})
assert.Nil(t, err)
err = manager.TagManager.UpsertByResourceTypeID(ctx, common.ResourceRegion, r1.ID, []*tagmodels.Tag{
err = manager.TagManager.UpsertByResourceTypeID(ctx, common.ResourceRegion, r3.ID, []*tagmodels.TagBasic{
{
ResourceID: r3.ID,
ResourceType: common.ResourceRegion,
Key: "a",
Value: "1",
Key: "a",
Value: "1",
},
})
assert.Nil(t, err)
Expand Down
35 changes: 29 additions & 6 deletions core/controller/cluster/controller_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
membermodels "github.com/horizoncd/horizon/pkg/member/models"
regionmodels "github.com/horizoncd/horizon/pkg/region/models"
tagmanager "github.com/horizoncd/horizon/pkg/tag/manager"
tagmodels "github.com/horizoncd/horizon/pkg/tag/models"
"github.com/horizoncd/horizon/pkg/util/jsonschema"
"github.com/horizoncd/horizon/pkg/util/log"
"github.com/horizoncd/horizon/pkg/util/mergemap"
Expand Down Expand Up @@ -283,11 +284,17 @@ func (c *controller) GetCluster(ctx context.Context, clusterID uint) (_ *GetClus
}
}

// 7. transfer model
// 7. get tags
tags, err := c.tagMgr.ListByResourceTypeID(ctx, common.ResourceCluster, clusterID)
if err != nil {
return nil, err
}

// 8. transfer model
clusterResp := ofClusterModel(application, cluster, fullPath, envValue.Namespace,
clusterFiles.PipelineJSONBlob, clusterFiles.ApplicationJSONBlob)
clusterFiles.PipelineJSONBlob, clusterFiles.ApplicationJSONBlob, tags...)

// 8. get latest deployed commit
// 9. get latest deployed commit
latestPR, err := c.pipelinerunMgr.GetLatestSuccessByClusterID(ctx, clusterID)
if err != nil {
return nil, err
Expand All @@ -297,7 +304,7 @@ func (c *controller) GetCluster(ctx context.Context, clusterID uint) (_ *GetClus
clusterResp.Image = latestPR.ImageURL
}

// 9. get createdBy and updatedBy users
// 10. get createdBy and updatedBy users
userMap, err := c.userManager.GetUserMapByIDs(ctx, []uint{cluster.CreatedBy, cluster.UpdatedBy})
if err != nil {
return nil, err
Expand Down Expand Up @@ -619,6 +626,9 @@ func (c *controller) UpdateCluster(ctx context.Context, clusterID uint,
if err != nil {
return nil, err
}

clusterModel, tags := r.toClusterModel(cluster, templateRelease, er)

// 4. if templateInput is not empty, validate templateInput and update templateInput in git repo
if r.TemplateInput != nil {
// merge cluster config and request config
Expand Down Expand Up @@ -681,13 +691,26 @@ func (c *controller) UpdateCluster(ctx context.Context, clusterID uint,
}

// 5. update cluster in db
clusterModel := r.toClusterModel(cluster, templateRelease, er)
// todo: atomicity
cluster, err = c.clusterMgr.UpdateByID(ctx, clusterID, clusterModel)
if err != nil {
return nil, err
}

// 7. update cluster tags
tagsInDB, err := c.tagMgr.ListByResourceTypeID(ctx, common.ResourceCluster, clusterID)
if err != nil {
return nil, err
}
if !tagmodels.Tags(tags).Eq(tagsInDB) {
if err := c.clusterGitRepo.UpdateTags(ctx, application.Name, cluster.Name, cluster.Template, tags); err != nil {
return nil, err
}
if err := c.tagMgr.UpsertByResourceTypeID(ctx, common.ResourceCluster, clusterID, r.Tags); err != nil {
return nil, err
}
}

// 6. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
Expand Down Expand Up @@ -715,7 +738,7 @@ func (c *controller) UpdateCluster(ctx context.Context, clusterID uint,
}

return ofClusterModel(application, cluster, fullPath, envValue.Namespace,
r.TemplateInput.Pipeline, r.TemplateInput.Application), nil
r.TemplateInput.Pipeline, r.TemplateInput.Application, tags...), nil
}

func (c *controller) GetClusterByName(ctx context.Context,
Expand Down
Loading

0 comments on commit 17edf24

Please sign in to comment.