Skip to content

Commit f275c0f

Browse files
authored
Merge pull request #163 from l1b0k/master
check nic status
2 parents 224ee8c + 4f342e3 commit f275c0f

File tree

13 files changed

+723
-99
lines changed

13 files changed

+723
-99
lines changed

daemon/daemon.go

+89-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"github.com/AliyunContainerService/terway/pkg/tracing"
1919
"github.com/AliyunContainerService/terway/rpc"
2020
"github.com/AliyunContainerService/terway/types"
21+
"github.com/containernetworking/cni/libcni"
22+
containertypes "github.com/containernetworking/cni/pkg/types"
2123

2224
"github.com/pkg/errors"
2325
log "github.com/sirupsen/logrus"
@@ -45,6 +47,11 @@ const (
4547
tracingKeyPendingPodsCount = "pending_pods_count"
4648

4749
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
4855
)
4956

5057
type networkService struct {
@@ -64,6 +71,8 @@ type networkService struct {
6471
pendingPodsLock sync.RWMutex
6572
sync.RWMutex
6673

74+
cniBinPath string
75+
6776
rpc.UnimplementedTerwayBackendServer
6877
}
6978

@@ -261,6 +270,9 @@ func (networkService *networkService) AllocIP(ctx context.Context, r *rpc.AllocI
261270
Type: eniMultiIP.GetType(),
262271
},
263272
},
273+
NetNs: func(s string) *string {
274+
return &s
275+
}(r.Netns),
264276
}
265277
networkContext.resources = append(networkContext.resources, newRes.Resources...)
266278
if networkService.eipResMgr != nil && podinfo.EipInfo.PodEip {
@@ -319,6 +331,9 @@ func (networkService *networkService) AllocIP(ctx context.Context, r *rpc.AllocI
319331
Type: vpcEni.GetType(),
320332
},
321333
},
334+
NetNs: func(s string) *string {
335+
return &s
336+
}(r.Netns),
322337
}
323338
networkContext.resources = append(networkContext.resources, newRes.Resources...)
324339
if networkService.eipResMgr != nil && podinfo.EipInfo.PodEip {
@@ -381,6 +396,9 @@ func (networkService *networkService) AllocIP(ctx context.Context, r *rpc.AllocI
381396
Type: vpcVeth.GetType(),
382397
},
383398
},
399+
NetNs: func(s string) *string {
400+
return &s
401+
}(r.Netns),
384402
}
385403
networkContext.resources = append(networkContext.resources, newRes.Resources...)
386404
err = networkService.resourceDB.Put(podInfoKey(podinfo.Namespace, podinfo.Name), newRes)
@@ -679,18 +697,73 @@ func (networkService *networkService) startGarbageCollectionLoop() {
679697
}
680698

681699
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
691707
}
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+
694767
}
695768

