diff --git a/npc/rule.go b/npc/rule.go index 5ed49129..d2fac379 100644 --- a/npc/rule.go +++ b/npc/rule.go @@ -30,13 +30,19 @@ func newRuleSpec(proto *string, srcHost *selectorSpec, dstHost *selectorSpec, ds args = append(args, "--dport", *dstPort) } args = append(args, "-j", "ACCEPT") - srcComment := "" - if srcHost.nsName != "" { - srcComment = fmt.Sprintf("pods: namespace: %s, selector: %s", srcHost.nsName, srcHost.key) - } else { - srcComment = fmt.Sprintf("namespaces: selector: %s", srcHost.key) + srcComment := "anywhere" + if srcHost != nil { + if srcHost.nsName != "" { + srcComment = fmt.Sprintf("pods: namespace: %s, selector: %s", srcHost.nsName, srcHost.key) + } else { + srcComment = fmt.Sprintf("namespaces: selector: %s", srcHost.key) + } } - args = append(args, "-m", "comment", "--comment", fmt.Sprintf("%s -> namespace: %s, key: %s", srcComment, dstHost.nsName, dstHost.key)) + dstComment := "anywhere" + if dstHost != nil { + dstComment = fmt.Sprintf("pods: namespace: %s, selector: %s", dstHost.nsName, dstHost.key) + } + args = append(args, "-m", "comment", "--comment", fmt.Sprintf("%s -> %s", srcComment, dstComment)) key := strings.Join(args, " ") return &ruleSpec{key, args} diff --git a/npc/rule_test.go b/npc/rule_test.go new file mode 100644 index 00000000..dee1802f --- /dev/null +++ b/npc/rule_test.go @@ -0,0 +1,16 @@ +package npc + +import ( + "github.com/stretchr/testify/require" + "testing" +) + +func TestRegressionHandleNil3095(t *testing.T) { + // Test for handling nil values + // https://github.com/weaveworks/weave/issues/3095 + + rs := newRuleSpec(nil, nil, nil, nil) + + require.NotEqual(t, rs.key, "") + +}