|
| 1 | +/* |
| 2 | +Copyright 2014 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 | +// This code is directly lifted from the Kubernetes codebase in order to avoid relying on the k8s.io/kubernetes package. |
| 18 | +// For reference: |
| 19 | +// https://github.com/kubernetes/kubernetes/blob/release-1.22/pkg/apis/core/helper/helpers.go#L57-L61 |
| 20 | +// https://github.com/kubernetes/kubernetes/blob/release-1.22/pkg/apis/core/helper/helpers.go#L169-L192 |
| 21 | +// https://github.com/kubernetes/kubernetes/blob/release-1.22/pkg/apis/core/helper/helpers.go#L212-L283 |
| 22 | + |
| 23 | +package helper |
| 24 | + |
| 25 | +import ( |
| 26 | + "fmt" |
| 27 | + "strings" |
| 28 | + |
| 29 | + corev1 "k8s.io/api/core/v1" |
| 30 | + "k8s.io/apimachinery/pkg/util/sets" |
| 31 | + "k8s.io/apimachinery/pkg/util/validation" |
| 32 | +) |
| 33 | + |
| 34 | +// IsQuotaHugePageResourceName returns true if the resource name has the quota |
| 35 | +// related huge page resource prefix. |
| 36 | +func IsQuotaHugePageResourceName(name corev1.ResourceName) bool { |
| 37 | + return strings.HasPrefix(string(name), corev1.ResourceHugePagesPrefix) || strings.HasPrefix(string(name), corev1.ResourceRequestsHugePagesPrefix) |
| 38 | +} |
| 39 | + |
| 40 | +// IsExtendedResourceName returns true if: |
| 41 | +// 1. the resource name is not in the default namespace; |
| 42 | +// 2. resource name does not have "requests." prefix, |
| 43 | +// to avoid confusion with the convention in quota |
| 44 | +// 3. it satisfies the rules in IsQualifiedName() after converted into quota resource name |
| 45 | +func IsExtendedResourceName(name corev1.ResourceName) bool { |
| 46 | + if IsNativeResource(name) || strings.HasPrefix(string(name), corev1.DefaultResourceRequestsPrefix) { |
| 47 | + return false |
| 48 | + } |
| 49 | + // Ensure it satisfies the rules in IsQualifiedName() after converted into quota resource name |
| 50 | + nameForQuota := fmt.Sprintf("%s%s", corev1.DefaultResourceRequestsPrefix, string(name)) |
| 51 | + if errs := validation.IsQualifiedName(string(nameForQuota)); len(errs) != 0 { |
| 52 | + return false |
| 53 | + } |
| 54 | + return true |
| 55 | +} |
| 56 | + |
| 57 | +// IsNativeResource returns true if the resource name is in the |
| 58 | +// *kubernetes.io/ namespace. Partially-qualified (unprefixed) names are |
| 59 | +// implicitly in the kubernetes.io/ namespace. |
| 60 | +func IsNativeResource(name corev1.ResourceName) bool { |
| 61 | + return !strings.Contains(string(name), "/") || |
| 62 | + strings.Contains(string(name), corev1.ResourceDefaultNamespacePrefix) |
| 63 | +} |
| 64 | + |
| 65 | +var standardQuotaResources = sets.NewString( |
| 66 | + string(corev1.ResourceCPU), |
| 67 | + string(corev1.ResourceMemory), |
| 68 | + string(corev1.ResourceEphemeralStorage), |
| 69 | + string(corev1.ResourceRequestsCPU), |
| 70 | + string(corev1.ResourceRequestsMemory), |
| 71 | + string(corev1.ResourceRequestsStorage), |
| 72 | + string(corev1.ResourceRequestsEphemeralStorage), |
| 73 | + string(corev1.ResourceLimitsCPU), |
| 74 | + string(corev1.ResourceLimitsMemory), |
| 75 | + string(corev1.ResourceLimitsEphemeralStorage), |
| 76 | + string(corev1.ResourcePods), |
| 77 | + string(corev1.ResourceQuotas), |
| 78 | + string(corev1.ResourceServices), |
| 79 | + string(corev1.ResourceReplicationControllers), |
| 80 | + string(corev1.ResourceSecrets), |
| 81 | + string(corev1.ResourcePersistentVolumeClaims), |
| 82 | + string(corev1.ResourceConfigMaps), |
| 83 | + string(corev1.ResourceServicesNodePorts), |
| 84 | + string(corev1.ResourceServicesLoadBalancers), |
| 85 | +) |
| 86 | + |
| 87 | +// IsStandardQuotaResourceName returns true if the resource is known to |
| 88 | +// the quota tracking system |
| 89 | +func IsStandardQuotaResourceName(str string) bool { |
| 90 | + return standardQuotaResources.Has(str) || IsQuotaHugePageResourceName(corev1.ResourceName(str)) |
| 91 | +} |
| 92 | + |
| 93 | +var standardResources = sets.NewString( |
| 94 | + string(corev1.ResourceCPU), |
| 95 | + string(corev1.ResourceMemory), |
| 96 | + string(corev1.ResourceEphemeralStorage), |
| 97 | + string(corev1.ResourceRequestsCPU), |
| 98 | + string(corev1.ResourceRequestsMemory), |
| 99 | + string(corev1.ResourceRequestsEphemeralStorage), |
| 100 | + string(corev1.ResourceLimitsCPU), |
| 101 | + string(corev1.ResourceLimitsMemory), |
| 102 | + string(corev1.ResourceLimitsEphemeralStorage), |
| 103 | + string(corev1.ResourcePods), |
| 104 | + string(corev1.ResourceQuotas), |
| 105 | + string(corev1.ResourceServices), |
| 106 | + string(corev1.ResourceReplicationControllers), |
| 107 | + string(corev1.ResourceSecrets), |
| 108 | + string(corev1.ResourceConfigMaps), |
| 109 | + string(corev1.ResourcePersistentVolumeClaims), |
| 110 | + string(corev1.ResourceStorage), |
| 111 | + string(corev1.ResourceRequestsStorage), |
| 112 | + string(corev1.ResourceServicesNodePorts), |
| 113 | + string(corev1.ResourceServicesLoadBalancers), |
| 114 | +) |
| 115 | + |
| 116 | +// IsStandardResourceName returns true if the resource is known to the system |
| 117 | +func IsStandardResourceName(str string) bool { |
| 118 | + return standardResources.Has(str) || IsQuotaHugePageResourceName(corev1.ResourceName(str)) |
| 119 | +} |
| 120 | + |
| 121 | +var integerResources = sets.NewString( |
| 122 | + string(corev1.ResourcePods), |
| 123 | + string(corev1.ResourceQuotas), |
| 124 | + string(corev1.ResourceServices), |
| 125 | + string(corev1.ResourceReplicationControllers), |
| 126 | + string(corev1.ResourceSecrets), |
| 127 | + string(corev1.ResourceConfigMaps), |
| 128 | + string(corev1.ResourcePersistentVolumeClaims), |
| 129 | + string(corev1.ResourceServicesNodePorts), |
| 130 | + string(corev1.ResourceServicesLoadBalancers), |
| 131 | +) |
| 132 | + |
| 133 | +// IsIntegerResourceName returns true if the resource is measured in integer values |
| 134 | +func IsIntegerResourceName(str string) bool { |
| 135 | + return integerResources.Has(str) || IsExtendedResourceName(corev1.ResourceName(str)) |
| 136 | +} |
0 commit comments