mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-15 14:14:39 +00:00
Merge pull request #22320 from timstclair/network
Use the default interface for reporting network stats.
This commit is contained in:
@@ -1858,7 +1858,7 @@ func (dm *DockerManager) SyncPod(pod *api.Pod, _ api.PodStatus, podStatus *kubec
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !usesHostNetwork(pod) && dm.configureHairpinMode {
|
if !usesHostNetwork(pod) && dm.configureHairpinMode {
|
||||||
if err = hairpin.SetUpContainer(podInfraContainer.State.Pid, "eth0"); err != nil {
|
if err = hairpin.SetUpContainer(podInfraContainer.State.Pid, network.DefaultInterfaceName); err != nil {
|
||||||
glog.Warningf("Hairpin setup failed for pod %q: %v", format.Pod(pod), err)
|
glog.Warningf("Hairpin setup failed for pod %q: %v", format.Pod(pod), err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -34,7 +34,6 @@ const (
|
|||||||
CNIPluginName = "cni"
|
CNIPluginName = "cni"
|
||||||
DefaultNetDir = "/etc/cni/net.d"
|
DefaultNetDir = "/etc/cni/net.d"
|
||||||
DefaultCNIDir = "/opt/cni/bin"
|
DefaultCNIDir = "/opt/cni/bin"
|
||||||
DefaultInterfaceName = "eth0"
|
|
||||||
VendorCNIDirTemplate = "%s/opt/%s/bin"
|
VendorCNIDirTemplate = "%s/opt/%s/bin"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -143,7 +142,7 @@ func (plugin *cniNetworkPlugin) Status(namespace string, name string, id kubecon
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("CNI execution called on non-docker runtime")
|
return nil, fmt.Errorf("CNI execution called on non-docker runtime")
|
||||||
}
|
}
|
||||||
ipStr, err := runtime.GetContainerIP(string(id), DefaultInterfaceName)
|
ipStr, err := runtime.GetContainerIP(string(id), network.DefaultInterfaceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -196,7 +195,7 @@ func buildCNIRuntimeConf(podName string, podNs string, podInfraContainerID kubec
|
|||||||
rt := &libcni.RuntimeConf{
|
rt := &libcni.RuntimeConf{
|
||||||
ContainerID: podInfraContainerID.ID,
|
ContainerID: podInfraContainerID.ID,
|
||||||
NetNS: podNetnsPath,
|
NetNS: podNetnsPath,
|
||||||
IfName: DefaultInterfaceName,
|
IfName: network.DefaultInterfaceName,
|
||||||
Args: [][2]string{
|
Args: [][2]string{
|
||||||
{"K8S_POD_NAMESPACE", podNs},
|
{"K8S_POD_NAMESPACE", podNs},
|
||||||
{"K8S_POD_NAME", podName},
|
{"K8S_POD_NAME", podName},
|
||||||
|
@@ -35,10 +35,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
KubenetPluginName = "kubenet"
|
KubenetPluginName = "kubenet"
|
||||||
BridgeName = "cbr0"
|
BridgeName = "cbr0"
|
||||||
DefaultCNIDir = "/opt/cni/bin"
|
DefaultCNIDir = "/opt/cni/bin"
|
||||||
DefaultInterfaceName = "eth0"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type kubenetNetworkPlugin struct {
|
type kubenetNetworkPlugin struct {
|
||||||
@@ -139,7 +138,7 @@ func (plugin *kubenetNetworkPlugin) Event(name string, details map[string]interf
|
|||||||
// Set bridge address to first address in IPNet
|
// Set bridge address to first address in IPNet
|
||||||
cidr.IP.To4()[3] += 1
|
cidr.IP.To4()[3] += 1
|
||||||
|
|
||||||
json := fmt.Sprintf(NET_CONFIG_TEMPLATE, BridgeName, plugin.MTU, DefaultInterfaceName, podCIDR, cidr.IP.String())
|
json := fmt.Sprintf(NET_CONFIG_TEMPLATE, BridgeName, plugin.MTU, network.DefaultInterfaceName, podCIDR, cidr.IP.String())
|
||||||
plugin.netConfig, err = libcni.ConfFromBytes([]byte(json))
|
plugin.netConfig, err = libcni.ConfFromBytes([]byte(json))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
glog.V(5).Infof("CNI network config:\n%s", json)
|
glog.V(5).Infof("CNI network config:\n%s", json)
|
||||||
@@ -282,6 +281,6 @@ func buildCNIRuntimeConf(podName string, podNs string, podInfraContainerID kubec
|
|||||||
return &libcni.RuntimeConf{
|
return &libcni.RuntimeConf{
|
||||||
ContainerID: podInfraContainerID.ID,
|
ContainerID: podInfraContainerID.ID,
|
||||||
NetNS: podNetnsPath,
|
NetNS: podNetnsPath,
|
||||||
IfName: DefaultInterfaceName,
|
IfName: network.DefaultInterfaceName,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
20
pkg/kubelet/network/network.go
Normal file
20
pkg/kubelet/network/network.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package network
|
||||||
|
|
||||||
|
// TODO: Consider making this value configurable.
|
||||||
|
const DefaultInterfaceName = "eth0"
|
@@ -27,6 +27,7 @@ import (
|
|||||||
"k8s.io/kubernetes/pkg/kubelet/cm"
|
"k8s.io/kubernetes/pkg/kubelet/cm"
|
||||||
"k8s.io/kubernetes/pkg/kubelet/dockertools"
|
"k8s.io/kubernetes/pkg/kubelet/dockertools"
|
||||||
"k8s.io/kubernetes/pkg/kubelet/leaky"
|
"k8s.io/kubernetes/pkg/kubelet/leaky"
|
||||||
|
"k8s.io/kubernetes/pkg/kubelet/network"
|
||||||
"k8s.io/kubernetes/pkg/types"
|
"k8s.io/kubernetes/pkg/types"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
@@ -107,7 +108,7 @@ func (sb *summaryBuilder) build() (*stats.Summary, error) {
|
|||||||
NodeName: sb.node.Name,
|
NodeName: sb.node.Name,
|
||||||
CPU: rootStats.CPU,
|
CPU: rootStats.CPU,
|
||||||
Memory: rootStats.Memory,
|
Memory: rootStats.Memory,
|
||||||
Network: sb.containerInfoV2ToNetworkStats(&rootInfo),
|
Network: sb.containerInfoV2ToNetworkStats("node:"+sb.node.Name, &rootInfo),
|
||||||
Fs: &stats.FsStats{
|
Fs: &stats.FsStats{
|
||||||
AvailableBytes: &sb.rootFsInfo.Available,
|
AvailableBytes: &sb.rootFsInfo.Available,
|
||||||
CapacityBytes: &sb.rootFsInfo.Capacity,
|
CapacityBytes: &sb.rootFsInfo.Capacity,
|
||||||
@@ -201,7 +202,7 @@ func (sb *summaryBuilder) buildSummaryPods() []stats.PodStats {
|
|||||||
containerName := dockertools.GetContainerName(cinfo.Spec.Labels)
|
containerName := dockertools.GetContainerName(cinfo.Spec.Labels)
|
||||||
if containerName == leaky.PodInfraContainerName {
|
if containerName == leaky.PodInfraContainerName {
|
||||||
// Special case for infrastructure container which is hidden from the user and has network stats
|
// Special case for infrastructure container which is hidden from the user and has network stats
|
||||||
podStats.Network = sb.containerInfoV2ToNetworkStats(&cinfo)
|
podStats.Network = sb.containerInfoV2ToNetworkStats("pod:"+ref.Namespace+"_"+ref.Name, &cinfo)
|
||||||
podStats.StartTime = unversioned.NewTime(cinfo.Spec.CreationTime)
|
podStats.StartTime = unversioned.NewTime(cinfo.Spec.CreationTime)
|
||||||
} else {
|
} else {
|
||||||
podStats.Containers = append(podStats.Containers, sb.containerInfoV2ToStats(containerName, &cinfo))
|
podStats.Containers = append(podStats.Containers, sb.containerInfoV2ToStats(containerName, &cinfo))
|
||||||
@@ -281,7 +282,7 @@ func (sb *summaryBuilder) containerInfoV2ToStats(
|
|||||||
return cStats
|
return cStats
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sb *summaryBuilder) containerInfoV2ToNetworkStats(info *cadvisorapiv2.ContainerInfo) *stats.NetworkStats {
|
func (sb *summaryBuilder) containerInfoV2ToNetworkStats(name string, info *cadvisorapiv2.ContainerInfo) *stats.NetworkStats {
|
||||||
if !info.Spec.HasNetwork {
|
if !info.Spec.HasNetwork {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -289,26 +290,19 @@ func (sb *summaryBuilder) containerInfoV2ToNetworkStats(info *cadvisorapiv2.Cont
|
|||||||
if !found {
|
if !found {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var (
|
|
||||||
rxBytes uint64
|
|
||||||
rxErrors uint64
|
|
||||||
txBytes uint64
|
|
||||||
txErrors uint64
|
|
||||||
)
|
|
||||||
// TODO(stclair): check for overflow
|
|
||||||
for _, inter := range cstat.Network.Interfaces {
|
for _, inter := range cstat.Network.Interfaces {
|
||||||
rxBytes += inter.RxBytes
|
if inter.Name == network.DefaultInterfaceName {
|
||||||
rxErrors += inter.RxErrors
|
return &stats.NetworkStats{
|
||||||
txBytes += inter.TxBytes
|
Time: unversioned.NewTime(cstat.Timestamp),
|
||||||
txErrors += inter.TxErrors
|
RxBytes: &inter.RxBytes,
|
||||||
}
|
RxErrors: &inter.RxErrors,
|
||||||
return &stats.NetworkStats{
|
TxBytes: &inter.TxBytes,
|
||||||
Time: unversioned.NewTime(cstat.Timestamp),
|
TxErrors: &inter.TxErrors,
|
||||||
RxBytes: &rxBytes,
|
}
|
||||||
RxErrors: &rxErrors,
|
}
|
||||||
TxBytes: &txBytes,
|
|
||||||
TxErrors: &txErrors,
|
|
||||||
}
|
}
|
||||||
|
glog.Warningf("Missing default interface %q for s", network.DefaultInterfaceName, name)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sb *summaryBuilder) containerInfoV2ToUserDefinedMetrics(info *cadvisorapiv2.ContainerInfo) []stats.UserDefinedMetric {
|
func (sb *summaryBuilder) containerInfoV2ToUserDefinedMetrics(info *cadvisorapiv2.ContainerInfo) []stats.UserDefinedMetric {
|
||||||
|
@@ -265,10 +265,17 @@ func summaryTestContainerInfo(seed int, podName string, podNamespace string, con
|
|||||||
},
|
},
|
||||||
Network: &v2.NetworkStats{
|
Network: &v2.NetworkStats{
|
||||||
Interfaces: []v1.InterfaceStats{{
|
Interfaces: []v1.InterfaceStats{{
|
||||||
|
Name: "eth0",
|
||||||
RxBytes: uint64(seed + offsetNetRxBytes),
|
RxBytes: uint64(seed + offsetNetRxBytes),
|
||||||
RxErrors: uint64(seed + offsetNetRxErrors),
|
RxErrors: uint64(seed + offsetNetRxErrors),
|
||||||
TxBytes: uint64(seed + offsetNetTxBytes),
|
TxBytes: uint64(seed + offsetNetTxBytes),
|
||||||
TxErrors: uint64(seed + offsetNetTxErrors),
|
TxErrors: uint64(seed + offsetNetTxErrors),
|
||||||
|
}, {
|
||||||
|
Name: "cbr0",
|
||||||
|
RxBytes: 100,
|
||||||
|
RxErrors: 100,
|
||||||
|
TxBytes: 100,
|
||||||
|
TxErrors: 100,
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
CustomMetrics: generateCustomMetrics(spec.CustomMetrics),
|
CustomMetrics: generateCustomMetrics(spec.CustomMetrics),
|
||||||
|
Reference in New Issue
Block a user