Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6,983 changes: 3,538 additions & 3,445 deletions bcs-common/pkg/bcsapi/bcsproject/bcsproject.pb.go

Large diffs are not rendered by default.

416 changes: 290 additions & 126 deletions bcs-common/pkg/bcsapi/bcsproject/bcsproject.pb.validate.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,16 @@ func (c *IndependentNamespaceAction) GetNamespace(ctx context.Context,
retData.Annotations = append(retData.Annotations, &proto.Annotation{Key: k, Value: v})
}
// get quota
quota, err := getNamespaceQuota(ctx, req.GetClusterID(), ns.GetName(), client)
quota, otherQuotas, err := getNamespaceQuota(ctx, req.GetClusterID(), ns.GetName(), client)
if err != nil {
return err
}
if quota != nil {
retData.Quota, retData.Used, retData.CpuUseRate, retData.MemoryUseRate = quotautils.TransferToProto(quota)
}
for _, q := range otherQuotas {
retData.OtherQuotas = append(retData.OtherQuotas, quotautils.TransferToProtoOtherQuota(q))
}

// get variables
variables, err := listNamespaceVariables(ctx, req.GetProjectCode(), req.GetClusterID(), ns.GetName())
Expand All @@ -99,18 +102,25 @@ func (c *IndependentNamespaceAction) GetNamespace(ctx context.Context,
}

func getNamespaceQuota(ctx context.Context, clusterID, namespace string, clientset *kubernetes.Clientset) (
*corev1.ResourceQuota, error) {
quota, err := clientset.CoreV1().ResourceQuotas(namespace).Get(ctx, namespace, metav1.GetOptions{})
*corev1.ResourceQuota, []*corev1.ResourceQuota, error) {
quotaList, err := clientset.CoreV1().ResourceQuotas(namespace).List(ctx, metav1.ListOptions{})
if err != nil && !errors.IsNotFound(err) {
logging.Error("get resourceQuota %s/%s failed, err: %s", clusterID, namespace, err.Error())
return nil, errorx.NewClusterErr(err.Error())
logging.Error("list resourceQuotas in namespace %s/%s failed, err: %s", clusterID, namespace, err.Error())
return nil, nil, errorx.NewClusterErr(err.Error())
}

// get quota
if errors.IsNotFound(err) {
return nil, nil
return nil, nil, nil
}
var defaultQuota *corev1.ResourceQuota
var otherQuotas []*corev1.ResourceQuota
for i := range quotaList.Items {
if quotaList.Items[i].GetName() == namespace {
defaultQuota = &quotaList.Items[i]
} else {
otherQuotas = append(otherQuotas, &quotaList.Items[i])
}
}
return quota, nil
return defaultQuota, otherQuotas, nil
}

