Merge pull request #3096 from weaveworks/wip-3095
fix: Missing nil checks
This commit is contained in:
commit
c2a224b436
2 files changed
+25
-7
No files matched your search
+9
-7
@@ -20,23 +20,25 @@ func newRuleSpec(proto *string, srcHost *selectorSpec, dstHost *selectorSpec, ds
|
|||||||
if proto != nil {
|
if proto != nil {
|
||||||
args = append(args, "-p", *proto)
|
args = append(args, "-p", *proto)
|
||||||
}
|
}
|
||||||
|
srcComment := "anywhere"
|
||||||
if srcHost != nil {
|
if srcHost != nil {
|
||||||
args = append(args, "-m", "set", "--match-set", string(srcHost.ipsetName), "src")
|
args = append(args, "-m", "set", "--match-set", string(srcHost.ipsetName), "src")
|
||||||
|
if srcHost.nsName != "" {
|
||||||
|
srcComment = fmt.Sprintf("pods: namespace: %s, selector: %s", srcHost.nsName, srcHost.key)
|
||||||
|
} else {
|
||||||
|
srcComment = fmt.Sprintf("namespaces: selector: %s", srcHost.key)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
dstComment := "anywhere"
|
||||||
if dstHost != nil {
|
if dstHost != nil {
|
||||||
args = append(args, "-m", "set", "--match-set", string(dstHost.ipsetName), "dst")
|
args = append(args, "-m", "set", "--match-set", string(dstHost.ipsetName), "dst")
|
||||||
|
dstComment = fmt.Sprintf("pods: namespace: %s, selector: %s", dstHost.nsName, dstHost.key)
|
||||||
}
|
}
|
||||||
if dstPort != nil {
|
if dstPort != nil {
|
||||||
args = append(args, "--dport", *dstPort)
|
args = append(args, "--dport", *dstPort)
|
||||||
}
|
}
|
||||||
args = append(args, "-j", "ACCEPT")
|
args = append(args, "-j", "ACCEPT")
|
||||||
srcComment := ""
|
args = append(args, "-m", "comment", "--comment", fmt.Sprintf("%s -> %s", srcComment, dstComment))
|
||||||
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))
|
|
||||||
key := strings.Join(args, " ")
|
key := strings.Join(args, " ")
|
||||||
|
|
||||||
return &ruleSpec{key, args}
|
return &ruleSpec{key, args}
|
||||||
|
|||||||
@@ -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, "")
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user