Add an ability to uninstall applications

This commit is contained in:
2024-05-13 13:43:26 +02:00
parent 1b31217ab3
commit aa13131b0d
2 changed files with 68 additions and 1 deletions

View File

@ -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",