Skip to content

Commit

Permalink
fix: bcs-container-executor ip panic, issue #675
Browse files Browse the repository at this point in the history
(cherry picked from commit 7dadbf7)
  • Loading branch information
DeveloperJim committed Dec 30, 2020
1 parent 858169d commit 4a3455b
Show file tree
Hide file tree
Showing 22 changed files with 122 additions and 429 deletions.
79 changes: 2 additions & 77 deletions bcs-common/common/util/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ package util
import (
"encoding/json"
"fmt"

"github.com/Tencent/bk-bcs/bcs-common/common/types"
)

// CheckKind check object if expected
func CheckKind(kind types.BcsDataType, by []byte) error {
var meta *types.TypeMeta

Expand All @@ -32,81 +34,4 @@ func CheckKind(kind types.BcsDataType, by []byte) error {
}

return nil
/*switch kind {
case types.BcsDataType_APP:
var obj types.ReplicaController
err = json.Unmarshal(by, &obj)
if err != nil {
break
}
if obj.Kind != types.BcsDataType_APP {
err = fmt.Errorf("Kind %s is invalid", obj.Kind)
}
case types.BcsDataType_PROCESS:
var obj types.ReplicaController
err = json.Unmarshal(by, &obj)
if err != nil {
break
}
if obj.Kind != types.BcsDataType_PROCESS {
err = fmt.Errorf("Kind %s is invalid", obj.Kind)
}
case types.BcsDataType_DEPLOYMENT:
var obj types.BcsDeployment
err = json.Unmarshal(by, &obj)
if err != nil {
break
}
if obj.Kind != types.BcsDataType_DEPLOYMENT {
err = fmt.Errorf("Kind %s is invalid", obj.Kind)
}
case types.BcsDataType_SERVICE:
var obj types.BcsService
err = json.Unmarshal(by, &obj)
if err != nil {
break
}
if obj.Kind != types.BcsDataType_SERVICE {
err = fmt.Errorf("Kind %s is invalid", obj.Kind)
}
case types.BcsDataType_CONFIGMAP:
var obj types.BcsConfigMap
err = json.Unmarshal(by, &obj)
if err != nil {
break
}
if obj.Kind != types.BcsDataType_CONFIGMAP {
err = fmt.Errorf("Kind %s is invalid", obj.Kind)
}
case types.BcsDataType_SECRET:
var obj types.BcsSecret
err = json.Unmarshal(by, &obj)
if err != nil {
break
}
if obj.Kind != types.BcsDataType_SECRET {
err = fmt.Errorf("Kind %s is invalid", obj.Kind)
}
default:
return fmt.Errorf("Kind %s is invalid", kind)
}*/

//return err
}
5 changes: 2 additions & 3 deletions bcs-common/common/util/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ package util

import (
goflag "flag"
"github.com/Tencent/bk-bcs/bcs-common/common/version"
"os"
"strings"

//"github.com/golang/glog"
"os"
"github.com/Tencent/bk-bcs/bcs-common/common/version"

"github.com/spf13/pflag"
)
Expand Down
67 changes: 45 additions & 22 deletions bcs-common/common/util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,65 @@ package util
import "net"

var (
_, classA, _ = net.ParseCIDR("10.0.0.0/8")
_, classA1, _ = net.ParseCIDR("9.0.0.0/8")
_, classAa, _ = net.ParseCIDR("100.64.0.0/10")
_, classB, _ = net.ParseCIDR("172.16.0.0/12")
_, classC, _ = net.ParseCIDR("192.168.0.0/16")
_, classA, _ = net.ParseCIDR("10.0.0.0/8")
_, classB, _ = net.ParseCIDR("172.16.0.0/12")
_, classC, _ = net.ParseCIDR("192.168.0.0/16")
)

