2024-03-19 15:49:29 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-04-03 18:05:23 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-03-19 15:49:29 +00:00
|
|
|
"log"
|
|
|
|
|
2024-04-19 14:23:53 +00:00
|
|
|
"github.com/joho/godotenv"
|
|
|
|
|
2024-04-03 18:05:23 +00:00
|
|
|
"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"
|
2024-04-15 14:30:47 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2024-04-03 18:05:23 +00:00
|
|
|
"k8s.io/client-go/rest"
|
2024-03-19 15:49:29 +00:00
|
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Environemnt struct {
|
|
|
|
Controller ctrl.Manager
|
2024-04-03 18:05:23 +00:00
|
|
|
UserID string
|
2024-03-19 15:49:29 +00:00
|
|
|
Data *EnvironemntData
|
2024-04-03 18:05:23 +00:00
|
|
|
Token string
|
2024-03-19 15:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type EnvironemntData struct {
|
2024-04-03 18:05:23 +00:00
|
|
|
Name string
|
2024-04-29 12:07:02 +00:00
|
|
|
Description string
|
2024-04-29 18:03:37 +00:00
|
|
|
Provider string
|
2024-04-03 18:05:23 +00:00
|
|
|
Kubernetes string
|
2024-04-29 17:34:55 +00:00
|
|
|
Location string
|
|
|
|
ServerType string
|
2024-03-19 15:49:29 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 16:28:13 +00:00
|
|
|
func (e *EnvironemntData) buildVars() (string, error) {
|
|
|
|
vars := fmt.Sprintf(`# -- Generated by the softplayer controller
|
|
|
|
SP_PROVIDER=%s
|
|
|
|
SP_KUBERNETES=%s
|
|
|
|
SP_SERVER_TYPE=%s
|
|
|
|
SP_SERVER_LOCATION=%s`,
|
|
|
|
e.Provider,
|
|
|
|
e.Kubernetes,
|
2024-04-29 17:34:55 +00:00
|
|
|
e.ServerType,
|
|
|
|
e.Location,
|
2024-04-29 16:28:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return vars, nil
|
2024-04-03 18:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
val, ok := ns.GetLabels()["email-verified"]
|
|
|
|
if !ok || val == "false" {
|
2024-04-29 18:03:37 +00:00
|
|
|
return errors.New("user email is not verified, can't create an new env")
|
2024-04-03 18:05:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create environment should create a new configmap in the user's namespace
|
|
|
|
// using a token that belongs to the user.
|
2024-03-19 15:49:29 +00:00
|
|
|
func (env *Environemnt) Create(ctx context.Context) error {
|
2024-04-03 18:05:23 +00:00
|
|
|
if err := env.isNsVerified(ctx); err != nil {
|
|
|
|
log.Println("Can't verify ns")
|
|
|
|
return err
|
|
|
|
}
|
2024-04-29 16:28:13 +00:00
|
|
|
|
2024-04-03 18:05:23 +00:00
|
|
|
env.Controller.GetClient()
|
|
|
|
conf := &rest.Config{
|
|
|
|
Host: "https://kubernetes.default.svc.cluster.local:443",
|
|
|
|
BearerToken: env.Token,
|
|
|
|
TLSClientConfig: rest.TLSClientConfig{
|
|
|
|
Insecure: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
controller, err := ctrl.NewManager(conf, ctrl.Options{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-29 16:28:13 +00:00
|
|
|
vars, err := env.Data.buildVars()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-03 18:05:23 +00:00
|
|
|
obj := corev1.ConfigMap{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: env.Data.Name,
|
|
|
|
Namespace: env.UserID,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"component": "bootstrap",
|
2024-04-15 13:45:05 +00:00
|
|
|
"kind": "environment",
|
2024-04-03 18:05:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Data: map[string]string{
|
2024-04-29 12:07:02 +00:00
|
|
|
"description": env.Data.Description,
|
2024-04-29 16:28:13 +00:00
|
|
|
"vars": vars,
|
2024-04-03 18:05:23 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := kube.Create(ctx, controller.GetClient(), &obj, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-19 15:49:29 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-04-05 15:40:20 +00:00
|
|
|
|
|
|
|
func (env *Environemnt) Delete(ctx context.Context) error {
|
|
|
|
env.Controller.GetClient()
|
|
|
|
conf := &rest.Config{
|
|
|
|
Host: "https://kubernetes.default.svc.cluster.local:443",
|
|
|
|
BearerToken: env.Token,
|
|
|
|
TLSClientConfig: rest.TLSClientConfig{
|
|
|
|
Insecure: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
controller, err := ctrl.NewManager(conf, ctrl.Options{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
obj := corev1.ConfigMap{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: env.Data.Name,
|
|
|
|
Namespace: env.UserID,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"component": "bootstrap",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := kube.Delete(ctx, controller.GetClient(), &obj, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-04-15 13:45:05 +00:00
|
|
|
|
|
|
|
func (env *Environemnt) ListEnvs(ctx context.Context) ([]string, error) {
|
2024-04-15 14:16:25 +00:00
|
|
|
env.Controller.GetClient()
|
2024-04-15 13:45:05 +00:00
|
|
|
conf := &rest.Config{
|
|
|
|
Host: "https://kubernetes.default.svc.cluster.local:443",
|
|
|
|
BearerToken: env.Token,
|
|
|
|
TLSClientConfig: rest.TLSClientConfig{
|
|
|
|
Insecure: true,
|
|
|
|
},
|
|
|
|
}
|
2024-04-15 14:30:47 +00:00
|
|
|
clientset, err := kubernetes.NewForConfig(conf)
|
2024-04-15 13:45:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
2024-04-19 14:23:53 +00:00
|
|
|
}
|
2024-04-15 15:12:52 +00:00
|
|
|
secrets, err := clientset.CoreV1().ConfigMaps(env.UserID).List(ctx, metav1.ListOptions{LabelSelector: "kind=environment"})
|
2024-04-15 14:44:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
2024-04-15 13:45:05 +00:00
|
|
|
|
|
|
|
result := []string{}
|
2024-04-15 15:06:23 +00:00
|
|
|
log.Println(secrets)
|
2024-04-15 14:30:47 +00:00
|
|
|
for _, env := range secrets.Items {
|
2024-04-15 13:45:05 +00:00
|
|
|
result = append(result, env.Name)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
2024-04-19 14:23:53 +00:00
|
|
|
|
|
|
|
func (env *Environemnt) Get(ctx context.Context) error {
|
|
|
|
env.Controller.GetClient()
|
|
|
|
conf := &rest.Config{
|
|
|
|
Host: "https://kubernetes.default.svc.cluster.local:443",
|
|
|
|
BearerToken: env.Token,
|
|
|
|
TLSClientConfig: rest.TLSClientConfig{
|
|
|
|
Insecure: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
clientset, err := kubernetes.NewForConfig(conf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
envData, err := clientset.CoreV1().ConfigMaps(env.UserID).Get(ctx, env.Data.Name, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := godotenv.Unmarshal(envData.Data["vars"])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-29 16:28:13 +00:00
|
|
|
// env.Data.Provider = res["SP_PROVIDER"]
|
2024-04-19 14:23:53 +00:00
|
|
|
env.Data.Kubernetes = res["SP_KUBERNETES"]
|
|
|
|
return nil
|
|
|
|
}
|