@@ -16,12 +16,16 @@ import (
1616 "github.com/Azure/azure-container-networking/telemetry"
1717
1818 cniSkel "github.com/containernetworking/cni/pkg/skel"
19+ cniTypes "github.com/containernetworking/cni/pkg/types"
1920 cniTypesCurr "github.com/containernetworking/cni/pkg/types/current"
2021)
2122
2223const (
2324 // Plugin name.
2425 name = "azure-vnet"
26+
27+ // Supported IP version. Currently support only IPv4
28+ ipVersion = "4"
2529)
2630
2731// NetPlugin represents the CNI network plugin.
@@ -362,6 +366,90 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
362366 return nil
363367}
364368
369+ // Get handles CNI Get commands.
370+ func (plugin * netPlugin ) Get (args * cniSkel.CmdArgs ) error {
371+ var (
372+ result cniTypesCurr.Result
373+ err error
374+ nwCfg * cni.NetworkConfig
375+ epInfo * network.EndpointInfo
376+ iface * cniTypesCurr.Interface
377+ )
378+
379+ log .Printf ("[cni-net] Processing GET command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v}." ,
380+ args .ContainerID , args .Netns , args .IfName , args .Args , args .Path )
381+
382+ defer func () {
383+ // Add Interfaces to result.
384+ iface = & cniTypesCurr.Interface {
385+ Name : args .IfName ,
386+ }
387+ result .Interfaces = append (result .Interfaces , iface )
388+
389+ if err == nil {
390+ // Convert result to the requested CNI version.
391+ res , err := result .GetAsVersion (nwCfg .CNIVersion )
392+ if err != nil {
393+ err = plugin .Error (err )
394+ }
395+ // Output the result to stdout.
396+ res .Print ()
397+ }
398+
399+ log .Printf ("[cni-net] GET command completed with result:%+v err:%v." , result , err )
400+ }()
401+
402+ // Parse network configuration from stdin.
403+ nwCfg , err = cni .ParseNetworkConfig (args .StdinData )
404+ if err != nil {
405+ err = plugin .Errorf ("Failed to parse network configuration: %v." , err )
406+ return err
407+ }
408+
409+ log .Printf ("[cni-net] Read network configuration %+v." , nwCfg )
410+
411+ // Initialize values from network config.
412+ networkId := nwCfg .Name
413+ endpointId := GetEndpointID (args )
414+
415+ // Query the network.
416+ _ , err = plugin .nm .GetNetworkInfo (networkId )
417+ if err != nil {
418+ plugin .Errorf ("Failed to query network: %v" , err )
419+ return err
420+ }
421+
422+ // Query the endpoint.
423+ epInfo , err = plugin .nm .GetEndpointInfo (networkId , endpointId )
424+ if err != nil {
425+ plugin .Errorf ("Failed to query endpoint: %v" , err )
426+ return err
427+ }
428+
429+ for _ , ipAddresses := range epInfo .IPAddresses {
430+ ipConfig := & cniTypesCurr.IPConfig {
431+ Version : ipVersion ,
432+ Interface : & epInfo .IfIndex ,
433+ Address : ipAddresses ,
434+ }
435+
436+ if epInfo .Gateways != nil {
437+ ipConfig .Gateway = epInfo .Gateways [0 ]
438+ }
439+
440+ result .IPs = append (result .IPs , ipConfig )
441+ }
442+
443+ for _ , route := range epInfo .Routes {
444+ result .Routes = append (result .Routes , & cniTypes.Route {Dst : route .Dst , GW : route .Gw })
445+ }
446+
447+ result .DNS .Nameservers = epInfo .DNS .Servers
448+ result .DNS .Domain = epInfo .DNS .Suffix
449+
450+ return nil
451+ }
452+
365453// Delete handles CNI delete commands.
366454func (plugin * netPlugin ) Delete (args * cniSkel.CmdArgs ) error {
367455 var err error
0 commit comments