Skip to content

Commit

Permalink
feat: support group-env-argocd mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
iamyeka committed Apr 24, 2024
1 parent 64e8fac commit 642454f
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 23 deletions.
2 changes: 1 addition & 1 deletion core/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ func Init(ctx context.Context, flags *Flags, coreConfig *config.Config) {
ScopeService: scopeService,
ApplicationGitRepo: applicationGitRepo,
TemplateSchemaGetter: templateSchemaGetter,
CD: cd.NewCD(regionInformers, clusterGitRepo, coreConfig.ArgoCDMapper,
CD: cd.NewCD(regionInformers, clusterGitRepo, coreConfig.ArgoCDMapper, coreConfig.GroupArgoCDMapper,
coreConfig.GitopsRepoConfig.DefaultBranch),
K8sUtil: cd.NewK8sUtil(regionInformers, manager.EventMgr),
OutputGetter: outputGetter,
Expand Down
14 changes: 14 additions & 0 deletions core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Config struct {
SessionConfig session.Config `yaml:"sessionConfig"`
GitopsRepoConfig gitlab.GitopsRepoConfig `yaml:"gitopsRepoConfig"`
ArgoCDMapper argocd.Mapper `yaml:"argoCDMapper"`
GroupArgoCDMapper argocd.GroupMapper `yaml:"groupArgoCDMapper"`
RedisConfig redis.Redis `yaml:"redisConfig"`
TektonMapper tekton.Mapper `yaml:"tektonMapper"`
TemplateRepo templaterepo.Repo `yaml:"templateRepo"`
Expand Down Expand Up @@ -91,6 +92,19 @@ func LoadConfig(configFilePath string) (*Config, error) {
}
config.ArgoCDMapper = newArgoCDMapper

newGroupArgoCDMapper := argocd.GroupMapper{}
for g, argoCDMapper := range config.GroupArgoCDMapper {
newMapper := argocd.Mapper{}
for key, v := range argoCDMapper {
ks := strings.Split(key, ",")
for i := 0; i < len(ks); i++ {
newMapper[ks[i]] = v
}
}
newGroupArgoCDMapper[g] = newMapper
}
config.GroupArgoCDMapper = newGroupArgoCDMapper

newTektonMapper := tekton.Mapper{}
for key, v := range config.TektonMapper {
ks := strings.Split(key, ",")
Expand Down
7 changes: 7 additions & 0 deletions core/controller/cluster/controller_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ func (c *controller) DeleteCluster(ctx context.Context, clusterID uint, hard boo
if err = c.cd.DeleteCluster(newctx, &cd.DeleteClusterParams{
Environment: cluster.EnvironmentName,
Cluster: cluster.Name,
GroupID: application.GroupID,
}); err != nil {
log.Errorf(newctx, "failed to delete cluster: %v in cd system, err: %v", cluster.Name, err)
return
Expand Down Expand Up @@ -962,10 +963,16 @@ func (c *controller) FreeCluster(ctx context.Context, clusterID uint) (err error
}
}()

application, err := c.applicationMgr.GetByID(ctx, cluster.ApplicationID)
if err != nil {
log.Errorf(newctx, "failed to get application: %v, err: %v", cluster.ApplicationID, err)
return
}
// 2. delete cluster in cd system
if err = c.cd.DeleteCluster(newctx, &cd.DeleteClusterParams{
Environment: cluster.EnvironmentName,
Cluster: cluster.Name,
GroupID: application.GroupID,
}); err != nil {
log.Errorf(newctx, "failed to delete cluster: %v in cd system, err: %v", cluster.Name, err)
return
Expand Down
1 change: 1 addition & 0 deletions core/controller/cluster/controller_basic_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func (c *controller) GetClusterStatusV2(ctx context.Context, clusterID uint) (*S
Environment: cluster.EnvironmentName,
Cluster: cluster.Name,
RegionEntity: regionEntity,
GroupID: application.GroupID,
}

// If cluster not found on argo, check the cluster is freed or has not been published yet,
Expand Down
2 changes: 2 additions & 0 deletions core/controller/cluster/controller_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func (c *controller) InternalDeploy(ctx context.Context, clusterID uint,
ValueFiles: repoInfo.ValueFiles,
RegionEntity: regionEntity,
Namespace: envValue.Namespace,
GroupID: application.GroupID,
}); err != nil {
return nil, err
}
Expand All @@ -142,6 +143,7 @@ func (c *controller) InternalDeploy(ctx context.Context, clusterID uint,
Environment: cluster.EnvironmentName,
Cluster: cluster.Name,
Revision: masterRevision,
GroupID: application.GroupID,
}); err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions core/controller/cluster/controller_internal_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func (c *controller) InternalDeployV2(ctx context.Context, clusterID uint,
ValueFiles: repoInfo.ValueFiles,
RegionEntity: regionEntity,
Namespace: envValue.Namespace,
GroupID: application.GroupID,
}); err != nil {
return nil, err
}
Expand All @@ -180,6 +181,7 @@ func (c *controller) InternalDeployV2(ctx context.Context, clusterID uint,
Environment: cluster.EnvironmentName,
Cluster: cluster.Name,
Revision: masterRevision,
GroupID: application.GroupID,
}); err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions core/controller/cluster/controller_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (c *controller) Restart(ctx context.Context, clusterID uint) (_ *Pipelineru
Environment: cluster.EnvironmentName,
Cluster: cluster.Name,
Revision: commit,
GroupID: application.GroupID,
}); err != nil {
return nil, err
}
Expand Down Expand Up @@ -394,6 +395,7 @@ func (c *controller) Rollback(ctx context.Context,
ValueFiles: repoInfo.ValueFiles,
RegionEntity: regionEntity,
Namespace: envValue.Namespace,
GroupID: application.GroupID,
}); err != nil {
return nil, err
}
Expand All @@ -412,6 +414,7 @@ func (c *controller) Rollback(ctx context.Context,
Environment: cluster.EnvironmentName,
Cluster: cluster.Name,
Revision: masterRevision,
GroupID: application.GroupID,
}); err != nil {
return nil, err
}
Expand Down
18 changes: 18 additions & 0 deletions core/controller/cluster/controller_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,15 @@ func (c *controller) GetClusterStatus(ctx context.Context, clusterID uint) (_ *G
return nil, perror.Wrapf(herrors.ErrNotSupport, "cd %s does not support legacy cd", reflect.TypeOf(c.cd))
}
// nolint
application, err := c.applicationMgr.GetByID(ctx, cluster.ApplicationID)
if err != nil {
return nil, err
}
clusterState, err := lagacyCD.GetClusterStateV1(ctx, &cd.GetClusterStateParams{
Environment: cluster.EnvironmentName,
Cluster: cluster.Name,
RegionEntity: regionEntity,
GroupID: application.GroupID,
})
if err != nil {
if _, ok := perror.Cause(err).(*herrors.HorizonErrNotFound); ok {
Expand Down Expand Up @@ -402,6 +407,7 @@ func (c *controller) GetPodEvents(ctx context.Context, clusterID uint, podName s
Cluster: cluster.Name,
Pod: podName,
RegionEntity: regionEntity,
GroupID: application.GroupID,
}
return c.cd.GetPodEvents(ctx, &param)
}
Expand Down Expand Up @@ -486,10 +492,16 @@ func (c *controller) GetResourceTree(ctx context.Context, clusterID uint) (resp
return nil, err
}

application, err := c.applicationMgr.GetByID(ctx, cluster.ApplicationID)
if err != nil {
return nil, err
}

resourceTree, err := c.cd.GetResourceTree(ctx, &cd.GetResourceTreeParams{
Environment: cluster.EnvironmentName,
Cluster: cluster.Name,
RegionEntity: regionEntity,
GroupID: application.GroupID,
})
if err != nil {
return
Expand Down Expand Up @@ -518,10 +530,16 @@ func (c *controller) GetStep(ctx context.Context, clusterID uint) (resp *GetStep
return nil, err
}

application, err := c.applicationMgr.GetByID(ctx, cluster.ApplicationID)
if err != nil {
return nil, err
}

steps, err := c.cd.GetStep(ctx, &cd.GetStepParams{
Environment: cluster.EnvironmentName,
Cluster: cluster.Name,
RegionEntity: regionEntity,
GroupID: application.GroupID,
})
if err != nil {
return
Expand Down
3 changes: 3 additions & 0 deletions core/controller/pipelinerun/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ func (c *controller) executeRestart(ctx context.Context, application *appmodels.
Environment: cluster.EnvironmentName,
Cluster: cluster.Name,
Revision: commit,
GroupID: application.GroupID,
}); err != nil {
return perror.Wrapf(err, "failed to deploy cluster in CD, cluster = %s, revision = %s",
cluster.Name, commit)
Expand Down Expand Up @@ -650,6 +651,7 @@ func (c *controller) executeRollback(ctx context.Context, application *appmodels
ValueFiles: repoInfo.ValueFiles,
RegionEntity: regionEntity,
Namespace: envValue.Namespace,
GroupID: application.GroupID,
}); err != nil {
return perror.Wrapf(err, "failed to create cluster in CD, cluster = %s", cluster.Name)
}
Expand All @@ -668,6 +670,7 @@ func (c *controller) executeRollback(ctx context.Context, application *appmodels
Environment: cluster.EnvironmentName,
Cluster: cluster.Name,
Revision: masterRevision,
GroupID: application.GroupID,
}); err != nil {
return perror.Wrapf(err, "failed to deploy cluster in CD, cluster = %s, revision = %s",
cluster.Name, masterRevision)
Expand Down
30 changes: 21 additions & 9 deletions pkg/argocd/facotry.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package argocd

import (
"fmt"
"sync"

herrors "github.com/horizoncd/horizon/core/errors"
Expand All @@ -24,31 +25,42 @@ import (
const _default = "default"

type Factory interface {
GetArgoCD(environment string) (ArgoCD, error)
GetArgoCD(groupID uint, environment string) (ArgoCD, error)
}

type factory struct {
cache *sync.Map
cache *sync.Map
groupCache *sync.Map
}

func NewFactory(argoCDMapper argocd.Mapper) Factory {
func NewFactory(argoCDMapper argocd.Mapper, groupArgoCDMapper argocd.GroupMapper) Factory {
cache := &sync.Map{}
for env, argoCDConf := range argoCDMapper {
argoCD := NewArgoCD(argoCDConf.URL, argoCDConf.Token, argoCDConf.Namespace)
cache.Store(env, argoCD)
}
groupCache := &sync.Map{}
for group, argoCDs := range groupArgoCDMapper {
for env, argoCDConf := range argoCDs {
argoCD := NewArgoCD(argoCDConf.URL, argoCDConf.Token, argoCDConf.Namespace)
groupCache.Store(fmt.Sprintf("%d-%s", group, env), argoCD)
}
}
return &factory{
cache: cache,
cache: cache,
groupCache: groupCache,
}
}

func (f *factory) GetArgoCD(environment string) (ArgoCD, error) {
func (f *factory) GetArgoCD(groupID uint, environment string) (ArgoCD, error) {
var ret interface{}
var ok bool
if ret, ok = f.cache.Load(environment); !ok {
// check and use default cd
if ret, ok = f.cache.Load(_default); !ok {
return nil, herrors.NewErrNotFound(herrors.ArgoCD, "default argo cd not found")
if ret, ok = f.groupCache.Load(fmt.Sprintf("%d-%s", groupID, environment)); !ok {
if ret, ok = f.cache.Load(environment); !ok {
// check and use default cd
if ret, ok = f.cache.Load(_default); !ok {
return nil, herrors.NewErrNotFound(herrors.ArgoCD, "default argo cd not found")
}
}
}
return ret.(ArgoCD), nil
Expand Down
37 changes: 33 additions & 4 deletions pkg/argocd/facotry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
)

func Test(t *testing.T) {
// env-argocd mapper
argoCDMapper := make(map[string]*argocd.ArgoCD)
argoCDTest := &argocd.ArgoCD{
URL: "http://test.argo.com",
Expand All @@ -35,20 +36,48 @@ func Test(t *testing.T) {
}
argoCDMapper["test"] = argoCDTest
argoCDMapper["reg"] = argoCDReg
factory := NewFactory(argoCDMapper)

// group-env-argocd mapper
groupArgoCDMapper := make(map[uint]argocd.Mapper)
argoCDTest2 := &argocd.ArgoCD{
URL: "http://test2.argo.com",
Token: "token2",
Namespace: "argocd2",
}
groupArgoCDMapper[1] = map[string]*argocd.ArgoCD{
"test": argoCDTest2,
}

// create factory
factory := NewFactory(argoCDMapper, groupArgoCDMapper)
assert.NotNil(t, factory)

argoCD, err := factory.GetArgoCD("test")
// 1. use test argocd from group-env-argocd mapper
argoCD, err := factory.GetArgoCD(1, "test")
assert.Nil(t, err)
assert.NotNil(t, argoCD)
assert.Equal(t, argoCD, NewArgoCD(argoCDTest2.URL, argoCDTest2.Token, argoCDTest2.Namespace))

// 2. use reg argocd from env-argocd mapper because not found in group-env-argocd mapper
argoCD, err = factory.GetArgoCD(1, "reg")
assert.Nil(t, err)
assert.NotNil(t, argoCD)
assert.Equal(t, argoCD, NewArgoCD(argoCDReg.URL, argoCDReg.Token, argoCDReg.Namespace))

// 3. use test argocd from env-argocd mapper
argoCD, err = factory.GetArgoCD(2, "test")
assert.Nil(t, err)
assert.NotNil(t, argoCD)
assert.Equal(t, argoCD, NewArgoCD(argoCDTest.URL, argoCDTest.Token, argoCDTest.Namespace))

argoCD, err = factory.GetArgoCD("reg")
// 4. use reg argocd from env-argocd mapper
argoCD, err = factory.GetArgoCD(2, "reg")
assert.Nil(t, err)
assert.NotNil(t, argoCD)
assert.Equal(t, argoCD, NewArgoCD(argoCDReg.URL, argoCDReg.Token, argoCDReg.Namespace))

argoCD, err = factory.GetArgoCD("not-exists")
// 5. report error because not found in group-env-argocd mapper and env-argocd mapper
argoCD, err = factory.GetArgoCD(2, "not-exists")
assert.Nil(t, argoCD)
assert.NotNil(t, err)
}
Loading

0 comments on commit 642454f

Please sign in to comment.