@@ -193,6 +193,7 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
193193 subnetPrefix net.IPNet
194194 cnsNetworkConfig * cns.GetNetworkContainerResponse
195195 enableInfraVnet bool
196+ nwDNSInfo network.DNSInfo
196197 )
197198
198199 log .Printf ("[cni-net] Processing ADD command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v}." ,
@@ -363,7 +364,7 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
363364 return err
364365 }
365366
366- nwDNSInfo , err : = getNetworkDNSSettings (nwCfg , result , k8sNamespace )
367+ nwDNSInfo , err = getNetworkDNSSettings (nwCfg , result , k8sNamespace )
367368 if err != nil {
368369 err = plugin .Errorf ("Failed to getDNSSettings: %v" , err )
369370 return err
@@ -505,11 +506,14 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
505506// Get handles CNI Get commands.
506507func (plugin * netPlugin ) Get (args * cniSkel.CmdArgs ) error {
507508 var (
508- result cniTypesCurr.Result
509- err error
510- nwCfg * cni.NetworkConfig
511- epInfo * network.EndpointInfo
512- iface * cniTypesCurr.Interface
509+ result cniTypesCurr.Result
510+ err error
511+ nwCfg * cni.NetworkConfig
512+ epInfo * network.EndpointInfo
513+ iface * cniTypesCurr.Interface
514+ k8sPodName string
515+ k8sNamespace string
516+ networkId string
513517 )
514518
515519 log .Printf ("[cni-net] Processing GET command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v}." ,
@@ -522,12 +526,14 @@ func (plugin *netPlugin) Get(args *cniSkel.CmdArgs) error {
522526 }
523527 result .Interfaces = append (result .Interfaces , iface )
524528
525- if err == nil {
526- // Convert result to the requested CNI version.
527- res , err := result .GetAsVersion (nwCfg .CNIVersion )
528- if err != nil {
529- err = plugin .Error (err )
530- }
529+ // Convert result to the requested CNI version.
530+ res , vererr := result .GetAsVersion (nwCfg .CNIVersion )
531+ if vererr != nil {
532+ log .Printf ("GetAsVersion failed with error %v" , vererr )
533+ plugin .Error (vererr )
534+ }
535+
536+ if err == nil && res != nil {
531537 // Output the result to stdout.
532538 res .Print ()
533539 }
@@ -536,38 +542,33 @@ func (plugin *netPlugin) Get(args *cniSkel.CmdArgs) error {
536542 }()
537543
538544 // Parse network configuration from stdin.
539- nwCfg , err = cni .ParseNetworkConfig (args .StdinData )
540- if err != nil {
545+ if nwCfg , err = cni .ParseNetworkConfig (args .StdinData ); err != nil {
541546 err = plugin .Errorf ("Failed to parse network configuration: %v." , err )
542547 return err
543548 }
544549
545550 log .Printf ("[cni-net] Read network configuration %+v." , nwCfg )
546551
547552 // Parse Pod arguments.
548- k8sPodName , k8sNamespace , err := plugin .getPodInfo (args .Args )
549- if err != nil {
553+ if k8sPodName , k8sNamespace , err = plugin .getPodInfo (args .Args ); err != nil {
550554 return err
551555 }
552556
553557 // Initialize values from network config.
554- networkId , err := getNetworkName (k8sPodName , k8sNamespace , args .IfName , nwCfg )
555- if err != nil {
558+ if networkId , err = getNetworkName (k8sPodName , k8sNamespace , args .IfName , nwCfg ); err != nil {
556559 log .Printf ("[cni-net] Failed to extract network name from network config. error: %v" , err )
557560 }
558561
559562 endpointId := GetEndpointID (args )
560563
561564 // Query the network.
562- _ , err = plugin .nm .GetNetworkInfo (networkId )
563- if err != nil {
565+ if _ , err = plugin .nm .GetNetworkInfo (networkId ); err != nil {
564566 plugin .Errorf ("Failed to query network: %v" , err )
565567 return err
566568 }
567569
568570 // Query the endpoint.
569- epInfo , err = plugin .nm .GetEndpointInfo (networkId , endpointId )
570- if err != nil {
571+ if epInfo , err = plugin .nm .GetEndpointInfo (networkId , endpointId ); err != nil {
571572 plugin .Errorf ("Failed to query endpoint: %v" , err )
572573 return err
573574 }
@@ -598,17 +599,24 @@ func (plugin *netPlugin) Get(args *cniSkel.CmdArgs) error {
598599
599600// Delete handles CNI delete commands.
600601func (plugin * netPlugin ) Delete (args * cniSkel.CmdArgs ) error {
601- var err error
602+ var (
603+ err error
604+ nwCfg * cni.NetworkConfig
605+ k8sPodName string
606+ k8sNamespace string
607+ networkId string
608+ nwInfo * network.NetworkInfo
609+ epInfo * network.EndpointInfo
610+ )
602611
603612 log .Printf ("[cni-net] Processing DEL command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v}." ,
604613 args .ContainerID , args .Netns , args .IfName , args .Args , args .Path )
605614
606615 defer func () { log .Printf ("[cni-net] DEL command completed with err:%v." , err ) }()
607616
608617 // Parse network configuration from stdin.
609- nwCfg , err := cni .ParseNetworkConfig (args .StdinData )
610- if err != nil {
611- err = plugin .Errorf ("Failed to parse network configuration: %v" , err )
618+ if nwCfg , err = cni .ParseNetworkConfig (args .StdinData ); err != nil {
619+ err = plugin .Errorf ("[cni-net] Failed to parse network configuration: %v" , err )
612620 return err
613621 }
614622
@@ -617,40 +625,35 @@ func (plugin *netPlugin) Delete(args *cniSkel.CmdArgs) error {
617625 plugin .setCNIReportDetails (nwCfg , CNI_DEL , "" )
618626
619627 // Parse Pod arguments.
620- k8sPodName , k8sNamespace , err := plugin .getPodInfo (args .Args )
621- if err != nil {
628+ if k8sPodName , k8sNamespace , err = plugin .getPodInfo (args .Args ); err != nil {
622629 log .Printf ("[cni-net] Failed to get POD info due to error: %v" , err )
623630 }
624631
625632 // Initialize values from network config.
626- networkId , err := getNetworkName (k8sPodName , k8sNamespace , args .IfName , nwCfg )
627- if err != nil {
633+ if networkId , err = getNetworkName (k8sPodName , k8sNamespace , args .IfName , nwCfg ); err != nil {
628634 log .Printf ("[cni-net] Failed to extract network name from network config. error: %v" , err )
629635 }
630636
631637 endpointId := GetEndpointID (args )
632638
633639 // Query the network.
634- nwInfo , err := plugin .nm .GetNetworkInfo (networkId )
635- if err != nil {
640+ if nwInfo , err = plugin .nm .GetNetworkInfo (networkId ); err != nil {
636641 // Log the error but return success if the endpoint being deleted is not found.
637- plugin .Errorf ("Failed to query network: %v" , err )
642+ plugin .Errorf ("[cni-net] Failed to query network: %v" , err )
638643 err = nil
639644 return err
640645 }
641646
642647 // Query the endpoint.
643- epInfo , err := plugin .nm .GetEndpointInfo (networkId , endpointId )
644- if err != nil {
648+ if epInfo , err = plugin .nm .GetEndpointInfo (networkId , endpointId ); err != nil {
645649 // Log the error but return success if the endpoint being deleted is not found.
646- plugin .Errorf ("Failed to query endpoint: %v" , err )
650+ plugin .Errorf ("[cni-net] Failed to query endpoint: %v" , err )
647651 err = nil
648652 return err
649653 }
650654
651655 // Delete the endpoint.
652- err = plugin .nm .DeleteEndpoint (networkId , endpointId )
653- if err != nil {
656+ if err = plugin .nm .DeleteEndpoint (networkId , endpointId ); err != nil {
654657 err = plugin .Errorf ("Failed to delete endpoint: %v" , err )
655658 return err
656659 }
@@ -686,18 +689,21 @@ func (plugin *netPlugin) Delete(args *cniSkel.CmdArgs) error {
686689// Update is only supported for multitenancy and to update routes.
687690func (plugin * netPlugin ) Update (args * cniSkel.CmdArgs ) error {
688691 var (
689- result * cniTypesCurr.Result
690- err error
691- nwCfg * cni.NetworkConfig
692- existingEpInfo * network.EndpointInfo
692+ result * cniTypesCurr.Result
693+ err error
694+ nwCfg * cni.NetworkConfig
695+ existingEpInfo * network.EndpointInfo
696+ podCfg * cni.K8SPodEnvArgs
697+ cnsClient * cnsclient.CNSClient
698+ orchestratorContext []byte
699+ targetNetworkConfig * cns.GetNetworkContainerResponse
693700 )
694701
695702 log .Printf ("[cni-net] Processing UPDATE command with args {Netns:%v Args:%v Path:%v}." ,
696703 args .Netns , args .Args , args .Path )
697704
698705 // Parse network configuration from stdin.
699- nwCfg , err = cni .ParseNetworkConfig (args .StdinData )
700- if err != nil {
706+ if nwCfg , err = cni .ParseNetworkConfig (args .StdinData ); err != nil {
701707 err = plugin .Errorf ("Failed to parse network configuration: %v." , err )
702708 return err
703709 }
@@ -727,9 +733,8 @@ func (plugin *netPlugin) Update(args *cniSkel.CmdArgs) error {
727733 }()
728734
729735 // Parse Pod arguments.
730- podCfg , err := cni .ParseCniArgs (args .Args )
731- if err != nil {
732- log .Printf ("Error while parsing CNI Args during UPDATE %v" , err )
736+ if podCfg , err = cni .ParseCniArgs (args .Args ); err != nil {
737+ log .Printf ("[cni-net] Error while parsing CNI Args during UPDATE %v" , err )
733738 return err
734739 }
735740
@@ -751,8 +756,7 @@ func (plugin *netPlugin) Update(args *cniSkel.CmdArgs) error {
751756 networkID := nwCfg .Name
752757
753758 // Query the network.
754- _ , err = plugin .nm .GetNetworkInfo (networkID )
755- if err != nil {
759+ if _ , err = plugin .nm .GetNetworkInfo (networkID ); err != nil {
756760 errMsg := fmt .Sprintf ("Failed to query network during CNI UPDATE: %v" , err )
757761 log .Printf (errMsg )
758762 return plugin .Errorf (errMsg )
@@ -764,29 +768,26 @@ func (plugin *netPlugin) Update(args *cniSkel.CmdArgs) error {
764768 if err != nil {
765769 plugin .Errorf ("Failed to retrieve target endpoint for CNI UPDATE [name=%v, namespace=%v]: %v" , k8sPodName , k8sNamespace , err )
766770 return err
767- } else {
768- log .Printf ("Retrieved existing endpoint from state that may get update: %+v" , existingEpInfo )
769771 }
770772
773+ log .Printf ("Retrieved existing endpoint from state that may get update: %+v" , existingEpInfo )
774+
771775 // now query CNS to get the target routes that should be there in the networknamespace (as a result of update)
772776 log .Printf ("Going to collect target routes for [name=%v, namespace=%v] from CNS." , k8sPodName , k8sNamespace )
773- cnsClient , err := cnsclient .NewCnsClient (nwCfg .CNSUrl )
774- if err != nil {
777+ if cnsClient , err = cnsclient .NewCnsClient (nwCfg .CNSUrl ); err != nil {
775778 log .Printf ("Initializing CNS client error in CNI Update%v" , err )
776779 log .Printf (err .Error ())
777780 return plugin .Errorf (err .Error ())
778781 }
779782
780783 // create struct with info for target POD
781784 podInfo := cns.KubernetesPodInfo {PodName : k8sPodName , PodNamespace : k8sNamespace }
782- orchestratorContext , err := json .Marshal (podInfo )
783- if err != nil {
785+ if orchestratorContext , err = json .Marshal (podInfo ); err != nil {
784786 log .Printf ("Marshalling KubernetesPodInfo failed with %v" , err )
785787 return plugin .Errorf (err .Error ())
786788 }
787789
788- targetNetworkConfig , err := cnsClient .GetNetworkConfiguration (orchestratorContext )
789- if err != nil {
790+ if targetNetworkConfig , err = cnsClient .GetNetworkConfiguration (orchestratorContext ); err != nil {
790791 log .Printf ("GetNetworkConfiguration failed with %v" , err )
791792 return plugin .Errorf (err .Error ())
792793 }
@@ -831,8 +832,7 @@ func (plugin *netPlugin) Update(args *cniSkel.CmdArgs) error {
831832
832833 // Update the endpoint.
833834 log .Printf ("Now updating existing endpoint %v with targetNetworkConfig %+v." , existingEpInfo .Id , targetNetworkConfig )
834- err = plugin .nm .UpdateEndpoint (networkID , existingEpInfo , targetEpInfo )
835- if err != nil {
835+ if err = plugin .nm .UpdateEndpoint (networkID , existingEpInfo , targetEpInfo ); err != nil {
836836 err = plugin .Errorf ("Failed to update endpoint: %v" , err )
837837 return err
838838 }
0 commit comments