Skip to content

Commit

Permalink
feat: support region-argocd mapper (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamyeka authored May 8, 2024
1 parent 5b40856 commit d125d34
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 25 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.RegionArgoCDMapper,
coreConfig.GitopsRepoConfig.DefaultBranch),
K8sUtil: cd.NewK8sUtil(regionInformers, manager.EventMgr),
OutputGetter: outputGetter,
Expand Down
10 changes: 10 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"`
RegionArgoCDMapper argocd.RegionMapper `yaml:"regionArgoCDMapper"`
RedisConfig redis.Redis `yaml:"redisConfig"`
TektonMapper tekton.Mapper `yaml:"tektonMapper"`
TemplateRepo templaterepo.Repo `yaml:"templateRepo"`
Expand Down Expand Up @@ -91,6 +92,15 @@ func LoadConfig(configFilePath string) (*Config, error) {
}
config.ArgoCDMapper = newArgoCDMapper

newRegionCDMapper := argocd.RegionMapper{}
for key, v := range config.RegionArgoCDMapper {
ks := strings.Split(key, ",")
for i := 0; i < len(ks); i++ {
newRegionCDMapper[ks[i]] = v
}
}
config.RegionArgoCDMapper = newRegionCDMapper

newTektonMapper := tekton.Mapper{}
for key, v := range config.TektonMapper {
ks := strings.Split(key, ",")
Expand Down
2 changes: 2 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,
Region: cluster.RegionName,
}); err != nil {
log.Errorf(newctx, "failed to delete cluster: %v in cd system, err: %v", cluster.Name, err)
return
Expand Down Expand Up @@ -966,6 +967,7 @@ func (c *controller) FreeCluster(ctx context.Context, clusterID uint) (err error
if err = c.cd.DeleteCluster(newctx, &cd.DeleteClusterParams{
Environment: cluster.EnvironmentName,
Cluster: cluster.Name,
Region: cluster.RegionName,
}); 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_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (c *controller) InternalDeploy(ctx context.Context, clusterID uint,
Environment: cluster.EnvironmentName,
Cluster: cluster.Name,
Revision: masterRevision,
Region: cluster.RegionName,
}); err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions core/controller/cluster/controller_internal_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func (c *controller) InternalDeployV2(ctx context.Context, clusterID uint,
Environment: cluster.EnvironmentName,
Cluster: cluster.Name,
Revision: masterRevision,
Region: cluster.RegionName,
}); err != nil {
return nil, err
}
Expand Down
2 changes: 2 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,
Region: cluster.RegionName,
}); err != nil {
return nil, err
}
Expand Down Expand Up @@ -412,6 +413,7 @@ func (c *controller) Rollback(ctx context.Context,
Environment: cluster.EnvironmentName,
Cluster: cluster.Name,
Revision: masterRevision,
Region: cluster.RegionName,
}); err != nil {
return nil, err
}
Expand Down
2 changes: 2 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,
Region: cluster.RegionName,
}); err != nil {
return perror.Wrapf(err, "failed to deploy cluster in CD, cluster = %s, revision = %s",
cluster.Name, commit)
Expand Down Expand Up @@ -668,6 +669,7 @@ func (c *controller) executeRollback(ctx context.Context, application *appmodels
Environment: cluster.EnvironmentName,
Cluster: cluster.Name,
Revision: masterRevision,
Region: cluster.RegionName,
}); err != nil {
return perror.Wrapf(err, "failed to deploy cluster in CD, cluster = %s, revision = %s",
cluster.Name, masterRevision)
Expand Down
27 changes: 18 additions & 9 deletions pkg/argocd/facotry.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,40 @@ import (
const _default = "default"

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

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

func NewFactory(argoCDMapper argocd.Mapper) Factory {
func NewFactory(argoCDMapper argocd.Mapper, regionArgoCDMapper argocd.RegionMapper) Factory {
cache := &sync.Map{}
for env, argoCDConf := range argoCDMapper {
argoCD := NewArgoCD(argoCDConf.URL, argoCDConf.Token, argoCDConf.Namespace)
cache.Store(env, argoCD)
}
regionCache := &sync.Map{}
for region, argoCDConf := range regionArgoCDMapper {
argoCD := NewArgoCD(argoCDConf.URL, argoCDConf.Token, argoCDConf.Namespace)
regionCache.Store(region, argoCD)
}
return &factory{
cache: cache,
cache: cache,
regionCache: regionCache,
}
}

func (f *factory) GetArgoCD(environment string) (ArgoCD, error) {
func (f *factory) GetArgoCD(region string, 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.regionCache.Load(region); !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
33 changes: 27 additions & 6 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,40 @@ func Test(t *testing.T) {
}
argoCDMapper["test"] = argoCDTest
argoCDMapper["reg"] = argoCDReg
factory := NewFactory(argoCDMapper)

// region-argocd mapper
regionArgoCDMapper := make(map[string]*argocd.ArgoCD)
argoCDTest2 := &argocd.ArgoCD{
URL: "http://test2.argo.com",
Token: "token2",
Namespace: "argocd2",
}
regionArgoCDMapper["region"] = argoCDTest2

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

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

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

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

argoCD, err = factory.GetArgoCD("not-exists")
// 4. report error because not found in region-argocd mapper and env-argocd mapper
argoCD, err = factory.GetArgoCD("region2", "not-exists")
assert.Nil(t, argoCD)
assert.NotNil(t, err)
}
18 changes: 9 additions & 9 deletions pkg/cd/cd.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ type cd struct {
}

func NewCD(informerFactories *regioninformers.RegionInformers, clusterGitRepo gitrepo.ClusterGitRepo,
argoCDMapper argocdconf.Mapper, targetRevision string) CD {
argoCDMapper argocdconf.Mapper, regionArgoCDMapper argocdconf.RegionMapper, targetRevision string) CD {
return &cd{
kubeClientFactory: kubeclient.Fty,
informerFactories: informerFactories,
factory: argocd.NewFactory(argoCDMapper),
factory: argocd.NewFactory(argoCDMapper, regionArgoCDMapper),
clusterGitRepo: clusterGitRepo,
targetRevision: targetRevision,
}
Expand All @@ -119,7 +119,7 @@ func (c *cd) CreateCluster(ctx context.Context, params *CreateClusterParams) (er
const op = "cd: create cluster"
defer wlog.Start(ctx, op).StopPrint()

argo, err := c.factory.GetArgoCD(params.Environment)
argo, err := c.factory.GetArgoCD(params.RegionEntity.Name, params.Environment)
if err != nil {
return err
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func (c *cd) DeployCluster(ctx context.Context, params *DeployClusterParams) (er
const op = "cd: deploy cluster"
defer wlog.Start(ctx, op).StopPrint()

argo, err := c.factory.GetArgoCD(params.Environment)
argo, err := c.factory.GetArgoCD(params.Region, params.Environment)
if err != nil {
return perror.Wrap(herrors.ErrParamInvalid, err.Error())
}
Expand All @@ -163,7 +163,7 @@ func (c *cd) DeleteCluster(ctx context.Context, params *DeleteClusterParams) (er
const op = "cd: delete cluster"
defer wlog.Start(ctx, op).StopPrint()

argo, err := c.factory.GetArgoCD(params.Environment)
argo, err := c.factory.GetArgoCD(params.Region, params.Environment)
if err != nil {
return err
}
Expand Down Expand Up @@ -191,7 +191,7 @@ func (c *cd) GetResourceTree(ctx context.Context,
const op = "cd: get resource tree"
defer wlog.Start(ctx, op).StopPrint()

argo, err := c.factory.GetArgoCD(params.Environment)
argo, err := c.factory.GetArgoCD(params.RegionEntity.Name, params.Environment)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -246,7 +246,7 @@ func (c *cd) GetStep(ctx context.Context, params *GetStepParams) (*Step, error)
return nil, err
}

argo, err := c.factory.GetArgoCD(params.Environment)
argo, err := c.factory.GetArgoCD(params.RegionEntity.Name, params.Environment)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -307,7 +307,7 @@ func (c *cd) GetClusterState(ctx context.Context,
const op = "cd: get cluster status"
defer wlog.Start(ctx, op).StopPrint()

argo, err := c.factory.GetArgoCD(params.Environment)
argo, err := c.factory.GetArgoCD(params.RegionEntity.Name, params.Environment)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -396,7 +396,7 @@ func (c *cd) GetClusterStateV1(ctx context.Context,
const op = "cd: get cluster status"
defer wlog.Start(ctx, op).StopPrint()

argo, err := c.factory.GetArgoCD(params.Environment)
argo, err := c.factory.GetArgoCD(params.RegionEntity.Name, params.Environment)
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/cd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type DeployClusterParams struct {
Environment string
Cluster string
Revision string
Region string
}

type GetPodEventsParams struct {
Expand All @@ -88,6 +89,7 @@ type DeletePodsParams struct {
type DeleteClusterParams struct {
Environment string
Cluster string
Region string
}

type ExecuteActionParams struct {
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

package argocd

// RegionMapper represents a region-to-ArgoCD Mapper configurations.
type RegionMapper map[string]*ArgoCD

type Mapper map[string]*ArgoCD

type ArgoCD struct {
Expand Down

0 comments on commit d125d34

Please sign in to comment.