Skip to content

Commit

Permalink
Wire up net-out call
Browse files Browse the repository at this point in the history
Signed-off-by: Mark St.Godard <markstgodard@gmail.com>
  • Loading branch information
David Bellotti authored and markstgodard committed Aug 25, 2016
1 parent 1d462b1 commit d310c7d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 75 deletions.
36 changes: 24 additions & 12 deletions netplugin/external_networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,21 +193,33 @@ func (p *externalBinaryNetworker) NetIn(log lager.Logger, handle string, externa
}

func (p *externalBinaryNetworker) NetOut(log lager.Logger, handle string, rule garden.NetOutRule) error {
rules := []garden.NetOutRule{}
value, ok := p.configStore.Get(handle, NetOutKey)
if ok {
err := json.Unmarshal([]byte(value), &rules)
if err != nil {
return fmt.Errorf("store net-out invalid JSON: %s", err)
}
containerIP, ok := p.configStore.Get(handle, gardener.ContainerIPKey)
if !ok {
return fmt.Errorf("cannot find container [%s]\n", handle)
}

var props = struct {
ContainerIP string `json:"container_ip"`
NetOutRule garden.NetOutRule `json:"netout_rule"`
}{
ContainerIP: containerIP,
NetOutRule: rule,
}

rules = append(rules, rule)
ruleJSON, err := json.Marshal(rules)
pathAndExtraArgs := append([]string{p.path}, p.extraArg...)
propertiesJSON, err := json.Marshal(props)
if err != nil {
return err
return fmt.Errorf("marshaling netout rule: %s", err)
}
networkPluginFlags := []string{
"--handle", handle,
"--properties", string(propertiesJSON),
}

p.configStore.Set(handle, NetOutKey, string(ruleJSON))
return nil
args := append(pathAndExtraArgs, "--action", "net-out")
args = append(args, networkPluginFlags...)

cmd := exec.Command(p.path)
cmd.Args = args
return p.commandRunner.Run(cmd)
}
87 changes: 24 additions & 63 deletions netplugin/external_networker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,75 +322,36 @@ var _ = Describe("ExternalNetworker", func() {

Describe("NetOut", func() {
handle := "my-handle"
BeforeEach(func() {
configStore.Set(handle, gardener.ContainerIPKey, "169.254.1.2")
})

It("writes to the config store", func() {
netOutRules := []garden.NetOutRule{{
It("executes the external plugin with the correct args", func() {
rule := garden.NetOutRule{
Protocol: garden.ProtocolTCP,
Networks: []garden.IPRange{{
Start: net.IPv4(10, 10, 10, 2),
End: net.IPv4(10, 10, 10, 2),
Start: net.ParseIP("1.1.1.1"),
End: net.ParseIP("2.2.2.2"),
}},
}}

expectedJSON, err := json.Marshal(netOutRules)
Expect(err).NotTo(HaveOccurred())
Expect(plugin.NetOut(logger, handle, netOutRules[0])).To(Succeed())
v, ok := configStore.Get(handle, netplugin.NetOutKey)
Expect(ok).To(BeTrue())
Expect(v).To(MatchJSON(expectedJSON))
})

Context("when config store has existing net-out rule", func() {
var oldNetOutRule garden.NetOutRule

BeforeEach(func() {
oldNetOutRule = garden.NetOutRule{
Protocol: garden.ProtocolTCP,
Networks: []garden.IPRange{{
Start: net.IPv4(10, 10, 10, 2),
End: net.IPv4(10, 10, 10, 2),
}},
}

netOutRules := []garden.NetOutRule{oldNetOutRule}
r, _ := json.Marshal(netOutRules)
configStore.Set(handle, netplugin.NetOutKey, string(r))
})
Ports: []garden.PortRange{{
Start: uint16(9000),
End: uint16(9999),
}},
}
Expect(plugin.NetOut(logger, handle, rule)).To(Succeed())

It("adds another net-out rule", func() {
newNetOutRule := garden.NetOutRule{
Protocol: garden.ProtocolTCP,
Networks: []garden.IPRange{{
Start: net.IPv4(10, 10, 10, 3),
End: net.IPv4(10, 10, 10, 3),
}},
}
Expect(plugin.NetOut(logger, handle, newNetOutRule)).To(Succeed())

expectedJSON, err := json.Marshal([]garden.NetOutRule{oldNetOutRule, newNetOutRule})
Expect(err).NotTo(HaveOccurred())
v, ok := configStore.Get(handle, netplugin.NetOutKey)
Expect(ok).To(BeTrue())
Expect(v).To(MatchJSON(expectedJSON))
})
cmd := fakeCommandRunner.ExecutedCommands()[0]
Expect(cmd.Path).To(Equal("some/path"))
Expect(cmd.Args).To(Equal([]string{
"some/path",
"arg1",
"arg2",
"arg3",
"--action", "net-out",
"--handle", handle,
"--properties", `{"container_ip":"169.254.1.2","netout_rule":{"protocol":1,"networks":[{"start":"1.1.1.1","end":"2.2.2.2"}],"ports":[{"start":9000,"end":9999}]}}`,
}))
})

Context("when config store has bad net-out rule data", func() {
BeforeEach(func() {
configStore.Set(handle, netplugin.NetOutKey, "bad-data")
})

It("returns an error", func() {
newNetOutRule := garden.NetOutRule{
Protocol: garden.ProtocolTCP,
Networks: []garden.IPRange{{
Start: net.IPv4(10, 10, 10, 3),
End: net.IPv4(10, 10, 10, 3),
}},
}
err := plugin.NetOut(logger, handle, newNetOutRule)
Expect(err).To(MatchError(ContainSubstring("store net-out invalid JSON")))
})
})
})
})

0 comments on commit d310c7d

Please sign in to comment.