|
| 1 | +package daemon |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + |
| 7 | + "github.com/samber/lo" |
| 8 | + "github.com/vishvananda/netlink" |
| 9 | + |
| 10 | + "github.com/AliyunContainerService/terway/pkg/link" |
| 11 | + "github.com/AliyunContainerService/terway/pkg/utils/nodecap" |
| 12 | + "github.com/AliyunContainerService/terway/plugin/datapath" |
| 13 | + "github.com/AliyunContainerService/terway/plugin/driver/types" |
| 14 | + "github.com/AliyunContainerService/terway/plugin/driver/utils" |
| 15 | + "github.com/AliyunContainerService/terway/rpc" |
| 16 | + terwayTypes "github.com/AliyunContainerService/terway/types" |
| 17 | + "github.com/AliyunContainerService/terway/types/daemon" |
| 18 | +) |
| 19 | + |
| 20 | +func ruleSync(ctx context.Context, res daemon.PodResources) error { |
| 21 | + if res.PodInfo == nil { |
| 22 | + return nil |
| 23 | + } |
| 24 | + |
| 25 | + if res.PodInfo.PodNetworkType != daemon.PodNetworkTypeENIMultiIP { |
| 26 | + return nil |
| 27 | + } |
| 28 | + |
| 29 | + switch nodecap.GetNodeCapabilities(nodecap.NodeCapabilityDataPath) { |
| 30 | + case "datapathv2", "veth", "": |
| 31 | + default: |
| 32 | + return nil |
| 33 | + } |
| 34 | + |
| 35 | + netConf := make([]*rpc.NetConf, 0) |
| 36 | + err := json.Unmarshal([]byte(res.NetConf), &netConf) |
| 37 | + if err != nil { |
| 38 | + return nil |
| 39 | + } |
| 40 | + |
| 41 | + links, err := netlink.LinkList() |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + for _, conf := range netConf { |
| 47 | + if conf.BasicInfo == nil || conf.ENIInfo == nil || |
| 48 | + conf.BasicInfo.PodIP == nil { |
| 49 | + continue |
| 50 | + } |
| 51 | + ifName := "eth0" |
| 52 | + if conf.IfName != "" { |
| 53 | + ifName = conf.IfName |
| 54 | + } |
| 55 | + |
| 56 | + hostVethName, _ := link.VethNameForPod(res.PodInfo.Name, res.PodInfo.Namespace, ifName, "cali") |
| 57 | + |
| 58 | + // check host veth ,make sure pod is present |
| 59 | + hostVeth, ok := lo.Find(links, func(item netlink.Link) bool { |
| 60 | + return hostVethName == item.Attrs().Name |
| 61 | + }) |
| 62 | + if !ok { |
| 63 | + continue |
| 64 | + } |
| 65 | + |
| 66 | + eni, ok := lo.Find(links, func(item netlink.Link) bool { |
| 67 | + if _, ok := item.(*netlink.Device); !ok { |
| 68 | + return false |
| 69 | + } |
| 70 | + return item.Attrs().HardwareAddr.String() == conf.ENIInfo.MAC |
| 71 | + }) |
| 72 | + if !ok { |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + setUp := &types.SetupConfig{ |
| 77 | + ContainerIPNet: &terwayTypes.IPNetSet{}, |
| 78 | + GatewayIP: &terwayTypes.IPSet{}, |
| 79 | + ENIIndex: eni.Attrs().Index, |
| 80 | + } |
| 81 | + if conf.BasicInfo.PodIP.IPv4 != "" { |
| 82 | + setUp.ContainerIPNet.SetIPNet(conf.BasicInfo.PodIP.IPv4 + "/32") |
| 83 | + } |
| 84 | + if conf.BasicInfo.PodIP.IPv6 != "" { |
| 85 | + setUp.ContainerIPNet.SetIPNet(conf.BasicInfo.PodIP.IPv6 + "/128") |
| 86 | + } |
| 87 | + setUp.GatewayIP.SetIP(conf.BasicInfo.GatewayIP.IPv4) |
| 88 | + setUp.GatewayIP.SetIP(conf.BasicInfo.GatewayIP.IPv6) |
| 89 | + |
| 90 | + // 1. route point to hostVeth |
| 91 | + table := utils.GetRouteTableID(eni.Attrs().Index) |
| 92 | + |
| 93 | + eniConf := datapath.GenerateENICfgForPolicy(setUp, eni, table) |
| 94 | + hostVethConf := datapath.GenerateHostPeerCfgForPolicy(setUp, hostVeth, table) |
| 95 | + |
| 96 | + // default via 10.xx.xx.253 dev eth1 onlink table 1003 |
| 97 | + for _, route := range eniConf.Routes { |
| 98 | + _, err = utils.EnsureRoute(ctx, route) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + } |
| 103 | + // 10.xx.xx.xx dev calixx scope link |
| 104 | + for _, route := range hostVethConf.Routes { |
| 105 | + _, err = utils.EnsureRoute(ctx, route) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + for _, rule := range hostVethConf.Rules { |
| 112 | + _, err = utils.EnsureIPRule(ctx, rule) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
0 commit comments