Commit ec3b0886 authored by yue9944882's avatar yue9944882

directly switching quota pkg

parent 48dd084a
package(default_visibility = ["//visibility:public"]) 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",
deps = [
"//pkg/apis/core: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/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema: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 = [
"//pkg/apis/core:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
],
)
filegroup( filegroup(
name = "package-srcs", name = "package-srcs",
srcs = glob(["**"]), srcs = glob(["**"]),
...@@ -46,9 +11,7 @@ filegroup( ...@@ -46,9 +11,7 @@ filegroup(
name = "all-srcs", name = "all-srcs",
srcs = [ srcs = [
":package-srcs", ":package-srcs",
"//pkg/quota/evaluator/core:all-srcs", "//pkg/quota/v1:all-srcs",
"//pkg/quota/generic:all-srcs",
"//pkg/quota/install:all-srcs",
], ],
tags = ["automanaged"], tags = ["automanaged"],
) )
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/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(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//pkg/quota/v1/evaluator/core:all-srcs",
"//pkg/quota/v1/generic:all-srcs",
"//pkg/quota/v1/install:all-srcs",
],
tags = ["automanaged"],
)
...@@ -15,16 +15,16 @@ go_library( ...@@ -15,16 +15,16 @@ go_library(
"registry.go", "registry.go",
"services.go", "services.go",
], ],
importpath = "k8s.io/kubernetes/pkg/quota/evaluator/core", importpath = "k8s.io/kubernetes/pkg/quota/v1/evaluator/core",
deps = [ deps = [
"//pkg/apis/core:go_default_library", "//pkg/apis/core:go_default_library",
"//pkg/apis/core/helper:go_default_library",
"//pkg/apis/core/helper/qos:go_default_library",
"//pkg/apis/core/v1:go_default_library", "//pkg/apis/core/v1:go_default_library",
"//pkg/apis/core/v1/helper:go_default_library",
"//pkg/apis/core/v1/helper/qos:go_default_library",
"//pkg/features:go_default_library", "//pkg/features:go_default_library",
"//pkg/kubeapiserver/admission/util:go_default_library", "//pkg/kubeapiserver/admission/util:go_default_library",
"//pkg/quota:go_default_library", "//pkg/quota/v1:go_default_library",
"//pkg/quota/generic:go_default_library", "//pkg/quota/v1/generic:go_default_library",
"//staging/src/k8s.io/api/core/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/api/resource:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
...@@ -50,9 +50,10 @@ go_test( ...@@ -50,9 +50,10 @@ go_test(
embed = [":go_default_library"], embed = [":go_default_library"],
deps = [ deps = [
"//pkg/apis/core:go_default_library", "//pkg/apis/core:go_default_library",
"//pkg/quota:go_default_library", "//pkg/quota/v1:go_default_library",
"//pkg/quota/generic:go_default_library", "//pkg/quota/v1/generic:go_default_library",
"//pkg/util/node: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/api/resource:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1: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/runtime/schema:go_default_library",
......
...@@ -15,4 +15,4 @@ limitations under the License. ...@@ -15,4 +15,4 @@ limitations under the License.
*/ */
// core contains modules that interface with the core api group // core contains modules that interface with the core api group
package core // import "k8s.io/kubernetes/pkg/quota/evaluator/core" package core // import "k8s.io/kubernetes/pkg/quota/v1/evaluator/core"
...@@ -20,7 +20,7 @@ import ( ...@@ -20,7 +20,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
...@@ -30,22 +30,22 @@ import ( ...@@ -30,22 +30,22 @@ import (
"k8s.io/apiserver/pkg/features" "k8s.io/apiserver/pkg/features"
utilfeature "k8s.io/apiserver/pkg/util/feature" utilfeature "k8s.io/apiserver/pkg/util/feature"
api "k8s.io/kubernetes/pkg/apis/core" api "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/apis/core/helper"
k8s_api_v1 "k8s.io/kubernetes/pkg/apis/core/v1" k8s_api_v1 "k8s.io/kubernetes/pkg/apis/core/v1"
"k8s.io/kubernetes/pkg/apis/core/v1/helper"
k8sfeatures "k8s.io/kubernetes/pkg/features" k8sfeatures "k8s.io/kubernetes/pkg/features"
"k8s.io/kubernetes/pkg/kubeapiserver/admission/util" "k8s.io/kubernetes/pkg/kubeapiserver/admission/util"
"k8s.io/kubernetes/pkg/quota" quota "k8s.io/kubernetes/pkg/quota/v1"
"k8s.io/kubernetes/pkg/quota/generic" "k8s.io/kubernetes/pkg/quota/v1/generic"
) )
// the name used for object count quota // the name used for object count quota
var pvcObjectCountName = generic.ObjectCountQuotaResourceNameFor(v1.SchemeGroupVersion.WithResource("persistentvolumeclaims").GroupResource()) var pvcObjectCountName = generic.ObjectCountQuotaResourceNameFor(corev1.SchemeGroupVersion.WithResource("persistentvolumeclaims").GroupResource())
// pvcResources are the set of static resources managed by quota associated with pvcs. // pvcResources are the set of static resources managed by quota associated with pvcs.
// for each resource in this list, it may be refined dynamically based on storage class. // for each resource in this list, it may be refined dynamically based on storage class.
var pvcResources = []api.ResourceName{ var pvcResources = []corev1.ResourceName{
api.ResourcePersistentVolumeClaims, corev1.ResourcePersistentVolumeClaims,
api.ResourceRequestsStorage, corev1.ResourceRequestsStorage,
} }
// storageClassSuffix is the suffix to the qualified portion of storage class resource name. // storageClassSuffix is the suffix to the qualified portion of storage class resource name.
...@@ -56,19 +56,21 @@ var pvcResources = []api.ResourceName{ ...@@ -56,19 +56,21 @@ var pvcResources = []api.ResourceName{
// * bronze.storageclass.storage.k8s.io/requests.storage: 500Gi // * bronze.storageclass.storage.k8s.io/requests.storage: 500Gi
const storageClassSuffix string = ".storageclass.storage.k8s.io/" const storageClassSuffix string = ".storageclass.storage.k8s.io/"
/* TODO: prune?
// ResourceByStorageClass returns a quota resource name by storage class. // ResourceByStorageClass returns a quota resource name by storage class.
func ResourceByStorageClass(storageClass string, resourceName api.ResourceName) api.ResourceName { func ResourceByStorageClass(storageClass string, resourceName corev1.ResourceName) corev1.ResourceName {
return api.ResourceName(string(storageClass + storageClassSuffix + string(resourceName))) return corev1.ResourceName(string(storageClass + storageClassSuffix + string(resourceName)))
} }
*/
// V1ResourceByStorageClass returns a quota resource name by storage class. // V1ResourceByStorageClass returns a quota resource name by storage class.
func V1ResourceByStorageClass(storageClass string, resourceName v1.ResourceName) v1.ResourceName { func V1ResourceByStorageClass(storageClass string, resourceName corev1.ResourceName) corev1.ResourceName {
return v1.ResourceName(string(storageClass + storageClassSuffix + string(resourceName))) return corev1.ResourceName(string(storageClass + storageClassSuffix + string(resourceName)))
} }
// NewPersistentVolumeClaimEvaluator returns an evaluator that can evaluate persistent volume claims // NewPersistentVolumeClaimEvaluator returns an evaluator that can evaluate persistent volume claims
func NewPersistentVolumeClaimEvaluator(f quota.ListerForResourceFunc) quota.Evaluator { func NewPersistentVolumeClaimEvaluator(f quota.ListerForResourceFunc) quota.Evaluator {
listFuncByNamespace := generic.ListResourceUsingListerFunc(f, v1.SchemeGroupVersion.WithResource("persistentvolumeclaims")) listFuncByNamespace := generic.ListResourceUsingListerFunc(f, corev1.SchemeGroupVersion.WithResource("persistentvolumeclaims"))
pvcEvaluator := &pvcEvaluator{listFuncByNamespace: listFuncByNamespace} pvcEvaluator := &pvcEvaluator{listFuncByNamespace: listFuncByNamespace}
return pvcEvaluator return pvcEvaluator
} }
...@@ -80,14 +82,14 @@ type pvcEvaluator struct { ...@@ -80,14 +82,14 @@ type pvcEvaluator struct {
} }
// Constraints verifies that all required resources are present on the item. // Constraints verifies that all required resources are present on the item.
func (p *pvcEvaluator) Constraints(required []api.ResourceName, item runtime.Object) error { func (p *pvcEvaluator) Constraints(required []corev1.ResourceName, item runtime.Object) error {
// no-op for persistent volume claims // no-op for persistent volume claims
return nil return nil
} }
// GroupResource that this evaluator tracks // GroupResource that this evaluator tracks
func (p *pvcEvaluator) GroupResource() schema.GroupResource { func (p *pvcEvaluator) GroupResource() schema.GroupResource {
return v1.SchemeGroupVersion.WithResource("persistentvolumeclaims").GroupResource() return corev1.SchemeGroupVersion.WithResource("persistentvolumeclaims").GroupResource()
} }
// Handles returns true if the evaluator should handle the specified operation. // Handles returns true if the evaluator should handle the specified operation.
...@@ -119,27 +121,27 @@ func (p *pvcEvaluator) Handles(a admission.Attributes) bool { ...@@ -119,27 +121,27 @@ func (p *pvcEvaluator) Handles(a admission.Attributes) bool {
} }
// Matches returns true if the evaluator matches the specified quota with the provided input item // Matches returns true if the evaluator matches the specified quota with the provided input item
func (p *pvcEvaluator) Matches(resourceQuota *api.ResourceQuota, item runtime.Object) (bool, error) { func (p *pvcEvaluator) Matches(resourceQuota *corev1.ResourceQuota, item runtime.Object) (bool, error) {
return generic.Matches(resourceQuota, item, p.MatchingResources, generic.MatchesNoScopeFunc) return generic.Matches(resourceQuota, item, p.MatchingResources, generic.MatchesNoScopeFunc)
} }
// MatchingScopes takes the input specified list of scopes and input object. Returns the set of scopes resource matches. // MatchingScopes takes the input specified list of scopes and input object. Returns the set of scopes resource matches.
func (p *pvcEvaluator) MatchingScopes(item runtime.Object, scopes []api.ScopedResourceSelectorRequirement) ([]api.ScopedResourceSelectorRequirement, error) { func (p *pvcEvaluator) MatchingScopes(item runtime.Object, scopes []corev1.ScopedResourceSelectorRequirement) ([]corev1.ScopedResourceSelectorRequirement, error) {
return []api.ScopedResourceSelectorRequirement{}, nil return []corev1.ScopedResourceSelectorRequirement{}, nil
} }
// UncoveredQuotaScopes takes the input matched scopes which are limited by configuration and the matched quota scopes. // 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 // It returns the scopes which are in limited scopes but dont have a corresponding covering quota scope
func (p *pvcEvaluator) UncoveredQuotaScopes(limitedScopes []api.ScopedResourceSelectorRequirement, matchedQuotaScopes []api.ScopedResourceSelectorRequirement) ([]api.ScopedResourceSelectorRequirement, error) { func (p *pvcEvaluator) UncoveredQuotaScopes(limitedScopes []corev1.ScopedResourceSelectorRequirement, matchedQuotaScopes []corev1.ScopedResourceSelectorRequirement) ([]corev1.ScopedResourceSelectorRequirement, error) {
return []api.ScopedResourceSelectorRequirement{}, nil return []corev1.ScopedResourceSelectorRequirement{}, nil
} }
// MatchingResources takes the input specified list of resources and returns the set of resources it matches. // MatchingResources takes the input specified list of resources and returns the set of resources it matches.
func (p *pvcEvaluator) MatchingResources(items []api.ResourceName) []api.ResourceName { func (p *pvcEvaluator) MatchingResources(items []corev1.ResourceName) []corev1.ResourceName {
result := []api.ResourceName{} result := []corev1.ResourceName{}
for _, item := range items { for _, item := range items {
// match object count quota fields // match object count quota fields
if quota.Contains([]api.ResourceName{pvcObjectCountName}, item) { if quota.Contains([]corev1.ResourceName{pvcObjectCountName}, item) {
result = append(result, item) result = append(result, item)
continue continue
} }
...@@ -161,15 +163,15 @@ func (p *pvcEvaluator) MatchingResources(items []api.ResourceName) []api.Resourc ...@@ -161,15 +163,15 @@ func (p *pvcEvaluator) MatchingResources(items []api.ResourceName) []api.Resourc
} }
// Usage knows how to measure usage associated with item. // Usage knows how to measure usage associated with item.
func (p *pvcEvaluator) Usage(item runtime.Object) (api.ResourceList, error) { func (p *pvcEvaluator) Usage(item runtime.Object) (corev1.ResourceList, error) {
result := api.ResourceList{} result := corev1.ResourceList{}
pvc, err := toInternalPersistentVolumeClaimOrError(item) pvc, err := toExternalPersistentVolumeClaimOrError(item)
if err != nil { if err != nil {
return result, err return result, err
} }
// charge for claim // charge for claim
result[api.ResourcePersistentVolumeClaims] = *(resource.NewQuantity(1, resource.DecimalSI)) result[corev1.ResourcePersistentVolumeClaims] = *(resource.NewQuantity(1, resource.DecimalSI))
result[pvcObjectCountName] = *(resource.NewQuantity(1, resource.DecimalSI)) result[pvcObjectCountName] = *(resource.NewQuantity(1, resource.DecimalSI))
if utilfeature.DefaultFeatureGate.Enabled(features.Initializers) { if utilfeature.DefaultFeatureGate.Enabled(features.Initializers) {
if !initialization.IsInitialized(pvc.Initializers) { if !initialization.IsInitialized(pvc.Initializers) {
...@@ -179,16 +181,16 @@ func (p *pvcEvaluator) Usage(item runtime.Object) (api.ResourceList, error) { ...@@ -179,16 +181,16 @@ func (p *pvcEvaluator) Usage(item runtime.Object) (api.ResourceList, error) {
} }
storageClassRef := helper.GetPersistentVolumeClaimClass(pvc) storageClassRef := helper.GetPersistentVolumeClaimClass(pvc)
if len(storageClassRef) > 0 { if len(storageClassRef) > 0 {
storageClassClaim := api.ResourceName(storageClassRef + storageClassSuffix + string(api.ResourcePersistentVolumeClaims)) storageClassClaim := corev1.ResourceName(storageClassRef + storageClassSuffix + string(corev1.ResourcePersistentVolumeClaims))
result[storageClassClaim] = *(resource.NewQuantity(1, resource.DecimalSI)) result[storageClassClaim] = *(resource.NewQuantity(1, resource.DecimalSI))
} }
// charge for storage // charge for storage
if request, found := pvc.Spec.Resources.Requests[api.ResourceStorage]; found { if request, found := pvc.Spec.Resources.Requests[corev1.ResourceStorage]; found {
result[api.ResourceRequestsStorage] = request result[corev1.ResourceRequestsStorage] = request
// charge usage to the storage class (if present) // charge usage to the storage class (if present)
if len(storageClassRef) > 0 { if len(storageClassRef) > 0 {
storageClassStorage := api.ResourceName(storageClassRef + storageClassSuffix + string(api.ResourceRequestsStorage)) storageClassStorage := corev1.ResourceName(storageClassRef + storageClassSuffix + string(corev1.ResourceRequestsStorage))
result[storageClassStorage] = request result[storageClassStorage] = request
} }
} }
...@@ -203,15 +205,15 @@ func (p *pvcEvaluator) UsageStats(options quota.UsageStatsOptions) (quota.UsageS ...@@ -203,15 +205,15 @@ func (p *pvcEvaluator) UsageStats(options quota.UsageStatsOptions) (quota.UsageS
// ensure we implement required interface // ensure we implement required interface
var _ quota.Evaluator = &pvcEvaluator{} var _ quota.Evaluator = &pvcEvaluator{}
func toInternalPersistentVolumeClaimOrError(obj runtime.Object) (*api.PersistentVolumeClaim, error) { func toExternalPersistentVolumeClaimOrError(obj runtime.Object) (*corev1.PersistentVolumeClaim, error) {
pvc := &api.PersistentVolumeClaim{} pvc := &corev1.PersistentVolumeClaim{}
switch t := obj.(type) { switch t := obj.(type) {
case *v1.PersistentVolumeClaim: case *corev1.PersistentVolumeClaim:
if err := k8s_api_v1.Convert_v1_PersistentVolumeClaim_To_core_PersistentVolumeClaim(t, pvc, nil); err != nil { pvc = t
case *api.PersistentVolumeClaim:
if err := k8s_api_v1.Convert_core_PersistentVolumeClaim_To_v1_PersistentVolumeClaim(t, pvc, nil); err != nil {
return nil, err return nil, err
} }
case *api.PersistentVolumeClaim:
pvc = t
default: default:
return nil, fmt.Errorf("expect *api.PersistentVolumeClaim or *v1.PersistentVolumeClaim, got %v", t) return nil, fmt.Errorf("expect *api.PersistentVolumeClaim or *v1.PersistentVolumeClaim, got %v", t)
} }
......
...@@ -19,12 +19,13 @@ package core ...@@ -19,12 +19,13 @@ package core
import ( import (
"testing" "testing"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
api "k8s.io/kubernetes/pkg/apis/core" api "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/quota" quota "k8s.io/kubernetes/pkg/quota/v1"
"k8s.io/kubernetes/pkg/quota/generic" "k8s.io/kubernetes/pkg/quota/v1/generic"
) )
func testVolumeClaim(name string, namespace string, spec api.PersistentVolumeClaimSpec) *api.PersistentVolumeClaim { func testVolumeClaim(name string, namespace string, spec api.PersistentVolumeClaimSpec) *api.PersistentVolumeClaim {
...@@ -79,23 +80,23 @@ func TestPersistentVolumeClaimEvaluatorUsage(t *testing.T) { ...@@ -79,23 +80,23 @@ func TestPersistentVolumeClaimEvaluatorUsage(t *testing.T) {
evaluator := NewPersistentVolumeClaimEvaluator(nil) evaluator := NewPersistentVolumeClaimEvaluator(nil)
testCases := map[string]struct { testCases := map[string]struct {
pvc *api.PersistentVolumeClaim pvc *api.PersistentVolumeClaim
usage api.ResourceList usage corev1.ResourceList
}{ }{
"pvc-usage": { "pvc-usage": {
pvc: validClaim, pvc: validClaim,
usage: api.ResourceList{ usage: corev1.ResourceList{
api.ResourceRequestsStorage: resource.MustParse("10Gi"), corev1.ResourceRequestsStorage: resource.MustParse("10Gi"),
api.ResourcePersistentVolumeClaims: resource.MustParse("1"), corev1.ResourcePersistentVolumeClaims: resource.MustParse("1"),
generic.ObjectCountQuotaResourceNameFor(schema.GroupResource{Resource: "persistentvolumeclaims"}): resource.MustParse("1"), generic.ObjectCountQuotaResourceNameFor(schema.GroupResource{Resource: "persistentvolumeclaims"}): resource.MustParse("1"),
}, },
}, },
"pvc-usage-by-class": { "pvc-usage-by-class": {
pvc: validClaimByStorageClass, pvc: validClaimByStorageClass,
usage: api.ResourceList{ usage: corev1.ResourceList{
api.ResourceRequestsStorage: resource.MustParse("10Gi"), corev1.ResourceRequestsStorage: resource.MustParse("10Gi"),
api.ResourcePersistentVolumeClaims: resource.MustParse("1"), corev1.ResourcePersistentVolumeClaims: resource.MustParse("1"),
ResourceByStorageClass(classGold, api.ResourceRequestsStorage): resource.MustParse("10Gi"), V1ResourceByStorageClass(classGold, corev1.ResourceRequestsStorage): resource.MustParse("10Gi"),
ResourceByStorageClass(classGold, api.ResourcePersistentVolumeClaims): resource.MustParse("1"), V1ResourceByStorageClass(classGold, corev1.ResourcePersistentVolumeClaims): resource.MustParse("1"),
generic.ObjectCountQuotaResourceNameFor(schema.GroupResource{Resource: "persistentvolumeclaims"}): resource.MustParse("1"), generic.ObjectCountQuotaResourceNameFor(schema.GroupResource{Resource: "persistentvolumeclaims"}): resource.MustParse("1"),
}, },
}, },
......
...@@ -17,20 +17,19 @@ limitations under the License. ...@@ -17,20 +17,19 @@ limitations under the License.
package core package core
import ( import (
"k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/clock" "k8s.io/apimachinery/pkg/util/clock"
api "k8s.io/kubernetes/pkg/apis/core" quota "k8s.io/kubernetes/pkg/quota/v1"
"k8s.io/kubernetes/pkg/quota" "k8s.io/kubernetes/pkg/quota/v1/generic"
"k8s.io/kubernetes/pkg/quota/generic"
) )
// legacyObjectCountAliases are what we used to do simple object counting quota with mapped to alias // legacyObjectCountAliases are what we used to do simple object counting quota with mapped to alias
var legacyObjectCountAliases = map[schema.GroupVersionResource]api.ResourceName{ var legacyObjectCountAliases = map[schema.GroupVersionResource]corev1.ResourceName{
v1.SchemeGroupVersion.WithResource("configmaps"): api.ResourceConfigMaps, corev1.SchemeGroupVersion.WithResource("configmaps"): corev1.ResourceConfigMaps,
v1.SchemeGroupVersion.WithResource("resourcequotas"): api.ResourceQuotas, corev1.SchemeGroupVersion.WithResource("resourcequotas"): corev1.ResourceQuotas,
v1.SchemeGroupVersion.WithResource("replicationcontrollers"): api.ResourceReplicationControllers, corev1.SchemeGroupVersion.WithResource("replicationcontrollers"): corev1.ResourceReplicationControllers,
v1.SchemeGroupVersion.WithResource("secrets"): api.ResourceSecrets, corev1.SchemeGroupVersion.WithResource("secrets"): corev1.ResourceSecrets,
} }
// NewEvaluators returns the list of static evaluators that manage more than counts // NewEvaluators returns the list of static evaluators that manage more than counts
......
...@@ -19,31 +19,31 @@ package core ...@@ -19,31 +19,31 @@ package core
import ( import (
"fmt" "fmt"
"k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/admission" "k8s.io/apiserver/pkg/admission"
api "k8s.io/kubernetes/pkg/apis/core" api "k8s.io/kubernetes/pkg/apis/core"
k8s_api_v1 "k8s.io/kubernetes/pkg/apis/core/v1" k8s_api_v1 "k8s.io/kubernetes/pkg/apis/core/v1"
"k8s.io/kubernetes/pkg/quota" "k8s.io/kubernetes/pkg/quota/v1"
"k8s.io/kubernetes/pkg/quota/generic" "k8s.io/kubernetes/pkg/quota/v1/generic"
) )
// the name used for object count quota // the name used for object count quota
var serviceObjectCountName = generic.ObjectCountQuotaResourceNameFor(v1.SchemeGroupVersion.WithResource("services").GroupResource()) var serviceObjectCountName = generic.ObjectCountQuotaResourceNameFor(corev1.SchemeGroupVersion.WithResource("services").GroupResource())
// serviceResources are the set of resources managed by quota associated with services. // serviceResources are the set of resources managed by quota associated with services.
var serviceResources = []api.ResourceName{ var serviceResources = []corev1.ResourceName{
serviceObjectCountName, serviceObjectCountName,
api.ResourceServices, corev1.ResourceServices,
api.ResourceServicesNodePorts, corev1.ResourceServicesNodePorts,
api.ResourceServicesLoadBalancers, corev1.ResourceServicesLoadBalancers,
} }
// NewServiceEvaluator returns an evaluator that can evaluate services. // NewServiceEvaluator returns an evaluator that can evaluate services.
func NewServiceEvaluator(f quota.ListerForResourceFunc) quota.Evaluator { func NewServiceEvaluator(f quota.ListerForResourceFunc) quota.Evaluator {
listFuncByNamespace := generic.ListResourceUsingListerFunc(f, v1.SchemeGroupVersion.WithResource("services")) listFuncByNamespace := generic.ListResourceUsingListerFunc(f, corev1.SchemeGroupVersion.WithResource("services"))
serviceEvaluator := &serviceEvaluator{listFuncByNamespace: listFuncByNamespace} serviceEvaluator := &serviceEvaluator{listFuncByNamespace: listFuncByNamespace}
return serviceEvaluator return serviceEvaluator
} }
...@@ -55,14 +55,14 @@ type serviceEvaluator struct { ...@@ -55,14 +55,14 @@ type serviceEvaluator struct {
} }
// Constraints verifies that all required resources are present on the item // Constraints verifies that all required resources are present on the item
func (p *serviceEvaluator) Constraints(required []api.ResourceName, item runtime.Object) error { func (p *serviceEvaluator) Constraints(required []corev1.ResourceName, item runtime.Object) error {
// this is a no-op for services // this is a no-op for services
return nil return nil
} }
// GroupResource that this evaluator tracks // GroupResource that this evaluator tracks
func (p *serviceEvaluator) GroupResource() schema.GroupResource { func (p *serviceEvaluator) GroupResource() schema.GroupResource {
return v1.SchemeGroupVersion.WithResource("services").GroupResource() return corev1.SchemeGroupVersion.WithResource("services").GroupResource()
} }
// Handles returns true of the evaluator should handle the specified operation. // Handles returns true of the evaluator should handle the specified operation.
...@@ -73,36 +73,36 @@ func (p *serviceEvaluator) Handles(a admission.Attributes) bool { ...@@ -73,36 +73,36 @@ func (p *serviceEvaluator) Handles(a admission.Attributes) bool {
} }
// Matches returns true if the evaluator matches the specified quota with the provided input item // Matches returns true if the evaluator matches the specified quota with the provided input item
func (p *serviceEvaluator) Matches(resourceQuota *api.ResourceQuota, item runtime.Object) (bool, error) { func (p *serviceEvaluator) Matches(resourceQuota *corev1.ResourceQuota, item runtime.Object) (bool, error) {
return generic.Matches(resourceQuota, item, p.MatchingResources, generic.MatchesNoScopeFunc) return generic.Matches(resourceQuota, item, p.MatchingResources, generic.MatchesNoScopeFunc)
} }
// MatchingResources takes the input specified list of resources and returns the set of resources it matches. // MatchingResources takes the input specified list of resources and returns the set of resources it matches.
func (p *serviceEvaluator) MatchingResources(input []api.ResourceName) []api.ResourceName { func (p *serviceEvaluator) MatchingResources(input []corev1.ResourceName) []corev1.ResourceName {
return quota.Intersection(input, serviceResources) return quota.Intersection(input, serviceResources)
} }
// MatchingScopes takes the input specified list of scopes and input object. Returns the set of scopes resource matches. // MatchingScopes takes the input specified list of scopes and input object. Returns the set of scopes resource matches.
func (p *serviceEvaluator) MatchingScopes(item runtime.Object, scopes []api.ScopedResourceSelectorRequirement) ([]api.ScopedResourceSelectorRequirement, error) { func (p *serviceEvaluator) MatchingScopes(item runtime.Object, scopes []corev1.ScopedResourceSelectorRequirement) ([]corev1.ScopedResourceSelectorRequirement, error) {
return []api.ScopedResourceSelectorRequirement{}, nil return []corev1.ScopedResourceSelectorRequirement{}, nil
} }
// UncoveredQuotaScopes takes the input matched scopes which are limited by configuration and the matched quota scopes. // 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 // It returns the scopes which are in limited scopes but dont have a corresponding covering quota scope
func (p *serviceEvaluator) UncoveredQuotaScopes(limitedScopes []api.ScopedResourceSelectorRequirement, matchedQuotaScopes []api.ScopedResourceSelectorRequirement) ([]api.ScopedResourceSelectorRequirement, error) { func (p *serviceEvaluator) UncoveredQuotaScopes(limitedScopes []corev1.ScopedResourceSelectorRequirement, matchedQuotaScopes []corev1.ScopedResourceSelectorRequirement) ([]corev1.ScopedResourceSelectorRequirement, error) {
return []api.ScopedResourceSelectorRequirement{}, nil return []corev1.ScopedResourceSelectorRequirement{}, nil
} }
// convert the input object to an internal service object or error. // convert the input object to an internal service object or error.
func toInternalServiceOrError(obj runtime.Object) (*api.Service, error) { func toExternalServiceOrError(obj runtime.Object) (*corev1.Service, error) {
svc := &api.Service{} svc := &corev1.Service{}
switch t := obj.(type) { switch t := obj.(type) {
case *v1.Service: case *corev1.Service:
if err := k8s_api_v1.Convert_v1_Service_To_core_Service(t, svc, nil); err != nil { svc = t
case *api.Service:
if err := k8s_api_v1.Convert_core_Service_To_v1_Service(t, svc, nil); err != nil {
return nil, err return nil, err
} }
case *api.Service:
svc = t
default: default:
return nil, fmt.Errorf("expect *api.Service or *v1.Service, got %v", t) return nil, fmt.Errorf("expect *api.Service or *v1.Service, got %v", t)
} }
...@@ -110,28 +110,28 @@ func toInternalServiceOrError(obj runtime.Object) (*api.Service, error) { ...@@ -110,28 +110,28 @@ func toInternalServiceOrError(obj runtime.Object) (*api.Service, error) {
} }
// Usage knows how to measure usage associated with services // Usage knows how to measure usage associated with services
func (p *serviceEvaluator) Usage(item runtime.Object) (api.ResourceList, error) { func (p *serviceEvaluator) Usage(item runtime.Object) (corev1.ResourceList, error) {
result := api.ResourceList{} result := corev1.ResourceList{}
svc, err := toInternalServiceOrError(item) svc, err := toExternalServiceOrError(item)
if err != nil { if err != nil {
return result, err return result, err
} }
ports := len(svc.Spec.Ports) ports := len(svc.Spec.Ports)
// default service usage // default service usage
result[serviceObjectCountName] = *(resource.NewQuantity(1, resource.DecimalSI)) result[serviceObjectCountName] = *(resource.NewQuantity(1, resource.DecimalSI))
result[api.ResourceServices] = *(resource.NewQuantity(1, resource.DecimalSI)) result[corev1.ResourceServices] = *(resource.NewQuantity(1, resource.DecimalSI))
result[api.ResourceServicesLoadBalancers] = resource.Quantity{Format: resource.DecimalSI} result[corev1.ResourceServicesLoadBalancers] = resource.Quantity{Format: resource.DecimalSI}
result[api.ResourceServicesNodePorts] = resource.Quantity{Format: resource.DecimalSI} result[corev1.ResourceServicesNodePorts] = resource.Quantity{Format: resource.DecimalSI}
switch svc.Spec.Type { switch svc.Spec.Type {
case api.ServiceTypeNodePort: case corev1.ServiceTypeNodePort:
// node port services need to count node ports // node port services need to count node ports
value := resource.NewQuantity(int64(ports), resource.DecimalSI) value := resource.NewQuantity(int64(ports), resource.DecimalSI)
result[api.ResourceServicesNodePorts] = *value result[corev1.ResourceServicesNodePorts] = *value
case api.ServiceTypeLoadBalancer: case corev1.ServiceTypeLoadBalancer:
// load balancer services need to count node ports and load balancers // load balancer services need to count node ports and load balancers
value := resource.NewQuantity(int64(ports), resource.DecimalSI) value := resource.NewQuantity(int64(ports), resource.DecimalSI)
result[api.ResourceServicesNodePorts] = *value result[corev1.ResourceServicesNodePorts] = *value
result[api.ResourceServicesLoadBalancers] = *(resource.NewQuantity(1, resource.DecimalSI)) result[corev1.ResourceServicesLoadBalancers] = *(resource.NewQuantity(1, resource.DecimalSI))
} }
return result, nil return result, nil
} }
...@@ -144,12 +144,12 @@ func (p *serviceEvaluator) UsageStats(options quota.UsageStatsOptions) (quota.Us ...@@ -144,12 +144,12 @@ func (p *serviceEvaluator) UsageStats(options quota.UsageStatsOptions) (quota.Us
var _ quota.Evaluator = &serviceEvaluator{} var _ quota.Evaluator = &serviceEvaluator{}
//GetQuotaServiceType returns ServiceType if the service type is eligible to track against a quota, nor return "" //GetQuotaServiceType returns ServiceType if the service type is eligible to track against a quota, nor return ""
func GetQuotaServiceType(service *v1.Service) v1.ServiceType { func GetQuotaServiceType(service *corev1.Service) corev1.ServiceType {
switch service.Spec.Type { switch service.Spec.Type {
case v1.ServiceTypeNodePort: case corev1.ServiceTypeNodePort:
return v1.ServiceTypeNodePort return corev1.ServiceTypeNodePort
case v1.ServiceTypeLoadBalancer: case corev1.ServiceTypeLoadBalancer:
return v1.ServiceTypeLoadBalancer return corev1.ServiceTypeLoadBalancer
} }
return v1.ServiceType("") return corev1.ServiceType("")
} }
...@@ -19,28 +19,29 @@ package core ...@@ -19,28 +19,29 @@ package core
import ( import (
"testing" "testing"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
api "k8s.io/kubernetes/pkg/apis/core" api "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/quota" quota "k8s.io/kubernetes/pkg/quota/v1"
"k8s.io/kubernetes/pkg/quota/generic" "k8s.io/kubernetes/pkg/quota/v1/generic"
) )
func TestServiceEvaluatorMatchesResources(t *testing.T) { func TestServiceEvaluatorMatchesResources(t *testing.T) {
evaluator := NewServiceEvaluator(nil) evaluator := NewServiceEvaluator(nil)
// we give a lot of resources // we give a lot of resources
input := []api.ResourceName{ input := []corev1.ResourceName{
api.ResourceConfigMaps, corev1.ResourceConfigMaps,
api.ResourceCPU, corev1.ResourceCPU,
api.ResourceServices, corev1.ResourceServices,
api.ResourceServicesNodePorts, corev1.ResourceServicesNodePorts,
api.ResourceServicesLoadBalancers, corev1.ResourceServicesLoadBalancers,
} }
// but we only match these... // but we only match these...
expected := quota.ToSet([]api.ResourceName{ expected := quota.ToSet([]corev1.ResourceName{
api.ResourceServices, corev1.ResourceServices,
api.ResourceServicesNodePorts, corev1.ResourceServicesNodePorts,
api.ResourceServicesLoadBalancers, corev1.ResourceServicesLoadBalancers,
}) })
actual := quota.ToSet(evaluator.MatchingResources(input)) actual := quota.ToSet(evaluator.MatchingResources(input))
if !expected.Equal(actual) { if !expected.Equal(actual) {
...@@ -52,7 +53,7 @@ func TestServiceEvaluatorUsage(t *testing.T) { ...@@ -52,7 +53,7 @@ func TestServiceEvaluatorUsage(t *testing.T) {
evaluator := NewServiceEvaluator(nil) evaluator := NewServiceEvaluator(nil)
testCases := map[string]struct { testCases := map[string]struct {
service *api.Service service *api.Service
usage api.ResourceList usage corev1.ResourceList
}{ }{
"loadbalancer": { "loadbalancer": {
service: &api.Service{ service: &api.Service{
...@@ -60,10 +61,10 @@ func TestServiceEvaluatorUsage(t *testing.T) { ...@@ -60,10 +61,10 @@ func TestServiceEvaluatorUsage(t *testing.T) {
Type: api.ServiceTypeLoadBalancer, Type: api.ServiceTypeLoadBalancer,
}, },
}, },
usage: api.ResourceList{ usage: corev1.ResourceList{
api.ResourceServicesNodePorts: resource.MustParse("0"), corev1.ResourceServicesNodePorts: resource.MustParse("0"),
api.ResourceServicesLoadBalancers: resource.MustParse("1"), corev1.ResourceServicesLoadBalancers: resource.MustParse("1"),
api.ResourceServices: resource.MustParse("1"), corev1.ResourceServices: resource.MustParse("1"),
generic.ObjectCountQuotaResourceNameFor(schema.GroupResource{Resource: "services"}): resource.MustParse("1"), generic.ObjectCountQuotaResourceNameFor(schema.GroupResource{Resource: "services"}): resource.MustParse("1"),
}, },
}, },
...@@ -78,10 +79,10 @@ func TestServiceEvaluatorUsage(t *testing.T) { ...@@ -78,10 +79,10 @@ func TestServiceEvaluatorUsage(t *testing.T) {
}, },
}, },
}, },
usage: api.ResourceList{ usage: corev1.ResourceList{
api.ResourceServicesNodePorts: resource.MustParse("1"), corev1.ResourceServicesNodePorts: resource.MustParse("1"),
api.ResourceServicesLoadBalancers: resource.MustParse("1"), corev1.ResourceServicesLoadBalancers: resource.MustParse("1"),
api.ResourceServices: resource.MustParse("1"), corev1.ResourceServices: resource.MustParse("1"),
generic.ObjectCountQuotaResourceNameFor(schema.GroupResource{Resource: "services"}): resource.MustParse("1"), generic.ObjectCountQuotaResourceNameFor(schema.GroupResource{Resource: "services"}): resource.MustParse("1"),
}, },
}, },
...@@ -91,10 +92,10 @@ func TestServiceEvaluatorUsage(t *testing.T) { ...@@ -91,10 +92,10 @@ func TestServiceEvaluatorUsage(t *testing.T) {
Type: api.ServiceTypeClusterIP, Type: api.ServiceTypeClusterIP,
}, },
}, },
usage: api.ResourceList{ usage: corev1.ResourceList{
api.ResourceServices: resource.MustParse("1"), corev1.ResourceServices: resource.MustParse("1"),
api.ResourceServicesNodePorts: resource.MustParse("0"), corev1.ResourceServicesNodePorts: resource.MustParse("0"),
api.ResourceServicesLoadBalancers: resource.MustParse("0"), corev1.ResourceServicesLoadBalancers: resource.MustParse("0"),
generic.ObjectCountQuotaResourceNameFor(schema.GroupResource{Resource: "services"}): resource.MustParse("1"), generic.ObjectCountQuotaResourceNameFor(schema.GroupResource{Resource: "services"}): resource.MustParse("1"),
}, },
}, },
...@@ -109,10 +110,10 @@ func TestServiceEvaluatorUsage(t *testing.T) { ...@@ -109,10 +110,10 @@ func TestServiceEvaluatorUsage(t *testing.T) {
}, },
}, },
}, },
usage: api.ResourceList{ usage: corev1.ResourceList{
api.ResourceServices: resource.MustParse("1"), corev1.ResourceServices: resource.MustParse("1"),
api.ResourceServicesNodePorts: resource.MustParse("1"), corev1.ResourceServicesNodePorts: resource.MustParse("1"),
api.ResourceServicesLoadBalancers: resource.MustParse("0"), corev1.ResourceServicesLoadBalancers: resource.MustParse("0"),
generic.ObjectCountQuotaResourceNameFor(schema.GroupResource{Resource: "services"}): resource.MustParse("1"), generic.ObjectCountQuotaResourceNameFor(schema.GroupResource{Resource: "services"}): resource.MustParse("1"),
}, },
}, },
...@@ -130,10 +131,10 @@ func TestServiceEvaluatorUsage(t *testing.T) { ...@@ -130,10 +131,10 @@ func TestServiceEvaluatorUsage(t *testing.T) {
}, },
}, },
}, },
usage: api.ResourceList{ usage: corev1.ResourceList{
api.ResourceServices: resource.MustParse("1"), corev1.ResourceServices: resource.MustParse("1"),
api.ResourceServicesNodePorts: resource.MustParse("2"), corev1.ResourceServicesNodePorts: resource.MustParse("2"),
api.ResourceServicesLoadBalancers: resource.MustParse("0"), corev1.ResourceServicesLoadBalancers: resource.MustParse("0"),
generic.ObjectCountQuotaResourceNameFor(schema.GroupResource{Resource: "services"}): resource.MustParse("1"), generic.ObjectCountQuotaResourceNameFor(schema.GroupResource{Resource: "services"}): resource.MustParse("1"),
}, },
}, },
...@@ -152,7 +153,7 @@ func TestServiceEvaluatorUsage(t *testing.T) { ...@@ -152,7 +153,7 @@ func TestServiceEvaluatorUsage(t *testing.T) {
func TestServiceConstraintsFunc(t *testing.T) { func TestServiceConstraintsFunc(t *testing.T) {
testCases := map[string]struct { testCases := map[string]struct {
service *api.Service service *api.Service
required []api.ResourceName required []corev1.ResourceName
err string err string
}{ }{
"loadbalancer": { "loadbalancer": {
...@@ -161,7 +162,7 @@ func TestServiceConstraintsFunc(t *testing.T) { ...@@ -161,7 +162,7 @@ func TestServiceConstraintsFunc(t *testing.T) {
Type: api.ServiceTypeLoadBalancer, Type: api.ServiceTypeLoadBalancer,
}, },
}, },
required: []api.ResourceName{api.ResourceServicesLoadBalancers}, required: []corev1.ResourceName{corev1.ResourceServicesLoadBalancers},
}, },
"clusterip": { "clusterip": {
service: &api.Service{ service: &api.Service{
...@@ -169,7 +170,7 @@ func TestServiceConstraintsFunc(t *testing.T) { ...@@ -169,7 +170,7 @@ func TestServiceConstraintsFunc(t *testing.T) {
Type: api.ServiceTypeClusterIP, Type: api.ServiceTypeClusterIP,
}, },
}, },
required: []api.ResourceName{api.ResourceServicesLoadBalancers, api.ResourceServices}, required: []corev1.ResourceName{corev1.ResourceServicesLoadBalancers, corev1.ResourceServices},
}, },
"nodeports": { "nodeports": {
service: &api.Service{ service: &api.Service{
...@@ -182,7 +183,7 @@ func TestServiceConstraintsFunc(t *testing.T) { ...@@ -182,7 +183,7 @@ func TestServiceConstraintsFunc(t *testing.T) {
}, },
}, },
}, },
required: []api.ResourceName{api.ResourceServicesNodePorts}, required: []corev1.ResourceName{corev1.ResourceServicesNodePorts},
}, },
"multi-nodeports": { "multi-nodeports": {
service: &api.Service{ service: &api.Service{
...@@ -198,7 +199,7 @@ func TestServiceConstraintsFunc(t *testing.T) { ...@@ -198,7 +199,7 @@ func TestServiceConstraintsFunc(t *testing.T) {
}, },
}, },
}, },
required: []api.ResourceName{api.ResourceServicesNodePorts}, required: []corev1.ResourceName{corev1.ResourceServicesNodePorts},
}, },
} }
......
...@@ -12,10 +12,10 @@ go_library( ...@@ -12,10 +12,10 @@ go_library(
"evaluator.go", "evaluator.go",
"registry.go", "registry.go",
], ],
importpath = "k8s.io/kubernetes/pkg/quota/generic", importpath = "k8s.io/kubernetes/pkg/quota/v1/generic",
deps = [ deps = [
"//pkg/apis/core:go_default_library", "//pkg/quota/v1:go_default_library",
"//pkg/quota: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/api/resource:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/labels: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:go_default_library",
......
...@@ -18,7 +18,7 @@ package generic ...@@ -18,7 +18,7 @@ package generic
import ( import (
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kubernetes/pkg/quota" quota "k8s.io/kubernetes/pkg/quota/v1"
) )
// implements a basic configuration // implements a basic configuration
......
...@@ -19,6 +19,7 @@ package generic ...@@ -19,6 +19,7 @@ package generic
import ( import (
"fmt" "fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
...@@ -26,8 +27,7 @@ import ( ...@@ -26,8 +27,7 @@ import (
"k8s.io/apiserver/pkg/admission" "k8s.io/apiserver/pkg/admission"
"k8s.io/client-go/informers" "k8s.io/client-go/informers"
"k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/cache"
api "k8s.io/kubernetes/pkg/apis/core" quota "k8s.io/kubernetes/pkg/quota/v1"
"k8s.io/kubernetes/pkg/quota"
) )
// InformerForResourceFunc knows how to provision an informer // InformerForResourceFunc knows how to provision an informer
...@@ -56,33 +56,33 @@ func ListResourceUsingListerFunc(l quota.ListerForResourceFunc, resource schema. ...@@ -56,33 +56,33 @@ func ListResourceUsingListerFunc(l quota.ListerForResourceFunc, resource schema.
} }
// ObjectCountQuotaResourceNameFor returns the object count quota name for specified groupResource // ObjectCountQuotaResourceNameFor returns the object count quota name for specified groupResource
func ObjectCountQuotaResourceNameFor(groupResource schema.GroupResource) api.ResourceName { func ObjectCountQuotaResourceNameFor(groupResource schema.GroupResource) corev1.ResourceName {
if len(groupResource.Group) == 0 { if len(groupResource.Group) == 0 {
return api.ResourceName("count/" + groupResource.Resource) return corev1.ResourceName("count/" + groupResource.Resource)
} }
return api.ResourceName("count/" + groupResource.Resource + "." + groupResource.Group) return corev1.ResourceName("count/" + groupResource.Resource + "." + groupResource.Group)
} }
// ListFuncByNamespace knows how to list resources in a namespace // ListFuncByNamespace knows how to list resources in a namespace
type ListFuncByNamespace func(namespace string) ([]runtime.Object, error) type ListFuncByNamespace func(namespace string) ([]runtime.Object, error)
// MatchesScopeFunc knows how to evaluate if an object matches a scope // MatchesScopeFunc knows how to evaluate if an object matches a scope
type MatchesScopeFunc func(scope api.ScopedResourceSelectorRequirement, object runtime.Object) (bool, error) type MatchesScopeFunc func(scope corev1.ScopedResourceSelectorRequirement, object runtime.Object) (bool, error)
// UsageFunc knows how to measure usage associated with an object // UsageFunc knows how to measure usage associated with an object
type UsageFunc func(object runtime.Object) (api.ResourceList, error) type UsageFunc func(object runtime.Object) (corev1.ResourceList, error)
// MatchingResourceNamesFunc is a function that returns the list of resources matched // MatchingResourceNamesFunc is a function that returns the list of resources matched
type MatchingResourceNamesFunc func(input []api.ResourceName) []api.ResourceName type MatchingResourceNamesFunc func(input []corev1.ResourceName) []corev1.ResourceName
// MatchesNoScopeFunc returns false on all match checks // MatchesNoScopeFunc returns false on all match checks
func MatchesNoScopeFunc(scope api.ScopedResourceSelectorRequirement, object runtime.Object) (bool, error) { func MatchesNoScopeFunc(scope corev1.ScopedResourceSelectorRequirement, object runtime.Object) (bool, error) {
return false, nil return false, nil
} }
// Matches returns true if the quota matches the specified item. // Matches returns true if the quota matches the specified item.
func Matches( func Matches(
resourceQuota *api.ResourceQuota, item runtime.Object, resourceQuota *corev1.ResourceQuota, item runtime.Object,
matchFunc MatchingResourceNamesFunc, scopeFunc MatchesScopeFunc) (bool, error) { matchFunc MatchingResourceNamesFunc, scopeFunc MatchesScopeFunc) (bool, error) {
if resourceQuota == nil { if resourceQuota == nil {
return false, fmt.Errorf("expected non-nil quota") return false, fmt.Errorf("expected non-nil quota")
...@@ -101,12 +101,12 @@ func Matches( ...@@ -101,12 +101,12 @@ func Matches(
return matchResource && matchScope, nil return matchResource && matchScope, nil
} }
func getScopeSelectorsFromQuota(quota *api.ResourceQuota) []api.ScopedResourceSelectorRequirement { func getScopeSelectorsFromQuota(quota *corev1.ResourceQuota) []corev1.ScopedResourceSelectorRequirement {
selectors := []api.ScopedResourceSelectorRequirement{} selectors := []corev1.ScopedResourceSelectorRequirement{}
for _, scope := range quota.Spec.Scopes { for _, scope := range quota.Spec.Scopes {
selectors = append(selectors, api.ScopedResourceSelectorRequirement{ selectors = append(selectors, corev1.ScopedResourceSelectorRequirement{
ScopeName: scope, ScopeName: scope,
Operator: api.ScopeSelectorOpExists}) Operator: corev1.ScopeSelectorOpExists})
} }
if quota.Spec.ScopeSelector != nil { if quota.Spec.ScopeSelector != nil {
for _, scopeSelector := range quota.Spec.ScopeSelector.MatchExpressions { for _, scopeSelector := range quota.Spec.ScopeSelector.MatchExpressions {
...@@ -122,7 +122,7 @@ func CalculateUsageStats(options quota.UsageStatsOptions, ...@@ -122,7 +122,7 @@ func CalculateUsageStats(options quota.UsageStatsOptions,
scopeFunc MatchesScopeFunc, scopeFunc MatchesScopeFunc,
usageFunc UsageFunc) (quota.UsageStats, error) { usageFunc UsageFunc) (quota.UsageStats, error) {
// default each tracked resource to zero // default each tracked resource to zero
result := quota.UsageStats{Used: api.ResourceList{}} result := quota.UsageStats{Used: corev1.ResourceList{}}
for _, resourceName := range options.Resources { for _, resourceName := range options.Resources {
result.Used[resourceName] = resource.Quantity{Format: resource.DecimalSI} result.Used[resourceName] = resource.Quantity{Format: resource.DecimalSI}
} }
...@@ -134,7 +134,7 @@ func CalculateUsageStats(options quota.UsageStatsOptions, ...@@ -134,7 +134,7 @@ func CalculateUsageStats(options quota.UsageStatsOptions,
// need to verify that the item matches the set of scopes // need to verify that the item matches the set of scopes
matchesScopes := true matchesScopes := true
for _, scope := range options.Scopes { for _, scope := range options.Scopes {
innerMatch, err := scopeFunc(api.ScopedResourceSelectorRequirement{ScopeName: scope}, item) innerMatch, err := scopeFunc(corev1.ScopedResourceSelectorRequirement{ScopeName: scope}, item)
if err != nil { if err != nil {
return result, nil return result, nil
} }
...@@ -174,11 +174,11 @@ type objectCountEvaluator struct { ...@@ -174,11 +174,11 @@ type objectCountEvaluator struct {
// TODO move to dynamic client in future // TODO move to dynamic client in future
listFuncByNamespace ListFuncByNamespace listFuncByNamespace ListFuncByNamespace
// Names associated with this resource in the quota for generic counting. // Names associated with this resource in the quota for generic counting.
resourceNames []api.ResourceName resourceNames []corev1.ResourceName
} }
// Constraints returns an error if the configured resource name is not in the required set. // Constraints returns an error if the configured resource name is not in the required set.
func (o *objectCountEvaluator) Constraints(required []api.ResourceName, item runtime.Object) error { func (o *objectCountEvaluator) Constraints(required []corev1.ResourceName, item runtime.Object) error {
// no-op for object counting // no-op for object counting
return nil return nil
} }
...@@ -190,30 +190,30 @@ func (o *objectCountEvaluator) Handles(a admission.Attributes) bool { ...@@ -190,30 +190,30 @@ func (o *objectCountEvaluator) Handles(a admission.Attributes) bool {
} }
// Matches returns true if the evaluator matches the specified quota with the provided input item // Matches returns true if the evaluator matches the specified quota with the provided input item
func (o *objectCountEvaluator) Matches(resourceQuota *api.ResourceQuota, item runtime.Object) (bool, error) { func (o *objectCountEvaluator) Matches(resourceQuota *corev1.ResourceQuota, item runtime.Object) (bool, error) {
return Matches(resourceQuota, item, o.MatchingResources, MatchesNoScopeFunc) return Matches(resourceQuota, item, o.MatchingResources, MatchesNoScopeFunc)
} }
// MatchingResources takes the input specified list of resources and returns the set of resources it matches. // MatchingResources takes the input specified list of resources and returns the set of resources it matches.
func (o *objectCountEvaluator) MatchingResources(input []api.ResourceName) []api.ResourceName { func (o *objectCountEvaluator) MatchingResources(input []corev1.ResourceName) []corev1.ResourceName {
return quota.Intersection(input, o.resourceNames) return quota.Intersection(input, o.resourceNames)
} }
// MatchingScopes takes the input specified list of scopes and input object. Returns the set of scopes resource matches. // 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 []api.ScopedResourceSelectorRequirement) ([]api.ScopedResourceSelectorRequirement, error) { func (o *objectCountEvaluator) MatchingScopes(item runtime.Object, scopes []corev1.ScopedResourceSelectorRequirement) ([]corev1.ScopedResourceSelectorRequirement, error) {
return []api.ScopedResourceSelectorRequirement{}, nil return []corev1.ScopedResourceSelectorRequirement{}, nil
} }
// UncoveredQuotaScopes takes the input matched scopes which are limited by configuration and the matched quota scopes. // 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 // It returns the scopes which are in limited scopes but dont have a corresponding covering quota scope
func (o *objectCountEvaluator) UncoveredQuotaScopes(limitedScopes []api.ScopedResourceSelectorRequirement, matchedQuotaScopes []api.ScopedResourceSelectorRequirement) ([]api.ScopedResourceSelectorRequirement, error) { func (o *objectCountEvaluator) UncoveredQuotaScopes(limitedScopes []corev1.ScopedResourceSelectorRequirement, matchedQuotaScopes []corev1.ScopedResourceSelectorRequirement) ([]corev1.ScopedResourceSelectorRequirement, error) {
return []api.ScopedResourceSelectorRequirement{}, nil return []corev1.ScopedResourceSelectorRequirement{}, nil
} }
// Usage returns the resource usage for the specified object // Usage returns the resource usage for the specified object
func (o *objectCountEvaluator) Usage(object runtime.Object) (api.ResourceList, error) { func (o *objectCountEvaluator) Usage(object runtime.Object) (corev1.ResourceList, error) {
quantity := resource.NewQuantity(1, resource.DecimalSI) quantity := resource.NewQuantity(1, resource.DecimalSI)
resourceList := api.ResourceList{} resourceList := corev1.ResourceList{}
for _, resourceName := range o.resourceNames { for _, resourceName := range o.resourceNames {
resourceList[resourceName] = *quantity resourceList[resourceName] = *quantity
} }
...@@ -239,9 +239,9 @@ var _ quota.Evaluator = &objectCountEvaluator{} ...@@ -239,9 +239,9 @@ var _ quota.Evaluator = &objectCountEvaluator{}
// backward compatibility, alias should not be used. // backward compatibility, alias should not be used.
func NewObjectCountEvaluator( func NewObjectCountEvaluator(
groupResource schema.GroupResource, listFuncByNamespace ListFuncByNamespace, groupResource schema.GroupResource, listFuncByNamespace ListFuncByNamespace,
alias api.ResourceName) quota.Evaluator { alias corev1.ResourceName) quota.Evaluator {
resourceNames := []api.ResourceName{ObjectCountQuotaResourceNameFor(groupResource)} resourceNames := []corev1.ResourceName{ObjectCountQuotaResourceNameFor(groupResource)}
if len(alias) > 0 { if len(alias) > 0 {
resourceNames = append(resourceNames, alias) resourceNames = append(resourceNames, alias)
} }
......
...@@ -20,7 +20,7 @@ import ( ...@@ -20,7 +20,7 @@ import (
"sync" "sync"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kubernetes/pkg/quota" quota "k8s.io/kubernetes/pkg/quota/v1"
) )
// implements a basic registry // implements a basic registry
......
...@@ -8,11 +8,11 @@ load( ...@@ -8,11 +8,11 @@ load(
go_library( go_library(
name = "go_default_library", name = "go_default_library",
srcs = ["registry.go"], srcs = ["registry.go"],
importpath = "k8s.io/kubernetes/pkg/quota/install", importpath = "k8s.io/kubernetes/pkg/quota/v1/install",
deps = [ deps = [
"//pkg/quota:go_default_library", "//pkg/quota/v1:go_default_library",
"//pkg/quota/evaluator/core:go_default_library", "//pkg/quota/v1/evaluator/core:go_default_library",
"//pkg/quota/generic: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/apimachinery/pkg/runtime/schema:go_default_library",
], ],
) )
......
...@@ -18,9 +18,9 @@ package install ...@@ -18,9 +18,9 @@ package install
import ( import (
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kubernetes/pkg/quota" quota "k8s.io/kubernetes/pkg/quota/v1"
"k8s.io/kubernetes/pkg/quota/evaluator/core" core "k8s.io/kubernetes/pkg/quota/v1/evaluator/core"
"k8s.io/kubernetes/pkg/quota/generic" generic "k8s.io/kubernetes/pkg/quota/v1/generic"
) )
// NewQuotaConfigurationForAdmission returns a quota configuration for admission control. // NewQuotaConfigurationForAdmission returns a quota configuration for admission control.
......
...@@ -17,11 +17,11 @@ limitations under the License. ...@@ -17,11 +17,11 @@ limitations under the License.
package quota package quota
import ( import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/admission" "k8s.io/apiserver/pkg/admission"
"k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/cache"
api "k8s.io/kubernetes/pkg/apis/core"
) )
// UsageStatsOptions is an options structs that describes how stats should be calculated // UsageStatsOptions is an options structs that describes how stats should be calculated
...@@ -29,37 +29,37 @@ type UsageStatsOptions struct { ...@@ -29,37 +29,37 @@ type UsageStatsOptions struct {
// Namespace where stats should be calculate // Namespace where stats should be calculate
Namespace string Namespace string
// Scopes that must match counted objects // Scopes that must match counted objects
Scopes []api.ResourceQuotaScope Scopes []corev1.ResourceQuotaScope
// Resources are the set of resources to include in the measurement // Resources are the set of resources to include in the measurement
Resources []api.ResourceName Resources []corev1.ResourceName
ScopeSelector *api.ScopeSelector ScopeSelector *corev1.ScopeSelector
} }
// UsageStats is result of measuring observed resource use in the system // UsageStats is result of measuring observed resource use in the system
type UsageStats struct { type UsageStats struct {
// Used maps resource to quantity used // Used maps resource to quantity used
Used api.ResourceList Used corev1.ResourceList
} }
// Evaluator knows how to evaluate quota usage for a particular group resource // Evaluator knows how to evaluate quota usage for a particular group resource
type Evaluator interface { type Evaluator interface {
// Constraints ensures that each required resource is present on item // Constraints ensures that each required resource is present on item
Constraints(required []api.ResourceName, item runtime.Object) error Constraints(required []corev1.ResourceName, item runtime.Object) error
// GroupResource returns the groupResource that this object knows how to evaluate // GroupResource returns the groupResource that this object knows how to evaluate
GroupResource() schema.GroupResource GroupResource() schema.GroupResource
// Handles determines if quota could be impacted by the specified attribute. // 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. // If true, admission control must perform quota processing for the operation, otherwise it is safe to ignore quota.
Handles(operation admission.Attributes) bool Handles(operation admission.Attributes) bool
// Matches returns true if the specified quota matches the input item // Matches returns true if the specified quota matches the input item
Matches(resourceQuota *api.ResourceQuota, item runtime.Object) (bool, error) 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 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 []api.ScopedResourceSelectorRequirement) ([]api.ScopedResourceSelectorRequirement, error) 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 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 []api.ScopedResourceSelectorRequirement, matchedQuotaScopes []api.ScopedResourceSelectorRequirement) ([]api.ScopedResourceSelectorRequirement, error) 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 takes the input specified list of resources and returns the set of resources evaluator matches.
MatchingResources(input []api.ResourceName) []api.ResourceName MatchingResources(input []corev1.ResourceName) []corev1.ResourceName
// Usage returns the resource usage for the specified object // Usage returns the resource usage for the specified object
Usage(item runtime.Object) (api.ResourceList, error) Usage(item runtime.Object) (corev1.ResourceList, error)
// UsageStats calculates latest observed usage stats for all objects // UsageStats calculates latest observed usage stats for all objects
UsageStats(options UsageStatsOptions) (UsageStats, error) UsageStats(options UsageStatsOptions) (UsageStats, error)
} }
......
...@@ -19,14 +19,13 @@ package quota ...@@ -19,14 +19,13 @@ package quota
import ( import (
"strings" "strings"
"k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/sets"
api "k8s.io/kubernetes/pkg/apis/core"
) )
// Equals returns true if the two lists are equivalent // Equals returns true if the two lists are equivalent
func Equals(a api.ResourceList, b api.ResourceList) bool { func Equals(a corev1.ResourceList, b corev1.ResourceList) bool {
if len(a) != len(b) { if len(a) != len(b) {
return false return false
} }
...@@ -45,7 +44,7 @@ func Equals(a api.ResourceList, b api.ResourceList) bool { ...@@ -45,7 +44,7 @@ func Equals(a api.ResourceList, b api.ResourceList) bool {
} }
// V1Equals returns true if the two lists are equivalent // V1Equals returns true if the two lists are equivalent
func V1Equals(a v1.ResourceList, b v1.ResourceList) bool { func V1Equals(a corev1.ResourceList, b corev1.ResourceList) bool {
if len(a) != len(b) { if len(a) != len(b) {
return false return false
} }
...@@ -65,9 +64,9 @@ func V1Equals(a v1.ResourceList, b v1.ResourceList) bool { ...@@ -65,9 +64,9 @@ func V1Equals(a v1.ResourceList, b v1.ResourceList) bool {
// LessThanOrEqual returns true if a < b for each key in b // LessThanOrEqual returns true if a < b for each key in b
// If false, it returns the keys in a that exceeded b // If false, it returns the keys in a that exceeded b
func LessThanOrEqual(a api.ResourceList, b api.ResourceList) (bool, []api.ResourceName) { func LessThanOrEqual(a corev1.ResourceList, b corev1.ResourceList) (bool, []corev1.ResourceName) {
result := true result := true
resourceNames := []api.ResourceName{} resourceNames := []corev1.ResourceName{}
for key, value := range b { for key, value := range b {
if other, found := a[key]; found { if other, found := a[key]; found {
if other.Cmp(value) > 0 { if other.Cmp(value) > 0 {
...@@ -80,8 +79,8 @@ func LessThanOrEqual(a api.ResourceList, b api.ResourceList) (bool, []api.Resour ...@@ -80,8 +79,8 @@ func LessThanOrEqual(a api.ResourceList, b api.ResourceList) (bool, []api.Resour
} }
// Max returns the result of Max(a, b) for each named resource // Max returns the result of Max(a, b) for each named resource
func Max(a api.ResourceList, b api.ResourceList) api.ResourceList { func Max(a corev1.ResourceList, b corev1.ResourceList) corev1.ResourceList {
result := api.ResourceList{} result := corev1.ResourceList{}
for key, value := range a { for key, value := range a {
if other, found := b[key]; found { if other, found := b[key]; found {
if value.Cmp(other) <= 0 { if value.Cmp(other) <= 0 {
...@@ -100,8 +99,8 @@ func Max(a api.ResourceList, b api.ResourceList) api.ResourceList { ...@@ -100,8 +99,8 @@ func Max(a api.ResourceList, b api.ResourceList) api.ResourceList {
} }
// Add returns the result of a + b for each named resource // Add returns the result of a + b for each named resource
func Add(a api.ResourceList, b api.ResourceList) api.ResourceList { func Add(a corev1.ResourceList, b corev1.ResourceList) corev1.ResourceList {
result := api.ResourceList{} result := corev1.ResourceList{}
for key, value := range a { for key, value := range a {
quantity := *value.Copy() quantity := *value.Copy()
if other, found := b[key]; found { if other, found := b[key]; found {
...@@ -120,10 +119,10 @@ func Add(a api.ResourceList, b api.ResourceList) api.ResourceList { ...@@ -120,10 +119,10 @@ func Add(a api.ResourceList, b api.ResourceList) api.ResourceList {
// SubtractWithNonNegativeResult - subtracts and returns result of a - b but // SubtractWithNonNegativeResult - subtracts and returns result of a - b but
// makes sure we don't return negative values to prevent negative resource usage. // makes sure we don't return negative values to prevent negative resource usage.
func SubtractWithNonNegativeResult(a api.ResourceList, b api.ResourceList) api.ResourceList { func SubtractWithNonNegativeResult(a corev1.ResourceList, b corev1.ResourceList) corev1.ResourceList {
zero := resource.MustParse("0") zero := resource.MustParse("0")
result := api.ResourceList{} result := corev1.ResourceList{}
for key, value := range a { for key, value := range a {
quantity := *value.Copy() quantity := *value.Copy()
if other, found := b[key]; found { if other, found := b[key]; found {
...@@ -145,8 +144,8 @@ func SubtractWithNonNegativeResult(a api.ResourceList, b api.ResourceList) api.R ...@@ -145,8 +144,8 @@ func SubtractWithNonNegativeResult(a api.ResourceList, b api.ResourceList) api.R
} }
// Subtract returns the result of a - b for each named resource // Subtract returns the result of a - b for each named resource
func Subtract(a api.ResourceList, b api.ResourceList) api.ResourceList { func Subtract(a corev1.ResourceList, b corev1.ResourceList) corev1.ResourceList {
result := api.ResourceList{} result := corev1.ResourceList{}
for key, value := range a { for key, value := range a {
quantity := *value.Copy() quantity := *value.Copy()
if other, found := b[key]; found { if other, found := b[key]; found {
...@@ -165,9 +164,9 @@ func Subtract(a api.ResourceList, b api.ResourceList) api.ResourceList { ...@@ -165,9 +164,9 @@ func Subtract(a api.ResourceList, b api.ResourceList) api.ResourceList {
} }
// Mask returns a new resource list that only has the values with the specified names // Mask returns a new resource list that only has the values with the specified names
func Mask(resources api.ResourceList, names []api.ResourceName) api.ResourceList { func Mask(resources corev1.ResourceList, names []corev1.ResourceName) corev1.ResourceList {
nameSet := ToSet(names) nameSet := ToSet(names)
result := api.ResourceList{} result := corev1.ResourceList{}
for key, value := range resources { for key, value := range resources {
if nameSet.Has(string(key)) { if nameSet.Has(string(key)) {
result[key] = *value.Copy() result[key] = *value.Copy()
...@@ -177,8 +176,8 @@ func Mask(resources api.ResourceList, names []api.ResourceName) api.ResourceList ...@@ -177,8 +176,8 @@ func Mask(resources api.ResourceList, names []api.ResourceName) api.ResourceList
} }
// ResourceNames returns a list of all resource names in the ResourceList // ResourceNames returns a list of all resource names in the ResourceList
func ResourceNames(resources api.ResourceList) []api.ResourceName { func ResourceNames(resources corev1.ResourceList) []corev1.ResourceName {
result := []api.ResourceName{} result := []corev1.ResourceName{}
for resourceName := range resources { for resourceName := range resources {
result = append(result, resourceName) result = append(result, resourceName)
} }
...@@ -186,12 +185,12 @@ func ResourceNames(resources api.ResourceList) []api.ResourceName { ...@@ -186,12 +185,12 @@ func ResourceNames(resources api.ResourceList) []api.ResourceName {
} }
// Contains returns true if the specified item is in the list of items // Contains returns true if the specified item is in the list of items
func Contains(items []api.ResourceName, item api.ResourceName) bool { func Contains(items []corev1.ResourceName, item corev1.ResourceName) bool {
return ToSet(items).Has(string(item)) return ToSet(items).Has(string(item))
} }
// ContainsPrefix returns true if the specified item has a prefix that contained in given prefix Set // ContainsPrefix returns true if the specified item has a prefix that contained in given prefix Set
func ContainsPrefix(prefixSet []string, item api.ResourceName) bool { func ContainsPrefix(prefixSet []string, item corev1.ResourceName) bool {
for _, prefix := range prefixSet { for _, prefix := range prefixSet {
if strings.HasPrefix(string(item), prefix) { if strings.HasPrefix(string(item), prefix) {
return true return true
...@@ -201,19 +200,19 @@ func ContainsPrefix(prefixSet []string, item api.ResourceName) bool { ...@@ -201,19 +200,19 @@ func ContainsPrefix(prefixSet []string, item api.ResourceName) bool {
} }
// Intersection returns the intersection of both list of resources // Intersection returns the intersection of both list of resources
func Intersection(a []api.ResourceName, b []api.ResourceName) []api.ResourceName { func Intersection(a []corev1.ResourceName, b []corev1.ResourceName) []corev1.ResourceName {
setA := ToSet(a) setA := ToSet(a)
setB := ToSet(b) setB := ToSet(b)
setC := setA.Intersection(setB) setC := setA.Intersection(setB)
result := []api.ResourceName{} result := []corev1.ResourceName{}
for _, resourceName := range setC.List() { for _, resourceName := range setC.List() {
result = append(result, api.ResourceName(resourceName)) result = append(result, corev1.ResourceName(resourceName))
} }
return result return result
} }
// IsZero returns true if each key maps to the quantity value 0 // IsZero returns true if each key maps to the quantity value 0
func IsZero(a api.ResourceList) bool { func IsZero(a corev1.ResourceList) bool {
zero := resource.MustParse("0") zero := resource.MustParse("0")
for _, v := range a { for _, v := range a {
if v.Cmp(zero) != 0 { if v.Cmp(zero) != 0 {
...@@ -224,8 +223,8 @@ func IsZero(a api.ResourceList) bool { ...@@ -224,8 +223,8 @@ func IsZero(a api.ResourceList) bool {
} }
// IsNegative returns the set of resource names that have a negative value. // IsNegative returns the set of resource names that have a negative value.
func IsNegative(a api.ResourceList) []api.ResourceName { func IsNegative(a corev1.ResourceList) []corev1.ResourceName {
results := []api.ResourceName{} results := []corev1.ResourceName{}
zero := resource.MustParse("0") zero := resource.MustParse("0")
for k, v := range a { for k, v := range a {
if v.Cmp(zero) < 0 { if v.Cmp(zero) < 0 {
...@@ -236,7 +235,7 @@ func IsNegative(a api.ResourceList) []api.ResourceName { ...@@ -236,7 +235,7 @@ func IsNegative(a api.ResourceList) []api.ResourceName {
} }
// ToSet takes a list of resource names and converts to a string set // ToSet takes a list of resource names and converts to a string set
func ToSet(resourceNames []api.ResourceName) sets.String { func ToSet(resourceNames []corev1.ResourceName) sets.String {
result := sets.NewString() result := sets.NewString()
for _, resourceName := range resourceNames { for _, resourceName := range resourceNames {
result.Insert(string(resourceName)) result.Insert(string(resourceName))
...@@ -245,12 +244,12 @@ func ToSet(resourceNames []api.ResourceName) sets.String { ...@@ -245,12 +244,12 @@ func ToSet(resourceNames []api.ResourceName) sets.String {
} }
// CalculateUsage calculates and returns the requested ResourceList usage // CalculateUsage calculates and returns the requested ResourceList usage
func CalculateUsage(namespaceName string, scopes []api.ResourceQuotaScope, hardLimits api.ResourceList, registry Registry, scopeSelector *api.ScopeSelector) (api.ResourceList, error) { 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 // find the intersection between the hard resources on the quota
// and the resources this controller can track to know what we can // and the resources this controller can track to know what we can
// look to measure updated usage stats for // look to measure updated usage stats for
hardResources := ResourceNames(hardLimits) hardResources := ResourceNames(hardLimits)
potentialResources := []api.ResourceName{} potentialResources := []corev1.ResourceName{}
evaluators := registry.List() evaluators := registry.List()
for _, evaluator := range evaluators { for _, evaluator := range evaluators {
potentialResources = append(potentialResources, evaluator.MatchingResources(hardResources)...) potentialResources = append(potentialResources, evaluator.MatchingResources(hardResources)...)
...@@ -259,7 +258,7 @@ func CalculateUsage(namespaceName string, scopes []api.ResourceQuotaScope, hardL ...@@ -259,7 +258,7 @@ func CalculateUsage(namespaceName string, scopes []api.ResourceQuotaScope, hardL
matchedResources := Intersection(hardResources, potentialResources) matchedResources := Intersection(hardResources, potentialResources)
// sum the observed usage from each evaluator // sum the observed usage from each evaluator
newUsage := api.ResourceList{} newUsage := corev1.ResourceList{}
for _, evaluator := range evaluators { for _, evaluator := range evaluators {
// only trigger the evaluator if it matches a resource in the quota, otherwise, skip calculating anything // only trigger the evaluator if it matches a resource in the quota, otherwise, skip calculating anything
intersection := evaluator.MatchingResources(matchedResources) intersection := evaluator.MatchingResources(matchedResources)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment