From 0fd688af61c1ea9bda5a0ef0f71fbb28fba0c12b Mon Sep 17 00:00:00 2001 From: Ivan Milchev Date: Wed, 21 Feb 2024 16:41:06 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20make=20sure=20gitlab=20discovers?= =?UTF-8?q?=20only=20unique=20assets=20(#3392)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ivan Milchev --- providers/gitlab/provider/discovery.go | 43 +++++++++++++++++++++++--- providers/runtime.go | 2 +- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/providers/gitlab/provider/discovery.go b/providers/gitlab/provider/discovery.go index f6df918371..6ad47a9f2c 100644 --- a/providers/gitlab/provider/discovery.go +++ b/providers/gitlab/provider/discovery.go @@ -34,12 +34,26 @@ 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) @@ -47,7 +61,20 @@ func (s *Service) discover(root *inventory.Asset, conn *connection.GitLabConnect 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) { @@ -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, @@ -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] @@ -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) { diff --git a/providers/runtime.go b/providers/runtime.go index da7e06fe17..feb423b7d4 100644 --- a/providers/runtime.go +++ b/providers/runtime.go @@ -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{}