@@ -18,6 +18,8 @@ import (
18
18
"github.com/AliyunContainerService/terway/pkg/tracing"
19
19
"github.com/AliyunContainerService/terway/rpc"
20
20
"github.com/AliyunContainerService/terway/types"
21
+ "github.com/containernetworking/cni/libcni"
22
+ containertypes "github.com/containernetworking/cni/pkg/types"
21
23
22
24
"github.com/pkg/errors"
23
25
log "github.com/sirupsen/logrus"
@@ -45,6 +47,11 @@ const (
45
47
tracingKeyPendingPodsCount = "pending_pods_count"
46
48
47
49
commandMapping = "mapping"
50
+
51
+ cniDefaultPath = "/opt/cni/bin"
52
+ // this file is generated from configmap
53
+ terwayCNIConf = "/etc/eni/10-terway.conf"
54
+ cniExecTimeout = 10 * time .Second
48
55
)
49
56
50
57
type networkService struct {
@@ -64,6 +71,8 @@ type networkService struct {
64
71
pendingPodsLock sync.RWMutex
65
72
sync.RWMutex
66
73
74
+ cniBinPath string
75
+
67
76
rpc.UnimplementedTerwayBackendServer
68
77
}
69
78
@@ -261,6 +270,9 @@ func (networkService *networkService) AllocIP(ctx context.Context, r *rpc.AllocI
261
270
Type : eniMultiIP .GetType (),
262
271
},
263
272
},
273
+ NetNs : func (s string ) * string {
274
+ return & s
275
+ }(r .Netns ),
264
276
}
265
277
networkContext .resources = append (networkContext .resources , newRes .Resources ... )
266
278
if networkService .eipResMgr != nil && podinfo .EipInfo .PodEip {
@@ -319,6 +331,9 @@ func (networkService *networkService) AllocIP(ctx context.Context, r *rpc.AllocI
319
331
Type : vpcEni .GetType (),
320
332
},
321
333
},
334
+ NetNs : func (s string ) * string {
335
+ return & s
336
+ }(r .Netns ),
322
337
}
323
338
networkContext .resources = append (networkContext .resources , newRes .Resources ... )
324
339
if networkService .eipResMgr != nil && podinfo .EipInfo .PodEip {
@@ -381,6 +396,9 @@ func (networkService *networkService) AllocIP(ctx context.Context, r *rpc.AllocI
381
396
Type : vpcVeth .GetType (),
382
397
},
383
398
},
399
+ NetNs : func (s string ) * string {
400
+ return & s
401
+ }(r .Netns ),
384
402
}
385
403
networkContext .resources = append (networkContext .resources , newRes .Resources ... )
386
404
err = networkService .resourceDB .Put (podInfoKey (podinfo .Namespace , podinfo .Name ), newRes )
@@ -679,18 +697,73 @@ func (networkService *networkService) startGarbageCollectionLoop() {
679
697
}
680
698
681
699
func (networkService * networkService ) startPeriodCheck () {
682
- log .Debugf ("compare poll with metadata" )
683
- podMapping , err := networkService .GetResourceMapping ()
684
- if err != nil {
685
- log .Error (err )
686
- return
687
- }
688
- for _ , res := range podMapping {
689
- if res .Valid {
690
- continue
700
+ // check pool
701
+ func () {
702
+ log .Debugf ("compare poll with metadata" )
703
+ podMapping , err := networkService .GetResourceMapping ()
704
+ if err != nil {
705
+ log .Error (err )
706
+ return
691
707
}
692
- _ = tracing .RecordPodEvent (res .Name , res .Namespace , corev1 .EventTypeWarning , "ResourceInvalid" , fmt .Sprintf ("resource %s" , res .LocalResID ))
693
- }
708
+ for _ , res := range podMapping {
709
+ if res .Valid {
710
+ continue
711
+ }
712
+ _ = tracing .RecordPodEvent (res .Name , res .Namespace , corev1 .EventTypeWarning , "ResourceInvalid" , fmt .Sprintf ("resource %s" , res .LocalResID ))
713
+ }
714
+ }()
715
+ // call CNI CHECK, make sure all dev is ok
716
+ func () {
717
+ log .Debugf ("call CNI CHECK" )
718
+ defer func () {
719
+ log .Debugf ("call CNI CHECK end" )
720
+ }()
721
+ networkService .RLock ()
722
+ podResList , err := networkService .resourceDB .List ()
723
+ networkService .RUnlock ()
724
+ if err != nil {
725
+ log .Error (err )
726
+ return
727
+ }
728
+ ff , err := ioutil .ReadFile (terwayCNIConf )
729
+ if err != nil {
730
+ log .Error (err )
731
+ return
732
+ }
733
+ for _ , v := range podResList {
734
+ res := v .(PodResources )
735
+ if res .NetNs == nil {
736
+ continue
737
+ }
738
+ log .Debugf ("checking pod name %s" , res .PodInfo .Name )
739
+ cniCfg := libcni .NewCNIConfig ([]string {networkService .cniBinPath }, nil )
740
+ func () {
741
+ ctx , cancel := context .WithTimeout (context .Background (), cniExecTimeout )
742
+ defer cancel ()
743
+ err := cniCfg .CheckNetwork (ctx , & libcni.NetworkConfig {
744
+ Network : & containertypes.NetConf {
745
+ CNIVersion : "0.4.0" ,
746
+ Name : "terway" ,
747
+ Type : "terway" ,
748
+ },
749
+ Bytes : ff ,
750
+ }, & libcni.RuntimeConf {
751
+ ContainerID : "fake" , // must provide
752
+ NetNS : * res .NetNs ,
753
+ IfName : "eth0" ,
754
+ Args : [][2 ]string {
755
+ {"K8S_POD_NAME" , res .PodInfo .Name },
756
+ {"K8S_POD_NAMESPACE" , res .PodInfo .Namespace },
757
+ },
758
+ })
759
+ if err != nil {
760
+ log .Error (err )
761
+ return
762
+ }
763
+ }()
764
+ }
765
+ }()
766
+
694
767
}
695
768
696
769
// tracing
@@ -806,12 +879,17 @@ func (networkService *networkService) GetResourceMapping() ([]tracing.PodMapping
806
879
807
880
func newNetworkService (configFilePath , kubeconfig , master , daemonMode string ) (rpc.TerwayBackendServer , error ) {
808
881
log .Debugf ("start network service with: %s, %s" , configFilePath , daemonMode )
882
+ cniBinPath := os .Getenv ("CNI_PATH" )
883
+ if cniBinPath == "" {
884
+ cniBinPath = cniDefaultPath
885
+ }
809
886
netSrv := & networkService {
810
887
configFilePath : configFilePath ,
811
888
kubeConfig : kubeconfig ,
812
889
master : master ,
813
890
pendingPods : map [string ]interface {}{},
814
891
pendingPodsLock : sync.RWMutex {},
892
+ cniBinPath : cniBinPath ,
815
893
}
816
894
if daemonMode == daemonModeENIMultiIP || daemonMode == daemonModeVPC || daemonMode == daemonModeENIOnly {
817
895
netSrv .daemonMode = daemonMode
0 commit comments