diff --git a/pkg/kubelet/network/kubenet/kubenet_linux.go b/pkg/kubelet/network/kubenet/kubenet_linux.go index f21aa929b8..166dcabc8a 100644 --- a/pkg/kubelet/network/kubenet/kubenet_linux.go +++ b/pkg/kubelet/network/kubenet/kubenet_linux.go @@ -307,7 +307,7 @@ func (plugin *kubenetNetworkPlugin) Capabilities() utilsets.Int { // setup sets up networking through CNI using the given ns/name and sandbox ID. // TODO: Don't pass the pod to this method, it only needs it for bandwidth // shaping and hostport management. -func (plugin *kubenetNetworkPlugin) setup(namespace string, name string, id kubecontainer.ContainerID, pod *v1.Pod) error { +func (plugin *kubenetNetworkPlugin) setup(namespace string, name string, id kubecontainer.ContainerID, pod *v1.Pod, annotations map[string]string) error { // Bring up container loopback interface if _, err := plugin.addContainerToNetwork(plugin.loConfig, "lo", namespace, name, id); err != nil { return err @@ -359,23 +359,22 @@ func (plugin *kubenetNetworkPlugin) setup(namespace string, name string, id kube plugin.podIPs[id] = ip4.String() + // The first SetUpPod call creates the bridge; get a shaper for the sake of initialization + // TODO: replace with CNI traffic shaper plugin + shaper := plugin.shaper() + ingress, egress, err := bandwidth.ExtractPodBandwidthResources(annotations) + if err != nil { + return fmt.Errorf("Error reading pod bandwidth annotations: %v", err) + } + if egress != nil || ingress != nil { + if err := shaper.ReconcileCIDR(fmt.Sprintf("%s/32", ip4.String()), egress, ingress); err != nil { + return fmt.Errorf("Failed to add pod to shaper: %v", err) + } + } + // The host can choose to not support "legacy" features. The remote // shim doesn't support it (#35457), but the kubelet does. if plugin.host.SupportsLegacyFeatures() { - // The first SetUpPod call creates the bridge; get a shaper for the sake of - // initialization - shaper := plugin.shaper() - - ingress, egress, err := bandwidth.ExtractPodBandwidthResources(pod.Annotations) - if err != nil { - return fmt.Errorf("Error reading pod bandwidth annotations: %v", err) - } - if egress != nil || ingress != nil { - if err := shaper.ReconcileCIDR(fmt.Sprintf("%s/32", ip4.String()), egress, ingress); err != nil { - return fmt.Errorf("Failed to add pod to shaper: %v", err) - } - } - // Open any hostport the pod's containers want activePodPortMappings, err := plugin.getPodPortMappings() if err != nil { @@ -387,6 +386,7 @@ func (plugin *kubenetNetworkPlugin) setup(namespace string, name string, id kube return err } } else { + // TODO: replace with CNI port-forwarding plugin portMappings, err := plugin.host.GetPodPortMappings(id.ID) if err != nil { return err @@ -425,7 +425,7 @@ func (plugin *kubenetNetworkPlugin) SetUpPod(namespace string, name string, id k return fmt.Errorf("Kubenet cannot SetUpPod: %v", err) } - if err := plugin.setup(namespace, name, id, pod); err != nil { + if err := plugin.setup(namespace, name, id, pod, annotations); err != nil { // Make sure everything gets cleaned up on errors podIP, _ := plugin.podIPs[id] if err := plugin.teardown(namespace, name, id, podIP); err != nil { diff --git a/pkg/util/bandwidth/utils.go b/pkg/util/bandwidth/utils.go index bcd64f528b..451ab68836 100644 --- a/pkg/util/bandwidth/utils.go +++ b/pkg/util/bandwidth/utils.go @@ -36,6 +36,9 @@ func validateBandwidthIsReasonable(rsrc *resource.Quantity) error { } func ExtractPodBandwidthResources(podAnnotations map[string]string) (ingress, egress *resource.Quantity, err error) { + if podAnnotations == nil { + return nil, nil, nil + } str, found := podAnnotations["kubernetes.io/ingress-bandwidth"] if found { ingressValue, err := resource.ParseQuantity(str)