Add an ability to uninstall applications
This commit is contained in:
		| @@ -6,6 +6,8 @@ import ( | |||||||
| 	"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers" | 	"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers" | ||||||
| 	proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/applications" | 	proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/applications" | ||||||
| 	"github.com/go-logr/logr" | 	"github.com/go-logr/logr" | ||||||
|  | 	"github.com/golang/protobuf/ptypes/empty" | ||||||
|  | 	"google.golang.org/protobuf/types/known/emptypb" | ||||||
| 	ctrl "sigs.k8s.io/controller-runtime" | 	ctrl "sigs.k8s.io/controller-runtime" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| @@ -56,3 +58,27 @@ func (app *ApplicationServer) Create(ctx context.Context, in *proto.CreateOption | |||||||
| 		Spec: in.GetSpec(), | 		Spec: in.GetSpec(), | ||||||
| 	}, nil | 	}, 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 | ||||||
|  | } | ||||||
|   | |||||||
| @@ -21,6 +21,7 @@ import ( | |||||||
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||
|  |  | ||||||
| 	corev1 "k8s.io/api/core/v1" | 	corev1 "k8s.io/api/core/v1" | ||||||
|  | 	"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" | ||||||
| ) | ) | ||||||
| @@ -185,6 +186,46 @@ func (app *Application) Create(ctx context.Context) error { | |||||||
| 	return nil | 	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 { | // func (env *Environemnt) Update(ctx context.Context) error { | ||||||
| // 	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") | ||||||
| @@ -253,7 +294,7 @@ func (app *Application) Create(ctx context.Context) error { | |||||||
|  |  | ||||||
| // 	return nil | // 	return nil | ||||||
| // } | // } | ||||||
| // func (env *Environemnt) Delete(ctx context.Context) error { | // func (*Environemnt) Delete(ctx context.Context) error { | ||||||
| // 	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", | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user