Skip to content

Commit

Permalink
Feat: add gpu prices alias (labring#3747)
Browse files Browse the repository at this point in the history
* add gpu prices alias

* ignore is not found error
  • Loading branch information
bxy4543 committed Aug 23, 2023
1 parent 08b30d8 commit 6ac3aa3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
6 changes: 6 additions & 0 deletions controllers/account/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ rules:
- patch
- update
- watch
- apiGroups:
- ""
resources:
- configmaps/status
verbs:
- get
- apiGroups:
- account.sealos.io
resources:
Expand Down
15 changes: 15 additions & 0 deletions controllers/account/controllers/billingrecordquery_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (
"os"
"time"

"k8s.io/apimachinery/pkg/api/errors"

"github.com/labring/sealos/controllers/pkg/common/gpu"

"github.com/labring/sealos/controllers/pkg/common"

"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -52,6 +56,8 @@ type BillingRecordQueryReconciler struct {
//+kubebuilder:rbac:groups=account.sealos.io,resources=pricequeries,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=account.sealos.io,resources=pricequeries/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=account.sealos.io,resources=pricequeries/finalizers,verbs=update
//+kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch
//+kubebuilder:rbac:groups="",resources=configmaps/status,verbs=get

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
Expand Down Expand Up @@ -147,7 +153,16 @@ func (r *BillingRecordQueryReconciler) ReconcilePriceQuery(ctx context.Context,
pricesMap = common.DefaultPrices
}
priceQuery.Status.BillingRecords = make([]accountv1.BillingRecord, 0)
alias, err := gpu.GetGPUAlias(r.Client)
if errors.IsNotFound(err) {
r.Logger.Error(err, "get gpu alias failed")
}
for property, v := range pricesMap {
if common.IsGpuResource(property) && alias != nil {
if propertyAlias := alias[common.GetGpuResourceProduct(property)]; propertyAlias != "" {
property = string(common.NewGpuResource(propertyAlias))
}
}
priceQuery.Status.BillingRecords = append(priceQuery.Status.BillingRecords, accountv1.BillingRecord{
ResourceType: property,
Price: v.Price,
Expand Down
6 changes: 6 additions & 0 deletions controllers/account/deploy/manifests/deploy.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,12 @@ rules:
- patch
- update
- watch
- apiGroups:
- ""
resources:
- configmaps/status
verbs:
- get
- apiGroups:
- account.sealos.io
resources:
Expand Down
31 changes: 31 additions & 0 deletions controllers/pkg/common/gpu/nvidia.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package gpu
import (
"context"

"k8s.io/apimachinery/pkg/util/json"

corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -132,3 +134,32 @@ func GetNodeGpuModel(c client.Client) (map[string]NvidiaGPU, error) {
}
return gpuModels, nil
}

const (
Alias = "alias"
NodeInfoConfigmapNamespace = "node-system"
NodeInfoConfigmapName = "node-gpu-info"
)

func GetGPUAlias(c client.Client) (map[string]string, error) {
/*
kubectl get cm -n node-system node-gpu-info -oyaml
apiVersion: v1
data:
alias: '{"NVIDIA-GeForce-RTX-4090":"GeForce-RTX-4090"}'
*/
cm := &corev1.ConfigMap{}
if err := c.Get(context.Background(), client.ObjectKey{
Namespace: NodeInfoConfigmapNamespace,
Name: NodeInfoConfigmapName,
}, cm); err != nil {
return nil, err
}
alias := make(map[string]string)
if aliasData := cm.Data[Alias]; aliasData != "" {
if err := json.Unmarshal([]byte(cm.Data[Alias]), &alias); err != nil {
return nil, err
}
}
return alias, nil
}
14 changes: 13 additions & 1 deletion controllers/pkg/common/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"math"
"strings"
"time"

"github.com/labring/sealos/controllers/pkg/utils"
Expand Down Expand Up @@ -132,6 +133,9 @@ const (
PropertyInfraDisk = "infra-disk"
)

// GpuResourcePrefix GPUResource = gpu- + gpu.Product ; ex. gpu-tesla-v100
const GpuResourcePrefix = "gpu-"

const ResourceGPU corev1.ResourceName = gpu.NvidiaGpuKey

const (
Expand All @@ -140,7 +144,15 @@ const (
)

func NewGpuResource(product string) corev1.ResourceName {
return corev1.ResourceName("gpu-" + product)
return corev1.ResourceName(GpuResourcePrefix + product)
}

func IsGpuResource(resource string) bool {
return strings.HasPrefix(resource, GpuResourcePrefix)
}

func GetGpuResourceProduct(resource string) string {
return strings.TrimPrefix(resource, GpuResourcePrefix)
}

var (
Expand Down

0 comments on commit 6ac3aa3

Please sign in to comment.