Skip to content

Commit

Permalink
Deduplicate slugs instead of failing when duplicates are detected
Browse files Browse the repository at this point in the history
  • Loading branch information
a18e authored and lnguyen committed Dec 12, 2024
1 parent cac1a04 commit 0f84de6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
9 changes: 2 additions & 7 deletions cmd/pcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,14 @@ func (c PcapCmd) Run(opts PcapOpts) error {

var result boshdir.SSHResult

// If no slugs are provided, default to capturing all instances by using an empty slug.
slugs := []boshdir.AllOrInstanceGroupOrInstanceSlug{{}}

if len(opts.Args.Slugs) > 0 {
slugs = opts.Args.Slugs
}

for i := 0; i < len(slugs); i++ {
for j := i + 1; j < len(slugs); j++ {
if slugs[i].Overlaps(slugs[j]) {
return fmt.Errorf("found redundant capture targets: %v and %v", slugs[i], slugs[j])
}
}
}
slugs = boshdir.DeduplicateSlugs(slugs)

for _, slug := range slugs {
res, err := c.deployment.SetUpSSH(slug, sshOpts)
Expand Down
38 changes: 31 additions & 7 deletions director/all_or_pool_or_instance_slug.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,27 @@ func (s AllOrInstanceGroupOrInstanceSlug) InstanceSlug() (InstanceSlug, bool) {
return InstanceSlug{}, false
}

func (s AllOrInstanceGroupOrInstanceSlug) Overlaps(other AllOrInstanceGroupOrInstanceSlug) bool {

func (s AllOrInstanceGroupOrInstanceSlug) ContainsOrEquals(other AllOrInstanceGroupOrInstanceSlug) bool {
// If the names/instance groups are different, there is no overlap
if s.name != other.name {
return false
}

// If either slug is empty, there is an overlap
if s.indexOrID == "" || other.indexOrID == "" {
// If the indexOrID matches, the slugs are equal
if s.indexOrID == other.indexOrID {
return true
}

// If the indexOrID matches, there is an overlap
if s.indexOrID == other.indexOrID {
// An instance group/empty slug contains all instances
if s.indexOrID == "" {
return true
}

// If the IPs match, there is an overlap
// If the other instance is empty, it cannot be contained in the current instance
if other.indexOrID == "" {
return false
}

return s.ip != "" && other.ip != "" && s.ip == other.ip
}

Expand All @@ -72,6 +75,27 @@ func (s *AllOrInstanceGroupOrInstanceSlug) UnmarshalFlag(data string) error {
return nil
}

func DeduplicateSlugs(slugs []AllOrInstanceGroupOrInstanceSlug) []AllOrInstanceGroupOrInstanceSlug {
var result []AllOrInstanceGroupOrInstanceSlug

for _, slug1 := range slugs {
duplicate := false

for _, slug2 := range result {
if slug1.ContainsOrEquals(slug2) || slug2.ContainsOrEquals(slug1) {
duplicate = true
break
}
}

if !duplicate {
result = append(result, slug1)
}
}

return result
}

func parseAllOrInstanceGroupOrInstanceSlug(str string) (AllOrInstanceGroupOrInstanceSlug, error) {
if len(str) == 0 {
return AllOrInstanceGroupOrInstanceSlug{}, nil
Expand Down
18 changes: 18 additions & 0 deletions director/all_or_pool_or_instance_slug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,22 @@ var _ = Describe("AllInstanceGroupOrInstanceSlug", func() {
Expect(err).To(Equal(errors.New("Expected instance 'name/' to specify non-empty ID or index")))
})
})

Describe("DeduplicateSlugs", func() {
It("deduplicates slugs", func() {
slugs := []AllOrInstanceGroupOrInstanceSlug{
NewAllOrInstanceGroupOrInstanceSlug("name1", ""),
NewAllOrInstanceGroupOrInstanceSlug("name1", "id2"),
NewAllOrInstanceGroupOrInstanceSlug("name2", "id1"),
NewAllOrInstanceGroupOrInstanceSlug("name2", "id2"),
NewAllOrInstanceGroupOrInstanceSlug("name1", ""),
}
Expect(DeduplicateSlugs(slugs)).To(Equal([]AllOrInstanceGroupOrInstanceSlug{
NewAllOrInstanceGroupOrInstanceSlug("name1", ""),
NewAllOrInstanceGroupOrInstanceSlug("name2", "id1"),
NewAllOrInstanceGroupOrInstanceSlug("name2", "id2"),
}))
})

})
})

0 comments on commit 0f84de6

Please sign in to comment.