-
Notifications
You must be signed in to change notification settings - Fork 260
Support k8s DNS & endpoint policy #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f11dfa6
6679330
53d08b6
a098531
f8c4803
7a1ad42
35167e5
6d84f14
c6bfe4b
d4792ac
41c627b
ec3f5ed
1dd1052
d8a4d64
1a7a2d8
2015486
2bc7f5c
06f7d3a
fc861cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,21 @@ | ||
| { | ||
| "cniVersion": "0.3.0", | ||
| "name": "azure", | ||
| "plugins": [{ | ||
| "type": "azure-vnet", | ||
| "mode": "bridge", | ||
| "bridge": "azure0", | ||
| "ipam": { | ||
| "type": "azure-vnet-ipam" | ||
| } | ||
| }, | ||
| { | ||
| "type": "portmap", | ||
| "capabilities": {"portMappings": true}, | ||
| "snat": true | ||
| } | ||
| ] | ||
| } | ||
| "cniVersion":"0.3.0", | ||
| "name":"azure", | ||
| "plugins":[ | ||
| { | ||
| "type":"azure-vnet", | ||
| "mode":"bridge", | ||
| "bridge":"azure0", | ||
| "ipam":{ | ||
| "type":"azure-vnet-ipam" | ||
| } | ||
| }, | ||
| { | ||
| "type":"portmap", | ||
| "capabilities":{ | ||
| "portMappings":true | ||
| }, | ||
| "snat":true | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,43 @@ | ||
| { | ||
| "cniVersion": "0.3.0", | ||
| "name": "azure", | ||
| "plugins": [{ | ||
| "type": "azure-vnet", | ||
| "mode": "bridge", | ||
| "bridge": "azure0", | ||
| "ipam": { | ||
| "type": "azure-vnet-ipam" | ||
| } | ||
| "cniVersion":"0.3.0", | ||
| "name":"azure", | ||
| "plugins":[ | ||
| { | ||
| "type":"azure-vnet", | ||
| "mode":"bridge", | ||
| "bridge":"azure0", | ||
| "ipam":{ | ||
| "type":"azure-vnet-ipam" | ||
| }, | ||
| "dns":{ | ||
| "Nameservers":[ | ||
| "168.63.129.16", | ||
| "10.0.0.10" | ||
| ], | ||
| "Search":[ | ||
| "svc.cluster.local" | ||
| ] | ||
| }, | ||
| "AdditionalArgs":[ | ||
| { | ||
| "Name":"EndpointPolicy", | ||
| "Value":{ | ||
| "Type":"OutBoundNAT", | ||
| "ExceptionList":[ | ||
| "10.240.0.0/16", | ||
| "10.0.0.0/8" | ||
| ] | ||
| } | ||
| }, | ||
| { | ||
| "Name":"EndpointPolicy", | ||
| "Value":{ | ||
| "Type":"ROUTE", | ||
| "DestinationPrefix":"10.0.0.0/8", | ||
| "NeedEncap":true | ||
| } | ||
| } | ||
| ] | ||
| ] | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ package network | |
|
|
||
| import ( | ||
| "net" | ||
| "strings" | ||
|
|
||
| "github.com/Azure/azure-container-networking/cni" | ||
| "github.com/Azure/azure-container-networking/common" | ||
|
|
@@ -123,16 +124,46 @@ func (plugin *netPlugin) findMasterInterface(nwCfg *cni.NetworkConfig, subnetPre | |
|
|
||
| // Add handles CNI add commands. | ||
| func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error { | ||
| var result *cniTypesCurr.Result | ||
| var err error | ||
| var ( | ||
| result *cniTypesCurr.Result | ||
| err error | ||
| nwCfg *cni.NetworkConfig | ||
| ipconfig *cniTypesCurr.IPConfig | ||
| epInfo *network.EndpointInfo | ||
| iface *cniTypesCurr.Interface | ||
| ) | ||
|
|
||
| log.Printf("[cni-net] Processing ADD command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v}.", | ||
| args.ContainerID, args.Netns, args.IfName, args.Args, args.Path) | ||
|
|
||
| defer func() { log.Printf("[cni-net] ADD command completed with result:%+v err:%v.", result, err) }() | ||
| defer func() { | ||
| // Add Interfaces to result. | ||
| iface = &cniTypesCurr.Interface{ | ||
| Name: args.IfName, | ||
| } | ||
| result.Interfaces = append(result.Interfaces, iface) | ||
|
|
||
| // Convert result to the requested CNI version. | ||
| res, err := result.GetAsVersion(nwCfg.CNIVersion) | ||
| if err != nil { | ||
| err = plugin.Error(err) | ||
| } | ||
|
|
||
| // Output the result to stdout. | ||
| res.Print() | ||
| log.Printf("[cni-net] ADD command completed with result:%+v err:%v.", result, err) | ||
| }() | ||
|
|
||
| // Parse Pod arguments. | ||
| podCfg, err := cni.ParseCniArgs(args.Args) | ||
| k8sNamespace := string(podCfg.K8S_POD_NAMESPACE) | ||
| if len(k8sNamespace) == 0 { | ||
| err = plugin.Errorf("No k8s pod namespace provided.") | ||
| return err | ||
| } | ||
|
|
||
| // Parse network configuration from stdin. | ||
| nwCfg, err := cni.ParseNetworkConfig(args.StdinData) | ||
| nwCfg, err = cni.ParseNetworkConfig(args.StdinData) | ||
| if err != nil { | ||
| err = plugin.Errorf("Failed to parse network configuration: %v.", err) | ||
| return err | ||
|
|
@@ -142,11 +173,31 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error { | |
|
|
||
| // Initialize values from network config. | ||
| networkId := nwCfg.Name | ||
| endpointId := plugin.GetEndpointID(args) | ||
| endpointId := network.GetEndpointID(args) | ||
|
|
||
| nwInfo, nwInfoErr := plugin.nm.GetNetworkInfo(networkId) | ||
|
|
||
| /* Handle consecutive ADD calls for infrastructure containers. | ||
| * This is a temporary work around for issue #57253 of Kubernetes. | ||
| * We can delete this if statement once they fix it. | ||
| * Issue link: https://github.com/kubernetes/kubernetes/issues/57253 | ||
| */ | ||
| epInfo, _ = plugin.nm.GetEndpointInfo(networkId, endpointId) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should not be common to linux and windows. can you please move this to separate file(windows)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
| if epInfo != nil { | ||
| result, err = handleConsecutiveAdd(args.ContainerID, endpointId, nwInfo, nwCfg) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if result != nil { | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| policies := cni.GetPoliciesFromNwCfg(nwCfg.AdditionalArgs) | ||
|
|
||
| // Check whether the network already exists. | ||
| nwInfo, err := plugin.nm.GetNetworkInfo(networkId) | ||
| if err != nil { | ||
| if nwInfoErr != nil { | ||
| // Network does not exist. | ||
| log.Printf("[cni-net] Creating network %v.", networkId) | ||
|
|
||
|
|
@@ -158,7 +209,7 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error { | |
| } | ||
|
|
||
| // Derive the subnet prefix from allocated IP address. | ||
| ipconfig := result.IPs[0] | ||
| ipconfig = result.IPs[0] | ||
| subnetPrefix := ipconfig.Address | ||
| subnetPrefix.IP = subnetPrefix.IP.Mask(subnetPrefix.Mask) | ||
|
|
||
|
|
@@ -201,6 +252,11 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error { | |
| }, | ||
| }, | ||
| BridgeName: nwCfg.Bridge, | ||
| DNS: network.DNSInfo{ | ||
| Servers: nwCfg.DNS.Nameservers, | ||
| Suffix: strings.Join(nwCfg.DNS.Search, ","), | ||
| }, | ||
| Policies: policies, | ||
| } | ||
|
|
||
| err = plugin.nm.CreateNetwork(&nwInfo) | ||
|
|
@@ -223,7 +279,7 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error { | |
| return err | ||
| } | ||
|
|
||
| ipconfig := result.IPs[0] | ||
| ipconfig = result.IPs[0] | ||
|
|
||
| // On failure, call into IPAM plugin to release the address. | ||
| defer func() { | ||
|
|
@@ -235,11 +291,31 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error { | |
| } | ||
|
|
||
| // Initialize endpoint info. | ||
| epInfo := &network.EndpointInfo{ | ||
| var dns network.DNSInfo | ||
| if (len(nwCfg.DNS.Search) == 0) != (len(nwCfg.DNS.Nameservers) == 0) { | ||
| err = plugin.Errorf("Wrong DNS configuration: %+v", nwCfg.DNS) | ||
| return err | ||
| } | ||
|
|
||
| if len(nwCfg.DNS.Search) > 0 { | ||
| dns = network.DNSInfo{ | ||
| Servers: nwCfg.DNS.Nameservers, | ||
| Suffix: strings.Join(nwCfg.DNS.Search, ","), | ||
| } | ||
| } else { | ||
| dns = network.DNSInfo{ | ||
| Suffix: result.DNS.Domain, | ||
| Servers: result.DNS.Nameservers, | ||
| } | ||
| } | ||
|
|
||
| epInfo = &network.EndpointInfo{ | ||
| Id: endpointId, | ||
| ContainerID: args.ContainerID, | ||
| NetNsPath: args.Netns, | ||
| IfName: args.IfName, | ||
| DNS: dns, | ||
| Policies: policies, | ||
| } | ||
|
|
||
| // Populate addresses. | ||
|
|
@@ -252,10 +328,6 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error { | |
| epInfo.Routes = append(epInfo.Routes, network.RouteInfo{Dst: route.Dst, Gw: route.GW}) | ||
| } | ||
|
|
||
| // Populate DNS info. | ||
| epInfo.DNS.Suffix = result.DNS.Domain | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check network config and if dns is specified, override epinfo dns otherwise use whatever ipam returns
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
| epInfo.DNS.Servers = result.DNS.Nameservers | ||
|
|
||
| // Create the endpoint. | ||
| log.Printf("[cni-net] Creating endpoint %v.", epInfo.Id) | ||
| err = plugin.nm.CreateEndpoint(networkId, epInfo) | ||
|
|
@@ -264,22 +336,6 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error { | |
| return err | ||
| } | ||
|
|
||
| // Add Interfaces to result. | ||
| iface := &cniTypesCurr.Interface{ | ||
| Name: epInfo.IfName, | ||
| } | ||
| result.Interfaces = append(result.Interfaces, iface) | ||
|
|
||
| // Convert result to the requested CNI version. | ||
| res, err := result.GetAsVersion(nwCfg.CNIVersion) | ||
| if err != nil { | ||
| err = plugin.Error(err) | ||
| return err | ||
| } | ||
|
|
||
| // Output the result to stdout. | ||
| res.Print() | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
|
|
@@ -303,7 +359,7 @@ func (plugin *netPlugin) Delete(args *cniSkel.CmdArgs) error { | |
|
|
||
| // Initialize values from network config. | ||
| networkId := nwCfg.Name | ||
| endpointId := plugin.GetEndpointID(args) | ||
| endpointId := network.GetEndpointID(args) | ||
|
|
||
| // Query the network. | ||
| nwInfo, err := plugin.nm.GetNetworkInfo(networkId) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package network | ||
|
|
||
| import ( | ||
| cniTypesCurr "github.com/containernetworking/cni/pkg/types/current" | ||
| ) | ||
|
|
||
| // handleConsecutiveAdd is a dummy function for Linux platform. | ||
| func handleConsecutiveAdd(containerId, endpointId string, nwInfo *NetworkInfo, nwCfg *NetworkConfig) (*cniTypesCurr.Result, error) { | ||
| return nil, nil | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
space damage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The alignment is correct.