Skip to content

Commit

Permalink
fixup! disruptors: error out if there are no targets
Browse files Browse the repository at this point in the history
extract group printing logic out of String to a different function
  • Loading branch information
roobre committed Aug 30, 2023
1 parent aa552d9 commit 521437a
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions pkg/disruptors/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,8 @@ func (p PodSelector) String() string {
str = "all pods"
} else {
str = "pods "
if len(p.Select.Labels) != 0 {
str += "including("
for k, v := range p.Select.Labels {
str += fmt.Sprintf("%s=%s, ", k, v)
}
str = strings.TrimSuffix(str, ", ")
str += "), "
}

if len(p.Exclude.Labels) != 0 {
str += "excluding("
for k, v := range p.Exclude.Labels {
str += fmt.Sprintf("%s=%s, ", k, v)
}
str = strings.TrimSuffix(str, ", ")
str += "), "
}

str += p.groupLabels("including", p.Select.Labels)
str += p.groupLabels("excluding", p.Exclude.Labels)
str = strings.TrimSuffix(str, ", ")
}

Expand All @@ -100,6 +84,24 @@ func (p PodSelector) String() string {
return str
}

// groupLabels returns a group of labels as a string, giving that group a name. The returned string has the form of:
// `groupName(foo=bar, boo=baz), `, including the trailing space and comma.
// An empty group of labels produces an empty string.
func (PodSelector) groupLabels(groupName string, labels map[string]string) string {
if len(labels) == 0 {
return ""
}

group := groupName + "("
for k, v := range labels {
group += fmt.Sprintf("%s=%s, ", k, v)
}
group = strings.TrimSuffix(group, ", ")
group += "), "

return group
}

// NewPodDisruptor creates a new instance of a PodDisruptor that acts on the pods
// that match the given PodSelector
func NewPodDisruptor(
Expand Down

0 comments on commit 521437a

Please sign in to comment.