forked from barak/tarpoon
Add glide.yaml and vendor deps
This commit is contained in:
parent
db918f12ad
commit
5b3d5e81bd
18880 changed files with 5166045 additions and 1 deletions
40
vendor/k8s.io/kubernetes/pkg/quota/BUILD
generated
vendored
Normal file
40
vendor/k8s.io/kubernetes/pkg/quota/BUILD
generated
vendored
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_binary",
|
||||
"go_library",
|
||||
"go_test",
|
||||
"cgo_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"interfaces.go",
|
||||
"resources.go",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/admission:go_default_library",
|
||||
"//pkg/api:go_default_library",
|
||||
"//pkg/api/resource:go_default_library",
|
||||
"//pkg/api/v1:go_default_library",
|
||||
"//pkg/runtime:go_default_library",
|
||||
"//pkg/runtime/schema:go_default_library",
|
||||
"//pkg/util/sets:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["resources_test.go"],
|
||||
library = "go_default_library",
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/api:go_default_library",
|
||||
"//pkg/api/resource:go_default_library",
|
||||
],
|
||||
)
|
||||
3
vendor/k8s.io/kubernetes/pkg/quota/OWNERS
generated
vendored
Normal file
3
vendor/k8s.io/kubernetes/pkg/quota/OWNERS
generated
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
assignees:
|
||||
- derekwaynecarr
|
||||
- vishh
|
||||
61
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/BUILD
generated
vendored
Normal file
61
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/BUILD
generated
vendored
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_binary",
|
||||
"go_library",
|
||||
"go_test",
|
||||
"cgo_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"configmap.go",
|
||||
"doc.go",
|
||||
"persistent_volume_claims.go",
|
||||
"pods.go",
|
||||
"registry.go",
|
||||
"replication_controllers.go",
|
||||
"resource_quotas.go",
|
||||
"secrets.go",
|
||||
"services.go",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/admission:go_default_library",
|
||||
"//pkg/api:go_default_library",
|
||||
"//pkg/api/resource:go_default_library",
|
||||
"//pkg/api/v1:go_default_library",
|
||||
"//pkg/api/validation:go_default_library",
|
||||
"//pkg/client/clientset_generated/release_1_5:go_default_library",
|
||||
"//pkg/controller/informers:go_default_library",
|
||||
"//pkg/kubelet/qos:go_default_library",
|
||||
"//pkg/quota:go_default_library",
|
||||
"//pkg/quota/generic:go_default_library",
|
||||
"//pkg/runtime:go_default_library",
|
||||
"//pkg/runtime/schema:go_default_library",
|
||||
"//pkg/util/sets:go_default_library",
|
||||
"//pkg/util/validation/field:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = [
|
||||
"persistent_volume_claims_test.go",
|
||||
"pods_test.go",
|
||||
"services_test.go",
|
||||
],
|
||||
library = "go_default_library",
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/api:go_default_library",
|
||||
"//pkg/api/resource:go_default_library",
|
||||
"//pkg/apis/meta/v1:go_default_library",
|
||||
"//pkg/client/clientset_generated/release_1_5/fake:go_default_library",
|
||||
"//pkg/quota:go_default_library",
|
||||
],
|
||||
)
|
||||
54
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/configmap.go
generated
vendored
Normal file
54
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/configmap.go
generated
vendored
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
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 core
|
||||
|
||||
import (
|
||||
"k8s.io/kubernetes/pkg/admission"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
|
||||
"k8s.io/kubernetes/pkg/quota"
|
||||
"k8s.io/kubernetes/pkg/quota/generic"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
)
|
||||
|
||||
// NewConfigMapEvaluator returns an evaluator that can evaluate configMaps
|
||||
func NewConfigMapEvaluator(kubeClient clientset.Interface) quota.Evaluator {
|
||||
allResources := []api.ResourceName{api.ResourceConfigMaps}
|
||||
return &generic.GenericEvaluator{
|
||||
Name: "Evaluator.ConfigMap",
|
||||
InternalGroupKind: api.Kind("ConfigMap"),
|
||||
InternalOperationResources: map[admission.Operation][]api.ResourceName{
|
||||
admission.Create: allResources,
|
||||
},
|
||||
MatchedResourceNames: allResources,
|
||||
MatchesScopeFunc: generic.MatchesNoScopeFunc,
|
||||
ConstraintsFunc: generic.ObjectCountConstraintsFunc(api.ResourceConfigMaps),
|
||||
UsageFunc: generic.ObjectCountUsageFunc(api.ResourceConfigMaps),
|
||||
ListFuncByNamespace: func(namespace string, options v1.ListOptions) ([]runtime.Object, error) {
|
||||
itemList, err := kubeClient.Core().ConfigMaps(namespace).List(options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results := make([]runtime.Object, 0, len(itemList.Items))
|
||||
for i := range itemList.Items {
|
||||
results = append(results, &itemList.Items[i])
|
||||
}
|
||||
return results, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
18
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/doc.go
generated
vendored
Normal file
18
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/doc.go
generated
vendored
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
// core contains modules that interface with the core api group
|
||||
package core // import "k8s.io/kubernetes/pkg/quota/evaluator/core"
|
||||
118
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/persistent_volume_claims.go
generated
vendored
Normal file
118
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/persistent_volume_claims.go
generated
vendored
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
/*
|
||||
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 core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"k8s.io/kubernetes/pkg/admission"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/resource"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
|
||||
"k8s.io/kubernetes/pkg/controller/informers"
|
||||
"k8s.io/kubernetes/pkg/quota"
|
||||
"k8s.io/kubernetes/pkg/quota/generic"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/runtime/schema"
|
||||
"k8s.io/kubernetes/pkg/util/sets"
|
||||
)
|
||||
|
||||
// listPersistentVolumeClaimsByNamespaceFuncUsingClient returns a pvc listing function based on the provided client.
|
||||
func listPersistentVolumeClaimsByNamespaceFuncUsingClient(kubeClient clientset.Interface) generic.ListFuncByNamespace {
|
||||
// TODO: ideally, we could pass dynamic client pool down into this code, and have one way of doing this.
|
||||
// unfortunately, dynamic client works with Unstructured objects, and when we calculate Usage, we require
|
||||
// structured objects.
|
||||
return func(namespace string, options v1.ListOptions) ([]runtime.Object, error) {
|
||||
itemList, err := kubeClient.Core().PersistentVolumeClaims(namespace).List(options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results := make([]runtime.Object, 0, len(itemList.Items))
|
||||
for i := range itemList.Items {
|
||||
results = append(results, &itemList.Items[i])
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
}
|
||||
|
||||
// NewPersistentVolumeClaimEvaluator returns an evaluator that can evaluate persistent volume claims
|
||||
// if the specified shared informer factory is not nil, evaluator may use it to support listing functions.
|
||||
func NewPersistentVolumeClaimEvaluator(kubeClient clientset.Interface, f informers.SharedInformerFactory) quota.Evaluator {
|
||||
allResources := []api.ResourceName{api.ResourcePersistentVolumeClaims, api.ResourceRequestsStorage}
|
||||
listFuncByNamespace := listPersistentVolumeClaimsByNamespaceFuncUsingClient(kubeClient)
|
||||
if f != nil {
|
||||
listFuncByNamespace = generic.ListResourceUsingInformerFunc(f, schema.GroupResource{Resource: "persistentvolumeclaims"})
|
||||
}
|
||||
|
||||
return &generic.GenericEvaluator{
|
||||
Name: "Evaluator.PersistentVolumeClaim",
|
||||
InternalGroupKind: api.Kind("PersistentVolumeClaim"),
|
||||
InternalOperationResources: map[admission.Operation][]api.ResourceName{
|
||||
admission.Create: allResources,
|
||||
},
|
||||
MatchedResourceNames: allResources,
|
||||
MatchesScopeFunc: generic.MatchesNoScopeFunc,
|
||||
ConstraintsFunc: PersistentVolumeClaimConstraintsFunc,
|
||||
UsageFunc: PersistentVolumeClaimUsageFunc,
|
||||
ListFuncByNamespace: listFuncByNamespace,
|
||||
}
|
||||
}
|
||||
|
||||
// PersistentVolumeClaimUsageFunc knows how to measure usage associated with persistent volume claims
|
||||
func PersistentVolumeClaimUsageFunc(object runtime.Object) api.ResourceList {
|
||||
result := api.ResourceList{}
|
||||
var found bool
|
||||
var request resource.Quantity
|
||||
|
||||
switch t := object.(type) {
|
||||
case *v1.PersistentVolumeClaim:
|
||||
request, found = t.Spec.Resources.Requests[v1.ResourceStorage]
|
||||
case *api.PersistentVolumeClaim:
|
||||
request, found = t.Spec.Resources.Requests[api.ResourceStorage]
|
||||
default:
|
||||
panic(fmt.Sprintf("expect *api.PersistenVolumeClaim or *v1.PersistentVolumeClaim, got %v", t))
|
||||
}
|
||||
|
||||
result[api.ResourcePersistentVolumeClaims] = resource.MustParse("1")
|
||||
if found {
|
||||
result[api.ResourceRequestsStorage] = request
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// PersistentVolumeClaimConstraintsFunc verifies that all required resources are present on the claim
|
||||
// In addition, it validates that the resources are valid (i.e. requests < limits)
|
||||
func PersistentVolumeClaimConstraintsFunc(required []api.ResourceName, object runtime.Object) error {
|
||||
pvc, ok := object.(*api.PersistentVolumeClaim)
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected input object %v", object)
|
||||
}
|
||||
|
||||
requiredSet := quota.ToSet(required)
|
||||
missingSet := sets.NewString()
|
||||
pvcUsage := PersistentVolumeClaimUsageFunc(pvc)
|
||||
pvcSet := quota.ToSet(quota.ResourceNames(pvcUsage))
|
||||
if diff := requiredSet.Difference(pvcSet); len(diff) > 0 {
|
||||
missingSet.Insert(diff.List()...)
|
||||
}
|
||||
if len(missingSet) == 0 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("must specify %s", strings.Join(missingSet.List(), ","))
|
||||
}
|
||||
149
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/persistent_volume_claims_test.go
generated
vendored
Normal file
149
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/persistent_volume_claims_test.go
generated
vendored
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
/*
|
||||
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 core
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/resource"
|
||||
metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5/fake"
|
||||
"k8s.io/kubernetes/pkg/quota"
|
||||
)
|
||||
|
||||
func testVolumeClaim(name string, namespace string, spec api.PersistentVolumeClaimSpec) *api.PersistentVolumeClaim {
|
||||
return &api.PersistentVolumeClaim{
|
||||
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespace},
|
||||
Spec: spec,
|
||||
}
|
||||
}
|
||||
|
||||
func TestPersistentVolumeClaimsConstraintsFunc(t *testing.T) {
|
||||
validClaim := testVolumeClaim("foo", "ns", api.PersistentVolumeClaimSpec{
|
||||
Selector: &metav1.LabelSelector{
|
||||
MatchExpressions: []metav1.LabelSelectorRequirement{
|
||||
{
|
||||
Key: "key2",
|
||||
Operator: "Exists",
|
||||
},
|
||||
},
|
||||
},
|
||||
AccessModes: []api.PersistentVolumeAccessMode{
|
||||
api.ReadWriteOnce,
|
||||
api.ReadOnlyMany,
|
||||
},
|
||||
Resources: api.ResourceRequirements{
|
||||
Requests: api.ResourceList{
|
||||
api.ResourceName(api.ResourceStorage): resource.MustParse("10G"),
|
||||
},
|
||||
},
|
||||
})
|
||||
missingStorage := testVolumeClaim("foo", "ns", api.PersistentVolumeClaimSpec{
|
||||
Selector: &metav1.LabelSelector{
|
||||
MatchExpressions: []metav1.LabelSelectorRequirement{
|
||||
{
|
||||
Key: "key2",
|
||||
Operator: "Exists",
|
||||
},
|
||||
},
|
||||
},
|
||||
AccessModes: []api.PersistentVolumeAccessMode{
|
||||
api.ReadWriteOnce,
|
||||
api.ReadOnlyMany,
|
||||
},
|
||||
Resources: api.ResourceRequirements{
|
||||
Requests: api.ResourceList{},
|
||||
},
|
||||
})
|
||||
|
||||
testCases := map[string]struct {
|
||||
pvc *api.PersistentVolumeClaim
|
||||
required []api.ResourceName
|
||||
err string
|
||||
}{
|
||||
"missing storage": {
|
||||
pvc: missingStorage,
|
||||
required: []api.ResourceName{api.ResourceRequestsStorage},
|
||||
err: `must specify requests.storage`,
|
||||
},
|
||||
"valid-claim-quota-storage": {
|
||||
pvc: validClaim,
|
||||
required: []api.ResourceName{api.ResourceRequestsStorage},
|
||||
},
|
||||
"valid-claim-quota-pvc": {
|
||||
pvc: validClaim,
|
||||
required: []api.ResourceName{api.ResourcePersistentVolumeClaims},
|
||||
},
|
||||
"valid-claim-quota-storage-and-pvc": {
|
||||
pvc: validClaim,
|
||||
required: []api.ResourceName{api.ResourceRequestsStorage, api.ResourcePersistentVolumeClaims},
|
||||
},
|
||||
}
|
||||
for testName, test := range testCases {
|
||||
err := PersistentVolumeClaimConstraintsFunc(test.required, test.pvc)
|
||||
switch {
|
||||
case err != nil && len(test.err) == 0,
|
||||
err == nil && len(test.err) != 0,
|
||||
err != nil && test.err != err.Error():
|
||||
t.Errorf("%s unexpected error: %v", testName, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPersistentVolumeClaimEvaluatorUsage(t *testing.T) {
|
||||
validClaim := testVolumeClaim("foo", "ns", api.PersistentVolumeClaimSpec{
|
||||
Selector: &metav1.LabelSelector{
|
||||
MatchExpressions: []metav1.LabelSelectorRequirement{
|
||||
{
|
||||
Key: "key2",
|
||||
Operator: "Exists",
|
||||
},
|
||||
},
|
||||
},
|
||||
AccessModes: []api.PersistentVolumeAccessMode{
|
||||
api.ReadWriteOnce,
|
||||
api.ReadOnlyMany,
|
||||
},
|
||||
Resources: api.ResourceRequirements{
|
||||
Requests: api.ResourceList{
|
||||
api.ResourceName(api.ResourceStorage): resource.MustParse("10Gi"),
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
kubeClient := fake.NewSimpleClientset()
|
||||
evaluator := NewPersistentVolumeClaimEvaluator(kubeClient, nil)
|
||||
testCases := map[string]struct {
|
||||
pvc *api.PersistentVolumeClaim
|
||||
usage api.ResourceList
|
||||
}{
|
||||
"pvc-usage": {
|
||||
pvc: validClaim,
|
||||
usage: api.ResourceList{
|
||||
api.ResourceRequestsStorage: resource.MustParse("10Gi"),
|
||||
api.ResourcePersistentVolumeClaims: resource.MustParse("1"),
|
||||
},
|
||||
},
|
||||
}
|
||||
for testName, testCase := range testCases {
|
||||
actual := evaluator.Usage(testCase.pvc)
|
||||
if !quota.Equals(testCase.usage, actual) {
|
||||
t.Errorf("%s expected: %v, actual: %v", testName, testCase.usage, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
245
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/pods.go
generated
vendored
Normal file
245
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/pods.go
generated
vendored
Normal file
|
|
@ -0,0 +1,245 @@
|
|||
/*
|
||||
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 core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"k8s.io/kubernetes/pkg/admission"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/resource"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
"k8s.io/kubernetes/pkg/api/validation"
|
||||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
|
||||
"k8s.io/kubernetes/pkg/controller/informers"
|
||||
"k8s.io/kubernetes/pkg/kubelet/qos"
|
||||
"k8s.io/kubernetes/pkg/quota"
|
||||
"k8s.io/kubernetes/pkg/quota/generic"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/runtime/schema"
|
||||
"k8s.io/kubernetes/pkg/util/sets"
|
||||
"k8s.io/kubernetes/pkg/util/validation/field"
|
||||
)
|
||||
|
||||
// listPodsByNamespaceFuncUsingClient returns a pod listing function based on the provided client.
|
||||
func listPodsByNamespaceFuncUsingClient(kubeClient clientset.Interface) generic.ListFuncByNamespace {
|
||||
// TODO: ideally, we could pass dynamic client pool down into this code, and have one way of doing this.
|
||||
// unfortunately, dynamic client works with Unstructured objects, and when we calculate Usage, we require
|
||||
// structured objects.
|
||||
return func(namespace string, options v1.ListOptions) ([]runtime.Object, error) {
|
||||
itemList, err := kubeClient.Core().Pods(namespace).List(options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results := make([]runtime.Object, 0, len(itemList.Items))
|
||||
for i := range itemList.Items {
|
||||
results = append(results, &itemList.Items[i])
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
}
|
||||
|
||||
// NewPodEvaluator returns an evaluator that can evaluate pods
|
||||
// if the specified shared informer factory is not nil, evaluator may use it to support listing functions.
|
||||
func NewPodEvaluator(kubeClient clientset.Interface, f informers.SharedInformerFactory) quota.Evaluator {
|
||||
computeResources := []api.ResourceName{
|
||||
api.ResourceCPU,
|
||||
api.ResourceMemory,
|
||||
api.ResourceRequestsCPU,
|
||||
api.ResourceRequestsMemory,
|
||||
api.ResourceLimitsCPU,
|
||||
api.ResourceLimitsMemory,
|
||||
}
|
||||
allResources := append(computeResources, api.ResourcePods)
|
||||
listFuncByNamespace := listPodsByNamespaceFuncUsingClient(kubeClient)
|
||||
if f != nil {
|
||||
listFuncByNamespace = generic.ListResourceUsingInformerFunc(f, schema.GroupResource{Resource: "pods"})
|
||||
}
|
||||
return &generic.GenericEvaluator{
|
||||
Name: "Evaluator.Pod",
|
||||
InternalGroupKind: api.Kind("Pod"),
|
||||
InternalOperationResources: map[admission.Operation][]api.ResourceName{
|
||||
admission.Create: allResources,
|
||||
// TODO: the quota system can only charge for deltas on compute resources when pods support updates.
|
||||
// admission.Update: computeResources,
|
||||
},
|
||||
GetFuncByNamespace: func(namespace, name string) (runtime.Object, error) {
|
||||
return kubeClient.Core().Pods(namespace).Get(name)
|
||||
},
|
||||
ConstraintsFunc: PodConstraintsFunc,
|
||||
MatchedResourceNames: allResources,
|
||||
MatchesScopeFunc: PodMatchesScopeFunc,
|
||||
UsageFunc: PodUsageFunc,
|
||||
ListFuncByNamespace: listFuncByNamespace,
|
||||
}
|
||||
}
|
||||
|
||||
// PodConstraintsFunc verifies that all required resources are present on the pod
|
||||
// In addition, it validates that the resources are valid (i.e. requests < limits)
|
||||
func PodConstraintsFunc(required []api.ResourceName, object runtime.Object) error {
|
||||
pod, ok := object.(*api.Pod)
|
||||
if !ok {
|
||||
return fmt.Errorf("Unexpected input object %v", object)
|
||||
}
|
||||
|
||||
// Pod level resources are often set during admission control
|
||||
// As a consequence, we want to verify that resources are valid prior
|
||||
// to ever charging quota prematurely in case they are not.
|
||||
allErrs := field.ErrorList{}
|
||||
fldPath := field.NewPath("spec").Child("containers")
|
||||
for i, ctr := range pod.Spec.Containers {
|
||||
allErrs = append(allErrs, validation.ValidateResourceRequirements(&ctr.Resources, fldPath.Index(i).Child("resources"))...)
|
||||
}
|
||||
fldPath = field.NewPath("spec").Child("initContainers")
|
||||
for i, ctr := range pod.Spec.InitContainers {
|
||||
allErrs = append(allErrs, validation.ValidateResourceRequirements(&ctr.Resources, fldPath.Index(i).Child("resources"))...)
|
||||
}
|
||||
if len(allErrs) > 0 {
|
||||
return allErrs.ToAggregate()
|
||||
}
|
||||
|
||||
// TODO: fix this when we have pod level cgroups
|
||||
// since we do not yet pod level requests/limits, we need to ensure each
|
||||
// container makes an explict request or limit for a quota tracked resource
|
||||
requiredSet := quota.ToSet(required)
|
||||
missingSet := sets.NewString()
|
||||
for i := range pod.Spec.Containers {
|
||||
enforcePodContainerConstraints(&pod.Spec.Containers[i], requiredSet, missingSet)
|
||||
}
|
||||
for i := range pod.Spec.InitContainers {
|
||||
enforcePodContainerConstraints(&pod.Spec.InitContainers[i], requiredSet, missingSet)
|
||||
}
|
||||
if len(missingSet) == 0 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("must specify %s", strings.Join(missingSet.List(), ","))
|
||||
}
|
||||
|
||||
// enforcePodContainerConstraints checks for required resources that are not set on this container and
|
||||
// adds them to missingSet.
|
||||
func enforcePodContainerConstraints(container *api.Container, requiredSet, missingSet sets.String) {
|
||||
requests := container.Resources.Requests
|
||||
limits := container.Resources.Limits
|
||||
containerUsage := podUsageHelper(requests, limits)
|
||||
containerSet := quota.ToSet(quota.ResourceNames(containerUsage))
|
||||
if !containerSet.Equal(requiredSet) {
|
||||
difference := requiredSet.Difference(containerSet)
|
||||
missingSet.Insert(difference.List()...)
|
||||
}
|
||||
}
|
||||
|
||||
// podUsageHelper can summarize the pod quota usage based on requests and limits
|
||||
func podUsageHelper(requests api.ResourceList, limits api.ResourceList) api.ResourceList {
|
||||
result := api.ResourceList{}
|
||||
result[api.ResourcePods] = resource.MustParse("1")
|
||||
if request, found := requests[api.ResourceCPU]; found {
|
||||
result[api.ResourceCPU] = request
|
||||
result[api.ResourceRequestsCPU] = request
|
||||
}
|
||||
if limit, found := limits[api.ResourceCPU]; found {
|
||||
result[api.ResourceLimitsCPU] = limit
|
||||
}
|
||||
if request, found := requests[api.ResourceMemory]; found {
|
||||
result[api.ResourceMemory] = request
|
||||
result[api.ResourceRequestsMemory] = request
|
||||
}
|
||||
if limit, found := limits[api.ResourceMemory]; found {
|
||||
result[api.ResourceLimitsMemory] = limit
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func toInternalPodOrDie(obj runtime.Object) *api.Pod {
|
||||
pod := &api.Pod{}
|
||||
switch t := obj.(type) {
|
||||
case *v1.Pod:
|
||||
if err := v1.Convert_v1_Pod_To_api_Pod(t, pod, nil); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
case *api.Pod:
|
||||
pod = t
|
||||
default:
|
||||
panic(fmt.Sprintf("expect *api.Pod or *v1.Pod, got %v", t))
|
||||
}
|
||||
return pod
|
||||
}
|
||||
|
||||
// PodUsageFunc knows how to measure usage associated with pods
|
||||
func PodUsageFunc(obj runtime.Object) api.ResourceList {
|
||||
pod := toInternalPodOrDie(obj)
|
||||
// by convention, we do not quota pods that have reached an end-of-life state
|
||||
if !QuotaPod(pod) {
|
||||
return api.ResourceList{}
|
||||
}
|
||||
requests := api.ResourceList{}
|
||||
limits := api.ResourceList{}
|
||||
// TODO: fix this when we have pod level cgroups
|
||||
// when we have pod level cgroups, we can just read pod level requests/limits
|
||||
for i := range pod.Spec.Containers {
|
||||
requests = quota.Add(requests, pod.Spec.Containers[i].Resources.Requests)
|
||||
limits = quota.Add(limits, pod.Spec.Containers[i].Resources.Limits)
|
||||
}
|
||||
// InitContainers are run sequentially before other containers start, so the highest
|
||||
// init container resource is compared against the sum of app containers to determine
|
||||
// the effective usage for both requests and limits.
|
||||
for i := range pod.Spec.InitContainers {
|
||||
requests = quota.Max(requests, pod.Spec.InitContainers[i].Resources.Requests)
|
||||
limits = quota.Max(limits, pod.Spec.InitContainers[i].Resources.Limits)
|
||||
}
|
||||
|
||||
return podUsageHelper(requests, limits)
|
||||
}
|
||||
|
||||
// PodMatchesScopeFunc is a function that knows how to evaluate if a pod matches a scope
|
||||
func PodMatchesScopeFunc(scope api.ResourceQuotaScope, object runtime.Object) bool {
|
||||
pod := toInternalPodOrDie(object)
|
||||
switch scope {
|
||||
case api.ResourceQuotaScopeTerminating:
|
||||
return isTerminating(pod)
|
||||
case api.ResourceQuotaScopeNotTerminating:
|
||||
return !isTerminating(pod)
|
||||
case api.ResourceQuotaScopeBestEffort:
|
||||
return isBestEffort(pod)
|
||||
case api.ResourceQuotaScopeNotBestEffort:
|
||||
return !isBestEffort(pod)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isBestEffort(pod *api.Pod) bool {
|
||||
return qos.InternalGetPodQOS(pod) == qos.BestEffort
|
||||
}
|
||||
|
||||
func isTerminating(pod *api.Pod) bool {
|
||||
if pod.Spec.ActiveDeadlineSeconds != nil && *pod.Spec.ActiveDeadlineSeconds >= int64(0) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// QuotaPod returns true if the pod is eligible to track against a quota
|
||||
// if it's not in a terminal state according to its phase.
|
||||
func QuotaPod(pod *api.Pod) bool {
|
||||
return !(api.PodFailed == pod.Status.Phase || api.PodSucceeded == pod.Status.Phase)
|
||||
}
|
||||
|
||||
// QuotaV1Pod returns true if the pod is eligible to track against a quota
|
||||
// if it's not in a terminal state according to its phase.
|
||||
func QuotaV1Pod(pod *v1.Pod) bool {
|
||||
return !(v1.PodFailed == pod.Status.Phase || v1.PodSucceeded == pod.Status.Phase)
|
||||
}
|
||||
253
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/pods_test.go
generated
vendored
Normal file
253
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/pods_test.go
generated
vendored
Normal file
|
|
@ -0,0 +1,253 @@
|
|||
/*
|
||||
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 core
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/resource"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5/fake"
|
||||
"k8s.io/kubernetes/pkg/quota"
|
||||
)
|
||||
|
||||
func TestPodConstraintsFunc(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
pod *api.Pod
|
||||
required []api.ResourceName
|
||||
err string
|
||||
}{
|
||||
"init container resource invalid": {
|
||||
pod: &api.Pod{
|
||||
Spec: api.PodSpec{
|
||||
InitContainers: []api.Container{{
|
||||
Resources: api.ResourceRequirements{
|
||||
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("2m")},
|
||||
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("1m")},
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
err: `spec.initContainers[0].resources.limits: Invalid value: "1m": must be greater than or equal to cpu request`,
|
||||
},
|
||||
"container resource invalid": {
|
||||
pod: &api.Pod{
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{{
|
||||
Resources: api.ResourceRequirements{
|
||||
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("2m")},
|
||||
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("1m")},
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
err: `spec.containers[0].resources.limits: Invalid value: "1m": must be greater than or equal to cpu request`,
|
||||
},
|
||||
"init container resource missing": {
|
||||
pod: &api.Pod{
|
||||
Spec: api.PodSpec{
|
||||
InitContainers: []api.Container{{
|
||||
Resources: api.ResourceRequirements{
|
||||
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("1m")},
|
||||
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("2m")},
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
required: []api.ResourceName{api.ResourceMemory},
|
||||
err: `must specify memory`,
|
||||
},
|
||||
"container resource missing": {
|
||||
pod: &api.Pod{
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{{
|
||||
Resources: api.ResourceRequirements{
|
||||
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("1m")},
|
||||
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("2m")},
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
required: []api.ResourceName{api.ResourceMemory},
|
||||
err: `must specify memory`,
|
||||
},
|
||||
}
|
||||
for testName, test := range testCases {
|
||||
err := PodConstraintsFunc(test.required, test.pod)
|
||||
switch {
|
||||
case err != nil && len(test.err) == 0,
|
||||
err == nil && len(test.err) != 0,
|
||||
err != nil && test.err != err.Error():
|
||||
t.Errorf("%s unexpected error: %v", testName, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPodEvaluatorUsage(t *testing.T) {
|
||||
kubeClient := fake.NewSimpleClientset()
|
||||
evaluator := NewPodEvaluator(kubeClient, nil)
|
||||
testCases := map[string]struct {
|
||||
pod *api.Pod
|
||||
usage api.ResourceList
|
||||
}{
|
||||
"init container CPU": {
|
||||
pod: &api.Pod{
|
||||
Spec: api.PodSpec{
|
||||
InitContainers: []api.Container{{
|
||||
Resources: api.ResourceRequirements{
|
||||
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("1m")},
|
||||
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("2m")},
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
usage: api.ResourceList{
|
||||
api.ResourceRequestsCPU: resource.MustParse("1m"),
|
||||
api.ResourceLimitsCPU: resource.MustParse("2m"),
|
||||
api.ResourcePods: resource.MustParse("1"),
|
||||
api.ResourceCPU: resource.MustParse("1m"),
|
||||
},
|
||||
},
|
||||
"init container MEM": {
|
||||
pod: &api.Pod{
|
||||
Spec: api.PodSpec{
|
||||
InitContainers: []api.Container{{
|
||||
Resources: api.ResourceRequirements{
|
||||
Requests: api.ResourceList{api.ResourceMemory: resource.MustParse("1m")},
|
||||
Limits: api.ResourceList{api.ResourceMemory: resource.MustParse("2m")},
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
usage: api.ResourceList{
|
||||
api.ResourceRequestsMemory: resource.MustParse("1m"),
|
||||
api.ResourceLimitsMemory: resource.MustParse("2m"),
|
||||
api.ResourcePods: resource.MustParse("1"),
|
||||
api.ResourceMemory: resource.MustParse("1m"),
|
||||
},
|
||||
},
|
||||
"container CPU": {
|
||||
pod: &api.Pod{
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{{
|
||||
Resources: api.ResourceRequirements{
|
||||
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("1m")},
|
||||
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("2m")},
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
usage: api.ResourceList{
|
||||
api.ResourceRequestsCPU: resource.MustParse("1m"),
|
||||
api.ResourceLimitsCPU: resource.MustParse("2m"),
|
||||
api.ResourcePods: resource.MustParse("1"),
|
||||
api.ResourceCPU: resource.MustParse("1m"),
|
||||
},
|
||||
},
|
||||
"container MEM": {
|
||||
pod: &api.Pod{
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{{
|
||||
Resources: api.ResourceRequirements{
|
||||
Requests: api.ResourceList{api.ResourceMemory: resource.MustParse("1m")},
|
||||
Limits: api.ResourceList{api.ResourceMemory: resource.MustParse("2m")},
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
usage: api.ResourceList{
|
||||
api.ResourceRequestsMemory: resource.MustParse("1m"),
|
||||
api.ResourceLimitsMemory: resource.MustParse("2m"),
|
||||
api.ResourcePods: resource.MustParse("1"),
|
||||
api.ResourceMemory: resource.MustParse("1m"),
|
||||
},
|
||||
},
|
||||
"init container maximums override sum of containers": {
|
||||
pod: &api.Pod{
|
||||
Spec: api.PodSpec{
|
||||
InitContainers: []api.Container{
|
||||
{
|
||||
Resources: api.ResourceRequirements{
|
||||
Requests: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("4"),
|
||||
api.ResourceMemory: resource.MustParse("100M"),
|
||||
},
|
||||
Limits: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("8"),
|
||||
api.ResourceMemory: resource.MustParse("200M"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Resources: api.ResourceRequirements{
|
||||
Requests: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("1"),
|
||||
api.ResourceMemory: resource.MustParse("50M"),
|
||||
},
|
||||
Limits: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("2"),
|
||||
api.ResourceMemory: resource.MustParse("100M"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Resources: api.ResourceRequirements{
|
||||
Requests: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("1"),
|
||||
api.ResourceMemory: resource.MustParse("50M"),
|
||||
},
|
||||
Limits: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("2"),
|
||||
api.ResourceMemory: resource.MustParse("100M"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Resources: api.ResourceRequirements{
|
||||
Requests: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("2"),
|
||||
api.ResourceMemory: resource.MustParse("25M"),
|
||||
},
|
||||
Limits: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("5"),
|
||||
api.ResourceMemory: resource.MustParse("50M"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
usage: api.ResourceList{
|
||||
api.ResourceRequestsCPU: resource.MustParse("4"),
|
||||
api.ResourceRequestsMemory: resource.MustParse("100M"),
|
||||
api.ResourceLimitsCPU: resource.MustParse("8"),
|
||||
api.ResourceLimitsMemory: resource.MustParse("200M"),
|
||||
api.ResourcePods: resource.MustParse("1"),
|
||||
api.ResourceCPU: resource.MustParse("4"),
|
||||
api.ResourceMemory: resource.MustParse("100M"),
|
||||
},
|
||||
},
|
||||
}
|
||||
for testName, testCase := range testCases {
|
||||
actual := evaluator.Usage(testCase.pod)
|
||||
if !quota.Equals(testCase.usage, actual) {
|
||||
t.Errorf("%s expected: %v, actual: %v", testName, testCase.usage, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
48
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/registry.go
generated
vendored
Normal file
48
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/registry.go
generated
vendored
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
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 core
|
||||
|
||||
import (
|
||||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
|
||||
"k8s.io/kubernetes/pkg/controller/informers"
|
||||
"k8s.io/kubernetes/pkg/quota"
|
||||
"k8s.io/kubernetes/pkg/quota/generic"
|
||||
"k8s.io/kubernetes/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
// NewRegistry returns a registry that knows how to deal with core kubernetes resources
|
||||
// If an informer factory is provided, evaluators will use them.
|
||||
func NewRegistry(kubeClient clientset.Interface, f informers.SharedInformerFactory) quota.Registry {
|
||||
pod := NewPodEvaluator(kubeClient, f)
|
||||
service := NewServiceEvaluator(kubeClient)
|
||||
replicationController := NewReplicationControllerEvaluator(kubeClient)
|
||||
resourceQuota := NewResourceQuotaEvaluator(kubeClient)
|
||||
secret := NewSecretEvaluator(kubeClient)
|
||||
configMap := NewConfigMapEvaluator(kubeClient)
|
||||
persistentVolumeClaim := NewPersistentVolumeClaimEvaluator(kubeClient, f)
|
||||
return &generic.GenericRegistry{
|
||||
InternalEvaluators: map[schema.GroupKind]quota.Evaluator{
|
||||
pod.GroupKind(): pod,
|
||||
service.GroupKind(): service,
|
||||
replicationController.GroupKind(): replicationController,
|
||||
secret.GroupKind(): secret,
|
||||
configMap.GroupKind(): configMap,
|
||||
resourceQuota.GroupKind(): resourceQuota,
|
||||
persistentVolumeClaim.GroupKind(): persistentVolumeClaim,
|
||||
},
|
||||
}
|
||||
}
|
||||
54
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/replication_controllers.go
generated
vendored
Normal file
54
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/replication_controllers.go
generated
vendored
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
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 core
|
||||
|
||||
import (
|
||||
"k8s.io/kubernetes/pkg/admission"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
|
||||
"k8s.io/kubernetes/pkg/quota"
|
||||
"k8s.io/kubernetes/pkg/quota/generic"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
)
|
||||
|
||||
// NewReplicationControllerEvaluator returns an evaluator that can evaluate replication controllers
|
||||
func NewReplicationControllerEvaluator(kubeClient clientset.Interface) quota.Evaluator {
|
||||
allResources := []api.ResourceName{api.ResourceReplicationControllers}
|
||||
return &generic.GenericEvaluator{
|
||||
Name: "Evaluator.ReplicationController",
|
||||
InternalGroupKind: api.Kind("ReplicationController"),
|
||||
InternalOperationResources: map[admission.Operation][]api.ResourceName{
|
||||
admission.Create: allResources,
|
||||
},
|
||||
MatchedResourceNames: allResources,
|
||||
MatchesScopeFunc: generic.MatchesNoScopeFunc,
|
||||
ConstraintsFunc: generic.ObjectCountConstraintsFunc(api.ResourceReplicationControllers),
|
||||
UsageFunc: generic.ObjectCountUsageFunc(api.ResourceReplicationControllers),
|
||||
ListFuncByNamespace: func(namespace string, options v1.ListOptions) ([]runtime.Object, error) {
|
||||
itemList, err := kubeClient.Core().ReplicationControllers(namespace).List(options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results := make([]runtime.Object, 0, len(itemList.Items))
|
||||
for i := range itemList.Items {
|
||||
results = append(results, &itemList.Items[i])
|
||||
}
|
||||
return results, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
54
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/resource_quotas.go
generated
vendored
Normal file
54
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/resource_quotas.go
generated
vendored
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
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 core
|
||||
|
||||
import (
|
||||
"k8s.io/kubernetes/pkg/admission"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
|
||||
"k8s.io/kubernetes/pkg/quota"
|
||||
"k8s.io/kubernetes/pkg/quota/generic"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
)
|
||||
|
||||
// NewResourceQuotaEvaluator returns an evaluator that can evaluate resource quotas
|
||||
func NewResourceQuotaEvaluator(kubeClient clientset.Interface) quota.Evaluator {
|
||||
allResources := []api.ResourceName{api.ResourceQuotas}
|
||||
return &generic.GenericEvaluator{
|
||||
Name: "Evaluator.ResourceQuota",
|
||||
InternalGroupKind: api.Kind("ResourceQuota"),
|
||||
InternalOperationResources: map[admission.Operation][]api.ResourceName{
|
||||
admission.Create: allResources,
|
||||
},
|
||||
MatchedResourceNames: allResources,
|
||||
MatchesScopeFunc: generic.MatchesNoScopeFunc,
|
||||
ConstraintsFunc: generic.ObjectCountConstraintsFunc(api.ResourceQuotas),
|
||||
UsageFunc: generic.ObjectCountUsageFunc(api.ResourceQuotas),
|
||||
ListFuncByNamespace: func(namespace string, options v1.ListOptions) ([]runtime.Object, error) {
|
||||
itemList, err := kubeClient.Core().ResourceQuotas(namespace).List(options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results := make([]runtime.Object, 0, len(itemList.Items))
|
||||
for i := range itemList.Items {
|
||||
results = append(results, &itemList.Items[i])
|
||||
}
|
||||
return results, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
54
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/secrets.go
generated
vendored
Normal file
54
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/secrets.go
generated
vendored
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
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 core
|
||||
|
||||
import (
|
||||
"k8s.io/kubernetes/pkg/admission"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
|
||||
"k8s.io/kubernetes/pkg/quota"
|
||||
"k8s.io/kubernetes/pkg/quota/generic"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
)
|
||||
|
||||
// NewSecretEvaluator returns an evaluator that can evaluate secrets
|
||||
func NewSecretEvaluator(kubeClient clientset.Interface) quota.Evaluator {
|
||||
allResources := []api.ResourceName{api.ResourceSecrets}
|
||||
return &generic.GenericEvaluator{
|
||||
Name: "Evaluator.Secret",
|
||||
InternalGroupKind: api.Kind("Secret"),
|
||||
InternalOperationResources: map[admission.Operation][]api.ResourceName{
|
||||
admission.Create: allResources,
|
||||
},
|
||||
MatchedResourceNames: allResources,
|
||||
MatchesScopeFunc: generic.MatchesNoScopeFunc,
|
||||
ConstraintsFunc: generic.ObjectCountConstraintsFunc(api.ResourceSecrets),
|
||||
UsageFunc: generic.ObjectCountUsageFunc(api.ResourceSecrets),
|
||||
ListFuncByNamespace: func(namespace string, options v1.ListOptions) ([]runtime.Object, error) {
|
||||
itemList, err := kubeClient.Core().Secrets(namespace).List(options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results := make([]runtime.Object, 0, len(itemList.Items))
|
||||
for i := range itemList.Items {
|
||||
results = append(results, &itemList.Items[i])
|
||||
}
|
||||
return results, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
138
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/services.go
generated
vendored
Normal file
138
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/services.go
generated
vendored
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
/*
|
||||
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 core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"k8s.io/kubernetes/pkg/admission"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/resource"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
|
||||
"k8s.io/kubernetes/pkg/quota"
|
||||
"k8s.io/kubernetes/pkg/quota/generic"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/util/sets"
|
||||
)
|
||||
|
||||
// NewServiceEvaluator returns an evaluator that can evaluate service quotas
|
||||
func NewServiceEvaluator(kubeClient clientset.Interface) quota.Evaluator {
|
||||
allResources := []api.ResourceName{
|
||||
api.ResourceServices,
|
||||
api.ResourceServicesNodePorts,
|
||||
api.ResourceServicesLoadBalancers,
|
||||
}
|
||||
return &generic.GenericEvaluator{
|
||||
Name: "Evaluator.Service",
|
||||
InternalGroupKind: api.Kind("Service"),
|
||||
InternalOperationResources: map[admission.Operation][]api.ResourceName{
|
||||
admission.Create: allResources,
|
||||
admission.Update: allResources,
|
||||
},
|
||||
MatchedResourceNames: allResources,
|
||||
MatchesScopeFunc: generic.MatchesNoScopeFunc,
|
||||
ConstraintsFunc: ServiceConstraintsFunc,
|
||||
UsageFunc: ServiceUsageFunc,
|
||||
ListFuncByNamespace: func(namespace string, options v1.ListOptions) ([]runtime.Object, error) {
|
||||
itemList, err := kubeClient.Core().Services(namespace).List(options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results := make([]runtime.Object, 0, len(itemList.Items))
|
||||
for i := range itemList.Items {
|
||||
results = append(results, &itemList.Items[i])
|
||||
}
|
||||
return results, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// ServiceUsageFunc knows how to measure usage associated with services
|
||||
func ServiceUsageFunc(object runtime.Object) api.ResourceList {
|
||||
result := api.ResourceList{}
|
||||
var serviceType api.ServiceType
|
||||
var ports int
|
||||
|
||||
switch t := object.(type) {
|
||||
case *v1.Service:
|
||||
serviceType = api.ServiceType(t.Spec.Type)
|
||||
ports = len(t.Spec.Ports)
|
||||
case *api.Service:
|
||||
serviceType = t.Spec.Type
|
||||
ports = len(t.Spec.Ports)
|
||||
default:
|
||||
panic(fmt.Sprintf("expect *api.Service or *v1.Service, got %v", t))
|
||||
}
|
||||
|
||||
// default service usage
|
||||
result[api.ResourceServices] = resource.MustParse("1")
|
||||
result[api.ResourceServicesLoadBalancers] = resource.MustParse("0")
|
||||
result[api.ResourceServicesNodePorts] = resource.MustParse("0")
|
||||
switch serviceType {
|
||||
case api.ServiceTypeNodePort:
|
||||
// node port services need to count node ports
|
||||
value := resource.NewQuantity(int64(ports), resource.DecimalSI)
|
||||
result[api.ResourceServicesNodePorts] = *value
|
||||
case api.ServiceTypeLoadBalancer:
|
||||
// load balancer services need to count load balancers
|
||||
result[api.ResourceServicesLoadBalancers] = resource.MustParse("1")
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// QuotaServiceType returns true if the service type is eligible to track against a quota
|
||||
func QuotaServiceType(service *v1.Service) bool {
|
||||
switch service.Spec.Type {
|
||||
case v1.ServiceTypeNodePort, v1.ServiceTypeLoadBalancer:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//GetQuotaServiceType returns ServiceType if the service type is eligible to track against a quota, nor return ""
|
||||
func GetQuotaServiceType(service *v1.Service) v1.ServiceType {
|
||||
switch service.Spec.Type {
|
||||
case v1.ServiceTypeNodePort:
|
||||
return v1.ServiceTypeNodePort
|
||||
case v1.ServiceTypeLoadBalancer:
|
||||
return v1.ServiceTypeLoadBalancer
|
||||
}
|
||||
return v1.ServiceType("")
|
||||
}
|
||||
|
||||
// ServiceConstraintsFunc verifies that all required resources are captured in service usage.
|
||||
func ServiceConstraintsFunc(required []api.ResourceName, object runtime.Object) error {
|
||||
service, ok := object.(*api.Service)
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected input object %v", object)
|
||||
}
|
||||
|
||||
requiredSet := quota.ToSet(required)
|
||||
missingSet := sets.NewString()
|
||||
serviceUsage := ServiceUsageFunc(service)
|
||||
serviceSet := quota.ToSet(quota.ResourceNames(serviceUsage))
|
||||
if diff := requiredSet.Difference(serviceSet); len(diff) > 0 {
|
||||
missingSet.Insert(diff.List()...)
|
||||
}
|
||||
|
||||
if len(missingSet) == 0 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("must specify %s", strings.Join(missingSet.List(), ","))
|
||||
}
|
||||
180
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/services_test.go
generated
vendored
Normal file
180
vendor/k8s.io/kubernetes/pkg/quota/evaluator/core/services_test.go
generated
vendored
Normal file
|
|
@ -0,0 +1,180 @@
|
|||
/*
|
||||
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 core
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/resource"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5/fake"
|
||||
"k8s.io/kubernetes/pkg/quota"
|
||||
)
|
||||
|
||||
func TestServiceEvaluatorMatchesResources(t *testing.T) {
|
||||
kubeClient := fake.NewSimpleClientset()
|
||||
evaluator := NewServiceEvaluator(kubeClient)
|
||||
expected := quota.ToSet([]api.ResourceName{
|
||||
api.ResourceServices,
|
||||
api.ResourceServicesNodePorts,
|
||||
api.ResourceServicesLoadBalancers,
|
||||
})
|
||||
actual := quota.ToSet(evaluator.MatchesResources())
|
||||
if !expected.Equal(actual) {
|
||||
t.Errorf("expected: %v, actual: %v", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceEvaluatorUsage(t *testing.T) {
|
||||
kubeClient := fake.NewSimpleClientset()
|
||||
evaluator := NewServiceEvaluator(kubeClient)
|
||||
testCases := map[string]struct {
|
||||
service *api.Service
|
||||
usage api.ResourceList
|
||||
}{
|
||||
"loadbalancer": {
|
||||
service: &api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeLoadBalancer,
|
||||
},
|
||||
},
|
||||
usage: api.ResourceList{
|
||||
api.ResourceServicesNodePorts: resource.MustParse("0"),
|
||||
api.ResourceServicesLoadBalancers: resource.MustParse("1"),
|
||||
api.ResourceServices: resource.MustParse("1"),
|
||||
},
|
||||
},
|
||||
"clusterip": {
|
||||
service: &api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeClusterIP,
|
||||
},
|
||||
},
|
||||
usage: api.ResourceList{
|
||||
api.ResourceServices: resource.MustParse("1"),
|
||||
api.ResourceServicesNodePorts: resource.MustParse("0"),
|
||||
api.ResourceServicesLoadBalancers: resource.MustParse("0"),
|
||||
},
|
||||
},
|
||||
"nodeports": {
|
||||
service: &api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeNodePort,
|
||||
Ports: []api.ServicePort{
|
||||
{
|
||||
Port: 27443,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
usage: api.ResourceList{
|
||||
api.ResourceServices: resource.MustParse("1"),
|
||||
api.ResourceServicesNodePorts: resource.MustParse("1"),
|
||||
api.ResourceServicesLoadBalancers: resource.MustParse("0"),
|
||||
},
|
||||
},
|
||||
"multi-nodeports": {
|
||||
service: &api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeNodePort,
|
||||
Ports: []api.ServicePort{
|
||||
{
|
||||
Port: 27443,
|
||||
},
|
||||
{
|
||||
Port: 27444,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
usage: api.ResourceList{
|
||||
api.ResourceServices: resource.MustParse("1"),
|
||||
api.ResourceServicesNodePorts: resource.MustParse("2"),
|
||||
api.ResourceServicesLoadBalancers: resource.MustParse("0"),
|
||||
},
|
||||
},
|
||||
}
|
||||
for testName, testCase := range testCases {
|
||||
actual := evaluator.Usage(testCase.service)
|
||||
if !quota.Equals(testCase.usage, actual) {
|
||||
t.Errorf("%s expected: %v, actual: %v", testName, testCase.usage, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceConstraintsFunc(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
service *api.Service
|
||||
required []api.ResourceName
|
||||
err string
|
||||
}{
|
||||
"loadbalancer": {
|
||||
service: &api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeLoadBalancer,
|
||||
},
|
||||
},
|
||||
required: []api.ResourceName{api.ResourceServicesLoadBalancers},
|
||||
},
|
||||
"clusterip": {
|
||||
service: &api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeClusterIP,
|
||||
},
|
||||
},
|
||||
required: []api.ResourceName{api.ResourceServicesLoadBalancers, api.ResourceServices},
|
||||
},
|
||||
"nodeports": {
|
||||
service: &api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeNodePort,
|
||||
Ports: []api.ServicePort{
|
||||
{
|
||||
Port: 27443,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
required: []api.ResourceName{api.ResourceServicesNodePorts},
|
||||
},
|
||||
"multi-nodeports": {
|
||||
service: &api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Type: api.ServiceTypeNodePort,
|
||||
Ports: []api.ServicePort{
|
||||
{
|
||||
Port: 27443,
|
||||
},
|
||||
{
|
||||
Port: 27444,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
required: []api.ResourceName{api.ResourceServicesNodePorts},
|
||||
},
|
||||
}
|
||||
for testName, test := range testCases {
|
||||
err := ServiceConstraintsFunc(test.required, test.service)
|
||||
switch {
|
||||
case err != nil && len(test.err) == 0,
|
||||
err == nil && len(test.err) != 0,
|
||||
err != nil && test.err != err.Error():
|
||||
t.Errorf("%s unexpected error: %v", testName, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
31
vendor/k8s.io/kubernetes/pkg/quota/generic/BUILD
generated
vendored
Normal file
31
vendor/k8s.io/kubernetes/pkg/quota/generic/BUILD
generated
vendored
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_binary",
|
||||
"go_library",
|
||||
"go_test",
|
||||
"cgo_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"evaluator.go",
|
||||
"registry.go",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/admission:go_default_library",
|
||||
"//pkg/api:go_default_library",
|
||||
"//pkg/api/resource:go_default_library",
|
||||
"//pkg/api/v1:go_default_library",
|
||||
"//pkg/controller/informers:go_default_library",
|
||||
"//pkg/labels:go_default_library",
|
||||
"//pkg/quota:go_default_library",
|
||||
"//pkg/runtime:go_default_library",
|
||||
"//pkg/runtime/schema:go_default_library",
|
||||
],
|
||||
)
|
||||
211
vendor/k8s.io/kubernetes/pkg/quota/generic/evaluator.go
generated
vendored
Normal file
211
vendor/k8s.io/kubernetes/pkg/quota/generic/evaluator.go
generated
vendored
Normal file
|
|
@ -0,0 +1,211 @@
|
|||
/*
|
||||
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"
|
||||
|
||||
"k8s.io/kubernetes/pkg/admission"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/resource"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
"k8s.io/kubernetes/pkg/controller/informers"
|
||||
"k8s.io/kubernetes/pkg/labels"
|
||||
"k8s.io/kubernetes/pkg/quota"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
// ListResourceUsingInformerFunc returns a listing function based on the shared informer factory for the specified resource.
|
||||
func ListResourceUsingInformerFunc(f informers.SharedInformerFactory, groupResource schema.GroupResource) ListFuncByNamespace {
|
||||
return func(namespace string, options v1.ListOptions) ([]runtime.Object, error) {
|
||||
labelSelector, err := labels.Parse(options.LabelSelector)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
informer, err := f.ForResource(groupResource)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return informer.Lister().ByNamespace(namespace).List(labelSelector)
|
||||
}
|
||||
}
|
||||
|
||||
// ConstraintsFunc takes a list of required resources that must match on the input item
|
||||
type ConstraintsFunc func(required []api.ResourceName, item runtime.Object) error
|
||||
|
||||
// GetFuncByNamespace knows how to get a resource with specified namespace and name
|
||||
type GetFuncByNamespace func(namespace, name string) (runtime.Object, error)
|
||||
|
||||
// ListFuncByNamespace knows how to list resources in a namespace
|
||||
type ListFuncByNamespace func(namespace string, options v1.ListOptions) ([]runtime.Object, error)
|
||||
|
||||
// MatchesScopeFunc knows how to evaluate if an object matches a scope
|
||||
type MatchesScopeFunc func(scope api.ResourceQuotaScope, object runtime.Object) bool
|
||||
|
||||
// UsageFunc knows how to measure usage associated with an object
|
||||
type UsageFunc func(object runtime.Object) api.ResourceList
|
||||
|
||||
// MatchesNoScopeFunc returns false on all match checks
|
||||
func MatchesNoScopeFunc(scope api.ResourceQuotaScope, object runtime.Object) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ObjectCountConstraintsFunc returns ConstraintsFunc that returns nil if the
|
||||
// specified resource name is in the required set of resource names
|
||||
func ObjectCountConstraintsFunc(resourceName api.ResourceName) ConstraintsFunc {
|
||||
return func(required []api.ResourceName, item runtime.Object) error {
|
||||
if !quota.Contains(required, resourceName) {
|
||||
return fmt.Errorf("missing %s", resourceName)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// ObjectCountUsageFunc is useful if you are only counting your object
|
||||
// It always returns 1 as the usage for the named resource
|
||||
func ObjectCountUsageFunc(resourceName api.ResourceName) UsageFunc {
|
||||
return func(object runtime.Object) api.ResourceList {
|
||||
return api.ResourceList{
|
||||
resourceName: resource.MustParse("1"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GenericEvaluator provides an implementation for quota.Evaluator
|
||||
type GenericEvaluator struct {
|
||||
// Name used for logging
|
||||
Name string
|
||||
// The GroupKind that this evaluator tracks
|
||||
InternalGroupKind schema.GroupKind
|
||||
// The set of resources that are pertinent to the mapped operation
|
||||
InternalOperationResources map[admission.Operation][]api.ResourceName
|
||||
// The set of resource names this evaluator matches
|
||||
MatchedResourceNames []api.ResourceName
|
||||
// A function that knows how to evaluate a matches scope request
|
||||
MatchesScopeFunc MatchesScopeFunc
|
||||
// A function that knows how to return usage for an object
|
||||
UsageFunc UsageFunc
|
||||
// A function that knows how to list resources by namespace
|
||||
ListFuncByNamespace ListFuncByNamespace
|
||||
// A function that knows how to get resource in a namespace
|
||||
// This function must be specified if the evaluator needs to handle UPDATE
|
||||
GetFuncByNamespace GetFuncByNamespace
|
||||
// A function that checks required constraints are satisfied
|
||||
ConstraintsFunc ConstraintsFunc
|
||||
}
|
||||
|
||||
// Ensure that GenericEvaluator implements quota.Evaluator
|
||||
var _ quota.Evaluator = &GenericEvaluator{}
|
||||
|
||||
// Constraints checks required constraints are satisfied on the input object
|
||||
func (g *GenericEvaluator) Constraints(required []api.ResourceName, item runtime.Object) error {
|
||||
return g.ConstraintsFunc(required, item)
|
||||
}
|
||||
|
||||
// Get returns the object by namespace and name
|
||||
func (g *GenericEvaluator) Get(namespace, name string) (runtime.Object, error) {
|
||||
return g.GetFuncByNamespace(namespace, name)
|
||||
}
|
||||
|
||||
// OperationResources returns the set of resources that could be updated for the
|
||||
// specified operation for this kind. If empty, admission control will ignore
|
||||
// quota processing for the operation.
|
||||
func (g *GenericEvaluator) OperationResources(operation admission.Operation) []api.ResourceName {
|
||||
return g.InternalOperationResources[operation]
|
||||
}
|
||||
|
||||
// GroupKind that this evaluator tracks
|
||||
func (g *GenericEvaluator) GroupKind() schema.GroupKind {
|
||||
return g.InternalGroupKind
|
||||
}
|
||||
|
||||
// MatchesResources is the list of resources that this evaluator matches
|
||||
func (g *GenericEvaluator) MatchesResources() []api.ResourceName {
|
||||
return g.MatchedResourceNames
|
||||
}
|
||||
|
||||
// Matches returns true if the evaluator matches the specified quota with the provided input item
|
||||
func (g *GenericEvaluator) Matches(resourceQuota *api.ResourceQuota, item runtime.Object) bool {
|
||||
if resourceQuota == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// verify the quota matches on resource, by default its false
|
||||
matchResource := false
|
||||
for resourceName := range resourceQuota.Status.Hard {
|
||||
if g.MatchesResource(resourceName) {
|
||||
matchResource = true
|
||||
break
|
||||
}
|
||||
}
|
||||
// by default, no scopes matches all
|
||||
matchScope := true
|
||||
for _, scope := range resourceQuota.Spec.Scopes {
|
||||
matchScope = matchScope && g.MatchesScope(scope, item)
|
||||
}
|
||||
return matchResource && matchScope
|
||||
}
|
||||
|
||||
// MatchesResource returns true if this evaluator can match on the specified resource
|
||||
func (g *GenericEvaluator) MatchesResource(resourceName api.ResourceName) bool {
|
||||
for _, matchedResourceName := range g.MatchedResourceNames {
|
||||
if resourceName == matchedResourceName {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// MatchesScope returns true if the input object matches the specified scope
|
||||
func (g *GenericEvaluator) MatchesScope(scope api.ResourceQuotaScope, object runtime.Object) bool {
|
||||
return g.MatchesScopeFunc(scope, object)
|
||||
}
|
||||
|
||||
// Usage returns the resource usage for the specified object
|
||||
func (g *GenericEvaluator) Usage(object runtime.Object) api.ResourceList {
|
||||
return g.UsageFunc(object)
|
||||
}
|
||||
|
||||
// UsageStats calculates latest observed usage stats for all objects
|
||||
func (g *GenericEvaluator) UsageStats(options quota.UsageStatsOptions) (quota.UsageStats, error) {
|
||||
// default each tracked resource to zero
|
||||
result := quota.UsageStats{Used: api.ResourceList{}}
|
||||
for _, resourceName := range g.MatchedResourceNames {
|
||||
result.Used[resourceName] = resource.MustParse("0")
|
||||
}
|
||||
items, err := g.ListFuncByNamespace(options.Namespace, v1.ListOptions{
|
||||
LabelSelector: labels.Everything().String(),
|
||||
})
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("%s: Failed to list %v: %v", g.Name, g.GroupKind(), err)
|
||||
}
|
||||
for _, item := range items {
|
||||
// need to verify that the item matches the set of scopes
|
||||
matchesScopes := true
|
||||
for _, scope := range options.Scopes {
|
||||
if !g.MatchesScope(scope, item) {
|
||||
matchesScopes = false
|
||||
}
|
||||
}
|
||||
// only count usage if there was a match
|
||||
if matchesScopes {
|
||||
result.Used = quota.Add(result.Used, g.Usage(item))
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
36
vendor/k8s.io/kubernetes/pkg/quota/generic/registry.go
generated
vendored
Normal file
36
vendor/k8s.io/kubernetes/pkg/quota/generic/registry.go
generated
vendored
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
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 (
|
||||
"k8s.io/kubernetes/pkg/quota"
|
||||
"k8s.io/kubernetes/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
// Ensure it implements the required interface
|
||||
var _ quota.Registry = &GenericRegistry{}
|
||||
|
||||
// GenericRegistry implements Registry
|
||||
type GenericRegistry struct {
|
||||
// internal evaluators by group kind
|
||||
InternalEvaluators map[schema.GroupKind]quota.Evaluator
|
||||
}
|
||||
|
||||
// Evaluators returns the map of evaluators by groupKind
|
||||
func (r *GenericRegistry) Evaluators() map[schema.GroupKind]quota.Evaluator {
|
||||
return r.InternalEvaluators
|
||||
}
|
||||
23
vendor/k8s.io/kubernetes/pkg/quota/install/BUILD
generated
vendored
Normal file
23
vendor/k8s.io/kubernetes/pkg/quota/install/BUILD
generated
vendored
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_binary",
|
||||
"go_library",
|
||||
"go_test",
|
||||
"cgo_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["registry.go"],
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/client/clientset_generated/release_1_5:go_default_library",
|
||||
"//pkg/controller/informers:go_default_library",
|
||||
"//pkg/quota:go_default_library",
|
||||
"//pkg/quota/evaluator/core:go_default_library",
|
||||
],
|
||||
)
|
||||
31
vendor/k8s.io/kubernetes/pkg/quota/install/registry.go
generated
vendored
Normal file
31
vendor/k8s.io/kubernetes/pkg/quota/install/registry.go
generated
vendored
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
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 install
|
||||
|
||||
import (
|
||||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
|
||||
"k8s.io/kubernetes/pkg/controller/informers"
|
||||
"k8s.io/kubernetes/pkg/quota"
|
||||
"k8s.io/kubernetes/pkg/quota/evaluator/core"
|
||||
)
|
||||
|
||||
// NewRegistry returns a registry of quota evaluators.
|
||||
// If a shared informer factory is provided, it is used by evaluators rather than performing direct queries.
|
||||
func NewRegistry(kubeClient clientset.Interface, f informers.SharedInformerFactory) quota.Registry {
|
||||
// TODO: when quota supports resources in other api groups, we will need to merge
|
||||
return core.NewRegistry(kubeClient, f)
|
||||
}
|
||||
82
vendor/k8s.io/kubernetes/pkg/quota/interfaces.go
generated
vendored
Normal file
82
vendor/k8s.io/kubernetes/pkg/quota/interfaces.go
generated
vendored
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
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 (
|
||||
"k8s.io/kubernetes/pkg/admission"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
// 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 []api.ResourceQuotaScope
|
||||
}
|
||||
|
||||
// UsageStats is result of measuring observed resource use in the system
|
||||
type UsageStats struct {
|
||||
// Used maps resource to quantity used
|
||||
Used api.ResourceList
|
||||
}
|
||||
|
||||
// Evaluator knows how to evaluate quota usage for a particular group kind
|
||||
type Evaluator interface {
|
||||
// Constraints ensures that each required resource is present on item
|
||||
Constraints(required []api.ResourceName, item runtime.Object) error
|
||||
// Get returns the object with specified namespace and name
|
||||
Get(namespace, name string) (runtime.Object, error)
|
||||
// GroupKind returns the groupKind that this object knows how to evaluate
|
||||
GroupKind() schema.GroupKind
|
||||
// MatchesResources is the list of resources that this evaluator matches
|
||||
MatchesResources() []api.ResourceName
|
||||
// Matches returns true if the specified quota matches the input item
|
||||
Matches(resourceQuota *api.ResourceQuota, item runtime.Object) bool
|
||||
// OperationResources returns the set of resources that could be updated for the
|
||||
// specified operation for this kind. If empty, admission control will ignore
|
||||
// quota processing for the operation.
|
||||
OperationResources(operation admission.Operation) []api.ResourceName
|
||||
// Usage returns the resource usage for the specified object
|
||||
Usage(object runtime.Object) api.ResourceList
|
||||
// UsageStats calculates latest observed usage stats for all objects
|
||||
UsageStats(options UsageStatsOptions) (UsageStats, error)
|
||||
}
|
||||
|
||||
// Registry holds the list of evaluators associated to a particular group kind
|
||||
type Registry interface {
|
||||
// Evaluators returns the set Evaluator objects registered to a groupKind
|
||||
Evaluators() map[schema.GroupKind]Evaluator
|
||||
}
|
||||
|
||||
// UnionRegistry combines multiple registries. Order matters because first registry to claim a GroupKind
|
||||
// is the "winner"
|
||||
type UnionRegistry []Registry
|
||||
|
||||
func (r UnionRegistry) Evaluators() map[schema.GroupKind]Evaluator {
|
||||
ret := map[schema.GroupKind]Evaluator{}
|
||||
|
||||
for i := len(r) - 1; i >= 0; i-- {
|
||||
for k, v := range r[i].Evaluators() {
|
||||
ret[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
250
vendor/k8s.io/kubernetes/pkg/quota/resources.go
generated
vendored
Normal file
250
vendor/k8s.io/kubernetes/pkg/quota/resources.go
generated
vendored
Normal file
|
|
@ -0,0 +1,250 @@
|
|||
/*
|
||||
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 (
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/resource"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
"k8s.io/kubernetes/pkg/util/sets"
|
||||
)
|
||||
|
||||
// Equals returns true if the two lists are equivalent
|
||||
func Equals(a api.ResourceList, b api.ResourceList) bool {
|
||||
for key, value1 := range a {
|
||||
value2, found := b[key]
|
||||
if !found {
|
||||
return false
|
||||
}
|
||||
if value1.Cmp(value2) != 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for key, value1 := range b {
|
||||
value2, found := a[key]
|
||||
if !found {
|
||||
return false
|
||||
}
|
||||
if value1.Cmp(value2) != 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// V1Equals returns true if the two lists are equivalent
|
||||
func V1Equals(a v1.ResourceList, b v1.ResourceList) bool {
|
||||
for key, value1 := range a {
|
||||
value2, found := b[key]
|
||||
if !found {
|
||||
return false
|
||||
}
|
||||
if value1.Cmp(value2) != 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for key, value1 := range b {
|
||||
value2, found := a[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 api.ResourceList, b api.ResourceList) (bool, []api.ResourceName) {
|
||||
result := true
|
||||
resourceNames := []api.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 api.ResourceList, b api.ResourceList) api.ResourceList {
|
||||
result := api.ResourceList{}
|
||||
for key, value := range a {
|
||||
if other, found := b[key]; found {
|
||||
if value.Cmp(other) <= 0 {
|
||||
result[key] = *other.Copy()
|
||||
continue
|
||||
}
|
||||
}
|
||||
result[key] = *value.Copy()
|
||||
}
|
||||
for key, value := range b {
|
||||
if _, found := result[key]; !found {
|
||||
result[key] = *value.Copy()
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Add returns the result of a + b for each named resource
|
||||
func Add(a api.ResourceList, b api.ResourceList) api.ResourceList {
|
||||
result := api.ResourceList{}
|
||||
for key, value := range a {
|
||||
quantity := *value.Copy()
|
||||
if other, found := b[key]; found {
|
||||
quantity.Add(other)
|
||||
}
|
||||
result[key] = quantity
|
||||
}
|
||||
for key, value := range b {
|
||||
if _, found := result[key]; !found {
|
||||
quantity := *value.Copy()
|
||||
result[key] = quantity
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Subtract returns the result of a - b for each named resource
|
||||
func Subtract(a api.ResourceList, b api.ResourceList) api.ResourceList {
|
||||
result := api.ResourceList{}
|
||||
for key, value := range a {
|
||||
quantity := *value.Copy()
|
||||
if other, found := b[key]; found {
|
||||
quantity.Sub(other)
|
||||
}
|
||||
result[key] = quantity
|
||||
}
|
||||
for key, value := range b {
|
||||
if _, found := result[key]; !found {
|
||||
quantity := *value.Copy()
|
||||
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 api.ResourceList, names []api.ResourceName) api.ResourceList {
|
||||
nameSet := ToSet(names)
|
||||
result := api.ResourceList{}
|
||||
for key, value := range resources {
|
||||
if nameSet.Has(string(key)) {
|
||||
result[key] = *value.Copy()
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// ResourceNames returns a list of all resource names in the ResourceList
|
||||
func ResourceNames(resources api.ResourceList) []api.ResourceName {
|
||||
result := []api.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 []api.ResourceName, item api.ResourceName) bool {
|
||||
return ToSet(items).Has(string(item))
|
||||
}
|
||||
|
||||
// Intersection returns the intersection of both list of resources
|
||||
func Intersection(a []api.ResourceName, b []api.ResourceName) []api.ResourceName {
|
||||
setA := ToSet(a)
|
||||
setB := ToSet(b)
|
||||
setC := setA.Intersection(setB)
|
||||
result := []api.ResourceName{}
|
||||
for _, resourceName := range setC.List() {
|
||||
result = append(result, api.ResourceName(resourceName))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// IsZero returns true if each key maps to the quantity value 0
|
||||
func IsZero(a api.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 api.ResourceList) []api.ResourceName {
|
||||
results := []api.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 []api.ResourceName) sets.String {
|
||||
result := sets.NewString()
|
||||
for _, resourceName := range resourceNames {
|
||||
result.Insert(string(resourceName))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// CalculateUsage calculates and returns the requested ResourceList usage
|
||||
func CalculateUsage(namespaceName string, scopes []api.ResourceQuotaScope, hardLimits api.ResourceList, registry Registry) (api.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 := []api.ResourceName{}
|
||||
evaluators := registry.Evaluators()
|
||||
for _, evaluator := range evaluators {
|
||||
potentialResources = append(potentialResources, evaluator.MatchesResources()...)
|
||||
}
|
||||
matchedResources := Intersection(hardResources, potentialResources)
|
||||
|
||||
// sum the observed usage from each evaluator
|
||||
newUsage := api.ResourceList{}
|
||||
usageStatsOptions := UsageStatsOptions{Namespace: namespaceName, Scopes: scopes}
|
||||
for _, evaluator := range evaluators {
|
||||
// only trigger the evaluator if it matches a resource in the quota, otherwise, skip calculating anything
|
||||
if intersection := Intersection(evaluator.MatchesResources(), matchedResources); len(intersection) == 0 {
|
||||
continue
|
||||
}
|
||||
stats, err := evaluator.UsageStats(usageStatsOptions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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, nil
|
||||
}
|
||||
292
vendor/k8s.io/kubernetes/pkg/quota/resources_test.go
generated
vendored
Normal file
292
vendor/k8s.io/kubernetes/pkg/quota/resources_test.go
generated
vendored
Normal file
|
|
@ -0,0 +1,292 @@
|
|||
/*
|
||||
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 (
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/resource"
|
||||
)
|
||||
|
||||
func TestEquals(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
a api.ResourceList
|
||||
b api.ResourceList
|
||||
expected bool
|
||||
}{
|
||||
"isEqual": {
|
||||
a: api.ResourceList{},
|
||||
b: api.ResourceList{},
|
||||
expected: true,
|
||||
},
|
||||
"isEqualWithKeys": {
|
||||
a: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("100m"),
|
||||
api.ResourceMemory: resource.MustParse("1Gi"),
|
||||
},
|
||||
b: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("100m"),
|
||||
api.ResourceMemory: resource.MustParse("1Gi"),
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
"isNotEqualSameKeys": {
|
||||
a: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("200m"),
|
||||
api.ResourceMemory: resource.MustParse("1Gi"),
|
||||
},
|
||||
b: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("100m"),
|
||||
api.ResourceMemory: resource.MustParse("1Gi"),
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
"isNotEqualDiffKeys": {
|
||||
a: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("100m"),
|
||||
api.ResourceMemory: resource.MustParse("1Gi"),
|
||||
},
|
||||
b: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("100m"),
|
||||
api.ResourceMemory: resource.MustParse("1Gi"),
|
||||
api.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 api.ResourceList
|
||||
b api.ResourceList
|
||||
expected api.ResourceList
|
||||
}{
|
||||
"noKeys": {
|
||||
a: api.ResourceList{},
|
||||
b: api.ResourceList{},
|
||||
expected: api.ResourceList{},
|
||||
},
|
||||
"toEmpty": {
|
||||
a: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
||||
b: api.ResourceList{},
|
||||
expected: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
||||
},
|
||||
"matching": {
|
||||
a: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
||||
b: api.ResourceList{api.ResourceCPU: resource.MustParse("150m")},
|
||||
expected: api.ResourceList{api.ResourceCPU: resource.MustParse("150m")},
|
||||
},
|
||||
"matching(reverse)": {
|
||||
a: api.ResourceList{api.ResourceCPU: resource.MustParse("150m")},
|
||||
b: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
||||
expected: api.ResourceList{api.ResourceCPU: resource.MustParse("150m")},
|
||||
},
|
||||
}
|
||||
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 api.ResourceList
|
||||
b api.ResourceList
|
||||
expected api.ResourceList
|
||||
}{
|
||||
"noKeys": {
|
||||
a: api.ResourceList{},
|
||||
b: api.ResourceList{},
|
||||
expected: api.ResourceList{},
|
||||
},
|
||||
"toEmpty": {
|
||||
a: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
||||
b: api.ResourceList{},
|
||||
expected: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
||||
},
|
||||
"matching": {
|
||||
a: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
||||
b: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
||||
expected: api.ResourceList{api.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 api.ResourceList
|
||||
b api.ResourceList
|
||||
expected api.ResourceList
|
||||
}{
|
||||
"noKeys": {
|
||||
a: api.ResourceList{},
|
||||
b: api.ResourceList{},
|
||||
expected: api.ResourceList{},
|
||||
},
|
||||
"value-empty": {
|
||||
a: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
||||
b: api.ResourceList{},
|
||||
expected: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
||||
},
|
||||
"empty-value": {
|
||||
a: api.ResourceList{},
|
||||
b: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
||||
expected: api.ResourceList{api.ResourceCPU: resource.MustParse("-100m")},
|
||||
},
|
||||
"value-value": {
|
||||
a: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
||||
b: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
||||
expected: api.ResourceList{api.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 api.ResourceList
|
||||
expected []api.ResourceName
|
||||
}{
|
||||
"empty": {
|
||||
a: api.ResourceList{},
|
||||
expected: []api.ResourceName{},
|
||||
},
|
||||
"values": {
|
||||
a: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("100m"),
|
||||
api.ResourceMemory: resource.MustParse("1Gi"),
|
||||
},
|
||||
expected: []api.ResourceName{api.ResourceMemory, api.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 []api.ResourceName
|
||||
b api.ResourceName
|
||||
expected bool
|
||||
}{
|
||||
"does-not-contain": {
|
||||
a: []api.ResourceName{api.ResourceMemory},
|
||||
b: api.ResourceCPU,
|
||||
expected: false,
|
||||
},
|
||||
"does-contain": {
|
||||
a: []api.ResourceName{api.ResourceMemory, api.ResourceCPU},
|
||||
b: api.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 TestIsZero(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
a api.ResourceList
|
||||
expected bool
|
||||
}{
|
||||
"empty": {
|
||||
a: api.ResourceList{},
|
||||
expected: true,
|
||||
},
|
||||
"zero": {
|
||||
a: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("0"),
|
||||
api.ResourceMemory: resource.MustParse("0"),
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
"non-zero": {
|
||||
a: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("200m"),
|
||||
api.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 api.ResourceList
|
||||
expected []api.ResourceName
|
||||
}{
|
||||
"empty": {
|
||||
a: api.ResourceList{},
|
||||
expected: []api.ResourceName{},
|
||||
},
|
||||
"some-negative": {
|
||||
a: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("-10"),
|
||||
api.ResourceMemory: resource.MustParse("0"),
|
||||
},
|
||||
expected: []api.ResourceName{api.ResourceCPU},
|
||||
},
|
||||
"all-negative": {
|
||||
a: api.ResourceList{
|
||||
api.ResourceCPU: resource.MustParse("-200m"),
|
||||
api.ResourceMemory: resource.MustParse("-1Gi"),
|
||||
},
|
||||
expected: []api.ResourceName{api.ResourceCPU, api.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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue