From 6c8d61db1f199ccd758287cf3d91f7d2956c0962 Mon Sep 17 00:00:00 2001 From: deads2k Date: Wed, 25 May 2016 13:01:07 -0400 Subject: [PATCH] add clusterquota types --- pkg/api/install/install.go | 1 + pkg/api/validation/register.go | 5 + pkg/quota/api/deep_copy_generated.go | 96 ++++++++ pkg/quota/api/install/install.go | 109 +++++++++ pkg/quota/api/register.go | 37 +++ pkg/quota/api/types.go | 53 +++++ pkg/quota/api/v1/conversion_generated.go | 238 ++++++++++++++++++++ pkg/quota/api/v1/deep_copy_generated.go | 97 ++++++++ pkg/quota/api/v1/register.go | 37 +++ pkg/quota/api/v1/swagger_doc.go | 47 ++++ pkg/quota/api/v1/types.go | 53 +++++ pkg/quota/api/validation/validation.go | 38 ++++ pkg/quota/api/validation/validation_test.go | 9 + tools/genconversion/conversion.go | 2 + tools/gendeepcopy/deep_copy.go | 2 + 15 files changed, 824 insertions(+) create mode 100644 pkg/quota/api/deep_copy_generated.go create mode 100644 pkg/quota/api/install/install.go create mode 100644 pkg/quota/api/register.go create mode 100644 pkg/quota/api/types.go create mode 100644 pkg/quota/api/v1/conversion_generated.go create mode 100644 pkg/quota/api/v1/deep_copy_generated.go create mode 100644 pkg/quota/api/v1/register.go create mode 100644 pkg/quota/api/v1/swagger_doc.go create mode 100644 pkg/quota/api/v1/types.go create mode 100644 pkg/quota/api/validation/validation.go create mode 100644 pkg/quota/api/validation/validation_test.go diff --git a/pkg/api/install/install.go b/pkg/api/install/install.go index 77259ab30558..0daf28ceffe2 100644 --- a/pkg/api/install/install.go +++ b/pkg/api/install/install.go @@ -12,6 +12,7 @@ import ( _ "github.com/openshift/origin/pkg/image/api/install" _ "github.com/openshift/origin/pkg/oauth/api/install" _ "github.com/openshift/origin/pkg/project/api/install" + _ "github.com/openshift/origin/pkg/quota/api/install" _ "github.com/openshift/origin/pkg/route/api/install" _ "github.com/openshift/origin/pkg/sdn/api/install" _ "github.com/openshift/origin/pkg/template/api/install" diff --git a/pkg/api/validation/register.go b/pkg/api/validation/register.go index b8b82e706b6d..8185986c5582 100644 --- a/pkg/api/validation/register.go +++ b/pkg/api/validation/register.go @@ -9,6 +9,7 @@ import ( imagevalidation "github.com/openshift/origin/pkg/image/api/validation" oauthvalidation "github.com/openshift/origin/pkg/oauth/api/validation" projectvalidation "github.com/openshift/origin/pkg/project/api/validation" + quotavalidation "github.com/openshift/origin/pkg/quota/api/validation" routevalidation "github.com/openshift/origin/pkg/route/api/validation" sdnvalidation "github.com/openshift/origin/pkg/sdn/api/validation" templatevalidation "github.com/openshift/origin/pkg/template/api/validation" @@ -21,6 +22,7 @@ import ( imageapi "github.com/openshift/origin/pkg/image/api" oauthapi "github.com/openshift/origin/pkg/oauth/api" projectapi "github.com/openshift/origin/pkg/project/api" + quotaapi "github.com/openshift/origin/pkg/quota/api" routeapi "github.com/openshift/origin/pkg/route/api" sdnapi "github.com/openshift/origin/pkg/sdn/api" templateapi "github.com/openshift/origin/pkg/template/api" @@ -88,4 +90,7 @@ func registerAll() { Validator.MustRegister(&userapi.Identity{}, uservalidation.ValidateIdentity, uservalidation.ValidateIdentityUpdate) Validator.MustRegister(&userapi.UserIdentityMapping{}, uservalidation.ValidateUserIdentityMapping, uservalidation.ValidateUserIdentityMappingUpdate) Validator.MustRegister(&userapi.Group{}, uservalidation.ValidateGroup, uservalidation.ValidateGroupUpdate) + + Validator.MustRegister("aapi.ClusterResourceQuota{}, quotavalidation.ValidateClusterResourceQuota, quotavalidation.ValidateClusterResourceQuotaUpdate) + } diff --git a/pkg/quota/api/deep_copy_generated.go b/pkg/quota/api/deep_copy_generated.go new file mode 100644 index 000000000000..435e88c2c27e --- /dev/null +++ b/pkg/quota/api/deep_copy_generated.go @@ -0,0 +1,96 @@ +// +build !ignore_autogenerated + +// This file was autogenerated by deepcopy-gen. Do not edit it manually! + +package api + +import ( + api "k8s.io/kubernetes/pkg/api" + unversioned "k8s.io/kubernetes/pkg/api/unversioned" + conversion "k8s.io/kubernetes/pkg/conversion" +) + +func init() { + if err := api.Scheme.AddGeneratedDeepCopyFuncs( + DeepCopy_api_ClusterResourceQuota, + DeepCopy_api_ClusterResourceQuotaList, + DeepCopy_api_ClusterResourceQuotaSpec, + DeepCopy_api_ClusterResourceQuotaStatus, + ); err != nil { + // if one of the deep copy functions is malformed, detect it immediately. + panic(err) + } +} + +func DeepCopy_api_ClusterResourceQuota(in ClusterResourceQuota, out *ClusterResourceQuota, c *conversion.Cloner) error { + if err := unversioned.DeepCopy_unversioned_TypeMeta(in.TypeMeta, &out.TypeMeta, c); err != nil { + return err + } + if err := api.DeepCopy_api_ObjectMeta(in.ObjectMeta, &out.ObjectMeta, c); err != nil { + return err + } + if err := DeepCopy_api_ClusterResourceQuotaSpec(in.Spec, &out.Spec, c); err != nil { + return err + } + if err := DeepCopy_api_ClusterResourceQuotaStatus(in.Status, &out.Status, c); err != nil { + return err + } + return nil +} + +func DeepCopy_api_ClusterResourceQuotaList(in ClusterResourceQuotaList, out *ClusterResourceQuotaList, c *conversion.Cloner) error { + if err := unversioned.DeepCopy_unversioned_TypeMeta(in.TypeMeta, &out.TypeMeta, c); err != nil { + return err + } + if err := unversioned.DeepCopy_unversioned_ListMeta(in.ListMeta, &out.ListMeta, c); err != nil { + return err + } + if in.Items != nil { + in, out := in.Items, &out.Items + *out = make([]ClusterResourceQuota, len(in)) + for i := range in { + if err := DeepCopy_api_ClusterResourceQuota(in[i], &(*out)[i], c); err != nil { + return err + } + } + } else { + out.Items = nil + } + return nil +} + +func DeepCopy_api_ClusterResourceQuotaSpec(in ClusterResourceQuotaSpec, out *ClusterResourceQuotaSpec, c *conversion.Cloner) error { + if in.Selector != nil { + in, out := in.Selector, &out.Selector + *out = make(map[string]string) + for key, val := range in { + (*out)[key] = val + } + } else { + out.Selector = nil + } + if err := api.DeepCopy_api_ResourceQuotaSpec(in.Quota, &out.Quota, c); err != nil { + return err + } + return nil +} + +func DeepCopy_api_ClusterResourceQuotaStatus(in ClusterResourceQuotaStatus, out *ClusterResourceQuotaStatus, c *conversion.Cloner) error { + if err := api.DeepCopy_api_ResourceQuotaStatus(in.Overall, &out.Overall, c); err != nil { + return err + } + if in.ByNamespace != nil { + in, out := in.ByNamespace, &out.ByNamespace + *out = make(map[string]api.ResourceList) + for key, val := range in { + if newVal, err := c.DeepCopy(val); err != nil { + return err + } else { + (*out)[key] = newVal.(api.ResourceList) + } + } + } else { + out.ByNamespace = nil + } + return nil +} diff --git a/pkg/quota/api/install/install.go b/pkg/quota/api/install/install.go new file mode 100644 index 000000000000..780d100a5cde --- /dev/null +++ b/pkg/quota/api/install/install.go @@ -0,0 +1,109 @@ +package install + +import ( + "fmt" + + "github.com/golang/glog" + + kapi "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/meta" + "k8s.io/kubernetes/pkg/api/unversioned" + "k8s.io/kubernetes/pkg/apimachinery" + "k8s.io/kubernetes/pkg/apimachinery/registered" + "k8s.io/kubernetes/pkg/runtime" + "k8s.io/kubernetes/pkg/util/sets" + + quotaapi "github.com/openshift/origin/pkg/quota/api" + quotaapiv1 "github.com/openshift/origin/pkg/quota/api/v1" +) + +const importPrefix = "github.com/openshift/origin/pkg/quota/api" + +var accessor = meta.NewAccessor() + +// availableVersions lists all known external versions for this group from most preferred to least preferred +var availableVersions = []unversioned.GroupVersion{quotaapiv1.SchemeGroupVersion} + +func init() { + registered.RegisterVersions(availableVersions) + externalVersions := []unversioned.GroupVersion{} + for _, v := range availableVersions { + if registered.IsAllowedVersion(v) { + externalVersions = append(externalVersions, v) + } + } + if len(externalVersions) == 0 { + glog.V(4).Infof("No version is registered for group %v", quotaapi.GroupName) + return + } + + if err := registered.EnableVersions(externalVersions...); err != nil { + glog.V(4).Infof("%v", err) + return + } + if err := enableVersions(externalVersions); err != nil { + glog.V(4).Infof("%v", err) + return + } +} + +// TODO: enableVersions should be centralized rather than spread in each API +// group. +// We can combine registered.RegisterVersions, registered.EnableVersions and +// registered.RegisterGroup once we have moved enableVersions there. +func enableVersions(externalVersions []unversioned.GroupVersion) error { + addVersionsToScheme(externalVersions...) + preferredExternalVersion := externalVersions[0] + + groupMeta := apimachinery.GroupMeta{ + GroupVersion: preferredExternalVersion, + GroupVersions: externalVersions, + RESTMapper: newRESTMapper(externalVersions), + SelfLinker: runtime.SelfLinker(accessor), + InterfacesFor: interfacesFor, + } + + if err := registered.RegisterGroup(groupMeta); err != nil { + return err + } + kapi.RegisterRESTMapper(groupMeta.RESTMapper) + return nil +} + +func newRESTMapper(externalVersions []unversioned.GroupVersion) meta.RESTMapper { + rootScoped := sets.NewString("ClusterResourceQuota") + ignoredKinds := sets.NewString() + + return kapi.NewDefaultRESTMapper(externalVersions, interfacesFor, importPrefix, ignoredKinds, rootScoped) +} + +// interfacesFor returns the default Codec and ResourceVersioner for a given version +// string, or an error if the version is not known. +func interfacesFor(version unversioned.GroupVersion) (*meta.VersionInterfaces, error) { + switch version { + case quotaapiv1.SchemeGroupVersion: + return &meta.VersionInterfaces{ + ObjectConvertor: kapi.Scheme, + MetadataAccessor: accessor, + }, nil + default: + g, _ := registered.Group(quotaapi.GroupName) + return nil, fmt.Errorf("unsupported storage version: %s (valid: %v)", version, g.GroupVersions) + } +} + +func addVersionsToScheme(externalVersions ...unversioned.GroupVersion) { + // add the internal version to Scheme + quotaapi.AddToScheme(kapi.Scheme) + // add the enabled external versions to Scheme + for _, v := range externalVersions { + if !registered.IsEnabledVersion(v) { + glog.Errorf("Version %s is not enabled, so it will not be added to the Scheme.", v) + continue + } + switch v { + case quotaapiv1.SchemeGroupVersion: + quotaapiv1.AddToScheme(kapi.Scheme) + } + } +} diff --git a/pkg/quota/api/register.go b/pkg/quota/api/register.go new file mode 100644 index 000000000000..2b830ccbf969 --- /dev/null +++ b/pkg/quota/api/register.go @@ -0,0 +1,37 @@ +package api + +import ( + kapi "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/unversioned" + "k8s.io/kubernetes/pkg/runtime" +) + +const GroupName = "quota.openshift.io" + +var SchemeGroupVersion = unversioned.GroupVersion{Group: GroupName, Version: runtime.APIVersionInternal} + +// Kind takes an unqualified kind and returns back a Group qualified GroupKind +func Kind(kind string) unversioned.GroupKind { + return SchemeGroupVersion.WithKind(kind).GroupKind() +} + +// Resource takes an unqualified resource and returns back a Group qualified GroupResource +func Resource(resource string) unversioned.GroupResource { + return SchemeGroupVersion.WithResource(resource).GroupResource() +} + +func AddToScheme(scheme *runtime.Scheme) { + addKnownTypes(scheme) +} + +// Adds the list of known types to api.Scheme. +func addKnownTypes(scheme *runtime.Scheme) { + scheme.AddKnownTypes(SchemeGroupVersion, + &kapi.ListOptions{}, + &ClusterResourceQuota{}, + &ClusterResourceQuotaList{}, + ) +} + +func (obj *ClusterResourceQuotaList) GetObjectKind() unversioned.ObjectKind { return &obj.TypeMeta } +func (obj *ClusterResourceQuota) GetObjectKind() unversioned.ObjectKind { return &obj.TypeMeta } diff --git a/pkg/quota/api/types.go b/pkg/quota/api/types.go new file mode 100644 index 000000000000..aa3abebe27d8 --- /dev/null +++ b/pkg/quota/api/types.go @@ -0,0 +1,53 @@ +package api + +import ( + kapi "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/unversioned" +) + +// ClusterResourceQuota mirrors ResourceQuota at a cluster scope. This object is easily convertible to +// synthetic ResourceQuota object to allow quota evaluation re-use. +type ClusterResourceQuota struct { + unversioned.TypeMeta + // Standard object's metadata. + kapi.ObjectMeta + + // Spec defines the desired quota + Spec ClusterResourceQuotaSpec + + // Status defines the actual enforced quota and its current usage + Status ClusterResourceQuotaStatus +} + +// ClusterResourceQuotaSpec defines the desired quota restrictions +type ClusterResourceQuotaSpec struct { + // Selector is the label selector used to match projects. It is not allowed to be empty + // and should only select active projects on the scale of dozens (though it can select + // many more less active projects). These projects will contend on object creation through + // this resource. + Selector map[string]string + + // Spec defines the desired quota + Quota kapi.ResourceQuotaSpec +} + +// ClusterResourceQuotaStatus defines the actual enforced quota and its current usage +type ClusterResourceQuotaStatus struct { + // Overall defines the actual enforced quota and its current usage across all namespaces + Overall kapi.ResourceQuotaStatus + + // ByNamespace slices the usage by namespace. This division allows for quick resolution of + // deletion reconcilation inside of a single namespace without requiring a recalculation + // across all namespaces. This map can be used to pull the deltas for a given namespace. + ByNamespace map[string]kapi.ResourceList +} + +// ClusterResourceQuotaList is a collection of ClusterResourceQuotas +type ClusterResourceQuotaList struct { + unversioned.TypeMeta + // Standard object's metadata. + unversioned.ListMeta + + // Items is a list of ClusterResourceQuotas + Items []ClusterResourceQuota +} diff --git a/pkg/quota/api/v1/conversion_generated.go b/pkg/quota/api/v1/conversion_generated.go new file mode 100644 index 000000000000..b9caf6fce07c --- /dev/null +++ b/pkg/quota/api/v1/conversion_generated.go @@ -0,0 +1,238 @@ +// +build !ignore_autogenerated + +// This file was autogenerated by conversion-gen. Do not edit it manually! + +package v1 + +import ( + quota_api "github.com/openshift/origin/pkg/quota/api" + api "k8s.io/kubernetes/pkg/api" + api_v1 "k8s.io/kubernetes/pkg/api/v1" + conversion "k8s.io/kubernetes/pkg/conversion" + reflect "reflect" +) + +func init() { + if err := api.Scheme.AddGeneratedConversionFuncs( + Convert_v1_ClusterResourceQuota_To_api_ClusterResourceQuota, + Convert_api_ClusterResourceQuota_To_v1_ClusterResourceQuota, + Convert_v1_ClusterResourceQuotaList_To_api_ClusterResourceQuotaList, + Convert_api_ClusterResourceQuotaList_To_v1_ClusterResourceQuotaList, + Convert_v1_ClusterResourceQuotaSpec_To_api_ClusterResourceQuotaSpec, + Convert_api_ClusterResourceQuotaSpec_To_v1_ClusterResourceQuotaSpec, + Convert_v1_ClusterResourceQuotaStatus_To_api_ClusterResourceQuotaStatus, + Convert_api_ClusterResourceQuotaStatus_To_v1_ClusterResourceQuotaStatus, + ); err != nil { + // if one of the conversion functions is malformed, detect it immediately. + panic(err) + } +} + +func autoConvert_v1_ClusterResourceQuota_To_api_ClusterResourceQuota(in *ClusterResourceQuota, out *quota_api.ClusterResourceQuota, s conversion.Scope) error { + if defaulting, found := s.DefaultingInterface(reflect.TypeOf(*in)); found { + defaulting.(func(*ClusterResourceQuota))(in) + } + if err := api.Convert_unversioned_TypeMeta_To_unversioned_TypeMeta(&in.TypeMeta, &out.TypeMeta, s); err != nil { + return err + } + // TODO: Inefficient conversion - can we improve it? + if err := s.Convert(&in.ObjectMeta, &out.ObjectMeta, 0); err != nil { + return err + } + if err := Convert_v1_ClusterResourceQuotaSpec_To_api_ClusterResourceQuotaSpec(&in.Spec, &out.Spec, s); err != nil { + return err + } + if err := Convert_v1_ClusterResourceQuotaStatus_To_api_ClusterResourceQuotaStatus(&in.Status, &out.Status, s); err != nil { + return err + } + return nil +} + +func Convert_v1_ClusterResourceQuota_To_api_ClusterResourceQuota(in *ClusterResourceQuota, out *quota_api.ClusterResourceQuota, s conversion.Scope) error { + return autoConvert_v1_ClusterResourceQuota_To_api_ClusterResourceQuota(in, out, s) +} + +func autoConvert_api_ClusterResourceQuota_To_v1_ClusterResourceQuota(in *quota_api.ClusterResourceQuota, out *ClusterResourceQuota, s conversion.Scope) error { + if defaulting, found := s.DefaultingInterface(reflect.TypeOf(*in)); found { + defaulting.(func(*quota_api.ClusterResourceQuota))(in) + } + if err := api.Convert_unversioned_TypeMeta_To_unversioned_TypeMeta(&in.TypeMeta, &out.TypeMeta, s); err != nil { + return err + } + // TODO: Inefficient conversion - can we improve it? + if err := s.Convert(&in.ObjectMeta, &out.ObjectMeta, 0); err != nil { + return err + } + if err := Convert_api_ClusterResourceQuotaSpec_To_v1_ClusterResourceQuotaSpec(&in.Spec, &out.Spec, s); err != nil { + return err + } + if err := Convert_api_ClusterResourceQuotaStatus_To_v1_ClusterResourceQuotaStatus(&in.Status, &out.Status, s); err != nil { + return err + } + return nil +} + +func Convert_api_ClusterResourceQuota_To_v1_ClusterResourceQuota(in *quota_api.ClusterResourceQuota, out *ClusterResourceQuota, s conversion.Scope) error { + return autoConvert_api_ClusterResourceQuota_To_v1_ClusterResourceQuota(in, out, s) +} + +func autoConvert_v1_ClusterResourceQuotaList_To_api_ClusterResourceQuotaList(in *ClusterResourceQuotaList, out *quota_api.ClusterResourceQuotaList, s conversion.Scope) error { + if defaulting, found := s.DefaultingInterface(reflect.TypeOf(*in)); found { + defaulting.(func(*ClusterResourceQuotaList))(in) + } + if err := api.Convert_unversioned_TypeMeta_To_unversioned_TypeMeta(&in.TypeMeta, &out.TypeMeta, s); err != nil { + return err + } + if err := api.Convert_unversioned_ListMeta_To_unversioned_ListMeta(&in.ListMeta, &out.ListMeta, s); err != nil { + return err + } + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]quota_api.ClusterResourceQuota, len(*in)) + for i := range *in { + if err := Convert_v1_ClusterResourceQuota_To_api_ClusterResourceQuota(&(*in)[i], &(*out)[i], s); err != nil { + return err + } + } + } else { + out.Items = nil + } + return nil +} + +func Convert_v1_ClusterResourceQuotaList_To_api_ClusterResourceQuotaList(in *ClusterResourceQuotaList, out *quota_api.ClusterResourceQuotaList, s conversion.Scope) error { + return autoConvert_v1_ClusterResourceQuotaList_To_api_ClusterResourceQuotaList(in, out, s) +} + +func autoConvert_api_ClusterResourceQuotaList_To_v1_ClusterResourceQuotaList(in *quota_api.ClusterResourceQuotaList, out *ClusterResourceQuotaList, s conversion.Scope) error { + if defaulting, found := s.DefaultingInterface(reflect.TypeOf(*in)); found { + defaulting.(func(*quota_api.ClusterResourceQuotaList))(in) + } + if err := api.Convert_unversioned_TypeMeta_To_unversioned_TypeMeta(&in.TypeMeta, &out.TypeMeta, s); err != nil { + return err + } + if err := api.Convert_unversioned_ListMeta_To_unversioned_ListMeta(&in.ListMeta, &out.ListMeta, s); err != nil { + return err + } + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]ClusterResourceQuota, len(*in)) + for i := range *in { + if err := Convert_api_ClusterResourceQuota_To_v1_ClusterResourceQuota(&(*in)[i], &(*out)[i], s); err != nil { + return err + } + } + } else { + out.Items = nil + } + return nil +} + +func Convert_api_ClusterResourceQuotaList_To_v1_ClusterResourceQuotaList(in *quota_api.ClusterResourceQuotaList, out *ClusterResourceQuotaList, s conversion.Scope) error { + return autoConvert_api_ClusterResourceQuotaList_To_v1_ClusterResourceQuotaList(in, out, s) +} + +func autoConvert_v1_ClusterResourceQuotaSpec_To_api_ClusterResourceQuotaSpec(in *ClusterResourceQuotaSpec, out *quota_api.ClusterResourceQuotaSpec, s conversion.Scope) error { + if defaulting, found := s.DefaultingInterface(reflect.TypeOf(*in)); found { + defaulting.(func(*ClusterResourceQuotaSpec))(in) + } + if in.Selector != nil { + in, out := &in.Selector, &out.Selector + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } else { + out.Selector = nil + } + // TODO: Inefficient conversion - can we improve it? + if err := s.Convert(&in.Quota, &out.Quota, 0); err != nil { + return err + } + return nil +} + +func Convert_v1_ClusterResourceQuotaSpec_To_api_ClusterResourceQuotaSpec(in *ClusterResourceQuotaSpec, out *quota_api.ClusterResourceQuotaSpec, s conversion.Scope) error { + return autoConvert_v1_ClusterResourceQuotaSpec_To_api_ClusterResourceQuotaSpec(in, out, s) +} + +func autoConvert_api_ClusterResourceQuotaSpec_To_v1_ClusterResourceQuotaSpec(in *quota_api.ClusterResourceQuotaSpec, out *ClusterResourceQuotaSpec, s conversion.Scope) error { + if defaulting, found := s.DefaultingInterface(reflect.TypeOf(*in)); found { + defaulting.(func(*quota_api.ClusterResourceQuotaSpec))(in) + } + if in.Selector != nil { + in, out := &in.Selector, &out.Selector + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } else { + out.Selector = nil + } + // TODO: Inefficient conversion - can we improve it? + if err := s.Convert(&in.Quota, &out.Quota, 0); err != nil { + return err + } + return nil +} + +func Convert_api_ClusterResourceQuotaSpec_To_v1_ClusterResourceQuotaSpec(in *quota_api.ClusterResourceQuotaSpec, out *ClusterResourceQuotaSpec, s conversion.Scope) error { + return autoConvert_api_ClusterResourceQuotaSpec_To_v1_ClusterResourceQuotaSpec(in, out, s) +} + +func autoConvert_v1_ClusterResourceQuotaStatus_To_api_ClusterResourceQuotaStatus(in *ClusterResourceQuotaStatus, out *quota_api.ClusterResourceQuotaStatus, s conversion.Scope) error { + if defaulting, found := s.DefaultingInterface(reflect.TypeOf(*in)); found { + defaulting.(func(*ClusterResourceQuotaStatus))(in) + } + // TODO: Inefficient conversion - can we improve it? + if err := s.Convert(&in.Overall, &out.Overall, 0); err != nil { + return err + } + if in.ByNamespace != nil { + in, out := &in.ByNamespace, &out.ByNamespace + *out = make(map[string]api.ResourceList, len(*in)) + for key, val := range *in { + newVal := new(api.ResourceList) + if err := api_v1.Convert_v1_ResourceList_To_api_ResourceList(&val, newVal, s); err != nil { + return err + } + (*out)[key] = *newVal + } + } else { + out.ByNamespace = nil + } + return nil +} + +func Convert_v1_ClusterResourceQuotaStatus_To_api_ClusterResourceQuotaStatus(in *ClusterResourceQuotaStatus, out *quota_api.ClusterResourceQuotaStatus, s conversion.Scope) error { + return autoConvert_v1_ClusterResourceQuotaStatus_To_api_ClusterResourceQuotaStatus(in, out, s) +} + +func autoConvert_api_ClusterResourceQuotaStatus_To_v1_ClusterResourceQuotaStatus(in *quota_api.ClusterResourceQuotaStatus, out *ClusterResourceQuotaStatus, s conversion.Scope) error { + if defaulting, found := s.DefaultingInterface(reflect.TypeOf(*in)); found { + defaulting.(func(*quota_api.ClusterResourceQuotaStatus))(in) + } + // TODO: Inefficient conversion - can we improve it? + if err := s.Convert(&in.Overall, &out.Overall, 0); err != nil { + return err + } + if in.ByNamespace != nil { + in, out := &in.ByNamespace, &out.ByNamespace + *out = make(map[string]api_v1.ResourceList, len(*in)) + for key, val := range *in { + newVal := new(api_v1.ResourceList) + // TODO: Inefficient conversion - can we improve it? + if err := s.Convert(&val, newVal, 0); err != nil { + return err + } + (*out)[key] = *newVal + } + } else { + out.ByNamespace = nil + } + return nil +} + +func Convert_api_ClusterResourceQuotaStatus_To_v1_ClusterResourceQuotaStatus(in *quota_api.ClusterResourceQuotaStatus, out *ClusterResourceQuotaStatus, s conversion.Scope) error { + return autoConvert_api_ClusterResourceQuotaStatus_To_v1_ClusterResourceQuotaStatus(in, out, s) +} diff --git a/pkg/quota/api/v1/deep_copy_generated.go b/pkg/quota/api/v1/deep_copy_generated.go new file mode 100644 index 000000000000..e7738231328e --- /dev/null +++ b/pkg/quota/api/v1/deep_copy_generated.go @@ -0,0 +1,97 @@ +// +build !ignore_autogenerated + +// This file was autogenerated by deepcopy-gen. Do not edit it manually! + +package v1 + +import ( + api "k8s.io/kubernetes/pkg/api" + unversioned "k8s.io/kubernetes/pkg/api/unversioned" + api_v1 "k8s.io/kubernetes/pkg/api/v1" + conversion "k8s.io/kubernetes/pkg/conversion" +) + +func init() { + if err := api.Scheme.AddGeneratedDeepCopyFuncs( + DeepCopy_v1_ClusterResourceQuota, + DeepCopy_v1_ClusterResourceQuotaList, + DeepCopy_v1_ClusterResourceQuotaSpec, + DeepCopy_v1_ClusterResourceQuotaStatus, + ); err != nil { + // if one of the deep copy functions is malformed, detect it immediately. + panic(err) + } +} + +func DeepCopy_v1_ClusterResourceQuota(in ClusterResourceQuota, out *ClusterResourceQuota, c *conversion.Cloner) error { + if err := unversioned.DeepCopy_unversioned_TypeMeta(in.TypeMeta, &out.TypeMeta, c); err != nil { + return err + } + if err := api_v1.DeepCopy_v1_ObjectMeta(in.ObjectMeta, &out.ObjectMeta, c); err != nil { + return err + } + if err := DeepCopy_v1_ClusterResourceQuotaSpec(in.Spec, &out.Spec, c); err != nil { + return err + } + if err := DeepCopy_v1_ClusterResourceQuotaStatus(in.Status, &out.Status, c); err != nil { + return err + } + return nil +} + +func DeepCopy_v1_ClusterResourceQuotaList(in ClusterResourceQuotaList, out *ClusterResourceQuotaList, c *conversion.Cloner) error { + if err := unversioned.DeepCopy_unversioned_TypeMeta(in.TypeMeta, &out.TypeMeta, c); err != nil { + return err + } + if err := unversioned.DeepCopy_unversioned_ListMeta(in.ListMeta, &out.ListMeta, c); err != nil { + return err + } + if in.Items != nil { + in, out := in.Items, &out.Items + *out = make([]ClusterResourceQuota, len(in)) + for i := range in { + if err := DeepCopy_v1_ClusterResourceQuota(in[i], &(*out)[i], c); err != nil { + return err + } + } + } else { + out.Items = nil + } + return nil +} + +func DeepCopy_v1_ClusterResourceQuotaSpec(in ClusterResourceQuotaSpec, out *ClusterResourceQuotaSpec, c *conversion.Cloner) error { + if in.Selector != nil { + in, out := in.Selector, &out.Selector + *out = make(map[string]string) + for key, val := range in { + (*out)[key] = val + } + } else { + out.Selector = nil + } + if err := api_v1.DeepCopy_v1_ResourceQuotaSpec(in.Quota, &out.Quota, c); err != nil { + return err + } + return nil +} + +func DeepCopy_v1_ClusterResourceQuotaStatus(in ClusterResourceQuotaStatus, out *ClusterResourceQuotaStatus, c *conversion.Cloner) error { + if err := api_v1.DeepCopy_v1_ResourceQuotaStatus(in.Overall, &out.Overall, c); err != nil { + return err + } + if in.ByNamespace != nil { + in, out := in.ByNamespace, &out.ByNamespace + *out = make(map[string]api_v1.ResourceList) + for key, val := range in { + if newVal, err := c.DeepCopy(val); err != nil { + return err + } else { + (*out)[key] = newVal.(api_v1.ResourceList) + } + } + } else { + out.ByNamespace = nil + } + return nil +} diff --git a/pkg/quota/api/v1/register.go b/pkg/quota/api/v1/register.go new file mode 100644 index 000000000000..16657c1ff310 --- /dev/null +++ b/pkg/quota/api/v1/register.go @@ -0,0 +1,37 @@ +package v1 + +import ( + kapi "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/unversioned" + "k8s.io/kubernetes/pkg/runtime" +) + +const GroupName = "quota.openshift.io" + +var SchemeGroupVersion = unversioned.GroupVersion{Group: GroupName, Version: "v1"} + +// Kind takes an unqualified kind and returns back a Group qualified GroupKind +func Kind(kind string) unversioned.GroupKind { + return SchemeGroupVersion.WithKind(kind).GroupKind() +} + +// Resource takes an unqualified resource and returns back a Group qualified GroupResource +func Resource(resource string) unversioned.GroupResource { + return SchemeGroupVersion.WithResource(resource).GroupResource() +} + +func AddToScheme(scheme *runtime.Scheme) { + addKnownTypes(scheme) +} + +// Adds the list of known types to api.Scheme. +func addKnownTypes(scheme *runtime.Scheme) { + scheme.AddKnownTypes(SchemeGroupVersion, + &kapi.ListOptions{}, + &ClusterResourceQuota{}, + &ClusterResourceQuotaList{}, + ) +} + +func (obj *ClusterResourceQuotaList) GetObjectKind() unversioned.ObjectKind { return &obj.TypeMeta } +func (obj *ClusterResourceQuota) GetObjectKind() unversioned.ObjectKind { return &obj.TypeMeta } diff --git a/pkg/quota/api/v1/swagger_doc.go b/pkg/quota/api/v1/swagger_doc.go new file mode 100644 index 000000000000..4690a3c737a8 --- /dev/null +++ b/pkg/quota/api/v1/swagger_doc.go @@ -0,0 +1,47 @@ +package v1 + +// This file contains methods that can be used by the go-restful package to generate Swagger +// documentation for the object types found in 'types.go' This file is automatically generated +// by hack/update-generated-swagger-descriptions.sh and should be run after a full build of OpenShift. +// ==== DO NOT EDIT THIS FILE MANUALLY ==== + +var map_ClusterResourceQuota = map[string]string{ + "": "ClusterResourceQuota mirrors ResourceQuota at a cluster scope. This object is easily convertible to synthetic ResourceQuota object to allow quota evaluation re-use.", + "metadata": "Standard object's metadata.", + "spec": "Spec defines the desired quota", + "status": "Status defines the actual enforced quota and its current usage", +} + +func (ClusterResourceQuota) SwaggerDoc() map[string]string { + return map_ClusterResourceQuota +} + +var map_ClusterResourceQuotaList = map[string]string{ + "": "ClusterResourceQuotaList is a collection of ClusterResourceQuotas", + "metadata": "Standard object's metadata.", + "items": "Items is a list of ClusterResourceQuotas", +} + +func (ClusterResourceQuotaList) SwaggerDoc() map[string]string { + return map_ClusterResourceQuotaList +} + +var map_ClusterResourceQuotaSpec = map[string]string{ + "": "ClusterResourceQuotaSpec defines the desired quota restrictions", + "selector": "Selector is the label selector used to match projects. It is not allowed to be empty and should only select active projects on the scale of dozens (though it can select many more less active projects). These projects will contend on object creation through this resource.", + "quota": "Spec defines the desired quota", +} + +func (ClusterResourceQuotaSpec) SwaggerDoc() map[string]string { + return map_ClusterResourceQuotaSpec +} + +var map_ClusterResourceQuotaStatus = map[string]string{ + "": "ClusterResourceQuotaStatus defines the actual enforced quota and its current usage", + "overall": "Overall defines the actual enforced quota and its current usage across all namespaces", + "byNamespace": "ByNamespace slices the usage by namespace. This division allows for quick resolution of deletion reconcilation inside of a single namespace without requiring a recalculation across all namespaces. This map can be used to pull the deltas for a given namespace.", +} + +func (ClusterResourceQuotaStatus) SwaggerDoc() map[string]string { + return map_ClusterResourceQuotaStatus +} diff --git a/pkg/quota/api/v1/types.go b/pkg/quota/api/v1/types.go new file mode 100644 index 000000000000..8f2015630e74 --- /dev/null +++ b/pkg/quota/api/v1/types.go @@ -0,0 +1,53 @@ +package v1 + +import ( + "k8s.io/kubernetes/pkg/api/unversioned" + kapi "k8s.io/kubernetes/pkg/api/v1" +) + +// ClusterResourceQuota mirrors ResourceQuota at a cluster scope. This object is easily convertible to +// synthetic ResourceQuota object to allow quota evaluation re-use. +type ClusterResourceQuota struct { + unversioned.TypeMeta `json:",inline"` + // Standard object's metadata. + kapi.ObjectMeta `json:"metadata"` + + // Spec defines the desired quota + Spec ClusterResourceQuotaSpec `json:"spec"` + + // Status defines the actual enforced quota and its current usage + Status ClusterResourceQuotaStatus `json:"status,omitempty"` +} + +// ClusterResourceQuotaSpec defines the desired quota restrictions +type ClusterResourceQuotaSpec struct { + // Selector is the label selector used to match projects. It is not allowed to be empty + // and should only select active projects on the scale of dozens (though it can select + // many more less active projects). These projects will contend on object creation through + // this resource. + Selector map[string]string `json:"selector"` + + // Spec defines the desired quota + Quota kapi.ResourceQuotaSpec `json:"quota"` +} + +// ClusterResourceQuotaStatus defines the actual enforced quota and its current usage +type ClusterResourceQuotaStatus struct { + // Overall defines the actual enforced quota and its current usage across all namespaces + Overall kapi.ResourceQuotaStatus `json:"overall"` + + // ByNamespace slices the usage by namespace. This division allows for quick resolution of + // deletion reconcilation inside of a single namespace without requiring a recalculation + // across all namespaces. This map can be used to pull the deltas for a given namespace. + ByNamespace map[string]kapi.ResourceList `json:"byNamespace"` +} + +// ClusterResourceQuotaList is a collection of ClusterResourceQuotas +type ClusterResourceQuotaList struct { + unversioned.TypeMeta `json:",inline"` + // Standard object's metadata. + unversioned.ListMeta `json:"metadata,omitempty"` + + // Items is a list of ClusterResourceQuotas + Items []ClusterResourceQuota `json:"items"` +} diff --git a/pkg/quota/api/validation/validation.go b/pkg/quota/api/validation/validation.go new file mode 100644 index 000000000000..020adfeaa5c9 --- /dev/null +++ b/pkg/quota/api/validation/validation.go @@ -0,0 +1,38 @@ +package validation + +import ( + "k8s.io/kubernetes/pkg/api/validation" + "k8s.io/kubernetes/pkg/labels" + "k8s.io/kubernetes/pkg/util/validation/field" + + quotaapi "github.com/openshift/origin/pkg/quota/api" +) + +func ValidateClusterResourceQuota(clusterquota *quotaapi.ClusterResourceQuota) field.ErrorList { + allErrs := validation.ValidateObjectMeta(&clusterquota.ObjectMeta, true, validation.ValidateResourceQuotaName, field.NewPath("metadata")) + + if clusterquota.Spec.Selector == nil || labels.Set(clusterquota.Spec.Selector).AsSelector().Empty() { + allErrs = append(allErrs, field.Required(field.NewPath("spec", "selector"), "must restrict the selected projects")) + } + + allErrs = append(allErrs, validation.ValidateResourceQuotaSpec(clusterquota.Spec.Quota, field.NewPath("spec", "quota"))...) + allErrs = append(allErrs, validation.ValidateResourceQuotaStatus(clusterquota.Status.Overall, field.NewPath("status", "overall"))...) + + for namespace, used := range clusterquota.Status.ByNamespace { + fldPath := field.NewPath("status", "byNamespace").Key(namespace) + for k, v := range used { + resPath := fldPath.Key(string(k)) + allErrs = append(allErrs, ValidateResourceQuotaResourceName(string(k), resPath)...) + allErrs = append(allErrs, ValidateResourceQuantityValue(string(k), v, resPath)...) + } + } + + return allErrs +} + +func ValidateClusterResourceQuotaUpdate(clusterquota *quotaapi.ClusterResourceQuota, oldClusterResourceQuota *quotaapi.ClusterResourceQuota) field.ErrorList { + allErrs := validation.ValidateObjectMetaUpdate(&clusterquota.ObjectMeta, &oldClusterResourceQuota.ObjectMeta, field.NewPath("metadata")) + allErrs = append(allErrs, ValidateClusterResourceQuota(clusterquota)...) + + return allErrs +} diff --git a/pkg/quota/api/validation/validation_test.go b/pkg/quota/api/validation/validation_test.go new file mode 100644 index 000000000000..744159af6aa7 --- /dev/null +++ b/pkg/quota/api/validation/validation_test.go @@ -0,0 +1,9 @@ +package validation + +import ( + "testing" +) + +func TestClusterQuota(t *testing.T) { + +} diff --git a/tools/genconversion/conversion.go b/tools/genconversion/conversion.go index 82473f0ee256..69d5e52875aa 100644 --- a/tools/genconversion/conversion.go +++ b/tools/genconversion/conversion.go @@ -36,6 +36,8 @@ func main() { "github.com/openshift/origin/pkg/oauth/api", "github.com/openshift/origin/pkg/project/api/v1", "github.com/openshift/origin/pkg/project/api", + "github.com/openshift/origin/pkg/quota/api/v1", + "github.com/openshift/origin/pkg/quota/api", "github.com/openshift/origin/pkg/route/api/v1", "github.com/openshift/origin/pkg/route/api", "github.com/openshift/origin/pkg/sdn/api/v1", diff --git a/tools/gendeepcopy/deep_copy.go b/tools/gendeepcopy/deep_copy.go index e3bcc554ce87..41745780cdf8 100644 --- a/tools/gendeepcopy/deep_copy.go +++ b/tools/gendeepcopy/deep_copy.go @@ -63,6 +63,8 @@ func main() { "github.com/openshift/origin/pkg/oauth/api", "github.com/openshift/origin/pkg/project/api/v1", "github.com/openshift/origin/pkg/project/api", + "github.com/openshift/origin/pkg/quota/api/v1", + "github.com/openshift/origin/pkg/quota/api", "github.com/openshift/origin/pkg/route/api/v1", "github.com/openshift/origin/pkg/route/api", "github.com/openshift/origin/pkg/sdn/api/v1",