Try a clientset instead of a client

This commit is contained in:
Nikolai Rodionov 2024-05-06 10:48:59 +02:00
parent 7d1effa22a
commit 0cb8295df1
Signed by: allanger
GPG Key ID: 0AA46A90E25592AD

View File

@ -13,7 +13,6 @@ import (
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/kube" "git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/kube"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest" "k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime" ctrl "sigs.k8s.io/controller-runtime"
@ -21,6 +20,7 @@ import (
type Environemnt struct { type Environemnt struct {
Controller ctrl.Manager Controller ctrl.Manager
Config *rest.Config
UserID string UserID string
Data *EnvironemntData Data *EnvironemntData
Token string Token string
@ -56,9 +56,12 @@ SP_DISK_SIZE=%d`,
// Check whether used has passed the email verification // Check whether used has passed the email verification
func (env *Environemnt) isNsVerified(ctx context.Context) error { func (env *Environemnt) isNsVerified(ctx context.Context) error {
client := env.Controller.GetClient() clientset, err := kubernetes.NewForConfig(env.Config)
ns := &corev1.Namespace{} if err != nil {
if err := client.Get(ctx, types.NamespacedName{Name: env.UserID}, ns); err != nil { return err
}
ns, err := clientset.CoreV1().Namespaces().Get(ctx, env.UserID, metav1.GetOptions{})
if err != nil {
return err return err
} }
@ -66,18 +69,21 @@ func (env *Environemnt) isNsVerified(ctx context.Context) error {
if !ok || val == "false" { if !ok || val == "false" {
return errors.New("user email is not verified, can't create an new env") return errors.New("user email is not verified, can't create an new env")
} }
return nil return nil
} }
// Create environment should create a new configmap in the user's namespace // Create environment should create a new configmap in the user's namespace
// using a token that belongs to the user. // using a token that belongs to the user.
func (env *Environemnt) Create(ctx context.Context) error { func (env *Environemnt) Create(ctx context.Context) error {
env.Data.UUID = uuid.New().String()
if err := env.isNsVerified(ctx); err != nil { if err := env.isNsVerified(ctx); err != nil {
log.Println("Can't verify ns") log.Println("Can't verify ns")
return err return err
} }
// Prepare a new ID for a enironment
env.Data.UUID = uuid.New().String()
env.Controller.GetClient() env.Controller.GetClient()
conf := &rest.Config{ conf := &rest.Config{
Host: "https://kubernetes.default.svc.cluster.local:443", Host: "https://kubernetes.default.svc.cluster.local:443",