Merge pull request #93537 from timuthy/enhancement.move-resourcequota
Move ResourceQuota admission to k8s.io/apiserver lib
This commit is contained in:
@@ -1,40 +1,5 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"interfaces.go",
|
||||
"resources.go",
|
||||
],
|
||||
importpath = "k8s.io/kubernetes/pkg/quota/v1",
|
||||
deps = [
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/errors:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/admission:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/tools/cache:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["resources_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
@@ -47,7 +12,6 @@ filegroup(
|
||||
srcs = [
|
||||
":package-srcs",
|
||||
"//pkg/quota/v1/evaluator/core:all-srcs",
|
||||
"//pkg/quota/v1/generic:all-srcs",
|
||||
"//pkg/quota/v1/install:all-srcs",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
|
@@ -22,8 +22,6 @@ go_library(
|
||||
"//pkg/apis/core/v1/helper:go_default_library",
|
||||
"//pkg/apis/core/v1/helper/qos:go_default_library",
|
||||
"//pkg/features:go_default_library",
|
||||
"//pkg/quota/v1:go_default_library",
|
||||
"//pkg/quota/v1/generic:go_default_library",
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
|
||||
@@ -32,6 +30,8 @@ go_library(
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/admission:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/quota/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/quota/v1/generic:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
|
||||
],
|
||||
)
|
||||
@@ -46,14 +46,14 @@ go_test(
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//pkg/apis/core:go_default_library",
|
||||
"//pkg/quota/v1:go_default_library",
|
||||
"//pkg/quota/v1/generic:go_default_library",
|
||||
"//pkg/util/node:go_default_library",
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/quota/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/quota/v1/generic:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
|
@@ -25,13 +25,13 @@ import (
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apiserver/pkg/admission"
|
||||
quota "k8s.io/apiserver/pkg/quota/v1"
|
||||
"k8s.io/apiserver/pkg/quota/v1/generic"
|
||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
k8s_api_v1 "k8s.io/kubernetes/pkg/apis/core/v1"
|
||||
"k8s.io/kubernetes/pkg/apis/core/v1/helper"
|
||||
k8sfeatures "k8s.io/kubernetes/pkg/features"
|
||||
quota "k8s.io/kubernetes/pkg/quota/v1"
|
||||
"k8s.io/kubernetes/pkg/quota/v1/generic"
|
||||
)
|
||||
|
||||
// the name used for object count quota
|
||||
|
@@ -23,9 +23,9 @@ import (
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
quota "k8s.io/apiserver/pkg/quota/v1"
|
||||
"k8s.io/apiserver/pkg/quota/v1/generic"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
quota "k8s.io/kubernetes/pkg/quota/v1"
|
||||
"k8s.io/kubernetes/pkg/quota/v1/generic"
|
||||
)
|
||||
|
||||
func testVolumeClaim(name string, namespace string, spec api.PersistentVolumeClaimSpec) *api.PersistentVolumeClaim {
|
||||
|
@@ -30,12 +30,12 @@ import (
|
||||
"k8s.io/apimachinery/pkg/util/clock"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/apiserver/pkg/admission"
|
||||
quota "k8s.io/apiserver/pkg/quota/v1"
|
||||
"k8s.io/apiserver/pkg/quota/v1/generic"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
k8s_api_v1 "k8s.io/kubernetes/pkg/apis/core/v1"
|
||||
"k8s.io/kubernetes/pkg/apis/core/v1/helper"
|
||||
"k8s.io/kubernetes/pkg/apis/core/v1/helper/qos"
|
||||
quota "k8s.io/kubernetes/pkg/quota/v1"
|
||||
"k8s.io/kubernetes/pkg/quota/v1/generic"
|
||||
)
|
||||
|
||||
// the name used for object count quota
|
||||
|
@@ -25,9 +25,9 @@ import (
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/util/clock"
|
||||
quota "k8s.io/apiserver/pkg/quota/v1"
|
||||
"k8s.io/apiserver/pkg/quota/v1/generic"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
quota "k8s.io/kubernetes/pkg/quota/v1"
|
||||
"k8s.io/kubernetes/pkg/quota/v1/generic"
|
||||
"k8s.io/kubernetes/pkg/util/node"
|
||||
)
|
||||
|
||||
|
@@ -20,8 +20,8 @@ import (
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/util/clock"
|
||||
quota "k8s.io/kubernetes/pkg/quota/v1"
|
||||
"k8s.io/kubernetes/pkg/quota/v1/generic"
|
||||
quota "k8s.io/apiserver/pkg/quota/v1"
|
||||
"k8s.io/apiserver/pkg/quota/v1/generic"
|
||||
)
|
||||
|
||||
// legacyObjectCountAliases are what we used to do simple object counting quota with mapped to alias
|
||||
|
@@ -24,10 +24,10 @@ import (
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apiserver/pkg/admission"
|
||||
quota "k8s.io/apiserver/pkg/quota/v1"
|
||||
"k8s.io/apiserver/pkg/quota/v1/generic"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
k8s_api_v1 "k8s.io/kubernetes/pkg/apis/core/v1"
|
||||
"k8s.io/kubernetes/pkg/quota/v1"
|
||||
"k8s.io/kubernetes/pkg/quota/v1/generic"
|
||||
)
|
||||
|
||||
// the name used for object count quota
|
||||
|
@@ -22,9 +22,9 @@ import (
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
quota "k8s.io/apiserver/pkg/quota/v1"
|
||||
"k8s.io/apiserver/pkg/quota/v1/generic"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
quota "k8s.io/kubernetes/pkg/quota/v1"
|
||||
"k8s.io/kubernetes/pkg/quota/v1/generic"
|
||||
)
|
||||
|
||||
func TestServiceEvaluatorMatchesResources(t *testing.T) {
|
||||
|
@@ -1,48 +0,0 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"configuration.go",
|
||||
"evaluator.go",
|
||||
"registry.go",
|
||||
],
|
||||
importpath = "k8s.io/kubernetes/pkg/quota/v1/generic",
|
||||
deps = [
|
||||
"//pkg/quota/v1:go_default_library",
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/admission:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/informers:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/tools/cache:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["evaluator_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/tools/cache:go_default_library",
|
||||
],
|
||||
)
|
@@ -1,5 +0,0 @@
|
||||
# See the OWNERS docs at https://go.k8s.io/owners
|
||||
|
||||
reviewers:
|
||||
- smarterclayton
|
||||
- derekwaynecarr
|
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
Copyright 2017 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package generic
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
quota "k8s.io/kubernetes/pkg/quota/v1"
|
||||
)
|
||||
|
||||
// implements a basic configuration
|
||||
type simpleConfiguration struct {
|
||||
evaluators []quota.Evaluator
|
||||
ignoredResources map[schema.GroupResource]struct{}
|
||||
}
|
||||
|
||||
// NewConfiguration creates a quota configuration
|
||||
func NewConfiguration(evaluators []quota.Evaluator, ignoredResources map[schema.GroupResource]struct{}) quota.Configuration {
|
||||
return &simpleConfiguration{
|
||||
evaluators: evaluators,
|
||||
ignoredResources: ignoredResources,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *simpleConfiguration) IgnoredResources() map[schema.GroupResource]struct{} {
|
||||
return c.ignoredResources
|
||||
}
|
||||
|
||||
func (c *simpleConfiguration) Evaluators() []quota.Evaluator {
|
||||
return c.evaluators
|
||||
}
|
@@ -1,319 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package generic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apiserver/pkg/admission"
|
||||
"k8s.io/client-go/informers"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
quota "k8s.io/kubernetes/pkg/quota/v1"
|
||||
)
|
||||
|
||||
// InformerForResourceFunc knows how to provision an informer
|
||||
type InformerForResourceFunc func(schema.GroupVersionResource) (informers.GenericInformer, error)
|
||||
|
||||
// ListerFuncForResourceFunc knows how to provision a lister from an informer func.
|
||||
// The lister returns errors until the informer has synced.
|
||||
func ListerFuncForResourceFunc(f InformerForResourceFunc) quota.ListerForResourceFunc {
|
||||
return func(gvr schema.GroupVersionResource) (cache.GenericLister, error) {
|
||||
informer, err := f(gvr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &protectedLister{
|
||||
hasSynced: cachedHasSynced(informer.Informer().HasSynced),
|
||||
notReadyErr: fmt.Errorf("%v not yet synced", gvr),
|
||||
delegate: informer.Lister(),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// cachedHasSynced returns a function that calls hasSynced() until it returns true once, then returns true
|
||||
func cachedHasSynced(hasSynced func() bool) func() bool {
|
||||
cache := &atomic.Value{}
|
||||
cache.Store(false)
|
||||
return func() bool {
|
||||
if cache.Load().(bool) {
|
||||
// short-circuit if already synced
|
||||
return true
|
||||
}
|
||||
if hasSynced() {
|
||||
// remember we synced
|
||||
cache.Store(true)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// protectedLister returns notReadyError if hasSynced returns false, otherwise delegates to delegate
|
||||
type protectedLister struct {
|
||||
hasSynced func() bool
|
||||
notReadyErr error
|
||||
delegate cache.GenericLister
|
||||
}
|
||||
|
||||
func (p *protectedLister) List(selector labels.Selector) (ret []runtime.Object, err error) {
|
||||
if !p.hasSynced() {
|
||||
return nil, p.notReadyErr
|
||||
}
|
||||
return p.delegate.List(selector)
|
||||
}
|
||||
func (p *protectedLister) Get(name string) (runtime.Object, error) {
|
||||
if !p.hasSynced() {
|
||||
return nil, p.notReadyErr
|
||||
}
|
||||
return p.delegate.Get(name)
|
||||
}
|
||||
func (p *protectedLister) ByNamespace(namespace string) cache.GenericNamespaceLister {
|
||||
return &protectedNamespaceLister{p.hasSynced, p.notReadyErr, p.delegate.ByNamespace(namespace)}
|
||||
}
|
||||
|
||||
// protectedNamespaceLister returns notReadyError if hasSynced returns false, otherwise delegates to delegate
|
||||
type protectedNamespaceLister struct {
|
||||
hasSynced func() bool
|
||||
notReadyErr error
|
||||
delegate cache.GenericNamespaceLister
|
||||
}
|
||||
|
||||
func (p *protectedNamespaceLister) List(selector labels.Selector) (ret []runtime.Object, err error) {
|
||||
if !p.hasSynced() {
|
||||
return nil, p.notReadyErr
|
||||
}
|
||||
return p.delegate.List(selector)
|
||||
}
|
||||
func (p *protectedNamespaceLister) Get(name string) (runtime.Object, error) {
|
||||
if !p.hasSynced() {
|
||||
return nil, p.notReadyErr
|
||||
}
|
||||
return p.delegate.Get(name)
|
||||
}
|
||||
|
||||
// ListResourceUsingListerFunc returns a listing function based on the shared informer factory for the specified resource.
|
||||
func ListResourceUsingListerFunc(l quota.ListerForResourceFunc, resource schema.GroupVersionResource) ListFuncByNamespace {
|
||||
return func(namespace string) ([]runtime.Object, error) {
|
||||
lister, err := l(resource)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return lister.ByNamespace(namespace).List(labels.Everything())
|
||||
}
|
||||
}
|
||||
|
||||
// ObjectCountQuotaResourceNameFor returns the object count quota name for specified groupResource
|
||||
func ObjectCountQuotaResourceNameFor(groupResource schema.GroupResource) corev1.ResourceName {
|
||||
if len(groupResource.Group) == 0 {
|
||||
return corev1.ResourceName("count/" + groupResource.Resource)
|
||||
}
|
||||
return corev1.ResourceName("count/" + groupResource.Resource + "." + groupResource.Group)
|
||||
}
|
||||
|
||||
// ListFuncByNamespace knows how to list resources in a namespace
|
||||
type ListFuncByNamespace func(namespace string) ([]runtime.Object, error)
|
||||
|
||||
// MatchesScopeFunc knows how to evaluate if an object matches a scope
|
||||
type MatchesScopeFunc func(scope corev1.ScopedResourceSelectorRequirement, object runtime.Object) (bool, error)
|
||||
|
||||
// UsageFunc knows how to measure usage associated with an object
|
||||
type UsageFunc func(object runtime.Object) (corev1.ResourceList, error)
|
||||
|
||||
// MatchingResourceNamesFunc is a function that returns the list of resources matched
|
||||
type MatchingResourceNamesFunc func(input []corev1.ResourceName) []corev1.ResourceName
|
||||
|
||||
// MatchesNoScopeFunc returns false on all match checks
|
||||
func MatchesNoScopeFunc(scope corev1.ScopedResourceSelectorRequirement, object runtime.Object) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// Matches returns true if the quota matches the specified item.
|
||||
func Matches(
|
||||
resourceQuota *corev1.ResourceQuota, item runtime.Object,
|
||||
matchFunc MatchingResourceNamesFunc, scopeFunc MatchesScopeFunc) (bool, error) {
|
||||
if resourceQuota == nil {
|
||||
return false, fmt.Errorf("expected non-nil quota")
|
||||
}
|
||||
// verify the quota matches on at least one resource
|
||||
matchResource := len(matchFunc(quota.ResourceNames(resourceQuota.Status.Hard))) > 0
|
||||
// by default, no scopes matches all
|
||||
matchScope := true
|
||||
for _, scope := range getScopeSelectorsFromQuota(resourceQuota) {
|
||||
innerMatch, err := scopeFunc(scope, item)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
matchScope = matchScope && innerMatch
|
||||
}
|
||||
return matchResource && matchScope, nil
|
||||
}
|
||||
|
||||
func getScopeSelectorsFromQuota(quota *corev1.ResourceQuota) []corev1.ScopedResourceSelectorRequirement {
|
||||
selectors := []corev1.ScopedResourceSelectorRequirement{}
|
||||
for _, scope := range quota.Spec.Scopes {
|
||||
selectors = append(selectors, corev1.ScopedResourceSelectorRequirement{
|
||||
ScopeName: scope,
|
||||
Operator: corev1.ScopeSelectorOpExists})
|
||||
}
|
||||
if quota.Spec.ScopeSelector != nil {
|
||||
selectors = append(selectors, quota.Spec.ScopeSelector.MatchExpressions...)
|
||||
}
|
||||
return selectors
|
||||
}
|
||||
|
||||
// CalculateUsageStats is a utility function that knows how to calculate aggregate usage.
|
||||
func CalculateUsageStats(options quota.UsageStatsOptions,
|
||||
listFunc ListFuncByNamespace,
|
||||
scopeFunc MatchesScopeFunc,
|
||||
usageFunc UsageFunc) (quota.UsageStats, error) {
|
||||
// default each tracked resource to zero
|
||||
result := quota.UsageStats{Used: corev1.ResourceList{}}
|
||||
for _, resourceName := range options.Resources {
|
||||
result.Used[resourceName] = resource.Quantity{Format: resource.DecimalSI}
|
||||
}
|
||||
items, err := listFunc(options.Namespace)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("failed to list content: %v", err)
|
||||
}
|
||||
for _, item := range items {
|
||||
// need to verify that the item matches the set of scopes
|
||||
matchesScopes := true
|
||||
for _, scope := range options.Scopes {
|
||||
innerMatch, err := scopeFunc(corev1.ScopedResourceSelectorRequirement{ScopeName: scope}, item)
|
||||
if err != nil {
|
||||
return result, nil
|
||||
}
|
||||
if !innerMatch {
|
||||
matchesScopes = false
|
||||
}
|
||||
}
|
||||
if options.ScopeSelector != nil {
|
||||
for _, selector := range options.ScopeSelector.MatchExpressions {
|
||||
innerMatch, err := scopeFunc(selector, item)
|
||||
if err != nil {
|
||||
return result, nil
|
||||
}
|
||||
matchesScopes = matchesScopes && innerMatch
|
||||
}
|
||||
}
|
||||
// only count usage if there was a match
|
||||
if matchesScopes {
|
||||
usage, err := usageFunc(item)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Used = quota.Add(result.Used, usage)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// objectCountEvaluator provides an implementation for quota.Evaluator
|
||||
// that associates usage of the specified resource based on the number of items
|
||||
// returned by the specified listing function.
|
||||
type objectCountEvaluator struct {
|
||||
// GroupResource that this evaluator tracks.
|
||||
// It is used to construct a generic object count quota name
|
||||
groupResource schema.GroupResource
|
||||
// A function that knows how to list resources by namespace.
|
||||
// TODO move to dynamic client in future
|
||||
listFuncByNamespace ListFuncByNamespace
|
||||
// Names associated with this resource in the quota for generic counting.
|
||||
resourceNames []corev1.ResourceName
|
||||
}
|
||||
|
||||
// Constraints returns an error if the configured resource name is not in the required set.
|
||||
func (o *objectCountEvaluator) Constraints(required []corev1.ResourceName, item runtime.Object) error {
|
||||
// no-op for object counting
|
||||
return nil
|
||||
}
|
||||
|
||||
// Handles returns true if the object count evaluator needs to track this attributes.
|
||||
func (o *objectCountEvaluator) Handles(a admission.Attributes) bool {
|
||||
operation := a.GetOperation()
|
||||
return operation == admission.Create
|
||||
}
|
||||
|
||||
// Matches returns true if the evaluator matches the specified quota with the provided input item
|
||||
func (o *objectCountEvaluator) Matches(resourceQuota *corev1.ResourceQuota, item runtime.Object) (bool, error) {
|
||||
return Matches(resourceQuota, item, o.MatchingResources, MatchesNoScopeFunc)
|
||||
}
|
||||
|
||||
// MatchingResources takes the input specified list of resources and returns the set of resources it matches.
|
||||
func (o *objectCountEvaluator) MatchingResources(input []corev1.ResourceName) []corev1.ResourceName {
|
||||
return quota.Intersection(input, o.resourceNames)
|
||||
}
|
||||
|
||||
// MatchingScopes takes the input specified list of scopes and input object. Returns the set of scopes resource matches.
|
||||
func (o *objectCountEvaluator) MatchingScopes(item runtime.Object, scopes []corev1.ScopedResourceSelectorRequirement) ([]corev1.ScopedResourceSelectorRequirement, error) {
|
||||
return []corev1.ScopedResourceSelectorRequirement{}, nil
|
||||
}
|
||||
|
||||
// UncoveredQuotaScopes takes the input matched scopes which are limited by configuration and the matched quota scopes.
|
||||
// It returns the scopes which are in limited scopes but dont have a corresponding covering quota scope
|
||||
func (o *objectCountEvaluator) UncoveredQuotaScopes(limitedScopes []corev1.ScopedResourceSelectorRequirement, matchedQuotaScopes []corev1.ScopedResourceSelectorRequirement) ([]corev1.ScopedResourceSelectorRequirement, error) {
|
||||
return []corev1.ScopedResourceSelectorRequirement{}, nil
|
||||
}
|
||||
|
||||
// Usage returns the resource usage for the specified object
|
||||
func (o *objectCountEvaluator) Usage(object runtime.Object) (corev1.ResourceList, error) {
|
||||
quantity := resource.NewQuantity(1, resource.DecimalSI)
|
||||
resourceList := corev1.ResourceList{}
|
||||
for _, resourceName := range o.resourceNames {
|
||||
resourceList[resourceName] = *quantity
|
||||
}
|
||||
return resourceList, nil
|
||||
}
|
||||
|
||||
// GroupResource tracked by this evaluator
|
||||
func (o *objectCountEvaluator) GroupResource() schema.GroupResource {
|
||||
return o.groupResource
|
||||
}
|
||||
|
||||
// UsageStats calculates aggregate usage for the object.
|
||||
func (o *objectCountEvaluator) UsageStats(options quota.UsageStatsOptions) (quota.UsageStats, error) {
|
||||
return CalculateUsageStats(options, o.listFuncByNamespace, MatchesNoScopeFunc, o.Usage)
|
||||
}
|
||||
|
||||
// Verify implementation of interface at compile time.
|
||||
var _ quota.Evaluator = &objectCountEvaluator{}
|
||||
|
||||
// NewObjectCountEvaluator returns an evaluator that can perform generic
|
||||
// object quota counting. It allows an optional alias for backwards compatibility
|
||||
// purposes for the legacy object counting names in quota. Unless its supporting
|
||||
// backward compatibility, alias should not be used.
|
||||
func NewObjectCountEvaluator(
|
||||
groupResource schema.GroupResource, listFuncByNamespace ListFuncByNamespace,
|
||||
alias corev1.ResourceName) quota.Evaluator {
|
||||
|
||||
resourceNames := []corev1.ResourceName{ObjectCountQuotaResourceNameFor(groupResource)}
|
||||
if len(alias) > 0 {
|
||||
resourceNames = append(resourceNames, alias)
|
||||
}
|
||||
|
||||
return &objectCountEvaluator{
|
||||
groupResource: groupResource,
|
||||
listFuncByNamespace: listFuncByNamespace,
|
||||
resourceNames: resourceNames,
|
||||
}
|
||||
}
|
@@ -1,131 +0,0 @@
|
||||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package generic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
)
|
||||
|
||||
func TestCachedHasSynced(t *testing.T) {
|
||||
|
||||
called := 0
|
||||
result := false
|
||||
cachedFunc := cachedHasSynced(func() bool {
|
||||
called++
|
||||
return result
|
||||
})
|
||||
|
||||
if cachedFunc() {
|
||||
t.Fatal("expected false")
|
||||
}
|
||||
if called != 1 {
|
||||
t.Fatalf("expected called=1, got %d", called)
|
||||
}
|
||||
|
||||
if cachedFunc() {
|
||||
t.Fatal("expected false")
|
||||
}
|
||||
if called != 2 {
|
||||
t.Fatalf("expected called=2, got %d", called)
|
||||
}
|
||||
|
||||
result = true
|
||||
if !cachedFunc() {
|
||||
t.Fatal("expected true")
|
||||
}
|
||||
if called != 3 {
|
||||
t.Fatalf("expected called=3, got %d", called)
|
||||
}
|
||||
|
||||
if !cachedFunc() {
|
||||
t.Fatal("expected true")
|
||||
}
|
||||
if called != 3 {
|
||||
// no more calls once we return true
|
||||
t.Fatalf("expected called=3, got %d", called)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProtectedLister(t *testing.T) {
|
||||
|
||||
hasSynced := false
|
||||
notReadyErr := errors.New("not ready")
|
||||
fake := &fakeLister{}
|
||||
l := &protectedLister{
|
||||
hasSynced: func() bool { return hasSynced },
|
||||
notReadyErr: notReadyErr,
|
||||
delegate: fake,
|
||||
}
|
||||
if _, err := l.List(nil); err != notReadyErr {
|
||||
t.Fatalf("expected %v, got %v", notReadyErr, err)
|
||||
}
|
||||
if _, err := l.Get(""); err != notReadyErr {
|
||||
t.Fatalf("expected %v, got %v", notReadyErr, err)
|
||||
}
|
||||
if fake.called != 0 {
|
||||
t.Fatalf("expected called=0, got %d", fake.called)
|
||||
}
|
||||
fake.called = 0
|
||||
|
||||
hasSynced = true
|
||||
|
||||
if _, err := l.List(nil); err != errFakeLister {
|
||||
t.Fatalf("expected %v, got %v", errFakeLister, err)
|
||||
}
|
||||
if _, err := l.Get(""); err != errFakeLister {
|
||||
t.Fatalf("expected %v, got %v", errFakeLister, err)
|
||||
}
|
||||
if fake.called != 2 {
|
||||
t.Fatalf("expected called=2, got %d", fake.called)
|
||||
}
|
||||
fake.called = 0
|
||||
|
||||
hasSynced = false
|
||||
|
||||
if _, err := l.List(nil); err != notReadyErr {
|
||||
t.Fatalf("expected %v, got %v", notReadyErr, err)
|
||||
}
|
||||
if _, err := l.Get(""); err != notReadyErr {
|
||||
t.Fatalf("expected %v, got %v", notReadyErr, err)
|
||||
}
|
||||
if fake.called != 0 {
|
||||
t.Fatalf("expected called=2, got %d", fake.called)
|
||||
}
|
||||
}
|
||||
|
||||
var errFakeLister = errors.New("errFakeLister")
|
||||
|
||||
type fakeLister struct {
|
||||
called int
|
||||
}
|
||||
|
||||
func (f *fakeLister) List(selector labels.Selector) (ret []runtime.Object, err error) {
|
||||
f.called++
|
||||
return nil, errFakeLister
|
||||
}
|
||||
func (f *fakeLister) Get(name string) (runtime.Object, error) {
|
||||
f.called++
|
||||
return nil, errFakeLister
|
||||
}
|
||||
func (f *fakeLister) ByNamespace(namespace string) cache.GenericNamespaceLister {
|
||||
panic("not implemented")
|
||||
}
|
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package generic
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
quota "k8s.io/kubernetes/pkg/quota/v1"
|
||||
)
|
||||
|
||||
// implements a basic registry
|
||||
type simpleRegistry struct {
|
||||
lock sync.RWMutex
|
||||
// evaluators tracked by the registry
|
||||
evaluators map[schema.GroupResource]quota.Evaluator
|
||||
}
|
||||
|
||||
// NewRegistry creates a simple registry with initial list of evaluators
|
||||
func NewRegistry(evaluators []quota.Evaluator) quota.Registry {
|
||||
return &simpleRegistry{
|
||||
evaluators: evaluatorsByGroupResource(evaluators),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *simpleRegistry) Add(e quota.Evaluator) {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
r.evaluators[e.GroupResource()] = e
|
||||
}
|
||||
|
||||
func (r *simpleRegistry) Remove(e quota.Evaluator) {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
delete(r.evaluators, e.GroupResource())
|
||||
}
|
||||
|
||||
func (r *simpleRegistry) Get(gr schema.GroupResource) quota.Evaluator {
|
||||
r.lock.RLock()
|
||||
defer r.lock.RUnlock()
|
||||
return r.evaluators[gr]
|
||||
}
|
||||
|
||||
func (r *simpleRegistry) List() []quota.Evaluator {
|
||||
r.lock.RLock()
|
||||
defer r.lock.RUnlock()
|
||||
|
||||
return evaluatorsList(r.evaluators)
|
||||
}
|
||||
|
||||
// evaluatorsByGroupResource converts a list of evaluators to a map by group resource.
|
||||
func evaluatorsByGroupResource(items []quota.Evaluator) map[schema.GroupResource]quota.Evaluator {
|
||||
result := map[schema.GroupResource]quota.Evaluator{}
|
||||
for _, item := range items {
|
||||
result[item.GroupResource()] = item
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// evaluatorsList converts a map of evaluators to list
|
||||
func evaluatorsList(input map[schema.GroupResource]quota.Evaluator) []quota.Evaluator {
|
||||
var result []quota.Evaluator
|
||||
for _, item := range input {
|
||||
result = append(result, item)
|
||||
}
|
||||
return result
|
||||
}
|
@@ -10,10 +10,10 @@ go_library(
|
||||
srcs = ["registry.go"],
|
||||
importpath = "k8s.io/kubernetes/pkg/quota/v1/install",
|
||||
deps = [
|
||||
"//pkg/quota/v1:go_default_library",
|
||||
"//pkg/quota/v1/evaluator/core:go_default_library",
|
||||
"//pkg/quota/v1/generic:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/quota/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/quota/v1/generic:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
|
@@ -18,9 +18,9 @@ package install
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
quota "k8s.io/kubernetes/pkg/quota/v1"
|
||||
core "k8s.io/kubernetes/pkg/quota/v1/evaluator/core"
|
||||
generic "k8s.io/kubernetes/pkg/quota/v1/generic"
|
||||
quota "k8s.io/apiserver/pkg/quota/v1"
|
||||
"k8s.io/apiserver/pkg/quota/v1/generic"
|
||||
"k8s.io/kubernetes/pkg/quota/v1/evaluator/core"
|
||||
)
|
||||
|
||||
// NewQuotaConfigurationForAdmission returns a quota configuration for admission control.
|
||||
|
@@ -1,88 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package quota
|
||||
|
||||
import (
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apiserver/pkg/admission"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
)
|
||||
|
||||
// UsageStatsOptions is an options structs that describes how stats should be calculated
|
||||
type UsageStatsOptions struct {
|
||||
// Namespace where stats should be calculate
|
||||
Namespace string
|
||||
// Scopes that must match counted objects
|
||||
Scopes []corev1.ResourceQuotaScope
|
||||
// Resources are the set of resources to include in the measurement
|
||||
Resources []corev1.ResourceName
|
||||
ScopeSelector *corev1.ScopeSelector
|
||||
}
|
||||
|
||||
// UsageStats is result of measuring observed resource use in the system
|
||||
type UsageStats struct {
|
||||
// Used maps resource to quantity used
|
||||
Used corev1.ResourceList
|
||||
}
|
||||
|
||||
// Evaluator knows how to evaluate quota usage for a particular group resource
|
||||
type Evaluator interface {
|
||||
// Constraints ensures that each required resource is present on item
|
||||
Constraints(required []corev1.ResourceName, item runtime.Object) error
|
||||
// GroupResource returns the groupResource that this object knows how to evaluate
|
||||
GroupResource() schema.GroupResource
|
||||
// Handles determines if quota could be impacted by the specified attribute.
|
||||
// If true, admission control must perform quota processing for the operation, otherwise it is safe to ignore quota.
|
||||
Handles(operation admission.Attributes) bool
|
||||
// Matches returns true if the specified quota matches the input item
|
||||
Matches(resourceQuota *corev1.ResourceQuota, item runtime.Object) (bool, error)
|
||||
// MatchingScopes takes the input specified list of scopes and input object and returns the set of scopes that matches input object.
|
||||
MatchingScopes(item runtime.Object, scopes []corev1.ScopedResourceSelectorRequirement) ([]corev1.ScopedResourceSelectorRequirement, error)
|
||||
// UncoveredQuotaScopes takes the input matched scopes which are limited by configuration and the matched quota scopes. It returns the scopes which are in limited scopes but dont have a corresponding covering quota scope
|
||||
UncoveredQuotaScopes(limitedScopes []corev1.ScopedResourceSelectorRequirement, matchedQuotaScopes []corev1.ScopedResourceSelectorRequirement) ([]corev1.ScopedResourceSelectorRequirement, error)
|
||||
// MatchingResources takes the input specified list of resources and returns the set of resources evaluator matches.
|
||||
MatchingResources(input []corev1.ResourceName) []corev1.ResourceName
|
||||
// Usage returns the resource usage for the specified object
|
||||
Usage(item runtime.Object) (corev1.ResourceList, error)
|
||||
// UsageStats calculates latest observed usage stats for all objects
|
||||
UsageStats(options UsageStatsOptions) (UsageStats, error)
|
||||
}
|
||||
|
||||
// Configuration defines how the quota system is configured.
|
||||
type Configuration interface {
|
||||
// IgnoredResources are ignored by quota.
|
||||
IgnoredResources() map[schema.GroupResource]struct{}
|
||||
// Evaluators for quota evaluation.
|
||||
Evaluators() []Evaluator
|
||||
}
|
||||
|
||||
// Registry maintains a list of evaluators
|
||||
type Registry interface {
|
||||
// Add to registry
|
||||
Add(e Evaluator)
|
||||
// Remove from registry
|
||||
Remove(e Evaluator)
|
||||
// Get by group resource
|
||||
Get(gr schema.GroupResource) Evaluator
|
||||
// List from registry
|
||||
List() []Evaluator
|
||||
}
|
||||
|
||||
// ListerForResourceFunc knows how to get a lister for a specific resource
|
||||
type ListerForResourceFunc func(schema.GroupVersionResource) (cache.GenericLister, error)
|
@@ -1,293 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package quota
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
)
|
||||
|
||||
// Equals returns true if the two lists are equivalent
|
||||
func Equals(a corev1.ResourceList, b corev1.ResourceList) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
|
||||
for key, value1 := range a {
|
||||
value2, found := b[key]
|
||||
if !found {
|
||||
return false
|
||||
}
|
||||
if value1.Cmp(value2) != 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// LessThanOrEqual returns true if a < b for each key in b
|
||||
// If false, it returns the keys in a that exceeded b
|
||||
func LessThanOrEqual(a corev1.ResourceList, b corev1.ResourceList) (bool, []corev1.ResourceName) {
|
||||
result := true
|
||||
resourceNames := []corev1.ResourceName{}
|
||||
for key, value := range b {
|
||||
if other, found := a[key]; found {
|
||||
if other.Cmp(value) > 0 {
|
||||
result = false
|
||||
resourceNames = append(resourceNames, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result, resourceNames
|
||||
}
|
||||
|
||||
// Max returns the result of Max(a, b) for each named resource
|
||||
func Max(a corev1.ResourceList, b corev1.ResourceList) corev1.ResourceList {
|
||||
result := corev1.ResourceList{}
|
||||
for key, value := range a {
|
||||
if other, found := b[key]; found {
|
||||
if value.Cmp(other) <= 0 {
|
||||
result[key] = other.DeepCopy()
|
||||
continue
|
||||
}
|
||||
}
|
||||
result[key] = value.DeepCopy()
|
||||
}
|
||||
for key, value := range b {
|
||||
if _, found := result[key]; !found {
|
||||
result[key] = value.DeepCopy()
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Add returns the result of a + b for each named resource
|
||||
func Add(a corev1.ResourceList, b corev1.ResourceList) corev1.ResourceList {
|
||||
result := corev1.ResourceList{}
|
||||
for key, value := range a {
|
||||
quantity := value.DeepCopy()
|
||||
if other, found := b[key]; found {
|
||||
quantity.Add(other)
|
||||
}
|
||||
result[key] = quantity
|
||||
}
|
||||
for key, value := range b {
|
||||
if _, found := result[key]; !found {
|
||||
result[key] = value.DeepCopy()
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// SubtractWithNonNegativeResult - subtracts and returns result of a - b but
|
||||
// makes sure we don't return negative values to prevent negative resource usage.
|
||||
func SubtractWithNonNegativeResult(a corev1.ResourceList, b corev1.ResourceList) corev1.ResourceList {
|
||||
zero := resource.MustParse("0")
|
||||
|
||||
result := corev1.ResourceList{}
|
||||
for key, value := range a {
|
||||
quantity := value.DeepCopy()
|
||||
if other, found := b[key]; found {
|
||||
quantity.Sub(other)
|
||||
}
|
||||
if quantity.Cmp(zero) > 0 {
|
||||
result[key] = quantity
|
||||
} else {
|
||||
result[key] = zero
|
||||
}
|
||||
}
|
||||
|
||||
for key := range b {
|
||||
if _, found := result[key]; !found {
|
||||
result[key] = zero
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Subtract returns the result of a - b for each named resource
|
||||
func Subtract(a corev1.ResourceList, b corev1.ResourceList) corev1.ResourceList {
|
||||
result := corev1.ResourceList{}
|
||||
for key, value := range a {
|
||||
quantity := value.DeepCopy()
|
||||
if other, found := b[key]; found {
|
||||
quantity.Sub(other)
|
||||
}
|
||||
result[key] = quantity
|
||||
}
|
||||
for key, value := range b {
|
||||
if _, found := result[key]; !found {
|
||||
quantity := value.DeepCopy()
|
||||
quantity.Neg()
|
||||
result[key] = quantity
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Mask returns a new resource list that only has the values with the specified names
|
||||
func Mask(resources corev1.ResourceList, names []corev1.ResourceName) corev1.ResourceList {
|
||||
nameSet := ToSet(names)
|
||||
result := corev1.ResourceList{}
|
||||
for key, value := range resources {
|
||||
if nameSet.Has(string(key)) {
|
||||
result[key] = value.DeepCopy()
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// ResourceNames returns a list of all resource names in the ResourceList
|
||||
func ResourceNames(resources corev1.ResourceList) []corev1.ResourceName {
|
||||
result := []corev1.ResourceName{}
|
||||
for resourceName := range resources {
|
||||
result = append(result, resourceName)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Contains returns true if the specified item is in the list of items
|
||||
func Contains(items []corev1.ResourceName, item corev1.ResourceName) bool {
|
||||
for _, i := range items {
|
||||
if i == item {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ContainsPrefix returns true if the specified item has a prefix that contained in given prefix Set
|
||||
func ContainsPrefix(prefixSet []string, item corev1.ResourceName) bool {
|
||||
for _, prefix := range prefixSet {
|
||||
if strings.HasPrefix(string(item), prefix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Intersection returns the intersection of both list of resources, deduped and sorted
|
||||
func Intersection(a []corev1.ResourceName, b []corev1.ResourceName) []corev1.ResourceName {
|
||||
result := make([]corev1.ResourceName, 0, len(a))
|
||||
for _, item := range a {
|
||||
if Contains(result, item) {
|
||||
continue
|
||||
}
|
||||
if !Contains(b, item) {
|
||||
continue
|
||||
}
|
||||
result = append(result, item)
|
||||
}
|
||||
sort.Slice(result, func(i, j int) bool { return result[i] < result[j] })
|
||||
return result
|
||||
}
|
||||
|
||||
// Difference returns the list of resources resulting from a-b, deduped and sorted
|
||||
func Difference(a []corev1.ResourceName, b []corev1.ResourceName) []corev1.ResourceName {
|
||||
result := make([]corev1.ResourceName, 0, len(a))
|
||||
for _, item := range a {
|
||||
if Contains(b, item) || Contains(result, item) {
|
||||
continue
|
||||
}
|
||||
result = append(result, item)
|
||||
}
|
||||
sort.Slice(result, func(i, j int) bool { return result[i] < result[j] })
|
||||
return result
|
||||
}
|
||||
|
||||
// IsZero returns true if each key maps to the quantity value 0
|
||||
func IsZero(a corev1.ResourceList) bool {
|
||||
zero := resource.MustParse("0")
|
||||
for _, v := range a {
|
||||
if v.Cmp(zero) != 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// IsNegative returns the set of resource names that have a negative value.
|
||||
func IsNegative(a corev1.ResourceList) []corev1.ResourceName {
|
||||
results := []corev1.ResourceName{}
|
||||
zero := resource.MustParse("0")
|
||||
for k, v := range a {
|
||||
if v.Cmp(zero) < 0 {
|
||||
results = append(results, k)
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// ToSet takes a list of resource names and converts to a string set
|
||||
func ToSet(resourceNames []corev1.ResourceName) sets.String {
|
||||
result := sets.NewString()
|
||||
for _, resourceName := range resourceNames {
|
||||
result.Insert(string(resourceName))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// CalculateUsage calculates and returns the requested ResourceList usage.
|
||||
// If an error is returned, usage only contains the resources which encountered no calculation errors.
|
||||
func CalculateUsage(namespaceName string, scopes []corev1.ResourceQuotaScope, hardLimits corev1.ResourceList, registry Registry, scopeSelector *corev1.ScopeSelector) (corev1.ResourceList, error) {
|
||||
// find the intersection between the hard resources on the quota
|
||||
// and the resources this controller can track to know what we can
|
||||
// look to measure updated usage stats for
|
||||
hardResources := ResourceNames(hardLimits)
|
||||
potentialResources := []corev1.ResourceName{}
|
||||
evaluators := registry.List()
|
||||
for _, evaluator := range evaluators {
|
||||
potentialResources = append(potentialResources, evaluator.MatchingResources(hardResources)...)
|
||||
}
|
||||
// NOTE: the intersection just removes duplicates since the evaluator match intersects with hard
|
||||
matchedResources := Intersection(hardResources, potentialResources)
|
||||
|
||||
errors := []error{}
|
||||
|
||||
// sum the observed usage from each evaluator
|
||||
newUsage := corev1.ResourceList{}
|
||||
for _, evaluator := range evaluators {
|
||||
// only trigger the evaluator if it matches a resource in the quota, otherwise, skip calculating anything
|
||||
intersection := evaluator.MatchingResources(matchedResources)
|
||||
if len(intersection) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
usageStatsOptions := UsageStatsOptions{Namespace: namespaceName, Scopes: scopes, Resources: intersection, ScopeSelector: scopeSelector}
|
||||
stats, err := evaluator.UsageStats(usageStatsOptions)
|
||||
if err != nil {
|
||||
// remember the error
|
||||
errors = append(errors, err)
|
||||
// exclude resources which encountered calculation errors
|
||||
matchedResources = Difference(matchedResources, intersection)
|
||||
continue
|
||||
}
|
||||
newUsage = Add(newUsage, stats.Used)
|
||||
}
|
||||
|
||||
// mask the observed usage to only the set of resources tracked by this quota
|
||||
// merge our observed usage with the quota usage status
|
||||
// if the new usage is different than the last usage, we will need to do an update
|
||||
newUsage = Mask(newUsage, matchedResources)
|
||||
return newUsage, utilerrors.NewAggregate(errors)
|
||||
}
|
@@ -1,412 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package quota
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
)
|
||||
|
||||
func TestEquals(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
a corev1.ResourceList
|
||||
b corev1.ResourceList
|
||||
expected bool
|
||||
}{
|
||||
"isEqual": {
|
||||
a: corev1.ResourceList{},
|
||||
b: corev1.ResourceList{},
|
||||
expected: true,
|
||||
},
|
||||
"isEqualWithKeys": {
|
||||
a: corev1.ResourceList{
|
||||
corev1.ResourceCPU: resource.MustParse("100m"),
|
||||
corev1.ResourceMemory: resource.MustParse("1Gi"),
|
||||
},
|
||||
b: corev1.ResourceList{
|
||||
corev1.ResourceCPU: resource.MustParse("100m"),
|
||||
corev1.ResourceMemory: resource.MustParse("1Gi"),
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
"isNotEqualSameKeys": {
|
||||
a: corev1.ResourceList{
|
||||
corev1.ResourceCPU: resource.MustParse("200m"),
|
||||
corev1.ResourceMemory: resource.MustParse("1Gi"),
|
||||
},
|
||||
b: corev1.ResourceList{
|
||||
corev1.ResourceCPU: resource.MustParse("100m"),
|
||||
corev1.ResourceMemory: resource.MustParse("1Gi"),
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
"isNotEqualDiffKeys": {
|
||||
a: corev1.ResourceList{
|
||||
corev1.ResourceCPU: resource.MustParse("100m"),
|
||||
corev1.ResourceMemory: resource.MustParse("1Gi"),
|
||||
},
|
||||
b: corev1.ResourceList{
|
||||
corev1.ResourceCPU: resource.MustParse("100m"),
|
||||
corev1.ResourceMemory: resource.MustParse("1Gi"),
|
||||
corev1.ResourcePods: resource.MustParse("1"),
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
for testName, testCase := range testCases {
|
||||
if result := Equals(testCase.a, testCase.b); result != testCase.expected {
|
||||
t.Errorf("%s expected: %v, actual: %v, a=%v, b=%v", testName, testCase.expected, result, testCase.a, testCase.b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMax(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
a corev1.ResourceList
|
||||
b corev1.ResourceList
|
||||
expected corev1.ResourceList
|
||||
}{
|
||||
"noKeys": {
|
||||
a: corev1.ResourceList{},
|
||||
b: corev1.ResourceList{},
|
||||
expected: corev1.ResourceList{},
|
||||
},
|
||||
"toEmpty": {
|
||||
a: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")},
|
||||
b: corev1.ResourceList{},
|
||||
expected: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")},
|
||||
},
|
||||
"matching": {
|
||||
a: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")},
|
||||
b: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("150m")},
|
||||
expected: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("150m")},
|
||||
},
|
||||
"matching(reverse)": {
|
||||
a: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("150m")},
|
||||
b: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")},
|
||||
expected: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("150m")},
|
||||
},
|
||||
"matching-equal": {
|
||||
a: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")},
|
||||
b: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")},
|
||||
expected: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")},
|
||||
},
|
||||
}
|
||||
for testName, testCase := range testCases {
|
||||
sum := Max(testCase.a, testCase.b)
|
||||
if result := Equals(testCase.expected, sum); !result {
|
||||
t.Errorf("%s expected: %v, actual: %v", testName, testCase.expected, sum)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdd(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
a corev1.ResourceList
|
||||
b corev1.ResourceList
|
||||
expected corev1.ResourceList
|
||||
}{
|
||||
"noKeys": {
|
||||
a: corev1.ResourceList{},
|
||||
b: corev1.ResourceList{},
|
||||
expected: corev1.ResourceList{},
|
||||
},
|
||||
"toEmpty": {
|
||||
a: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")},
|
||||
b: corev1.ResourceList{},
|
||||
expected: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")},
|
||||
},
|
||||
"matching": {
|
||||
a: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")},
|
||||
b: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")},
|
||||
expected: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("200m")},
|
||||
},
|
||||
}
|
||||
for testName, testCase := range testCases {
|
||||
sum := Add(testCase.a, testCase.b)
|
||||
if result := Equals(testCase.expected, sum); !result {
|
||||
t.Errorf("%s expected: %v, actual: %v", testName, testCase.expected, sum)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubtract(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
a corev1.ResourceList
|
||||
b corev1.ResourceList
|
||||
expected corev1.ResourceList
|
||||
}{
|
||||
"noKeys": {
|
||||
a: corev1.ResourceList{},
|
||||
b: corev1.ResourceList{},
|
||||
expected: corev1.ResourceList{},
|
||||
},
|
||||
"value-empty": {
|
||||
a: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")},
|
||||
b: corev1.ResourceList{},
|
||||
expected: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")},
|
||||
},
|
||||
"empty-value": {
|
||||
a: corev1.ResourceList{},
|
||||
b: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")},
|
||||
expected: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("-100m")},
|
||||
},
|
||||
"value-value": {
|
||||
a: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("200m")},
|
||||
b: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")},
|
||||
expected: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")},
|
||||
},
|
||||
}
|
||||
for testName, testCase := range testCases {
|
||||
sub := Subtract(testCase.a, testCase.b)
|
||||
if result := Equals(testCase.expected, sub); !result {
|
||||
t.Errorf("%s expected: %v, actual: %v", testName, testCase.expected, sub)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceNames(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
a corev1.ResourceList
|
||||
expected []corev1.ResourceName
|
||||
}{
|
||||
"empty": {
|
||||
a: corev1.ResourceList{},
|
||||
expected: []corev1.ResourceName{},
|
||||
},
|
||||
"values": {
|
||||
a: corev1.ResourceList{
|
||||
corev1.ResourceCPU: resource.MustParse("100m"),
|
||||
corev1.ResourceMemory: resource.MustParse("1Gi"),
|
||||
},
|
||||
expected: []corev1.ResourceName{corev1.ResourceMemory, corev1.ResourceCPU},
|
||||
},
|
||||
}
|
||||
for testName, testCase := range testCases {
|
||||
actualSet := ToSet(ResourceNames(testCase.a))
|
||||
expectedSet := ToSet(testCase.expected)
|
||||
if !actualSet.Equal(expectedSet) {
|
||||
t.Errorf("%s expected: %v, actual: %v", testName, expectedSet, actualSet)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestContains(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
a []corev1.ResourceName
|
||||
b corev1.ResourceName
|
||||
expected bool
|
||||
}{
|
||||
"does-not-contain": {
|
||||
a: []corev1.ResourceName{corev1.ResourceMemory},
|
||||
b: corev1.ResourceCPU,
|
||||
expected: false,
|
||||
},
|
||||
"does-contain": {
|
||||
a: []corev1.ResourceName{corev1.ResourceMemory, corev1.ResourceCPU},
|
||||
b: corev1.ResourceCPU,
|
||||
expected: true,
|
||||
},
|
||||
}
|
||||
for testName, testCase := range testCases {
|
||||
if actual := Contains(testCase.a, testCase.b); actual != testCase.expected {
|
||||
t.Errorf("%s expected: %v, actual: %v", testName, testCase.expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainsPrefix(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
a []string
|
||||
b corev1.ResourceName
|
||||
expected bool
|
||||
}{
|
||||
"does-not-contain": {
|
||||
a: []string{corev1.ResourceHugePagesPrefix},
|
||||
b: corev1.ResourceCPU,
|
||||
expected: false,
|
||||
},
|
||||
"does-contain": {
|
||||
a: []string{corev1.ResourceHugePagesPrefix},
|
||||
b: corev1.ResourceName(corev1.ResourceHugePagesPrefix + "2Mi"),
|
||||
expected: true,
|
||||
},
|
||||
}
|
||||
for testName, testCase := range testCases {
|
||||
if actual := ContainsPrefix(testCase.a, testCase.b); actual != testCase.expected {
|
||||
t.Errorf("%s expected: %v, actual: %v", testName, testCase.expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsZero(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
a corev1.ResourceList
|
||||
expected bool
|
||||
}{
|
||||
"empty": {
|
||||
a: corev1.ResourceList{},
|
||||
expected: true,
|
||||
},
|
||||
"zero": {
|
||||
a: corev1.ResourceList{
|
||||
corev1.ResourceCPU: resource.MustParse("0"),
|
||||
corev1.ResourceMemory: resource.MustParse("0"),
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
"non-zero": {
|
||||
a: corev1.ResourceList{
|
||||
corev1.ResourceCPU: resource.MustParse("200m"),
|
||||
corev1.ResourceMemory: resource.MustParse("1Gi"),
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
for testName, testCase := range testCases {
|
||||
if result := IsZero(testCase.a); result != testCase.expected {
|
||||
t.Errorf("%s expected: %v, actual: %v", testName, testCase.expected, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsNegative(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
a corev1.ResourceList
|
||||
expected []corev1.ResourceName
|
||||
}{
|
||||
"empty": {
|
||||
a: corev1.ResourceList{},
|
||||
expected: []corev1.ResourceName{},
|
||||
},
|
||||
"some-negative": {
|
||||
a: corev1.ResourceList{
|
||||
corev1.ResourceCPU: resource.MustParse("-10"),
|
||||
corev1.ResourceMemory: resource.MustParse("0"),
|
||||
},
|
||||
expected: []corev1.ResourceName{corev1.ResourceCPU},
|
||||
},
|
||||
"all-negative": {
|
||||
a: corev1.ResourceList{
|
||||
corev1.ResourceCPU: resource.MustParse("-200m"),
|
||||
corev1.ResourceMemory: resource.MustParse("-1Gi"),
|
||||
},
|
||||
expected: []corev1.ResourceName{corev1.ResourceCPU, corev1.ResourceMemory},
|
||||
},
|
||||
}
|
||||
for testName, testCase := range testCases {
|
||||
actual := IsNegative(testCase.a)
|
||||
actualSet := ToSet(actual)
|
||||
expectedSet := ToSet(testCase.expected)
|
||||
if !actualSet.Equal(expectedSet) {
|
||||
t.Errorf("%s expected: %v, actual: %v", testName, expectedSet, actualSet)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntersection(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
a []corev1.ResourceName
|
||||
b []corev1.ResourceName
|
||||
expected []corev1.ResourceName
|
||||
}{
|
||||
"empty": {
|
||||
a: []corev1.ResourceName{},
|
||||
b: []corev1.ResourceName{},
|
||||
expected: []corev1.ResourceName{},
|
||||
},
|
||||
"equal": {
|
||||
a: []corev1.ResourceName{corev1.ResourceCPU, corev1.ResourceMemory},
|
||||
b: []corev1.ResourceName{corev1.ResourceCPU, corev1.ResourceMemory},
|
||||
expected: []corev1.ResourceName{corev1.ResourceCPU, corev1.ResourceMemory},
|
||||
},
|
||||
"a has extra": {
|
||||
a: []corev1.ResourceName{corev1.ResourceCPU, corev1.ResourceMemory},
|
||||
b: []corev1.ResourceName{corev1.ResourceCPU},
|
||||
expected: []corev1.ResourceName{corev1.ResourceCPU},
|
||||
},
|
||||
"b has extra": {
|
||||
a: []corev1.ResourceName{corev1.ResourceCPU},
|
||||
b: []corev1.ResourceName{corev1.ResourceCPU, corev1.ResourceMemory},
|
||||
expected: []corev1.ResourceName{corev1.ResourceCPU},
|
||||
},
|
||||
"dedupes": {
|
||||
a: []corev1.ResourceName{corev1.ResourceCPU, corev1.ResourceCPU, corev1.ResourceMemory, corev1.ResourceMemory},
|
||||
b: []corev1.ResourceName{corev1.ResourceCPU},
|
||||
expected: []corev1.ResourceName{corev1.ResourceCPU},
|
||||
},
|
||||
"sorts": {
|
||||
a: []corev1.ResourceName{corev1.ResourceMemory, corev1.ResourceMemory, corev1.ResourceCPU, corev1.ResourceCPU},
|
||||
b: []corev1.ResourceName{corev1.ResourceMemory, corev1.ResourceMemory, corev1.ResourceCPU, corev1.ResourceCPU},
|
||||
expected: []corev1.ResourceName{corev1.ResourceCPU, corev1.ResourceMemory},
|
||||
},
|
||||
}
|
||||
for testName, testCase := range testCases {
|
||||
actual := Intersection(testCase.a, testCase.b)
|
||||
if !reflect.DeepEqual(actual, testCase.expected) {
|
||||
t.Errorf("%s expected: %#v, actual: %#v", testName, testCase.expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDifference(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
a []corev1.ResourceName
|
||||
b []corev1.ResourceName
|
||||
expected []corev1.ResourceName
|
||||
}{
|
||||
"empty": {
|
||||
a: []corev1.ResourceName{},
|
||||
b: []corev1.ResourceName{},
|
||||
expected: []corev1.ResourceName{},
|
||||
},
|
||||
"equal": {
|
||||
a: []corev1.ResourceName{corev1.ResourceCPU, corev1.ResourceMemory},
|
||||
b: []corev1.ResourceName{corev1.ResourceCPU, corev1.ResourceMemory},
|
||||
expected: []corev1.ResourceName{},
|
||||
},
|
||||
"a has extra": {
|
||||
a: []corev1.ResourceName{corev1.ResourceCPU, corev1.ResourceMemory},
|
||||
b: []corev1.ResourceName{corev1.ResourceCPU},
|
||||
expected: []corev1.ResourceName{corev1.ResourceMemory},
|
||||
},
|
||||
"b has extra": {
|
||||
a: []corev1.ResourceName{corev1.ResourceCPU},
|
||||
b: []corev1.ResourceName{corev1.ResourceCPU, corev1.ResourceMemory},
|
||||
expected: []corev1.ResourceName{},
|
||||
},
|
||||
"dedupes": {
|
||||
a: []corev1.ResourceName{corev1.ResourceCPU, corev1.ResourceCPU, corev1.ResourceMemory, corev1.ResourceMemory},
|
||||
b: []corev1.ResourceName{corev1.ResourceCPU},
|
||||
expected: []corev1.ResourceName{corev1.ResourceMemory},
|
||||
},
|
||||
"sorts": {
|
||||
a: []corev1.ResourceName{corev1.ResourceMemory, corev1.ResourceMemory, corev1.ResourceCPU, corev1.ResourceCPU},
|
||||
b: []corev1.ResourceName{},
|
||||
expected: []corev1.ResourceName{corev1.ResourceCPU, corev1.ResourceMemory},
|
||||
},
|
||||
}
|
||||
for testName, testCase := range testCases {
|
||||
actual := Difference(testCase.a, testCase.b)
|
||||
if !reflect.DeepEqual(actual, testCase.expected) {
|
||||
t.Errorf("%s expected: %#v, actual: %#v", testName, testCase.expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user