696769
// tracing
@@ -806,12 +879,17 @@ func (networkService *networkService) GetResourceMapping() ([]tracing.PodMapping
806879

807880
func newNetworkService(configFilePath, kubeconfig, master, daemonMode string) (rpc.TerwayBackendServer, error) {
808881
log.Debugf("start network service with: %s, %s", configFilePath, daemonMode)
882+
cniBinPath := os.Getenv("CNI_PATH")
883+
if cniBinPath == "" {
884+
cniBinPath = cniDefaultPath
885+
}
809886
netSrv := &networkService{
810887
configFilePath: configFilePath,
811888
kubeConfig: kubeconfig,
812889
master: master,
813890
pendingPods: map[string]interface{}{},
814891
pendingPodsLock: sync.RWMutex{},
892+
cniBinPath: cniBinPath,
815893
}
816894
if daemonMode == daemonModeENIMultiIP || daemonMode == daemonModeVPC || daemonMode == daemonModeENIOnly {
817895
netSrv.daemonMode = daemonMode

daemon/resource_manager.go

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type ResourceItem struct {
3232
type PodResources struct {
3333
Resources []ResourceItem
3434
PodInfo *podInfo
35+
NetNs *string
3536
}
3637

3738
type resourceManagerInitItem struct {

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ require (
2626
github.com/vishvananda/netlink v1.1.1-0.20201206203632-88079d98e65d
2727
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae
2828
golang.org/x/net v0.0.0-20201209123823-ac852fbbde11
29-
golang.org/x/sys v0.0.0-20201214210602-f9fddec55a1e
29+
golang.org/x/sys v0.0.0-20201221093633-bc327ba9c2f0
3030
google.golang.org/grpc v1.34.0
3131
google.golang.org/protobuf v1.25.0
3232
gopkg.in/yaml.v2 v2.4.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7w
291291
golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
292292
golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
293293
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
294-
golang.org/x/sys v0.0.0-20201214210602-f9fddec55a1e h1:AyodaIpKjppX+cBfTASF2E1US3H2JFBj920Ot3rtDjs=
295-
golang.org/x/sys v0.0.0-20201214210602-f9fddec55a1e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
294+
golang.org/x/sys v0.0.0-20201221093633-bc327ba9c2f0 h1:n+DPcgTwkgWzIFpLmoimYR2K2b0Ga5+Os4kayIN0vGo=
295+
golang.org/x/sys v0.0.0-20201221093633-bc327ba9c2f0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
296296
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
297297
golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
298298
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

pkg/link/interface.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func GetDeviceNumber(mac string) (int32, error) {
1919
return int32(link.Attrs().Index), nil
2020
}
2121
}
22-
return 0, errors.Errorf("cannot found mac address: %s", mac)
22+
return 0, errors.Wrapf(ErrNotFound, "can't found dev by mac %s", mac)
2323
}
2424

2525
// GetDeviceName get interface device name by mac address
@@ -34,5 +34,5 @@ func GetDeviceName(mac string) (string, error) {
3434
return link.Attrs().Name, nil
3535
}
3636
}
37-
return "", errors.Errorf("cannot found mac address: %s", mac)
37+
return "", errors.Wrapf(ErrNotFound, "can't found dev by mac %s", mac)
3838
}

pkg/link/interface_unsupport.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22

33
package link
44

5-
import (
6-
"github.com/pkg/errors"
7-
)
8-
95
// GetDeviceNumber get interface device number by mac address
106
func GetDeviceNumber(mac string) (int32, error) {
11-
return 0, errors.Errorf("not supported arch")
7+
return 0, ErrUnsupported
128
}
139

1410
// GetDeviceName get interface device name by mac address
1511
func GetDeviceName(mac string) (string, error) {
16-
return "", errors.Errorf("not supported arch")
12+
return "", ErrUnsupported
1713
}

pkg/link/type.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package link
2+
3+
import (
4+
"errors"
5+
)
6+
7+
var (
8+
ErrUnsupported = errors.New("not supported arch")
9+
ErrNotFound = errors.New("not found")
10+
)

pkg/sysctl/sysctl.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2019 Authors of Cilium
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package sysctl
16+
17+
import (
18+
"fmt"
19+
"io"
20+
"io/ioutil"
21+
"os"
22+
"path/filepath"
23+
"strings"
24+
)
25+
26+
const (
27+
prefixDir = "/proc/sys"
28+
)
29+
30+
// Setting represents a sysctl setting. Its purpose it to be able to iterate
31+
// over a slice of settings.
32+
type Setting struct {
33+
Name string
34+
Val string
35+
IgnoreErr bool
36+
}
37+
38+
func fullPath(name string) string {
39+
return filepath.Join(prefixDir, strings.Replace(name, ".", "/", -1))
40+
}
41+
42+
func writeSysctl(name string, value string) error {
43+
fPath := fullPath(name)
44+
f, err := os.OpenFile(fPath, os.O_RDWR, 0644)
45+
if err != nil {
46+
return fmt.Errorf("could not open the sysctl file %s: %s",
47+
fPath, err)
48+
}
49+
defer f.Close()
50+
if _, err := io.WriteString(f, value); err != nil {
51+
return fmt.Errorf("could not write to the systctl file %s: %s",
52+
fPath, err)
53+
}
54+
return nil
55+
}
56+
57+
// Disable disables the given sysctl parameter.
58+
func Disable(name string) error {
59+
return writeSysctl(name, "0")
60+
}
61+
62+
// Enable enables the given sysctl parameter.
63+
func Enable(name string) error {
64+
return writeSysctl(name, "1")
65+
}
66+
67+
// Write writes the given sysctl parameter.
68+
func Write(name string, val string) error {
69+
return writeSysctl(name, val)
70+
}
71+
72+
// Read reads the given sysctl parameter.
73+
func Read(name string) (string, error) {
74+
fPath := fullPath(name)
75+
val, err := ioutil.ReadFile(fPath)
76+
if err != nil {
77+
return "", fmt.Errorf("Failed to read %s: %s", fPath, val)
78+
}
79+
80+
return strings.TrimRight(string(val), "\n"), nil
81+
}

plugin/driver/drivers.go

+22
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ var (
2121
IPVlanDriver NetnsDriver = newIPVlanDriver()
2222
)
2323

24+
type RecordPodEvent func(msg string)
25+
26+
type CheckConfig struct {
27+
RecordPodEvent
28+
29+
NetNS ns.NetNS
30+
31+
HostVethName string
32+
DeviceID int32
33+
34+
ContainerIFName string
35+
// for pod
36+
IPv4Addr *net.IPNet
37+
Gateway net.IP
38+
}
39+
2440
// NetnsDriver to config container netns interface and routes
2541
type NetnsDriver interface {
2642
Setup(hostVeth string,
@@ -40,6 +56,8 @@ type NetnsDriver interface {
4056
containerVeth string,
4157
netNS ns.NetNS,
4258
containerIP net.IP) error
59+
60+
Check(cfg *CheckConfig) error
4361
}
4462

4563
type vethDriver struct {
@@ -430,6 +448,10 @@ func (d *vethDriver) Teardown(hostIfName string,
430448
return netlink.LinkDel(hostVeth)
431449
}
432450

451+
func (d *vethDriver) Check(cfg *CheckConfig) error {
452+
return nil
453+
}
454+
433455
func setupVethPair(contVethName, pairName string, mtu int, hostNS ns.NetNS) (net.Interface, net.Interface, error) {
434456
contVeth, err := makeVethPair(contVethName, pairName, mtu)
435457
if err != nil {

0 commit comments

Comments
 (0)