Files
Olares/cli/cmd/ctl/user/utils.go
Eliott van Nuffel df38148149
Some checks failed
Native Verification / Build Docs (pull_request) Successful in 1m12s
Native Verification / Build App-Service Native (pull_request) Successful in 1m27s
Native Verification / Build Daemon Native (pull_request) Successful in 42s
Lint and Test Charts / lint-test (pull_request_target) Failing after 19s
Lint and Test Charts / test-version (pull_request_target) Successful in 0s
Lint and Test Charts / push-image (pull_request_target) Failing after 15s
Lint and Test Charts / upload-cli (pull_request_target) Failing after 1m15s
Lint and Test Charts / upload-daemon (pull_request_target) Failing after 1m12s
Lint and Test Charts / push-deps (pull_request_target) Has been skipped
Lint and Test Charts / push-deps-arm64 (pull_request_target) Has been skipped
Lint and Test Charts / push-image-arm64 (pull_request_target) Has been cancelled
Lint and Test Charts / upload-package (pull_request_target) Has been cancelled
Lint and Test Charts / install-test (pull_request_target) Has been cancelled
continue beOS rebrand and add native verification
2026-03-10 13:48:45 +01:00

122 lines
3.5 KiB
Go

package user
import (
"fmt"
"os"
"strconv"
"time"
iamv1alpha2 "github.com/beclab/api/iam/v1alpha2"
"github.com/beclab/beos/framework/app-service/api/sys.bytetrade.io/v1alpha1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func newUserClientFromKubeConfig(kubeconfig string) (client.Client, error) {
if kubeconfig == "" {
kubeconfig = os.Getenv("KUBECONFIG")
if kubeconfig == "" {
kubeconfig = clientcmd.RecommendedHomeFile
}
}
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, fmt.Errorf("failed to get kubeconfig: %w", err)
}
scheme := runtime.NewScheme()
if err := iamv1alpha2.AddToScheme(scheme); err != nil {
return nil, fmt.Errorf("failed to add user scheme: %w", err)
}
if err := v1alpha1.AddToScheme(scheme); err != nil {
return nil, fmt.Errorf("failed to add system scheme: %w", err)
}
userClient, err := client.New(config, client.Options{Scheme: scheme})
if err != nil {
return nil, fmt.Errorf("failed to create user client: %w", err)
}
return userClient, nil
}
func validateResourceLimit(limit resourceLimit) error {
if limit.memoryLimit != "" {
memLimit, err := resource.ParseQuantity(limit.memoryLimit)
if err != nil {
return fmt.Errorf("invalid memory limit: %v", err)
}
minMemLimit, _ := resource.ParseQuantity(defaultMemoryLimit)
if memLimit.Cmp(minMemLimit) < 0 {
return fmt.Errorf("invalid memory limit: %s is less than minimum required: %s", memLimit.String(), minMemLimit.String())
}
}
if limit.cpuLimit != "" {
cpuLimit, err := resource.ParseQuantity(limit.cpuLimit)
if err != nil {
return fmt.Errorf("invalid cpu limit: %v", err)
}
minCPULimit, _ := resource.ParseQuantity(defaultCPULimit)
if cpuLimit.Cmp(minCPULimit) < 0 {
return fmt.Errorf("invalid cpu limit: %s is less than minimum required: %s", cpuLimit.String(), minCPULimit.String())
}
}
return nil
}
func convertUserObjectToUserInfo(user iamv1alpha2.User) userInfo {
info := userInfo{
UID: string(user.UID),
Name: user.Name,
DisplayName: user.Spec.DisplayName,
Description: user.Spec.Description,
Email: user.Spec.Email,
State: string(user.Status.State),
CreationTimestamp: user.CreationTimestamp.Unix(),
}
if user.Annotations != nil {
if role, ok := user.Annotations[annotationKeyRole]; ok {
info.Roles = []string{role}
}
if terminusName, ok := user.Annotations["bytetrade.io/terminus-name"]; ok {
info.TerminusName = terminusName
}
if avatar, ok := user.Annotations["bytetrade.io/avatar"]; ok {
info.Avatar = avatar
}
if memoryLimit, ok := user.Annotations[annotationKeyMemoryLimit]; ok {
info.MemoryLimit = memoryLimit
}
if cpuLimit, ok := user.Annotations[annotationKeyCPULimit]; ok {
info.CpuLimit = cpuLimit
}
}
if user.Status.LastLoginTime != nil {
lastLogin := user.Status.LastLoginTime.Unix()
info.LastLoginTime = &lastLogin
}
return info
}
func printUserTableHeaders() {
fmt.Printf("%-20s %-10s %-10s %-30s %-10s %-10s %-10s\n", "NAME", "ROLE", "STATE", "CREATE TIME", "ACTIVATED", "MEMORY", "CPU")
}
func printUserTableRow(info userInfo) {
role := roleNormal
if len(info.Roles) > 0 {
role = info.Roles[0]
}
fmt.Printf("%-20s %-10s %-10s %-30s %-10s %-10s %-10s\n",
info.Name, role, info.State, time.Unix(info.CreationTimestamp, 0).Format(time.RFC3339), strconv.FormatBool(info.WizardComplete), info.MemoryLimit, info.CpuLimit)
}