Skip to content

Commit

Permalink
add clusterquota types
Browse files Browse the repository at this point in the history
  • Loading branch information
deads2k committed May 25, 2016
1 parent 71179ab commit 6c8d61d
Show file tree
Hide file tree
Showing 15 changed files with 824 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/api/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions pkg/api/validation/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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(&quotaapi.ClusterResourceQuota{}, quotavalidation.ValidateClusterResourceQuota, quotavalidation.ValidateClusterResourceQuotaUpdate)

}
96 changes: 96 additions & 0 deletions pkg/quota/api/deep_copy_generated.go
Original file line number Diff line number Diff line change
@@ -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
}
109 changes: 109 additions & 0 deletions pkg/quota/api/install/install.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
37 changes: 37 additions & 0 deletions pkg/quota/api/register.go
Original file line number Diff line number Diff line change
@@ -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 }
53 changes: 53 additions & 0 deletions pkg/quota/api/types.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 6c8d61d

Please sign in to comment.