//GetIPAddress get local usable inner ip address
func GetIPAddress() (addrList []string) {
// check eth1 and eth0 first, if no IP address from eth1 and eth0,
// try to filter all network interface with private address
func GetIPAddress() string {
//try eth1 first
eth1Addr := getInterfaceIPv4Addr("eth1")
if len(eth1Addr) != 0 {
return eth1Addr
}
//try eth0
eth0Addr := getInterfaceIPv4Addr("eth0")
if len(eth0Addr) != 0 {
return eth0Addr
}
//try all private network address
addrs, err := net.InterfaceAddrs()
if err != nil {
return addrList
return ""
}
for _, addr := range addrs {
if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() && ip.IP.To4() != nil {
if classA.Contains(ip.IP) {
addrList = append(addrList, ip.IP.String())
continue
}
if classA1.Contains(ip.IP) {
addrList = append(addrList, ip.IP.String())
continue
}
if classAa.Contains(ip.IP) {
addrList = append(addrList, ip.IP.String())
continue
return ip.IP.String()
}
if classB.Contains(ip.IP) {
addrList = append(addrList, ip.IP.String())
continue
return ip.IP.String()
}
if classC.Contains(ip.IP) {
addrList = append(addrList, ip.IP.String())
continue
return ip.IP.String()
}
}
}
return addrList
return ""
}

//getInterfaceIPv4Addr get specified network interface IPv4 address
// if interface has multiple available IP addresses and already UP
// just return the first one
func getInterfaceIPv4Addr(name string) string {
itf, err := net.InterfaceByName(name)
if err != nil {
return ""
}
if (itf.Flags & net.FlagUp) == 0 {
return ""
}
addrs, err := itf.Addrs()
if err != nil {
return ""
}
for _, addr := range addrs {
if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() && ip.IP.To4() != nil {
return ip.IP.String()
}
}
return ""
}
25 changes: 2 additions & 23 deletions bcs-mesos/bcs-container-executor/app/bcs_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

"github.com/Tencent/bk-bcs/bcs-common/common/blog"
"github.com/Tencent/bk-bcs/bcs-common/common/util"
"github.com/Tencent/bk-bcs/bcs-common/pkg/scheduler/mesosproto/mesos"
"github.com/Tencent/bk-bcs/bcs-mesos/bcs-container-executor/container"
"github.com/Tencent/bk-bcs/bcs-mesos/bcs-container-executor/container/cni"
Expand All @@ -36,11 +37,9 @@ import (
"github.com/Tencent/bk-bcs/bcs-mesos/bcs-container-executor/network"
cninet "github.com/Tencent/bk-bcs/bcs-mesos/bcs-container-executor/network/cni"
cnmnet "github.com/Tencent/bk-bcs/bcs-mesos/bcs-container-executor/network/cnm"
"github.com/Tencent/bk-bcs/bcs-mesos/bcs-container-executor/util"
bcstype "github.com/Tencent/bk-bcs/bcs-mesos/bcs-scheduler/src/types"

"github.com/golang/protobuf/proto"
//"github.com/pborman/uuid"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -97,35 +96,16 @@ func NewBcsExecutor(flag *CommandFlags) exec.Executor {
}

func createNetManager(bcsExecutor *BcsExecutor, flag *CommandFlags) {
// exist, _ := util.FileExists(flag.CNIPluginDir + "/conf")
// if flag.NetworkMode == "" {
// if exist {
// //cni pod
// bcsExecutor.netManager = cninet.NewNetManager(flag.CNIPluginDir+"/bin", flag.CNIPluginDir+"/conf")
// } else {
// //docker pod
// bcsExecutor.netManager = cnmnet.NewNetManager()
// }
// } else {
if flag.NetworkMode == "cni" {
//cni pod
bcsExecutor.netManager = cninet.NewNetManager(flag.CNIPluginDir+"/bin", flag.CNIPluginDir+"/conf")
} else {
//docker pod
bcsExecutor.netManager = cnmnet.NewNetManager()
}
// }
}

func createPod(executor *BcsExecutor, flag *CommandFlags, containerTasks []*container.BcsContainerTask, podEvent *container.PodEventHandler) {
// exist, _ := util.FileExists(flag.CNIPluginDir + "/conf")
// if flag.NetworkMode == "" {
// if exist {
// executor.podInst = cni.NewPod(executor.container, containerTasks, podEvent)
// } else {
// executor.podInst = cnm.NewPod(executor.container, containerTasks, podEvent)
// }
// } else {
if flag.NetworkMode == "cni" {
//cni pod
executor.podInst = cni.NewPod(executor.container, containerTasks, podEvent, flag.NetworkImage)
Expand Down Expand Up @@ -850,10 +830,9 @@ func (executor *BcsExecutor) customSettingContainer(taskInfo *container.BcsConta
// }
// taskInfo.Env = append(taskInfo.Env, caliName)
//setting mesos slave host ip
addrList := util.GetIPAddress()
ipAddr := container.BcsKV{
Key: "BCS_NODE_IP",
Value: addrList[0],
Value: util.GetIPAddress(),
}
taskInfo.Env = append(taskInfo.Env, ipAddr)
podID := container.BcsKV{
Expand Down
4 changes: 2 additions & 2 deletions bcs-mesos/bcs-container-executor/app/bcs_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (executor *BcsExecutor) frameworkMessageCommandTask(msg *bcstype.RequestCom
**/

const (
defaultHttpRequestTimeout = 120
defaultHTTPRequestTimeout = 120
)

//dataClassRemote handle bcs_remote message, download data from remote http url, push to created container
Expand Down Expand Up @@ -316,7 +316,7 @@ func (executor *BcsExecutor) downloadRemoteFile(remote *bcstype.Msg_Remote) (*bc

//download content from remote http url.
client := http.Client{
Timeout: time.Duration(defaultHttpRequestTimeout * time.Second),
Timeout: time.Duration(defaultHTTPRequestTimeout * time.Second),
}
request, reqErr := http.NewRequest("GET", *remote.From, nil)
if reqErr != nil {
Expand Down
20 changes: 11 additions & 9 deletions bcs-mesos/bcs-container-executor/container/cni/cni_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ package cni
import (
"bytes"
"fmt"
comtypes "github.com/Tencent/bk-bcs/bcs-common/common/types"
"github.com/Tencent/bk-bcs/bcs-mesos/bcs-container-executor/container"
device_plugin_manager "github.com/Tencent/bk-bcs/bcs-mesos/bcs-container-executor/device-plugin-manager"
"github.com/Tencent/bk-bcs/bcs-mesos/bcs-container-executor/healthcheck"
"github.com/Tencent/bk-bcs/bcs-mesos/bcs-container-executor/logs"
"github.com/Tencent/bk-bcs/bcs-mesos/bcs-container-executor/util"
bcstypes "github.com/Tencent/bk-bcs/bcs-mesos/bcs-scheduler/src/types"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"sync"
"time"

comtypes "github.com/Tencent/bk-bcs/bcs-common/common/types"
"github.com/Tencent/bk-bcs/bcs-common/common/util"
"github.com/Tencent/bk-bcs/bcs-mesos/bcs-container-executor/container"
device_plugin_manager "github.com/Tencent/bk-bcs/bcs-mesos/bcs-container-executor/device-plugin-manager"
"github.com/Tencent/bk-bcs/bcs-mesos/bcs-container-executor/healthcheck"
"github.com/Tencent/bk-bcs/bcs-mesos/bcs-container-executor/logs"
exeutil "github.com/Tencent/bk-bcs/bcs-mesos/bcs-container-executor/util"
bcstypes "github.com/Tencent/bk-bcs/bcs-mesos/bcs-scheduler/src/types"

//"github.com/pborman/uuid"
schedTypes "github.com/Tencent/bk-bcs/bcs-mesos/bcs-scheduler/src/types"

Expand Down Expand Up @@ -241,7 +243,7 @@ func (p *CNIPod) Init() error {
}

if p.cniHostName == "" {
p.cniHostName = "pod-" + util.RandomString(12)
p.cniHostName = "pod-" + exeutil.RandomString(12)
}

p.netTask = &container.BcsContainerTask{
Expand Down Expand Up @@ -441,7 +443,7 @@ func (p *CNIPod) Start() error {
task.HostName = hostname

task.RuntimeConf.ID = createInst.ID
task.RuntimeConf.NodeAddress = util.GetIPAddress()[0]
task.RuntimeConf.NodeAddress = util.GetIPAddress()
task.RuntimeConf.IPAddress = p.cniIPAddr
task.RuntimeConf.Status = container.ContainerStatus_CREATED
task.RuntimeConf.Message = "container created"
Expand Down
Loading

0 comments on commit 4a3455b

Please sign in to comment.