diff --git a/api/v1/application.go b/api/v1/application.go index 20bf5d8..0f03580 100644 --- a/api/v1/application.go +++ b/api/v1/application.go @@ -6,6 +6,8 @@ import ( "git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers" proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/applications" "github.com/go-logr/logr" + "github.com/golang/protobuf/ptypes/empty" + "google.golang.org/protobuf/types/known/emptypb" ctrl "sigs.k8s.io/controller-runtime" ) @@ -56,3 +58,27 @@ func (app *ApplicationServer) Create(ctx context.Context, in *proto.CreateOption Spec: in.GetSpec(), }, nil } + +func (app *ApplicationServer) Delete(ctx context.Context, in *proto.DeleteOptions) (*empty.Empty, error) { + log := app.logInstance + log.WithValues("user_id", in.GetOwnerId().GetUuid(), "app_id", in.GetId().GetUuid()) + ctx = logr.NewContext(ctx, log) + + data := &controllers.ApplicationData{ + Name: in.Metadata.Name, + UUID: in.GetId().GetUuid(), + } + + application := &controllers.Application{ + UserID: in.GetOwnerId().GetUuid(), + Controller: app.controller, + Data: data, + Token: in.GetToken().GetToken(), + } + + err := application.Delete(ctx) + if err != nil { + return nil, err + } + return &emptypb.Empty{}, nil +} diff --git a/internal/controllers/applications.go b/internal/controllers/applications.go index bf79028..668cc43 100644 --- a/internal/controllers/applications.go +++ b/internal/controllers/applications.go @@ -21,6 +21,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" corev1 "k8s.io/api/core/v1" + "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" ctrl "sigs.k8s.io/controller-runtime" ) @@ -185,6 +186,46 @@ func (app *Application) Create(ctx context.Context) error { return nil } +func (app *Application) Delete(ctx context.Context) error { + log, err := logr.FromContext(ctx) + if err != nil { + zapLog, err := zap.NewDevelopment() + if err != nil { + panic(fmt.Sprintf("who watches the watchmen (%v)?", err)) + } + log = zapr.NewLogger(zapLog) + } + + app.Controller.GetClient() + conf := &rest.Config{ + Host: "https://kubernetes.default.svc.cluster.local:443", + BearerToken: app.Token, + TLSClientConfig: rest.TLSClientConfig{ + Insecure: true, + }, + } + + clientset, err := kubernetes.NewForConfig(conf) + if err != nil { + log.Error(err, "Couldn't create a new clientset") + return consts.ErrSystemError + } + + configmaps, err := clientset.CoreV1().ConfigMaps(app.UserID).List(ctx, metav1.ListOptions{LabelSelector: fmt.Sprintf("uuid=%s", app.Data.UUID)}) + if err != nil { + log.Error(err, "Couldn't list configmaps") + return consts.ErrSystemError + } + for _, cm := range configmaps.Items { + if err := clientset.CoreV1().ConfigMaps(app.UserID).Delete(ctx, cm.GetName(), *metav1.NewDeleteOptions(100)); err != nil { + log.Error(err, "Couldn't remove configmap", "name", cm.GetName(), "namespace", cm.GetNamespace()) + return consts.ErrSystemError + } + } + + return nil +} + // func (env *Environemnt) Update(ctx context.Context) error { // if err := env.isNsVerified(ctx); err != nil { // log.Println("Can't verify ns") @@ -253,7 +294,7 @@ func (app *Application) Create(ctx context.Context) error { // return nil // } -// func (env *Environemnt) Delete(ctx context.Context) error { +// func (*Environemnt) Delete(ctx context.Context) error { // env.Controller.GetClient() // conf := &rest.Config{ // Host: "https://kubernetes.default.svc.cluster.local:443",