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