|
| 1 | +/* |
| 2 | +Copyright 2024 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 incubating |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + resourceapi "k8s.io/api/resource/v1" |
| 23 | + "k8s.io/apimachinery/pkg/util/sets" |
| 24 | + draapi "k8s.io/dynamic-resource-allocation/api" |
| 25 | + "k8s.io/klog/v2" |
| 26 | +) |
| 27 | + |
| 28 | +// distinctAttributeConstraint compares an attribute value across devices. |
| 29 | +// All devices must share the same value. When the set of devices is |
| 30 | +// empty, any device that has the attribute can be added. After that, |
| 31 | +// only matching devices can be added. |
| 32 | +// |
| 33 | +// We don't need to track *which* devices are part of the set, only |
| 34 | +// how many. |
| 35 | +type distinctAttributeConstraint struct { |
| 36 | + logger klog.Logger // Includes name and attribute name, so no need to repeat in log messages. |
| 37 | + requestNames sets.Set[string] |
| 38 | + attributeName resourceapi.FullyQualifiedName |
| 39 | + |
| 40 | + attributes map[string]resourceapi.DeviceAttribute |
| 41 | + numDevices int |
| 42 | +} |
| 43 | + |
| 44 | +func (m *distinctAttributeConstraint) add(requestName, subRequestName string, device *draapi.Device, deviceID DeviceID) bool { |
| 45 | + if m.requestNames.Len() > 0 && !m.matches(requestName, subRequestName) { |
| 46 | + // Device not affected by constraint. |
| 47 | + return true |
| 48 | + } |
| 49 | + |
| 50 | + attribute := lookupAttribute(device, deviceID, m.attributeName) |
| 51 | + if attribute == nil { |
| 52 | + // Doesn't have the attribute. |
| 53 | + m.logger.V(7).Info("Constraint not satisfied, attribute not set") |
| 54 | + return false |
| 55 | + } |
| 56 | + |
| 57 | + if m.numDevices == 0 { |
| 58 | + // The first device can always get picked. |
| 59 | + m.attributes[requestName] = *attribute |
| 60 | + m.numDevices = 1 |
| 61 | + m.logger.V(7).Info("First attribute added") |
| 62 | + return true |
| 63 | + } |
| 64 | + |
| 65 | + if !m.matchesAttribute(*attribute) { |
| 66 | + m.logger.V(7).Info("Constraint not satisfied, has some duplicated attributes") |
| 67 | + return false |
| 68 | + } |
| 69 | + m.attributes[requestName] = *attribute |
| 70 | + m.numDevices++ |
| 71 | + m.logger.V(7).Info("Constraint satisfied by device", "device", deviceID, "numDevices", m.numDevices) |
| 72 | + return true |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | +func (m *distinctAttributeConstraint) remove(requestName, subRequestName string, device *draapi.Device, deviceID DeviceID) { |
| 77 | + if m.requestNames.Len() > 0 && !m.matches(requestName, subRequestName) { |
| 78 | + // Device not affected by constraint. |
| 79 | + return |
| 80 | + } |
| 81 | + delete(m.attributes, requestName) |
| 82 | + m.numDevices-- |
| 83 | + m.logger.V(7).Info("Device removed from constraint set", "device", deviceID, "numDevices", m.numDevices) |
| 84 | +} |
| 85 | + |
| 86 | +func (m *distinctAttributeConstraint) matches(requestName, subRequestName string) bool { |
| 87 | + if subRequestName == "" { |
| 88 | + return m.requestNames.Has(requestName) |
| 89 | + } else { |
| 90 | + fullSubRequestName := fmt.Sprintf("%s/%s", requestName, subRequestName) |
| 91 | + return m.requestNames.Has(requestName) || m.requestNames.Has(fullSubRequestName) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func (m *distinctAttributeConstraint) matchesAttribute(attribute resourceapi.DeviceAttribute) bool { |
| 96 | + for _, attr := range m.attributes { |
| 97 | + switch { |
| 98 | + case attribute.StringValue != nil: |
| 99 | + if attr.StringValue != nil && *attribute.StringValue == *attr.StringValue { |
| 100 | + m.logger.V(7).Info("String values duplicated") |
| 101 | + return false |
| 102 | + } |
| 103 | + case attribute.IntValue != nil: |
| 104 | + if attr.IntValue != nil && *attribute.IntValue == *attr.IntValue { |
| 105 | + m.logger.V(7).Info("Int values duplicated") |
| 106 | + return false |
| 107 | + } |
| 108 | + case attribute.BoolValue != nil: |
| 109 | + if attr.BoolValue != nil && *attribute.BoolValue == *attr.BoolValue { |
| 110 | + m.logger.V(7).Info("Bool values duplicated") |
| 111 | + return false |
| 112 | + } |
| 113 | + case attribute.VersionValue != nil: |
| 114 | + // semver 2.0.0 requires that version strings are in their |
| 115 | + // minimal form (in particular, no leading zeros). Therefore a |
| 116 | + // strict "exact equal" check can do a string comparison. |
| 117 | + if attr.VersionValue != nil && *attribute.VersionValue == *attr.VersionValue { |
| 118 | + m.logger.V(7).Info("Version values duplicated") |
| 119 | + return false |
| 120 | + } |
| 121 | + default: |
| 122 | + // Unknown value type, cannot match. |
| 123 | + // This condition should not be reached |
| 124 | + // as the unknown value type should be failed on CEL compile (getAttributeValue). |
| 125 | + m.logger.V(7).Info("Distinct attribute type unknown") |
| 126 | + return false |
| 127 | + } |
| 128 | + } |
| 129 | + // All distinct |
| 130 | + return true |
| 131 | +} |
0 commit comments