37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
resource_quota_common.go 4.00 KB
一键复制 编辑 原始数据 按行查看 历史
Alena Prokharchyk 提交于 2018-08-30 23:42 . Quota validation on api side
package resourcequota
import (
"encoding/json"
"strings"
"github.com/rancher/norman/types/convert"
"github.com/rancher/types/apis/management.cattle.io/v3"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
api "k8s.io/kubernetes/pkg/apis/core"
)
func convertResourceListToLimit(rList api.ResourceList) (*v3.ResourceQuotaLimit, error) {
converted, err := convert.EncodeToMap(rList)
if err != nil {
return nil, err
}
convertedMap := map[string]string{}
for key, value := range converted {
convertedMap[key] = convert.ToString(value)
}
toReturn := &v3.ResourceQuotaLimit{}
err = convert.ToObj(convertedMap, toReturn)
return toReturn, err
}
func convertResourceLimitResourceQuotaSpec(limit *v3.ResourceQuotaLimit) (*corev1.ResourceQuotaSpec, error) {
converted, err := convertProjectResourceLimitToResourceList(limit)
if err != nil {
return nil, err
}
quotaSpec := &corev1.ResourceQuotaSpec{
Hard: converted,
}
return quotaSpec, err
}
func convertProjectResourceLimitToResourceList(limit *v3.ResourceQuotaLimit) (corev1.ResourceList, error) {
in, err := json.Marshal(limit)
if err != nil {
return nil, err
}
limitsMap := map[string]string{}
err = json.Unmarshal(in, &limitsMap)
if err != nil {
return nil, err
}
limits := corev1.ResourceList{}
for key, value := range limitsMap {
var resourceName corev1.ResourceName
if val, ok := conversion[key]; ok {
resourceName = corev1.ResourceName(val)
} else {
resourceName = corev1.ResourceName(key)
}
resourceQuantity, err := resource.ParseQuantity(value)
if err != nil {
return nil, err
}
limits[resourceName] = resourceQuantity
}
return limits, nil
}
var conversion = map[string]string{
"replicationControllers": "replicationcontrollers",
"configMaps": "configmaps",
"persistentVolumeClaims": "persistentvolumeclaims",
"servicesNodePorts": "services.nodeports",
"servicesLoadBalancers": "services.loadbalancers",
"requestsCpu": "requests.cpu",
"requestsMemory": "requests.memory",
"requestsStorage": "requests.storage",
"limitsCpu": "limits.cpu",
"limitsMemory": "limits.memory",
}
func getNamespaceResourceQuota(ns *corev1.Namespace) string {
if ns.Annotations == nil {
return ""
}
return ns.Annotations[resourceQuotaAnnotation]
}
func getProjectResourceQuotaLimit(ns *corev1.Namespace, projectLister v3.ProjectLister) (*v3.ResourceQuotaLimit, string, error) {
projectID := getProjectID(ns)
if projectID == "" {
return nil, "", nil
}
projectNamespace, projectName := getProjectNamespaceName(projectID)
if projectName == "" {
return nil, "", nil
}
project, err := projectLister.Get(projectNamespace, projectName)
if err != nil || project.Spec.ResourceQuota == nil {
return nil, "", err
}
return &project.Spec.ResourceQuota.Limit, projectID, nil
}
func getProjectNamespaceDefaultQuota(ns *corev1.Namespace, projectLister v3.ProjectLister) (*v3.NamespaceResourceQuota, error) {
projectID := getProjectID(ns)
if projectID == "" {
return nil, nil
}
projectNamespace, projectName := getProjectNamespaceName(projectID)
if projectName == "" {
return nil, nil
}
project, err := projectLister.Get(projectNamespace, projectName)
if err != nil || project.Spec.ResourceQuota == nil {
return nil, err
}
return project.Spec.NamespaceDefaultResourceQuota, nil
}
func getNamespaceLimit(ns *corev1.Namespace) (*v3.ResourceQuotaLimit, error) {
value := getNamespaceResourceQuota(ns)
if value == "" {
return nil, nil
}
var nsQuota v3.NamespaceResourceQuota
err := json.Unmarshal([]byte(convert.ToString(value)), &nsQuota)
if err != nil {
return nil, err
}
return &nsQuota.Limit, err
}
func getProjectID(ns *corev1.Namespace) string {
if ns.Annotations != nil {
return ns.Annotations[projectIDAnnotation]
}
return ""
}
func getProjectNamespaceName(projectID string) (string, string) {
if projectID == "" {
return "", ""
}
parts := strings.Split(projectID, ":")
if len(parts) == 2 {
return parts[0], parts[1]
}
return "", ""
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.1.9-rc3

搜索帮助