@@ -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,34 @@ 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 {
103
- n .ApplyOutboundNatPolicy (n .clusterNetworkPrefix .String ())
125
+ // if n.IPMasq {
126
+ // n.ApplyOutboundNatPolicy(n.clusterNetworkPrefix.String())
127
+ // }
128
+ nameservers := strings .Join (n .DNS .Nameservers , "," )
129
+ if result .DNS .Nameservers != nil {
130
+ nameservers = strings .Join (result .DNS .Nameservers , "," )
104
131
}
105
132
133
+ dnsSuffix := ""
134
+ if len (n .DNS .Search ) > 0 {
135
+ dnsSuffix = k8sNamespace + "." + n .DNS .Search [0 ]
136
+ }
137
+
138
+ // if len(result.DNS.Domain) != 0 {
139
+ // dnsSuffix = result.DNS.Domain
140
+ // }
141
+
106
142
hnsEndpoint := & hcsshim.HNSEndpoint {
107
143
Name : epName ,
108
144
VirtualNetwork : hnsNetwork .Id ,
109
- DNSServerList : strings . Join ( result . DNS . Nameservers , "," ) ,
110
- DNSSuffix : result . DNS . Domain ,
145
+ DNSServerList : nameservers ,
146
+ DNSSuffix : dnsSuffix ,
111
147
GatewayAddress : gw .String (),
112
148
IPAddress : result .IPs [0 ].Address .IP ,
113
149
Policies : n .MarshalPolicies (),
114
150
}
115
151
152
+ log .Printf ("Added Hns Endpoint %v" , hnsEndpoint )
116
153
return hnsEndpoint , nil
117
154
})
118
155
@@ -129,6 +166,8 @@ func cmdAdd(args *skel.CmdArgs) error {
129
166
}
130
167
131
168
func cmdDel (args * skel.CmdArgs ) error {
169
+ log .Printf ("[cni-net] Processing ADD command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v}." ,
170
+ args .ContainerID , args .Netns , args .IfName , args .Args , args .Path )
132
171
n , _ , err := loadNetConf (args .StdinData )
133
172
if err != nil {
134
173
return err
0 commit comments