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

Commit

Permalink
MAke the cleaner working on target cluster (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmiskiewicz authored Aug 4, 2021
1 parent dfb687f commit 9ecf293
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 37 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ COPY --from=builder /etc/ssl/certs /etc/ssl/certs
COPY --from=builder /go/bin/sap-btp-service-operator-migration /bin/sap-btp-service-operator-migration
COPY --from=builder /go/bin/cleaner /bin/cleaner
COPY --from=builder /run.sh /bin/run.sh
RUN chmod a+x /bin/run.sh

ENTRYPOINT ["run.sh"]
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# sc-removal
This repository holds code for Service Management removal.

## building
## Build and push the image

```shell
# authorize and configure gcloud
gcloud auth login
gcloud auth configure-docker

# build and push
SSH_PRIVATE_KEY="$(cat ~/.ssh/id_rsa)" make build-image push-image
```

65 changes: 43 additions & 22 deletions cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
helmclient "github.com/mittwald/go-helm-client"
"helm.sh/helm/v3/pkg/storage/driver"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/rest"

apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -28,19 +29,30 @@ const (
type Cleaner struct {
k8sCli client.Client
kubeConfigContent []byte
helmClient helmclient.Client
}

func NewCleaner(kubeConfigContent []byte) (*Cleaner, error) {
kubeconfig, err := clientcmd.NewClientConfigFromBytes(kubeConfigContent)
if err != nil {
return nil, err
var restConfig *rest.Config
if len(kubeConfigContent) > 0 {
var err error
kubeconfig, err := clientcmd.NewClientConfigFromBytes(kubeConfigContent)
if err != nil {
return nil, err
}
restConfig, err = kubeconfig.ClientConfig()
if err != nil {
return nil, err
}
} else {
var err error
restConfig, err = rest.InClusterConfig()
if err != nil {
return nil, err
}
}

rc, err := kubeconfig.ClientConfig()
if err != nil {
return nil, err
}
k8sCli, err := client.New(rc, client.Options{
k8sCli, err := client.New(restConfig, client.Options{
Scheme: scheme.Scheme,
})
if err != nil {
Expand All @@ -51,22 +63,31 @@ func NewCleaner(kubeConfigContent []byte) (*Cleaner, error) {
return nil, err
}

helmClient, err := helmclient.NewClientFromRestConf(&helmclient.RestConfClientOptions{
Options: &helmclient.Options{
Namespace: "kyma-system",
RepositoryConfig: "",
RepositoryCache: "",
Debug: false,
Linting: false,
DebugLog: nil,
},
RestConfig: restConfig,
})
if err != nil {
return nil, err
}

return &Cleaner{
k8sCli: k8sCli,
kubeConfigContent: kubeConfigContent,
helmClient: helmClient,
}, nil
}

func (c *Cleaner) RemoveRelease(releaseName string) error {
helmCli, err := helmclient.NewClientFromKubeConf(&helmclient.KubeConfClientOptions{
Options: &helmclient.Options{
Namespace: "kyma-system",
},
KubeConfig: c.kubeConfigContent,
})

log.Println("Looking for Service Catalog release...")
release, err := helmCli.GetRelease(releaseName)
release, err := c.helmClient.GetRelease(releaseName)
if err == driver.ErrReleaseNotFound {
log.Println("service-catalog release not found, nothing to do")
return nil
Expand All @@ -77,7 +98,7 @@ func (c *Cleaner) RemoveRelease(releaseName string) error {

log.Printf("Found %s release in the namespace %s: status %s", release.Name, release.Namespace, release.Info.Status.String())
log.Println(" Uninstalling...")
err = helmCli.UninstallRelease(&helmclient.ChartSpec{
err = c.helmClient.UninstallRelease(&helmclient.ChartSpec{
ReleaseName: releaseName,
Timeout: time.Minute,
Wait: true,
Expand Down Expand Up @@ -109,8 +130,8 @@ func (c *Cleaner) RemoveResources() error {
},
{
Kind: "ServiceBroker",
Group: "servicecatalog.kyma-project.io",
Version: "v1alpha1",
Group: "servicecatalog.k8s.io",
Version: "v1beta1",
},
}

Expand All @@ -135,8 +156,8 @@ func (c *Cleaner) RemoveResources() error {
u := &unstructured.Unstructured{}
u.SetGroupVersionKind(schema.GroupVersionKind{
Kind: "ClusterServiceBroker",
Group: "servicecatalog.kyma-project.io",
Version: "v1alpha1",
Group: "servicecatalog.k8s.io",
Version: "v1beta1",
})
err = c.k8sCli.DeleteAllOf(context.Background(), u, client.InNamespace(""))
if err != nil {
Expand Down Expand Up @@ -272,7 +293,7 @@ func (c *Cleaner) RemnoveCRDs() error {

for _, crd := range crdsList.Items {
if crd.Spec.Group == "servicecatalog.k8s.io" || crd.Spec.Group == "servicecatalog.kyma-project.io" {
log.Println("Removing CRD %s", crd.Name)
log.Printf("Removing CRD %s", crd.Name)
c.k8sCli.Delete(context.Background(), &crd)
}
}
Expand Down
48 changes: 45 additions & 3 deletions deploy/chart/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,55 @@ kind: ClusterRole
metadata:
name: sap-btp-operator-migration
rules:
- apiGroups:
- "apiextensions.k8s.io"
resources:
- customresourcedefinitions
verbs:
- get
- list
- apiGroups:
- "apps"
resources:
- customresourcedefinitions
verbs:
- get
- list
- delete
- apiGroups:
- "rbac.authorization.k8s.io apps"
resources:
- customresourcedefinitions
verbs:
- get
- list
- delete
- apiGroups:
- "admissionregistration.k8s.io"
resources:
- "*"
verbs:
- get
- list
- delete
- apiGroups:
- ""
resources:
# this tool changes owner reference of the secret to prevent
# cascade delete when service catalog servicebindings is deleted
- secrets
- "*"
verbs:
- get
- list
- watch
- patch
- update
- delete
- apiGroups:
- ""
resources:
# needs to get sap-btp-operator ConfigMap
- configmaps
- customresourcedefinitions
- namespaces
verbs:
- get
- list
Expand All @@ -40,7 +72,17 @@ rules:
- update
- patch
- delete
- deletecollection
- apiGroups:
- servicecatalog.kyma-project.io
verbs:
- get
- list
- watch
- create
- update
- patch
- delete
- deletecollection
resources:
- servicebindingusages
3 changes: 0 additions & 3 deletions deploy/chart/templates/job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,5 @@ spec:
restartPolicy: Never
containers:
- name: migration
command:
- /bin/sh
- run.sh
image: {{.Values.image.repository}}:{{.Values.image.tag}}
imagePullPolicy: Always
35 changes: 27 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth"
"log"
"os"
"path"
"time"
)

Expand All @@ -14,15 +15,25 @@ The application expects environment varialbe "KUBECONFIG" to be set, then uninst
func main() {
kubeconfigPath := os.Getenv("KUBECONFIG")
if kubeconfigPath == "" {
kubeconfigPath = "~/.kube/config"
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
kubeconfigPath = path.Join(home, ".kube/config")
}

log.Printf("Using kubeconfig file: %s", kubeconfigPath)

// read the kubeconfig
kcContent, err := ioutil.ReadFile(kubeconfigPath)
if err != nil {
panic(err)
if os.IsNotExist(err) {
// empty kubeconfig content means - use "in cluster config"
log.Println("Kubeconfig does not exists, using in-cluster config")
kcContent = []byte{}
} else {
panic(err)
}
}

cleaner, err := NewCleaner(kcContent)
Expand All @@ -31,14 +42,23 @@ func main() {
}

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

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

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

log.Println()
log.Println("Removing finalizers")
Expand All @@ -47,7 +67,7 @@ func main() {
panic(err)
}

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

log.Println()
log.Println("Deleting resources")
Expand All @@ -56,12 +76,11 @@ func main() {
panic(err)
}

time.Sleep(2 * time.Second)

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



}

0 comments on commit 9ecf293

Please sign in to comment.