Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
Split cleaner (#7)
Browse files Browse the repository at this point in the history
* Split cleaner

* wip
  • Loading branch information
piotrmiskiewicz authored Aug 25, 2021
1 parent 76082d3 commit 7b2e673
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
IMG ?= eu.gcr.io/sap-se-cx-gopher/sap-btp-service-operator-migration:v0.3.0
IMG ?= eu.gcr.io/sap-se-cx-gopher/sap-btp-service-operator-migration:v0.4.0

.PHONY: build-image
build-image:
Expand Down
53 changes: 31 additions & 22 deletions cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,37 @@ func (c *Cleaner) removeFinalizers(gvk schema.GroupVersionKind, ns string) error
return nil
}

func (c *Cleaner) PrepareSBUForRemoval() error {
namespaces := &v1.NamespaceList{}
err := c.k8sCli.List(context.Background(), namespaces)
if err != nil {
return err
}

for _, ns := range namespaces.Items {
ul := &unstructured.UnstructuredList{}
ul.SetGroupVersionKind(schema.GroupVersionKind{
Kind: "ServiceBindingUsage",
Group: "servicecatalog.kyma-project.io",
Version: "v1alpha1",
})
err := c.k8sCli.List(context.Background(), ul, client.InNamespace(ns.Name))
if err != nil {
return err
}

for _, sbu := range ul.Items {
log.Printf("Removing owner reference from SBU %s/%s", sbu.GetNamespace(), sbu.GetName())
sbu.SetOwnerReferences([]metav1.OwnerReference{})
err := c.k8sCli.Update(context.Background(), &sbu)
if err != nil {
return err
}
}
}
return nil
}

func (c *Cleaner) PrepareForRemoval() error {
// listing

Expand Down Expand Up @@ -249,28 +280,6 @@ func (c *Cleaner) PrepareForRemoval() error {
}
}

for _, ns := range namespaces.Items {
ul := &unstructured.UnstructuredList{}
ul.SetGroupVersionKind(schema.GroupVersionKind{
Kind: "ServiceBindingUsage",
Group: "servicecatalog.kyma-project.io",
Version: "v1alpha1",
})
err := c.k8sCli.List(context.Background(), ul, client.InNamespace(ns.Name))
if err != nil {
return err
}

for _, sbu := range ul.Items {
log.Printf("Removing owner reference from SBU %s/%s", sbu.GetNamespace(), sbu.GetName())
sbu.SetOwnerReferences([]metav1.OwnerReference{})
err := c.k8sCli.Update(context.Background(), &sbu)
if err != nil {
return err
}
}
}

log.Println("ServiceBindings secrets owner references")
var bindings = &v1beta1.ServiceBindingList{}
err = c.k8sCli.List(context.Background(), bindings, client.InNamespace(""))
Expand Down
4 changes: 3 additions & 1 deletion deploy/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Exit immediately if a command exits with a non-zero status.
set -e

cleaner sbu-prepare

sap-btp-service-operator-migration run

cleaner
cleaner final-clean
86 changes: 53 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ import (
"time"
)

const CommandSBUPrepare = "sbu-prepare"
const CommandFinalClean = "final-clean"
/*
The application expects environment varialbe "KUBECONFIG" to be set, then uninstalls Service Catalog and removes all SC resources.
*/
func main() {
command := CommandFinalClean
if len(os.Args) > 1 {
command = os.Args[1]
}
kubeconfigPath := os.Getenv("KUBECONFIG")
if kubeconfigPath == "" {
home, err := os.UserHomeDir()
Expand Down Expand Up @@ -41,46 +47,60 @@ func main() {
panic(err)
}

log.Println("Removing Service Catalog release")
err = cleaner.RemoveRelease(ServiceCatalogReleaseName)
if err != nil {
panic(err)
}
if command == CommandSBUPrepare {
log.Println("Removing service-catalog-addons release")
err = cleaner.RemoveRelease(ServiceCatalogAddonsReleaseName)
if err != nil {
panic(err)
}

log.Println("Removing service-catalog-addons release")
err = cleaner.RemoveRelease(ServiceCatalogAddonsReleaseName)
if err != nil {
panic(err)
}
log.Println("Removing finalizers and ownerreferences from SBU")
err = cleaner.PrepareSBUForRemoval()
if err != nil {
panic(err)
}

log.Println("Removing Helm Broker release")
err = cleaner.RemoveRelease(HelmBrokerReleaseName)
if err != nil {
panic(err)
return
}
time.Sleep(10 * time.Second)

log.Println()
log.Println("Removing finalizers")
err = cleaner.PrepareForRemoval()
if err != nil {
panic(err)
}
if command == CommandFinalClean {
log.Println("Removing Service Catalog release")
err = cleaner.RemoveRelease(ServiceCatalogReleaseName)
if err != nil {
panic(err)
}

time.Sleep(4 * time.Second)
log.Println("Removing Helm Broker release")
err = cleaner.RemoveRelease(HelmBrokerReleaseName)
if err != nil {
panic(err)
}

log.Println()
log.Println("Deleting resources")
err = cleaner.RemoveResources()
if err != nil {
panic(err)
}
time.Sleep(10 * time.Second)
log.Println("Removing finalizers")
err = cleaner.PrepareForRemoval()
if err != nil {
panic(err)
}

time.Sleep(2 * time.Second)
time.Sleep(4 * time.Second)

log.Println("Deleting CRDs")
err = cleaner.RemnoveCRDs()
if err != nil {
panic(err)
log.Println()
log.Println("Deleting resources")
if command == CommandFinalClean {
err = cleaner.RemoveResources()
if err != nil {
panic(err)
}
}

log.Println("Deleting CRDs")
err = cleaner.RemnoveCRDs()
if err != nil {
panic(err)
}

return
}

}

0 comments on commit 7b2e673

Please sign in to comment.