Skip to content

Commit

Permalink
🐛 make sure gitlab discovers only unique assets (#3392)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev authored Feb 21, 2024
1 parent d7cff22 commit 0fd688a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
43 changes: 38 additions & 5 deletions providers/gitlab/provider/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,47 @@ func (s *Service) discover(root *inventory.Asset, conn *connection.GitLabConnect
// gitlab.Group and gitlab.Project objects, no matter if we connect to only
// one system or many. This reduces code complexity.

platformIds := map[string]struct{}{}
groupAssets, groups, err := s.discoverGroups(root, conn)
if err != nil {
return nil, err
}
if slices.Contains(targets, DiscoveryGroup) || slices.Contains(targets, DiscoveryAuto) {
assets = append(assets, groupAssets...)
for _, g := range groupAssets {
duplicate := false
for _, platformId := range g.PlatformIds {
if _, ok := platformIds[platformId]; ok {
duplicate = true
break
}
platformIds[platformId] = struct{}{}
}
if duplicate {
continue
}
assets = append(assets, g)
}
}

projectAssets, projects, err := s.discoverProjects(root, conn, groups)
if err != nil {
return nil, err
}
if slices.Contains(targets, DiscoveryProject) {
assets = append(assets, projectAssets...)
for _, p := range projectAssets {
duplicate := false
for _, platformId := range p.PlatformIds {
if _, ok := platformIds[platformId]; ok {
duplicate = true
break
}
platformIds[platformId] = struct{}{}
}
if duplicate {
continue
}
assets = append(assets, p)
}
}

if slices.Contains(targets, DiscoveryTerraform) {
Expand All @@ -61,6 +88,7 @@ func (s *Service) discover(root *inventory.Asset, conn *connection.GitLabConnect
if len(assets) == 0 {
return nil, nil
}

return &inventory.Inventory{
Spec: &inventory.InventorySpec{
Assets: assets,
Expand Down Expand Up @@ -146,7 +174,7 @@ func (s *Service) discoverProjects(root *inventory.Asset, conn *connection.GitLa
}

var assets []*inventory.Asset
var projects []*gitlab.Project
projects := map[int]*gitlab.Project{}

for i := range groups {
group := groups[i]
Expand Down Expand Up @@ -177,10 +205,15 @@ func (s *Service) discoverProjects(root *inventory.Asset, conn *connection.GitLa
}

assets = append(assets, asset)
projects = append(projects, project)
projects[project.ID] = project
}
}
return assets, projects, nil

projectsArr := make([]*gitlab.Project, 0, len(projects))
for _, project := range projects {
projectsArr = append(projectsArr, project)
}
return assets, projectsArr, nil
}

func discoverGroupProjects(conn *connection.GitLabConnection, gid interface{}) ([]*gitlab.Project, error) {
Expand Down
2 changes: 1 addition & 1 deletion providers/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (r *Runtime) tryShutdown() shutdownResult {
continue
}
}
log.Error().Msg("failed to disconnect from provider " + provider.Instance.Name)
log.Error().Err(err).Msg("failed to disconnect from provider " + provider.Instance.Name)
}
}
return shutdownResult{}
Expand Down

0 comments on commit 0fd688a

Please sign in to comment.