|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package noderesourcetopology |
| 18 | + |
| 19 | +import ( |
| 20 | + "k8s.io/klog/v2" |
| 21 | + kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config" |
| 22 | + |
| 23 | + topologyv1alpha2 "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/apis/topology/v1alpha2" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + AttributeScope = "topologyManagerScope" |
| 28 | + AttributePolicy = "topologyManagerPolicy" |
| 29 | +) |
| 30 | + |
| 31 | +// TODO: handle topologyManagerPolicyOptions added in k8s 1.26 |
| 32 | + |
| 33 | +func IsValidScope(scope string) bool { |
| 34 | + if scope == kubeletconfig.ContainerTopologyManagerScope || scope == kubeletconfig.PodTopologyManagerScope { |
| 35 | + return true |
| 36 | + } |
| 37 | + return false |
| 38 | +} |
| 39 | + |
| 40 | +func IsValidPolicy(policy string) bool { |
| 41 | + if policy == kubeletconfig.NoneTopologyManagerPolicy || policy == kubeletconfig.BestEffortTopologyManagerPolicy || |
| 42 | + policy == kubeletconfig.RestrictedTopologyManagerPolicy || policy == kubeletconfig.SingleNumaNodeTopologyManagerPolicy { |
| 43 | + return true |
| 44 | + } |
| 45 | + return false |
| 46 | +} |
| 47 | + |
| 48 | +type TopologyManagerConfig struct { |
| 49 | + Scope string |
| 50 | + Policy string |
| 51 | +} |
| 52 | + |
| 53 | +func makeTopologyManagerConfigDefaults() TopologyManagerConfig { |
| 54 | + return TopologyManagerConfig{ |
| 55 | + Scope: kubeletconfig.ContainerTopologyManagerScope, |
| 56 | + Policy: kubeletconfig.NoneTopologyManagerPolicy, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func topologyManagerConfigFromNodeResourceTopology(nodeTopology *topologyv1alpha2.NodeResourceTopology) TopologyManagerConfig { |
| 61 | + conf := makeTopologyManagerConfigDefaults() |
| 62 | + // Backward compatibility (v1alpha2 and previous). Deprecated, will be removed when the NRT API moves to v1beta1. |
| 63 | + updateTopologyManagerConfigFromTopologyPolicies(&conf, nodeTopology.Name, nodeTopology.TopologyPolicies) |
| 64 | + // preferred new configuration source (v1alpha2 and onwards) |
| 65 | + updateTopologyManagerConfigFromAttributes(&conf, nodeTopology.Attributes) |
| 66 | + return conf |
| 67 | +} |
| 68 | + |
| 69 | +func updateTopologyManagerConfigFromAttributes(conf *TopologyManagerConfig, attrs topologyv1alpha2.AttributeList) { |
| 70 | + for _, attr := range attrs { |
| 71 | + if attr.Name == AttributeScope && IsValidScope(attr.Value) { |
| 72 | + conf.Scope = attr.Value |
| 73 | + continue |
| 74 | + } |
| 75 | + if attr.Name == AttributePolicy && IsValidPolicy(attr.Value) { |
| 76 | + conf.Policy = attr.Value |
| 77 | + continue |
| 78 | + } |
| 79 | + // TODO: handle topologyManagerPolicyOptions added in k8s 1.26 |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func updateTopologyManagerConfigFromTopologyPolicies(conf *TopologyManagerConfig, nodeName string, topologyPolicies []string) { |
| 84 | + if len(topologyPolicies) == 0 { |
| 85 | + klog.V(3).InfoS("Cannot determine policy", "node", nodeName) |
| 86 | + return |
| 87 | + } |
| 88 | + if len(topologyPolicies) > 1 { |
| 89 | + klog.V(4).InfoS("Ignoring extra policies", "node", nodeName, "policies count", len(topologyPolicies)-1) |
| 90 | + } |
| 91 | + |
| 92 | + policyName := topologyv1alpha2.TopologyManagerPolicy(topologyPolicies[0]) |
| 93 | + klog.Warning("The `topologyPolicies` field is deprecated and will be removed with the NRT API v1beta1.") |
| 94 | + klog.Warning("The `topologyPolicies` field is deprecated, please use top-level Attributes field instead.") |
| 95 | + |
| 96 | + switch policyName { |
| 97 | + case topologyv1alpha2.SingleNUMANodePodLevel: |
| 98 | + conf.Policy = kubeletconfig.SingleNumaNodeTopologyManagerPolicy |
| 99 | + conf.Scope = kubeletconfig.PodTopologyManagerScope |
| 100 | + case topologyv1alpha2.SingleNUMANodeContainerLevel: |
| 101 | + conf.Policy = kubeletconfig.SingleNumaNodeTopologyManagerPolicy |
| 102 | + conf.Scope = kubeletconfig.ContainerTopologyManagerScope |
| 103 | + case topologyv1alpha2.BestEffortPodLevel: |
| 104 | + conf.Policy = kubeletconfig.BestEffortTopologyManagerPolicy |
| 105 | + conf.Scope = kubeletconfig.PodTopologyManagerScope |
| 106 | + case topologyv1alpha2.BestEffortContainerLevel: |
| 107 | + conf.Policy = kubeletconfig.BestEffortTopologyManagerPolicy |
| 108 | + conf.Scope = kubeletconfig.ContainerTopologyManagerScope |
| 109 | + case topologyv1alpha2.RestrictedPodLevel: |
| 110 | + conf.Policy = kubeletconfig.RestrictedTopologyManagerPolicy |
| 111 | + conf.Scope = kubeletconfig.PodTopologyManagerScope |
| 112 | + case topologyv1alpha2.RestrictedContainerLevel: |
| 113 | + conf.Policy = kubeletconfig.RestrictedTopologyManagerPolicy |
| 114 | + conf.Scope = kubeletconfig.ContainerTopologyManagerScope |
| 115 | + } |
| 116 | +} |
0 commit comments