From 54ad0db98f1ebbe80d5e6f5857be2947b3308855 Mon Sep 17 00:00:00 2001 From: halfrost Date: Mon, 15 Aug 2022 22:44:58 -0700 Subject: [PATCH] Change resource name to enum --- pkg/apis/cluster/types.go | 31 ++++++++++++++++--- pkg/apis/cluster/v1alpha1/types.go | 31 ++++++++++++++++--- .../v1alpha1/zz_generated.conversion.go | 16 +++++----- .../cluster/v1alpha1/zz_generated.deepcopy.go | 28 +++++++++++++++-- pkg/apis/cluster/zz_generated.deepcopy.go | 28 +++++++++++++++-- 5 files changed, 112 insertions(+), 22 deletions(-) diff --git a/pkg/apis/cluster/types.go b/pkg/apis/cluster/types.go index 57c6ae52a52e..cdbeb7c117ec 100644 --- a/pkg/apis/cluster/types.go +++ b/pkg/apis/cluster/types.go @@ -6,6 +6,29 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) +// ResourceName is the name identifying various resources in a ResourceList. +type ResourceName string + +// Resource names must be not more than 63 characters, consisting of upper- or lower-case alphanumeric characters, +// with the -, _, and . characters allowed anywhere, except the first or last character. +// The default convention, matching that for annotations, is to use lower-case names, with dashes, rather than +// camel case, separating compound words. +// Fully-qualified resource typenames are constructed from a DNS-style subdomain, followed by a slash `/` and a name. +const ( + // CPU, in cores. (500m = .5 cores) + ResourceCPU ResourceName = "cpu" + // Memory, in bytes. (500Gi = 500GiB = 500 * 1024 * 1024 * 1024) + ResourceMemory ResourceName = "memory" + // Volume size, in bytes (e,g. 5Gi = 5GiB = 5 * 1024 * 1024 * 1024) + ResourceStorage ResourceName = "storage" + // Local ephemeral storage, in bytes. (500Gi = 500GiB = 500 * 1024 * 1024 * 1024) + // The resource name for ResourceEphemeralStorage is alpha and it can change across releases. + ResourceEphemeralStorage ResourceName = "ephemeral-storage" +) + +// ResourceList is a set of (resource name, quantity) pairs. +type ResourceList map[ResourceName]resource.Quantity + //revive:disable:exported // +genclient @@ -156,7 +179,7 @@ type ResourceModel struct { type ResourceModelItem struct { // Name is the name for the resource that you want to categorize. // +optional - Name corev1.ResourceName + Name ResourceName // Min is the minimum amount of this resource represented by resource name。 // +optional @@ -266,17 +289,17 @@ type ResourceSummary struct { // Allocatable represents the resources of a cluster that are available for scheduling. // Total amount of allocatable resources on all nodes. // +optional - Allocatable corev1.ResourceList + Allocatable ResourceList // Allocating represents the resources of a cluster that are pending for scheduling. // Total amount of required resources of all Pods that are waiting for scheduling. // +optional - Allocating corev1.ResourceList + Allocating ResourceList // Allocated represents the resources of a cluster that have been scheduled. // Total amount of required resources of all Pods that have been scheduled to nodes. // +optional - Allocated corev1.ResourceList + Allocated ResourceList // AllocatableModeling represents the number of each resources modeling in a cluster that are available for scheduling. // Total amount of allocatable resources on all nodes. diff --git a/pkg/apis/cluster/v1alpha1/types.go b/pkg/apis/cluster/v1alpha1/types.go index 568efba7724a..9f31a33a911a 100644 --- a/pkg/apis/cluster/v1alpha1/types.go +++ b/pkg/apis/cluster/v1alpha1/types.go @@ -17,6 +17,29 @@ const ( ResourceNamespaceScopedCluster = false ) +// ResourceName is the name identifying various resources in a ResourceList. +type ResourceName string + +// Resource names must be not more than 63 characters, consisting of upper- or lower-case alphanumeric characters, +// with the -, _, and . characters allowed anywhere, except the first or last character. +// The default convention, matching that for annotations, is to use lower-case names, with dashes, rather than +// camel case, separating compound words. +// Fully-qualified resource typenames are constructed from a DNS-style subdomain, followed by a slash `/` and a name. +const ( + // CPU, in cores. (500m = .5 cores) + ResourceCPU ResourceName = "cpu" + // Memory, in bytes. (500Gi = 500GiB = 500 * 1024 * 1024 * 1024) + ResourceMemory ResourceName = "memory" + // Volume size, in bytes (e,g. 5Gi = 5GiB = 5 * 1024 * 1024 * 1024) + ResourceStorage ResourceName = "storage" + // Local ephemeral storage, in bytes. (500Gi = 500GiB = 500 * 1024 * 1024 * 1024) + // The resource name for ResourceEphemeralStorage is alpha and it can change across releases. + ResourceEphemeralStorage ResourceName = "ephemeral-storage" +) + +// ResourceList is a set of (resource name, quantity) pairs. +type ResourceList map[ResourceName]resource.Quantity + // +genclient // +genclient:nonNamespaced // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object @@ -168,7 +191,7 @@ type ResourceModel struct { type ResourceModelItem struct { // Name is the name for the resource that you want to categorize. // +optional - Name corev1.ResourceName `json:"name,omitempty"` + Name ResourceName `json:"name,omitempty"` // Min is the minimum amount of this resource represented by resource name。 // +optional @@ -275,15 +298,15 @@ type ResourceSummary struct { // Allocatable represents the resources of a cluster that are available for scheduling. // Total amount of allocatable resources on all nodes. // +optional - Allocatable corev1.ResourceList `json:"allocatable,omitempty"` + Allocatable ResourceList `json:"allocatable,omitempty"` // Allocating represents the resources of a cluster that are pending for scheduling. // Total amount of required resources of all Pods that are waiting for scheduling. // +optional - Allocating corev1.ResourceList `json:"allocating,omitempty"` + Allocating ResourceList `json:"allocating,omitempty"` // Allocated represents the resources of a cluster that have been scheduled. // Total amount of required resources of all Pods that have been scheduled to nodes. // +optional - Allocated corev1.ResourceList `json:"allocated,omitempty"` + Allocated ResourceList `json:"allocated,omitempty"` } // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object diff --git a/pkg/apis/cluster/v1alpha1/zz_generated.conversion.go b/pkg/apis/cluster/v1alpha1/zz_generated.conversion.go index 7ce5edc8f0df..a13adc2a8fa2 100644 --- a/pkg/apis/cluster/v1alpha1/zz_generated.conversion.go +++ b/pkg/apis/cluster/v1alpha1/zz_generated.conversion.go @@ -462,7 +462,7 @@ func Convert_cluster_ResourceModel_To_v1alpha1_ResourceModel(in *cluster.Resourc } func autoConvert_v1alpha1_ResourceModelItem_To_cluster_ResourceModelItem(in *ResourceModelItem, out *cluster.ResourceModelItem, s conversion.Scope) error { - out.Name = v1.ResourceName(in.Name) + out.Name = cluster.ResourceName(in.Name) out.Min = in.Min out.Max = in.Max return nil @@ -474,7 +474,7 @@ func Convert_v1alpha1_ResourceModelItem_To_cluster_ResourceModelItem(in *Resourc } func autoConvert_cluster_ResourceModelItem_To_v1alpha1_ResourceModelItem(in *cluster.ResourceModelItem, out *ResourceModelItem, s conversion.Scope) error { - out.Name = v1.ResourceName(in.Name) + out.Name = ResourceName(in.Name) out.Min = in.Min out.Max = in.Max return nil @@ -486,9 +486,9 @@ func Convert_cluster_ResourceModelItem_To_v1alpha1_ResourceModelItem(in *cluster } func autoConvert_v1alpha1_ResourceSummary_To_cluster_ResourceSummary(in *ResourceSummary, out *cluster.ResourceSummary, s conversion.Scope) error { - out.Allocatable = *(*v1.ResourceList)(unsafe.Pointer(&in.Allocatable)) - out.Allocating = *(*v1.ResourceList)(unsafe.Pointer(&in.Allocating)) - out.Allocated = *(*v1.ResourceList)(unsafe.Pointer(&in.Allocated)) + out.Allocatable = *(*cluster.ResourceList)(unsafe.Pointer(&in.Allocatable)) + out.Allocating = *(*cluster.ResourceList)(unsafe.Pointer(&in.Allocating)) + out.Allocated = *(*cluster.ResourceList)(unsafe.Pointer(&in.Allocated)) return nil } @@ -498,9 +498,9 @@ func Convert_v1alpha1_ResourceSummary_To_cluster_ResourceSummary(in *ResourceSum } func autoConvert_cluster_ResourceSummary_To_v1alpha1_ResourceSummary(in *cluster.ResourceSummary, out *ResourceSummary, s conversion.Scope) error { - out.Allocatable = *(*v1.ResourceList)(unsafe.Pointer(&in.Allocatable)) - out.Allocating = *(*v1.ResourceList)(unsafe.Pointer(&in.Allocating)) - out.Allocated = *(*v1.ResourceList)(unsafe.Pointer(&in.Allocated)) + out.Allocatable = *(*ResourceList)(unsafe.Pointer(&in.Allocatable)) + out.Allocating = *(*ResourceList)(unsafe.Pointer(&in.Allocating)) + out.Allocated = *(*ResourceList)(unsafe.Pointer(&in.Allocated)) // WARNING: in.AllocatableModeling requires manual conversion: does not exist in peer-type return nil } diff --git a/pkg/apis/cluster/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/cluster/v1alpha1/zz_generated.deepcopy.go index 3612c769d7ab..01fd6815da6d 100644 --- a/pkg/apis/cluster/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/cluster/v1alpha1/zz_generated.deepcopy.go @@ -253,6 +253,28 @@ func (in *NodeSummary) DeepCopy() *NodeSummary { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in ResourceList) DeepCopyInto(out *ResourceList) { + { + in := &in + *out = make(ResourceList, len(*in)) + for key, val := range *in { + (*out)[key] = val.DeepCopy() + } + return + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ResourceList. +func (in ResourceList) DeepCopy() ResourceList { + if in == nil { + return nil + } + out := new(ResourceList) + in.DeepCopyInto(out) + return *out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ResourceModel) DeepCopyInto(out *ResourceModel) { *out = *in @@ -299,21 +321,21 @@ func (in *ResourceSummary) DeepCopyInto(out *ResourceSummary) { *out = *in if in.Allocatable != nil { in, out := &in.Allocatable, &out.Allocatable - *out = make(v1.ResourceList, len(*in)) + *out = make(ResourceList, len(*in)) for key, val := range *in { (*out)[key] = val.DeepCopy() } } if in.Allocating != nil { in, out := &in.Allocating, &out.Allocating - *out = make(v1.ResourceList, len(*in)) + *out = make(ResourceList, len(*in)) for key, val := range *in { (*out)[key] = val.DeepCopy() } } if in.Allocated != nil { in, out := &in.Allocated, &out.Allocated - *out = make(v1.ResourceList, len(*in)) + *out = make(ResourceList, len(*in)) for key, val := range *in { (*out)[key] = val.DeepCopy() } diff --git a/pkg/apis/cluster/zz_generated.deepcopy.go b/pkg/apis/cluster/zz_generated.deepcopy.go index 7fd0d121295f..9f55eb54bd00 100644 --- a/pkg/apis/cluster/zz_generated.deepcopy.go +++ b/pkg/apis/cluster/zz_generated.deepcopy.go @@ -269,6 +269,28 @@ func (in *NodeSummary) DeepCopy() *NodeSummary { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in ResourceList) DeepCopyInto(out *ResourceList) { + { + in := &in + *out = make(ResourceList, len(*in)) + for key, val := range *in { + (*out)[key] = val.DeepCopy() + } + return + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ResourceList. +func (in ResourceList) DeepCopy() ResourceList { + if in == nil { + return nil + } + out := new(ResourceList) + in.DeepCopyInto(out) + return *out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ResourceModel) DeepCopyInto(out *ResourceModel) { *out = *in @@ -315,21 +337,21 @@ func (in *ResourceSummary) DeepCopyInto(out *ResourceSummary) { *out = *in if in.Allocatable != nil { in, out := &in.Allocatable, &out.Allocatable - *out = make(v1.ResourceList, len(*in)) + *out = make(ResourceList, len(*in)) for key, val := range *in { (*out)[key] = val.DeepCopy() } } if in.Allocating != nil { in, out := &in.Allocating, &out.Allocating - *out = make(v1.ResourceList, len(*in)) + *out = make(ResourceList, len(*in)) for key, val := range *in { (*out)[key] = val.DeepCopy() } } if in.Allocated != nil { in, out := &in.Allocated, &out.Allocated - *out = make(v1.ResourceList, len(*in)) + *out = make(ResourceList, len(*in)) for key, val := range *in { (*out)[key] = val.DeepCopy() }