Skip to content

Commit

Permalink
wip: disruptors: error out if there are no targets
Browse files Browse the repository at this point in the history
  • Loading branch information
roobre committed Aug 5, 2023
1 parent 5af6e37 commit 5bfbb82
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
54 changes: 50 additions & 4 deletions pkg/disruptors/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"reflect"
"strings"
"time"

"github.com/grafana/xk6-disruptor/pkg/kubernetes"
Expand Down Expand Up @@ -52,6 +53,50 @@ type PodAttributes struct {
Labels map[string]string
}

// NamespaceOrDefault returns the configured namespace for this selector, and the name of the default namespace if it
// is not configured.
func (p PodSelector) NamespaceOrDefault() string {
if p.Namespace != "" {
return p.Namespace
}

return metav1.NamespaceDefault
}

// String returns a human-readable explanation of the pods matched by a PodSelector.
func (p PodSelector) String() string {
var str string

if len(p.Select.Labels) == 0 && len(p.Exclude.Labels) == 0 {
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 = strings.TrimSuffix(str, ", ")
}

str += fmt.Sprintf(" in ns %q", p.NamespaceOrDefault())

return str
}

// NewPodDisruptor creates a new instance of a PodDisruptor that acts on the pods
// that match the given PodSelector
func NewPodDisruptor(
Expand All @@ -68,10 +113,7 @@ func NewPodDisruptor(
}

// ensure selector and controller use default namespace if none specified
namespace := selector.Namespace
if selector.Namespace == "" {
selector.Namespace = metav1.NamespaceDefault
}
namespace := selector.NamespaceOrDefault()
helper := k8s.PodHelper(namespace)

filter := helpers.PodFilter{
Expand All @@ -84,6 +126,10 @@ func NewPodDisruptor(
return nil, err
}

if len(targets) == 0 {
return nil, fmt.Errorf("couldn't find any pods matching '%s'", selector)
}

controller := NewAgentController(
ctx,
helper,
Expand Down
50 changes: 50 additions & 0 deletions pkg/disruptors/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,53 @@ func Test_PodGrpcPFaultInjection(t *testing.T) {
})
}
}

func TestPodSelector_String(t *testing.T) {
t.Parallel()

for _, tc := range []struct {
name string
selector PodSelector
expected string
}{
{
name: "Empty selector",
expected: `all pods in ns "default"`,
},
{
name: "Only inclusions",
selector: PodSelector{
Namespace: "testns",
Select: PodAttributes{map[string]string{"foo": "bar"}},
},
expected: `pods including(foo=bar) in ns "testns"`,
},
{
name: "Only exclusions",
selector: PodSelector{
Namespace: "testns",
Exclude: PodAttributes{map[string]string{"foo": "bar"}},
},
expected: `pods excluding(foo=bar) in ns "testns"`,
},
{
name: "Both inclusions and exclusions",
selector: PodSelector{
Namespace: "testns",
Select: PodAttributes{map[string]string{"foo": "bar"}},
Exclude: PodAttributes{map[string]string{"boo": "baa"}},
},
expected: `pods including(foo=bar), excluding(boo=baa) in ns "testns"`,
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

output := tc.selector.String()
if tc.expected != output {
t.Errorf("expected string does not match output string:\n%s\n%s", tc.expected, output)
}
})
}
}
4 changes: 4 additions & 0 deletions pkg/disruptors/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func NewServiceDisruptor(
return nil, err
}

if len(targets) == 0 {
return nil, fmt.Errorf("could not find any pods backing service %q in namespace %q", service, namespace)
}

ph := k8s.PodHelper(namespace)
controller := NewAgentController(
ctx,
Expand Down

0 comments on commit 5bfbb82

Please sign in to comment.