@@ -20,6 +20,7 @@ import (
20
20
"encoding/json"
21
21
"errors"
22
22
"fmt"
23
+ "log"
23
24
"net"
24
25
"runtime"
25
26
@@ -36,8 +37,14 @@ import (
36
37
type NetConf struct {
37
38
hns.NetConf
38
39
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"`
41
48
}
42
49
43
50
func init () {
@@ -47,20 +54,36 @@ func init() {
47
54
runtime .LockOSThread ()
48
55
}
49
56
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
+
50
66
func loadNetConf (bytes []byte ) (* NetConf , string , error ) {
51
67
n := & NetConf {}
52
68
if err := json .Unmarshal (bytes , n ); err != nil {
53
69
return nil , "" , fmt .Errorf ("failed to load netconf: %v" , err )
54
70
}
71
+ log .Printf ("Loaded NetConf %v" , n )
55
72
return n , n .CNIVersion , nil
56
73
}
57
74
58
75
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 )
59
78
n , cniVersion , err := loadNetConf (args .StdinData )
60
79
if err != nil {
61
80
return err
62
81
}
63
-
82
+ cniargs , err := parseCniArgs (args .Args )
83
+ k8sNamespace := "default"
84
+ if err == nil {
85
+ k8sNamespace = string (cniargs .K8S_POD_NAMESPACE )
86
+ }
64
87
networkName := n .Name
65
88
hnsNetwork , err := hcsshim .GetHNSNetworkByName (networkName )
66
89
if err != nil {
@@ -71,7 +94,7 @@ func cmdAdd(args *skel.CmdArgs) error {
71
94
return fmt .Errorf ("network %v not found" , networkName )
72
95
}
73
96
74
- if ! strings .EqualFold (hnsNetwork .Type ,"L2Bridge" ) {
97
+ if ! strings .EqualFold (hnsNetwork .Type , "L2Bridge" ) {
75
98
return fmt .Errorf ("network %v is of an unexpected type: %v" , networkName , hnsNetwork .Type )
76
99
}
77
100
@@ -99,20 +122,31 @@ func cmdAdd(args *skel.CmdArgs) error {
99
122
gw [len (gw )- 1 ] += 2
100
123
101
124
// NAT based on the the configured cluster network
102
- if n .IPMasq {
125
+ if n .ipmasq {
103
126
n .ApplyOutboundNatPolicy (n .clusterNetworkPrefix .String ())
104
127
}
105
128
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
+
106
139
hnsEndpoint := & hcsshim.HNSEndpoint {
107
140
Name : epName ,
108
141
VirtualNetwork : hnsNetwork .Id ,
109
- DNSServerList : strings . Join ( result . DNS . Nameservers , "," ) ,
110
- DNSSuffix : result . DNS . Domain ,
142
+ DNSServerList : nameservers ,
143
+ DNSSuffix : dnsSuffix ,
111
144
GatewayAddress : gw .String (),
112
145
IPAddress : result .IPs [0 ].Address .IP ,
113
146
Policies : n .MarshalPolicies (),
114
147
}
115
148
149
+ log .Printf ("Adding Hns Endpoint %v" , hnsEndpoint )
116
150
return hnsEndpoint , nil
117
151
})
118
152
@@ -129,6 +163,8 @@ func cmdAdd(args *skel.CmdArgs) error {
129
163
}
130
164
131
165
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 )
132
168
n , _ , err := loadNetConf (args .StdinData )
133
169
if err != nil {
134
170
return err
0 commit comments