From 8305425d479f07deb8213cc41867f73ecfc39fb4 Mon Sep 17 00:00:00 2001 From: Nikolai Rodionov Date: Fri, 5 Apr 2024 17:40:20 +0200 Subject: [PATCH] Some updates --- api/v1/environments.go | 29 +++++++++++++++++++++++ dataexample.yaml | 4 ---- internal/controllers/accounts.go | 1 + internal/controllers/environments.go | 35 ++++++++++++++++++++++++++++ internal/helpers/kube/kube.go | 7 ++++++ 5 files changed, 72 insertions(+), 4 deletions(-) delete mode 100644 dataexample.yaml diff --git a/api/v1/environments.go b/api/v1/environments.go index 2ed1090..bda481e 100644 --- a/api/v1/environments.go +++ b/api/v1/environments.go @@ -60,6 +60,35 @@ func (e *EnvironmentsServer) Update(ctx context.Context, in *proto.EnvironmentFu } func (e *EnvironmentsServer) Delete(ctx context.Context, in *proto.EnvironmentFull) (*empty.Empty, error) { + md, ok := metadata.FromIncomingContext(ctx) + if !ok { + return nil, errors.New("metadata is not provided") + } + token, ok := md["token"] + if !ok { + return nil, errors.New("token is not sent via metadata") + } + + uuid, ok := md["uuid"] + if !ok { + return nil, errors.New("used id is not sent via metadata") + } + + data := &controllers.EnvironemntData{ + Name: in.Data.GetName(), + Provider: in.Data.GetProvider().String(), + Kubernetes: in.Data.GetKubernetes().String(), + } + environment := &controllers.Environemnt{ + UserID: uuid[0], + Controller: e.controller, + Data: data, + Token: token[0], + } + err := environment.Create(ctx) + if err != nil { + return nil, err + } return nil, nil } diff --git a/dataexample.yaml b/dataexample.yaml deleted file mode 100644 index 1857901..0000000 --- a/dataexample.yaml +++ /dev/null @@ -1,4 +0,0 @@ -provider: hetzner -servers: - - name: some name - kind: cax11 diff --git a/internal/controllers/accounts.go b/internal/controllers/accounts.go index bd34ee7..e31a40a 100644 --- a/internal/controllers/accounts.go +++ b/internal/controllers/accounts.go @@ -49,6 +49,7 @@ func (acc *Account) Create(ctx context.Context) error { Labels: map[string]string{ "username": acc.Data.Username, "email-verified": "false", + "managed-by": "softplayer", }, }, } diff --git a/internal/controllers/environments.go b/internal/controllers/environments.go index b8d41b8..a713903 100644 --- a/internal/controllers/environments.go +++ b/internal/controllers/environments.go @@ -103,3 +103,38 @@ func (env *Environemnt) Create(ctx context.Context) error { return nil } + +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", + }, + }, + Data: map[string]string{ + "vars": env.Data.buildVars(), + }, + } + if err := kube.Delete(ctx, controller.GetClient(), &obj, false); err != nil { + return err + } + + return nil +} diff --git a/internal/helpers/kube/kube.go b/internal/helpers/kube/kube.go index c8c9af8..68cf19c 100644 --- a/internal/helpers/kube/kube.go +++ b/internal/helpers/kube/kube.go @@ -52,3 +52,10 @@ func WaitUntilCreated(ctx context.Context, client client.Client, obj client.Obje } return nil } + +func Delete(ctx context.Context, client client.Client, obj client.Object, wait bool) error { + if err := client.Delete(ctx, obj); err != nil { + return err + } + return nil +}