Skip to content

Commit f82a614

Browse files
committed
* Fix duplicate endpoint creation. Fix leaking of endpoints when containers are killed. Fix DNS & Suffix plumging. Add a sample cni conf. Modify the AdditionalParam structure from Type to Name
Update the delegate type Fix overlay to print debug message. Pass along the DNS
1 parent 6909a68 commit f82a614

File tree

12 files changed

+206
-29
lines changed

12 files changed

+206
-29
lines changed

pkg/hns/endpoint_windows.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ func DeprovisionEndpoint(epName string, netns string, containerID string) error
6262
err = hcsshim.HotDetachEndpoint(containerID, hnsEndpoint.Id)
6363
if err != nil {
6464
log.Printf("[win-cni] Failed to detach endpoint %v, err:%v", epName, err)
65-
return nil
65+
// Do not consider this as failure, else this would leak endpoints
6666
}
6767

6868
_, err = hnsEndpoint.Delete()
6969
if err != nil {
7070
log.Printf("[win-cni] Failed to delete endpoint %v, err:%v", epName, err)
71-
return nil
71+
// Do not return error
7272
}
7373

7474
return nil
@@ -83,7 +83,7 @@ func ProvisionEndpoint(epName string, expectedNetworkId string, containerID stri
8383
// check if endpoint already exists
8484
createEndpoint := true
8585
hnsEndpoint, err := hcsshim.GetHNSEndpointByName(epName)
86-
if hnsEndpoint != nil && hnsEndpoint.VirtualNetwork != expectedNetworkId {
86+
if hnsEndpoint != nil && hnsEndpoint.VirtualNetwork == expectedNetworkId {
8787
log.Printf("[win-cni] Found existing endpoint %v", epName)
8888
createEndpoint = false
8989
}

pkg/hns/netconf.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package hns
1717
import (
1818
"encoding/json"
1919
"github.com/containernetworking/cni/pkg/types"
20+
"log"
2021
"strings"
2122
)
2223

@@ -27,7 +28,7 @@ type NetConf struct {
2728
}
2829

2930
type policyArgument struct {
30-
Type string
31+
Name string
3132
Value map[string]interface{}
3233
}
3334

@@ -40,7 +41,8 @@ func (n *NetConf) MarshalPolicies() []json.RawMessage {
4041

4142
var result []json.RawMessage
4243
for _, policyArg := range n.AdditionalArgs {
43-
if !strings.EqualFold(policyArg.Type, "EndpointPolicy") {
44+
log.Printf("PolicyArgs[%v]", policyArg)
45+
if !strings.EqualFold(policyArg.Name, "EndpointPolicy") {
4446
continue
4547
}
4648
if data, err := json.Marshal(policyArg.Value); err == nil {
@@ -51,7 +53,6 @@ func (n *NetConf) MarshalPolicies() []json.RawMessage {
5153
return result
5254
}
5355

54-
5556
// ApplyOutboundNatPolicy applies NAT Policy in VFP using HNS
5657
// Simultaneously an exception is added for the network that has to be Nat'd
5758
func (n *NetConf) ApplyOutboundNatPolicy(nwToNat string) {
@@ -60,7 +61,7 @@ func (n *NetConf) ApplyOutboundNatPolicy(nwToNat string) {
6061
}
6162

6263
for _, policy := range n.AdditionalArgs {
63-
if !strings.EqualFold(policy.Type, "EndpointPolicy") {
64+
if !strings.EqualFold(policy.Name, "EndpointPolicy") {
6465
continue
6566
}
6667

@@ -94,7 +95,7 @@ func (n *NetConf) ApplyOutboundNatPolicy(nwToNat string) {
9495

9596
// didn't find the policy, add it
9697
natEntry := policyArgument{
97-
Type: "EndpointPolicy",
98+
Name: "EndpointPolicy",
9899
Value: map[string]interface{}{
99100
"Type": "OutBoundNAT",
100101
"ExceptionList": []interface{}{
@@ -114,7 +115,7 @@ func (n *NetConf) ApplyDefaultPAPolicy(paAddress string) {
114115

115116
// if its already present, leave untouched
116117
for _, policy := range n.AdditionalArgs {
117-
if policy.Type == "EndpointPolicy" {
118+
if policy.Name == "EndpointPolicy" {
118119
if hasKey(policy.Value, "PA") {
119120
// found it, don't override
120121
return
@@ -128,7 +129,7 @@ func (n *NetConf) ApplyDefaultPAPolicy(paAddress string) {
128129
"PA": paAddress,
129130
}
130131
paPolicy := &policyArgument{
131-
Type: "EndpointPolicy",
132+
Name: "EndpointPolicy",
132133
Value: paPolicyData,
133134
}
134135

pkg/hns/netconf_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var _ = Describe("HNS NetConf", func() {
3232
Expect(addlArgs).Should(HaveLen(1))
3333

3434
policy := addlArgs[0]
35-
Expect(policy.Type).Should(Equal("EndpointPolicy"))
35+
Expect(policy.Name).Should(Equal("EndpointPolicy"))
3636

3737
value := policy.Value
3838
Expect(value).Should(HaveKey("Type"))
@@ -59,7 +59,7 @@ var _ = Describe("HNS NetConf", func() {
5959
Expect(addlArgs).Should(HaveLen(1))
6060

6161
policy := addlArgs[0]
62-
Expect(policy.Type).Should(Equal("EndpointPolicy"))
62+
Expect(policy.Name).Should(Equal("EndpointPolicy"))
6363

6464
value := policy.Value
6565
Expect(value).Should(HaveKey("Type"))
@@ -85,7 +85,7 @@ var _ = Describe("HNS NetConf", func() {
8585
Expect(addlArgs).Should(HaveLen(1))
8686

8787
policy := addlArgs[0]
88-
Expect(policy.Type).Should(Equal("EndpointPolicy"))
88+
Expect(policy.Name).Should(Equal("EndpointPolicy"))
8989

9090
value := policy.Value
9191
Expect(value).Should(HaveKey("Type"))
@@ -106,7 +106,7 @@ var _ = Describe("HNS NetConf", func() {
106106
Expect(addlArgs).Should(HaveLen(1))
107107

108108
policy := addlArgs[0]
109-
Expect(policy.Type).Should(Equal("EndpointPolicy"))
109+
Expect(policy.Name).Should(Equal("EndpointPolicy"))
110110

111111
value := policy.Value
112112
Expect(value).Should(HaveKey("Type"))
@@ -126,13 +126,13 @@ var _ = Describe("HNS NetConf", func() {
126126
n := NetConf{
127127
AdditionalArgs: []policyArgument{
128128
{
129-
Type: "EndpointPolicy",
129+
Name: "EndpointPolicy",
130130
Value: map[string]interface{}{
131131
"someKey": "someValue",
132132
},
133133
},
134134
{
135-
Type: "someOtherType",
135+
Name: "someOtherType",
136136
Value: map[string]interface{}{
137137
"someOtherKey": "someOtherValue",
138138
},
@@ -165,7 +165,7 @@ var _ = Describe("HNS NetConf", func() {
165165
Expect(addlArgs).Should(HaveLen(1))
166166

167167
policy := addlArgs[0]
168-
Expect(policy.Type).Should(Equal("EndpointPolicy"))
168+
Expect(policy.Name).Should(Equal("EndpointPolicy"))
169169

170170
value := policy.Value
171171
Expect(value).Should(HaveKey("Type"))

plugins/main/windows/build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ for d in $PLUGINS; do
1414
then
1515
GOBIN=${OUTDIR} go install -pkgdir $GOPATH/pkg "$@" $REPO_PATH/$d
1616
else
17-
go build -o "${OUTDIR}/$plugin.exe" -pkgdir "$GOPATH/pkg" "$@" "$REPO_PATH/$d"
17+
go build -o "${OUTDIR}/$plugin.exe" -pkgdir "$GOPATH/pkg" "$@" "$REPO_PATH/$d"
1818
fi
1919
fi
2020
done
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
all: $(shell find . -type f -name '*.go')
4+
GOOS=windows go build -o l2bridge.exe
5+
clean:
6+
rm -rf l2bridge.exe
7+

plugins/main/windows/l2bridge/l2bridge_windows.go

+43-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/json"
2121
"errors"
2222
"fmt"
23+
"log"
2324
"net"
2425
"runtime"
2526

@@ -36,8 +37,14 @@ import (
3637
type NetConf struct {
3738
hns.NetConf
3839

39-
IPMasq bool
40-
clusterNetworkPrefix net.IPNet
40+
ipmasq bool `json:"ipmasq,omitempty"`
41+
clusterNetworkPrefix net.IPNet `json:"clusterprefix,omitempty"`
42+
}
43+
type K8sCniEnvArgs struct {
44+
types.CommonArgs
45+
K8S_POD_NAMESPACE types.UnmarshallableString `json:"K8S_POD_NAMESPACE,omitempty"`
46+
K8S_POD_NAME types.UnmarshallableString `json:"K8S_POD_NAME,omitempty"`
47+
K8S_POD_INFRA_CONTAINER_ID types.UnmarshallableString `json:"K8S_POD_INFRA_CONTAINER_ID,omitempty"`
4148
}
4249

4350
func init() {
@@ -47,20 +54,36 @@ func init() {
4754
runtime.LockOSThread()
4855
}
4956

57+
func parseCniArgs(args string) (*K8sCniEnvArgs, error) {
58+
podConfig := K8sCniEnvArgs{}
59+
err := types.LoadArgs(args, &podConfig)
60+
if err != nil {
61+
return nil, err
62+
}
63+
return &podConfig, nil
64+
}
65+
5066
func loadNetConf(bytes []byte) (*NetConf, string, error) {
5167
n := &NetConf{}
5268
if err := json.Unmarshal(bytes, n); err != nil {
5369
return nil, "", fmt.Errorf("failed to load netconf: %v", err)
5470
}
71+
log.Printf("Loaded NetConf %v", n)
5572
return n, n.CNIVersion, nil
5673
}
5774

5875
func cmdAdd(args *skel.CmdArgs) error {
76+
log.Printf("[cni-net] Processing ADD command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v}.",
77+
args.ContainerID, args.Netns, args.IfName, args.Args, args.Path)
5978
n, cniVersion, err := loadNetConf(args.StdinData)
6079
if err != nil {
6180
return err
6281
}
63-
82+
cniargs, err := parseCniArgs(args.Args)
83+
k8sNamespace := "default"
84+
if err == nil {
85+
k8sNamespace = string(cniargs.K8S_POD_NAMESPACE)
86+
}
6487
networkName := n.Name
6588
hnsNetwork, err := hcsshim.GetHNSNetworkByName(networkName)
6689
if err != nil {
@@ -71,7 +94,7 @@ func cmdAdd(args *skel.CmdArgs) error {
7194
return fmt.Errorf("network %v not found", networkName)
7295
}
7396

74-
if !strings.EqualFold(hnsNetwork.Type,"L2Bridge") {
97+
if !strings.EqualFold(hnsNetwork.Type, "L2Bridge") {
7598
return fmt.Errorf("network %v is of an unexpected type: %v", networkName, hnsNetwork.Type)
7699
}
77100

@@ -99,20 +122,31 @@ func cmdAdd(args *skel.CmdArgs) error {
99122
gw[len(gw)-1] += 2
100123

101124
// NAT based on the the configured cluster network
102-
if n.IPMasq {
125+
if n.ipmasq {
103126
n.ApplyOutboundNatPolicy(n.clusterNetworkPrefix.String())
104127
}
105128

129+
nameservers := strings.Join(n.DNS.Nameservers, ",")
130+
if result.DNS.Nameservers != nil {
131+
nameservers = strings.Join(result.DNS.Nameservers, ",")
132+
}
133+
134+
dnsSuffix := ""
135+
if len(n.DNS.Search) > 0 {
136+
dnsSuffix = k8sNamespace + "." + n.DNS.Search[0]
137+
}
138+
106139
hnsEndpoint := &hcsshim.HNSEndpoint{
107140
Name: epName,
108141
VirtualNetwork: hnsNetwork.Id,
109-
DNSServerList: strings.Join(result.DNS.Nameservers, ","),
110-
DNSSuffix: result.DNS.Domain,
142+
DNSServerList: nameservers,
143+
DNSSuffix: dnsSuffix,
111144
GatewayAddress: gw.String(),
112145
IPAddress: result.IPs[0].Address.IP,
113146
Policies: n.MarshalPolicies(),
114147
}
115148

149+
log.Printf("Adding Hns Endpoint %v", hnsEndpoint)
116150
return hnsEndpoint, nil
117151
})
118152

@@ -129,6 +163,8 @@ func cmdAdd(args *skel.CmdArgs) error {
129163
}
130164

131165
func cmdDel(args *skel.CmdArgs) error {
166+
log.Printf("[cni-net] Processing DEL command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v}.",
167+
args.ContainerID, args.Netns, args.IfName, args.Args, args.Path)
132168
n, _, err := loadNetConf(args.StdinData)
133169
if err != nil {
134170
return err
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "cbr0",
3+
"type": "flannel",
4+
"delegate": {
5+
"type": "l2bridge",
6+
"dns": {
7+
"Nameservers": [
8+
"11.0.0.10"
9+
],
10+
"Search": [
11+
"svc.cluster.local"
12+
]
13+
},
14+
"AdditionalArgs": [
15+
{
16+
"Name": "EndpointPolicy",
17+
"Value": {
18+
"Type": "OutBoundNAT",
19+
"ExceptionList": [
20+
"192.168.0.0/16",
21+
"11.0.0.0/8",
22+
"10.137.196.0/23"
23+
]
24+
}
25+
},
26+
{
27+
"Name": "EndpointPolicy",
28+
"Value": {
29+
"Type": "ROUTE",
30+
"DestinationPrefix": "11.0.0.0/8",
31+
"NeedEncap": true
32+
}
33+
},
34+
{
35+
"Name": "EndpointPolicy",
36+
"Value": {
37+
"Type": "ROUTE",
38+
"DestinationPrefix": "10.137.198.27/32",
39+
"NeedEncap": true
40+
}
41+
}
42+
]
43+
}
44+
}

plugins/main/windows/overlay/Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
all: $(shell find . -type f -name '*.go')
4+
GOOS=windows go build -o overlay.exe
5+
clean:
6+
rm -rf overlay.exe
7+

0 commit comments

Comments
 (0)