github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/qrm-plugins/network/state/state_net.go (about) 1 /* 2 Copyright 2022 The Katalyst 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 state 18 19 import ( 20 "fmt" 21 "sync" 22 23 info "github.com/google/cadvisor/info/v1" 24 25 "github.com/kubewharf/katalyst-core/pkg/config/agent/qrm" 26 "github.com/kubewharf/katalyst-core/pkg/util/machine" 27 ) 28 29 // networkPluginState is an in-memory implementation of State; 30 // everytime we want to read or write states, those requests will always 31 // go to in-memory State, and then go to disk State, i.e. in write-back mode 32 type networkPluginState struct { 33 sync.RWMutex 34 35 qrmConf *qrm.QRMPluginsConfiguration 36 machineInfo *info.MachineInfo 37 nics []machine.InterfaceInfo 38 reservedBandwidth map[string]uint32 39 40 machineState NICMap 41 podEntries PodEntries 42 } 43 44 var _ State = &networkPluginState{} 45 46 func NewNetworkPluginState(conf *qrm.QRMPluginsConfiguration, machineInfo *info.MachineInfo, nics []machine.InterfaceInfo, reservedBandwidth map[string]uint32) (State, error) { 47 generalLog.InfoS("initializing new network plugin in-memory state store") 48 49 defaultMachineState, err := GenerateMachineState(conf, nics, reservedBandwidth) 50 if err != nil { 51 return nil, fmt.Errorf("GenerateMachineState failed with error: %v", err) 52 } 53 54 return &networkPluginState{ 55 qrmConf: conf, 56 machineState: defaultMachineState, 57 machineInfo: machineInfo.Clone(), 58 reservedBandwidth: reservedBandwidth, 59 podEntries: make(PodEntries), 60 }, nil 61 } 62 63 func (s *networkPluginState) GetReservedBandwidth() map[string]uint32 { 64 s.RLock() 65 defer s.RUnlock() 66 67 clonedReservedBandwidth := make(map[string]uint32) 68 for iface, bandwidth := range s.reservedBandwidth { 69 clonedReservedBandwidth[iface] = bandwidth 70 } 71 72 return clonedReservedBandwidth 73 } 74 75 func (s *networkPluginState) GetMachineState() NICMap { 76 s.RLock() 77 defer s.RUnlock() 78 79 return s.machineState.Clone() 80 } 81 82 func (s *networkPluginState) GetMachineInfo() *info.MachineInfo { 83 s.RLock() 84 defer s.RUnlock() 85 86 return s.machineInfo.Clone() 87 } 88 89 func (s *networkPluginState) GetEnabledNICs() []machine.InterfaceInfo { 90 s.RLock() 91 defer s.RUnlock() 92 93 clonedNics := make([]machine.InterfaceInfo, len(s.nics)) 94 copy(clonedNics, s.nics) 95 96 return clonedNics 97 } 98 99 func (s *networkPluginState) GetAllocationInfo(podUID, containerName string) *AllocationInfo { 100 s.RLock() 101 defer s.RUnlock() 102 103 if res, ok := s.podEntries[podUID][containerName]; ok { 104 return res.Clone() 105 } 106 return nil 107 } 108 109 func (s *networkPluginState) GetPodEntries() PodEntries { 110 s.RLock() 111 defer s.RUnlock() 112 113 return s.podEntries.Clone() 114 } 115 116 func (s *networkPluginState) SetMachineState(nicMap NICMap) { 117 s.Lock() 118 defer s.Unlock() 119 120 s.machineState = nicMap.Clone() 121 generalLog.InfoS("updated network plugin machine state", 122 "NICMap", nicMap.String()) 123 } 124 125 func (s *networkPluginState) SetAllocationInfo(podUID, containerName string, allocationInfo *AllocationInfo) { 126 s.Lock() 127 defer s.Unlock() 128 129 if _, ok := s.podEntries[podUID]; !ok { 130 s.podEntries[podUID] = make(ContainerEntries) 131 } 132 133 s.podEntries[podUID][containerName] = allocationInfo.Clone() 134 generalLog.InfoS("updated network plugin pod resource entries", 135 "podUID", podUID, 136 "containerName", containerName, 137 "allocationInfo", allocationInfo.String()) 138 } 139 140 func (s *networkPluginState) SetPodEntries(podEntries PodEntries) { 141 s.Lock() 142 defer s.Unlock() 143 144 s.podEntries = podEntries.Clone() 145 generalLog.InfoS("updated network plugin pod resource entries", 146 "podEntries", podEntries.String()) 147 } 148 149 func (s *networkPluginState) Delete(podUID, containerName string) { 150 s.Lock() 151 defer s.Unlock() 152 153 if _, ok := s.podEntries[podUID]; !ok { 154 return 155 } 156 157 delete(s.podEntries[podUID], containerName) 158 if len(s.podEntries[podUID]) == 0 { 159 delete(s.podEntries, podUID) 160 } 161 generalLog.InfoS("deleted container entry", "podUID", podUID, "containerName", containerName) 162 } 163 164 func (s *networkPluginState) ClearState() { 165 s.Lock() 166 defer s.Unlock() 167 168 s.machineState, _ = GenerateMachineState(s.qrmConf, s.nics, s.reservedBandwidth) 169 s.podEntries = make(PodEntries) 170 171 generalLog.InfoS("cleared state") 172 }