func listNamespaceVariables(ctx context.Context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ func (c *IndependentNamespaceAction) ListNamespaces(ctx context.Context,
return errorx.NewClusterErr(err.Error())
}
quotaMap := map[string]corev1.ResourceQuota{}
otherQuotasMap := map[string][]*proto.OtherQuota{}
if quotaList, e := client.CoreV1().ResourceQuotas("").List(ctx, metav1.ListOptions{}); e == nil {
for _, quota := range quotaList.Items {
if quota.GetName() == quota.GetNamespace() {
quotaMap[quota.GetName()] = quota
nsName := quota.GetNamespace()
if quota.GetName() == nsName {
quotaMap[nsName] = quota
} else {
otherQuotasMap[nsName] = append(otherQuotasMap[nsName], quotautils.TransferToProtoOtherQuota(&quota))
}
}
}
Expand Down Expand Up @@ -81,6 +85,9 @@ func (c *IndependentNamespaceAction) ListNamespaces(ctx context.Context,
if quota, ok := quotaMap[ns.GetName()]; ok {
retData.Quota, retData.Used, retData.CpuUseRate, retData.MemoryUseRate = quotautils.TransferToProto(&quota)
}
if otherQuotas, ok := otherQuotasMap[ns.GetName()]; ok {
retData.OtherQuotas = otherQuotas
}
// get variables
retData.Variables = variablesMap[ns.GetName()]
// get managers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ func (a *SharedNamespaceAction) GetNamespace(ctx context.Context,
}
// get quota
// nolint
if quota, err := getNamespaceQuota(ctx, clusterID, namespace.GetName(), client); err != nil {
if quota, otherQuotas, err := getNamespaceQuota(ctx, clusterID, namespace.GetName(), client); err != nil {
return err
} else if quota != nil {
retData.Quota, retData.Used, retData.CpuUseRate, retData.MemoryUseRate = quotautils.TransferToProto(quota)
} else {
if quota != nil {
retData.Quota, retData.Used, retData.CpuUseRate, retData.MemoryUseRate = quotautils.TransferToProto(quota)
}
for _, q := range otherQuotas {
retData.OtherQuotas = append(retData.OtherQuotas, quotautils.TransferToProtoOtherQuota(q))
}
}
// get variables
variables, err := listNamespaceVariables(ctx, projectCode, clusterID, namespace.GetName())
Expand Down Expand Up @@ -115,17 +120,25 @@ func (a *SharedNamespaceAction) GetNamespace(ctx context.Context,
}

func getNamespaceQuota(ctx context.Context, clusterID, namespace string, clientset *kubernetes.Clientset) (
*corev1.ResourceQuota, error) {
quota, err := clientset.CoreV1().ResourceQuotas(namespace).Get(ctx, namespace, metav1.GetOptions{})
*corev1.ResourceQuota, []*corev1.ResourceQuota, error) {
quotaList, err := clientset.CoreV1().ResourceQuotas(namespace).List(ctx, metav1.ListOptions{})
if err != nil && !errors.IsNotFound(err) {
logging.Error("get resourceQuota %s/%s failed, err: %s", clusterID, namespace, err.Error())
return nil, errorx.NewClusterErr(err.Error())
logging.Error("list resourceQuotas in namespace %s/%s failed, err: %s", clusterID, namespace, err.Error())
return nil, nil, errorx.NewClusterErr(err.Error())
}

if errors.IsNotFound(err) {
return nil, nil
return nil, nil, nil
}
var defaultQuota *corev1.ResourceQuota
var otherQuotas []*corev1.ResourceQuota
for i := range quotaList.Items {
if quotaList.Items[i].GetName() == namespace {
defaultQuota = &quotaList.Items[i]
} else {
otherQuotas = append(otherQuotas, &quotaList.Items[i])
}
}
return quota, nil
return defaultQuota, otherQuotas, nil
}

func constructCreatingNamespace(staging *nsm.Namespace) *proto.NamespaceData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ func (a *SharedNamespaceAction) ListNamespaces(ctx context.Context,
}
// list all quota in cluster
quotaMap := map[string]corev1.ResourceQuota{}
otherQuotasMap := map[string][]*proto.OtherQuota{}
if quotaList, e := client.CoreV1().ResourceQuotas("").List(ctx, metav1.ListOptions{}); e == nil {
for _, quota := range quotaList.Items {
if quota.GetName() == quota.GetNamespace() {
quotaMap[quota.GetName()] = quota
nsName := quota.GetNamespace()
if quota.GetName() == nsName {
quotaMap[nsName] = quota
} else {
otherQuotasMap[nsName] = append(otherQuotasMap[nsName], quotautils.TransferToProtoOtherQuota(&quota))
}
}
}
Expand All @@ -96,7 +100,7 @@ func (a *SharedNamespaceAction) ListNamespaces(ctx context.Context,
logging.Error("batch list variables failed, err: %s", err.Error())
return errorx.NewClusterErr(err.Error())
}
list, err := loadRetDatasFromCluster(ctx, req.ClusterID, namespaces, variablesMap, quotaMap, existns)
list, err := loadRetDatasFromCluster(ctx, req.ClusterID, namespaces, variablesMap, quotaMap, otherQuotasMap, existns)
if err != nil {
return err
}
Expand Down Expand Up @@ -227,7 +231,7 @@ func loadListRetDataFromDB(namespace nsm.Namespace) *proto.NamespaceData {

func loadRetDatasFromCluster(ctx context.Context, clusterID string, namespaces []corev1.Namespace,
variablesMap map[string][]*proto.VariableValue, quotaMap map[string]corev1.ResourceQuota,
existns map[string]nsm.Namespace) ([]*proto.NamespaceData, error) {
otherQuotasMap map[string][]*proto.OtherQuota, existns map[string]nsm.Namespace) ([]*proto.NamespaceData, error) {
retDatas := []*proto.NamespaceData{}
for _, namespace := range namespaces {
retData := &proto.NamespaceData{
Expand All @@ -242,6 +246,9 @@ func loadRetDatasFromCluster(ctx context.Context, clusterID string, namespaces [
retData.Quota, retData.Used, retData.CpuUseRate, retData.MemoryUseRate =
quotautils.TransferToProto(&quota)
}
if otherQuotas, ok := otherQuotasMap[namespace.GetName()]; ok {
retData.OtherQuotas = otherQuotas
}
// get variables
retData.Variables = variablesMap[namespace.GetName()]
if ns, ok := existns[retData.GetName()]; ok {
Expand Down
20 changes: 20 additions & 0 deletions bcs-services/bcs-project-manager/internal/util/quota/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@ func TransferToProto(q *corev1.ResourceQuota) (
return quota, used, cpuUseRate, memoryUseRate
}

// TransferToProtoOtherQuota transfer k8s ResourceQuota to proto OtherQuota
func TransferToProtoOtherQuota(q *corev1.ResourceQuota) *proto.OtherQuota {
if q == nil {
return nil
}
cpuLimitsQuota := q.Status.Hard[corev1.ResourceLimitsCPU]
cpuRequestQuota := q.Status.Hard[corev1.ResourceRequestsCPU]
memoryLimitsQuota := q.Status.Hard[corev1.ResourceLimitsMemory]
memoryRequestsQuota := q.Status.Hard[corev1.ResourceRequestsMemory]
return &proto.OtherQuota{
Name: q.GetName(),
Quota: &proto.ResourceQuota{
CpuLimits: cpuLimitsQuota.String(),
CpuRequests: cpuRequestQuota.String(),
MemoryLimits: memoryLimitsQuota.String(),
MemoryRequests: memoryRequestsQuota.String(),
},
}
}

// LoadFromProto load k8s ResourceQuota from proto ResourceQuota
func LoadFromProto(k8sQuota *corev1.ResourceQuota, protoQuota *proto.ResourceQuota) error {
return load(k8sQuota, protoQuota.GetCpuLimits(), protoQuota.GetCpuRequests(),
Expand Down
Loading
Loading