{"action":"created","comment":{"url":"https://api.github.com/repos/rancher/system-tools/pulls/comments/217808149","pull_request_review_id":155617572,"id":217808149,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIxNzgwODE0OQ==","diff_hunk":"@@ -1,23 +1,637 @@\n package main\n \n import (\n+\t\"fmt\"\n \t\"os\"\n+\t\"strings\"\n+\t\"time\"\n \n-\t\"github.com/Sirupsen/logrus\"\n+\t\"github.com/rancher/types/apis/management.cattle.io/v3\"\n+\t\"github.com/rancher/types/config\"\n+\t\"github.com/sirupsen/logrus\"\n \t\"github.com/urfave/cli\"\n+\t\"k8s.io/apimachinery/pkg/api/errors\"\n+\t\"k8s.io/apimachinery/pkg/apis/meta/v1\"\n+\t\"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured\"\n+\t\"k8s.io/apimachinery/pkg/runtime/schema\"\n+\t\"k8s.io/client-go/discovery\"\n+\t\"k8s.io/client-go/dynamic\"\n+\t\"k8s.io/client-go/kubernetes\"\n+\t\"k8s.io/client-go/rest\"\n+\t\"k8s.io/client-go/tools/clientcmd\"\n )\n \n-var VERSION = \"v0.0.0-dev\"\n+const (\n+\tCattleControllerName   = \"controller.cattle.io\"\n+\tDefaultCattleNamespace = \"cattle-system\"\n+\tCattleLabelBase        = \"cattle.io\"\n+)\n+\n+var VERSION = \"dev\"\n+\n+var staticClusterRoles = []string{\n+\t\"cluster-owner\",\n+\t\"create-ns\",\n+\t\"project-owner\",\n+\t\"project-owner-promoted\",\n+}\n+var cattleListOptions = v1.ListOptions{\n+\tLabelSelector: \"cattle.io/creator=norman\",\n+}\n+var deletePolicy = v1.DeletePropagationOrphan\n+var deleteGracePeriod int64 = 120\n \n func main() {\n+\tcommonFlags := []cli.Flag{\n+\t\tcli.StringFlag{\n+\t\t\tName:   \"kubeconfig,c\",\n+\t\t\tEnvVar: \"KUBECONFIG\",\n+\t\t\tUsage:  \"kubeconfig absolute path\",\n+\t\t},\n+\t\tcli.StringFlag{\n+\t\t\tName:  \"namespace,n\",\n+\t\t\tValue: DefaultCattleNamespace,\n+\t\t\tUsage: \"rancher 2.x deployment namespace. default is `cattle-system`\",\n+\t\t},\n+\t}\n \tapp := cli.NewApp()\n-\tapp.Name = \"cleanup-tools\"\n+\tapp.Name = \"system-tools\"\n \tapp.Version = VERSION\n-\tapp.Usage = \"You need help!\"\n-\tapp.Action = func(c *cli.Context) error {\n-\t\tlogrus.Info(\"I'm a turkey\")\n-\t\treturn nil\n+\tapp.Usage = \"Rancher 2.0 operations tool kit\"\n+\tapp.Commands = []cli.Command{\n+\t\tcli.Command{\n+\t\t\tName:   \"remove\",\n+\t\t\tUsage:  \"safely remove rancher 2.x management plane\",\n+\t\t\tAction: doRemoveRancher,\n+\t\t\tFlags:  commonFlags,\n+\t\t},\n+\t}\n+\n+\tif err := app.Run(os.Args); err != nil {\n+\t\tlogrus.Fatal(err)\n+\t}\n+}\n+\n+func doRemoveRancher(ctx *cli.Context) error {\n+\tcattleNamespace := ctx.String(\"namespace\")\n+\tlogrus.Infof(\"Removing rancher deployment in namespace: [%s]\", cattleNamespace)\n+\t// setup\n+\tlogrus.Infof(\"Getting conenction configuration\")\n+\trestConfig, err := getRestConfig(ctx)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\tmanagement, err := config.NewManagementContext(*restConfig)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\tk8sClient, err := getClientSet(ctx)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\n+\tif err := removeCattleDeployment(ctx); err != nil {\n+\t\treturn err\n+\t}\n+\n+\tif err := removeCattleFinalizers(ctx); err != nil {\n+\t\treturn err\n+\t}\n+\n+\tif err := namespacesCleanup(k8sClient); err != nil {\n+\t\treturn err\n+\t}\n+\n+\tif err := secretsCleanup(k8sClient); err != nil {\n+\t\treturn err\n+\t}\n+\n+\tif err := projectsCleanup(management, k8sClient); err != nil {\n+\t\treturn err\n+\t}\n+\n+\tif err = nodesCleanup(management); err != nil {\n+\t\treturn err\n+\t}\n+\n+\tif err := clustersCleanup(management, k8sClient); err != nil {\n+\t\treturn err\n+\t}\n+\n+\tif err := usersCleanup(management, k8sClient); err != nil {\n+\t\treturn err\n+\t}\n+\n+\tif err := clusterRoleBindginsCleanup(k8sClient); err != nil {\n+\t\treturn err\n+\t}\n+\n+\tif err := clusterRolesCleanup(k8sClient); err != nil {\n+\t\treturn err\n+\t}\n+\t// final cleanup\n+\tlogrus.Infof(\"Removing Rancher Namespace [%s]\", cattleNamespace)\n+\tif err := deleteNamespace(k8sClient, cattleNamespace); err != nil && !errors.IsNotFound(err) {\n+\t\treturn err\n+\t}\n+\tlogrus.Infof(\"Successfully removed namespace [%s]\", cattleNamespace)\n+\tlogrus.Infof(\"Rancher Management Plane removed successfully\")\n+\treturn nil\n+}\n+\n+func removeCattleDeployment(ctx *cli.Context) error {\n+\tlogrus.Infof(\"Removing Cattle deployment\")\n+\tclientSet, err := getClientSet(ctx)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\tcattleNamespace := ctx.String(\"namespace\")\n+\tdeployments, err := clientSet.AppsV1().Deployments(cattleNamespace).List(v1.ListOptions{})\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\tfor _, deployment := range deployments.Items {\n+\t\tif err := clientSet.AppsV1().Deployments(cattleNamespace).Delete(deployment.Name, &v1.DeleteOptions{}); err != nil {\n+\t\t\treturn err","path":"main.go","position":155,"original_position":162,"commit_id":"32f2e68c022917912078b176382badc00b36bec9","original_commit_id":"32f2e68c022917912078b176382badc00b36bec9","user":{"login":"alena1108","id":1562176,"node_id":"MDQ6VXNlcjE1NjIxNzY=","avatar_url":"https://avatars0.githubusercontent.com/u/1562176?v=4","gravatar_id":"","url":"https://api.github.com/users/alena1108","html_url":"https://github.com/alena1108","followers_url":"https://api.github.com/users/alena1108/followers","following_url":"https://api.github.com/users/alena1108/following{/other_user}","gists_url":"https://api.github.com/users/alena1108/gists{/gist_id}","starred_url":"https://api.github.com/users/alena1108/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alena1108/subscriptions","organizations_url":"https://api.github.com/users/alena1108/orgs","repos_url":"https://api.github.com/users/alena1108/repos","events_url":"https://api.github.com/users/alena1108/events{/privacy}","received_events_url":"https://api.github.com/users/alena1108/received_events","type":"User","site_admin":false},"body":"I feel like we should have a retry in the code. Like 3 times retry. Otherwise the chances are high that something might fail, and we will just keep on spitting the error to the user.","created_at":"2018-09-14T18:45:04Z","updated_at":"2018-09-14T18:45:04Z","html_url":"https://github.com/rancher/system-tools/pull/2#discussion_r217808149","pull_request_url":"https://api.github.com/repos/rancher/system-tools/pulls/2","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/rancher/system-tools/pulls/comments/217808149"},"html":{"href":"https://github.com/rancher/system-tools/pull/2#discussion_r217808149"},"pull_request":{"href":"https://api.github.com/repos/rancher/system-tools/pulls/2"}}},"pull_request":{"url":"https://api.github.com/repos/rancher/system-tools/pulls/2","id":215675832,"node_id":"MDExOlB1bGxSZXF1ZXN0MjE1Njc1ODMy","html_url":"https://github.com/rancher/system-tools/pull/2","diff_url":"https://github.com/rancher/system-tools/pull/2.diff","patch_url":"https://github.com/rancher/system-tools/pull/2.patch","issue_url":"https://api.github.com/repos/rancher/system-tools/issues/2","number":2,"state":"open","locked":false,"title":"Add system-tools","user":{"login":"moelsayed","id":4588065,"node_id":"MDQ6VXNlcjQ1ODgwNjU=","avatar_url":"https://avatars1.githubusercontent.com/u/4588065?v=4","gravatar_id":"","url":"https://api.github.com/users/moelsayed","html_url":"https://github.com/moelsayed","followers_url":"https://api.github.com/users/moelsayed/followers","following_url":"https://api.github.com/users/moelsayed/following{/other_user}","gists_url":"https://api.github.com/users/moelsayed/gists{/gist_id}","starred_url":"https://api.github.com/users/moelsayed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/moelsayed/subscriptions","organizations_url":"https://api.github.com/users/moelsayed/orgs","repos_url":"https://api.github.com/users/moelsayed/repos","events_url":"https://api.github.com/users/moelsayed/events{/privacy}","received_events_url":"https://api.github.com/users/moelsayed/received_events","type":"User","site_admin":false},"body":"","created_at":"2018-09-14T18:30:08Z","updated_at":"2018-09-14T18:45:04Z","closed_at":null,"merged_at":null,"merge_commit_sha":"7ddb01d76c9d276995c0a4186e56ef8bcd79057f","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/rancher/system-tools/pulls/2/commits","review_comments_url":"https://api.github.com/repos/rancher/system-tools/pulls/2/comments","review_comment_url":"https://api.github.com/repos/rancher/system-tools/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rancher/system-tools/issues/2/comments","statuses_url":"https://api.github.com/repos/rancher/system-tools/statuses/32f2e68c022917912078b176382badc00b36bec9","head":{"label":"moelsayed:add_tools","ref":"add_tools","sha":"32f2e68c022917912078b176382badc00b36bec9","user":{"login":"moelsayed","id":4588065,"node_id":"MDQ6VXNlcjQ1ODgwNjU=","avatar_url":"https://avatars1.githubusercontent.com/u/4588065?v=4","gravatar_id":"","url":"https://api.github.com/users/moelsayed","html_url":"https://github.com/moelsayed","followers_url":"https://api.github.com/users/moelsayed/followers","following_url":"https://api.github.com/users/moelsayed/following{/other_user}","gists_url":"https://api.github.com/users/moelsayed/gists{/gist_id}","starred_url":"https://api.github.com/users/moelsayed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/moelsayed/subscriptions","organizations_url":"https://api.github.com/users/moelsayed/orgs","repos_url":"https://api.github.com/users/moelsayed/repos","events_url":"https://api.github.com/users/moelsayed/events{/privacy}","received_events_url":"https://api.github.com/users/moelsayed/received_events","type":"User","site_admin":false},"repo":{"id":147716708,"node_id":"MDEwOlJlcG9zaXRvcnkxNDc3MTY3MDg=","name":"system-tools","full_name":"moelsayed/system-tools","owner":{"login":"moelsayed","id":4588065,"node_id":"MDQ6VXNlcjQ1ODgwNjU=","avatar_url":"https://avatars1.githubusercontent.com/u/4588065?v=4","gravatar_id":"","url":"https://api.github.com/users/moelsayed","html_url":"https://github.com/moelsayed","followers_url":"https://api.github.com/users/moelsayed/followers","following_url":"https://api.github.com/users/moelsayed/following{/other_user}","gists_url":"https://api.github.com/users/moelsayed/gists{/gist_id}","starred_url":"https://api.github.com/users/moelsayed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/moelsayed/subscriptions","organizations_url":"https://api.github.com/users/moelsayed/orgs","repos_url":"https://api.github.com/users/moelsayed/repos","events_url":"https://api.github.com/users/moelsayed/events{/privacy}","received_events_url":"https://api.github.com/users/moelsayed/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/moelsayed/system-tools","description":"This repo is for tools helping with various cleanup tasks for rancher projects. Example: rancher installation cleanup","fork":true,"url":"https://api.github.com/repos/moelsayed/system-tools","forks_url":"https://api.github.com/repos/moelsayed/system-tools/forks","keys_url":"https://api.github.com/repos/moelsayed/system-tools/keys{/key_id}","collaborators_url":"https://api.github.com/repos/moelsayed/system-tools/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/moelsayed/system-tools/teams","hooks_url":"https://api.github.com/repos/moelsayed/system-tools/hooks","issue_events_url":"https://api.github.com/repos/moelsayed/system-tools/issues/events{/number}","events_url":"https://api.github.com/repos/moelsayed/system-tools/events","assignees_url":"https://api.github.com/repos/moelsayed/system-tools/assignees{/user}","branches_url":"https://api.github.com/repos/moelsayed/system-tools/branches{/branch}","tags_url":"https://api.github.com/repos/moelsayed/system-tools/tags","blobs_url":"https://api.github.com/repos/moelsayed/system-tools/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/moelsayed/system-tools/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/moelsayed/system-tools/git/refs{/sha}","trees_url":"https://api.github.com/repos/moelsayed/system-tools/git/trees{/sha}","statuses_url":"https://api.github.com/repos/moelsayed/system-tools/statuses/{sha}","languages_url":"https://api.github.com/repos/moelsayed/system-tools/languages","stargazers_url":"https://api.github.com/repos/moelsayed/system-tools/stargazers","contributors_url":"https://api.github.com/repos/moelsayed/system-tools/contributors","subscribers_url":"https://api.github.com/repos/moelsayed/system-tools/subscribers","subscription_url":"https://api.github.com/repos/moelsayed/system-tools/subscription","commits_url":"https://api.github.com/repos/moelsayed/system-tools/commits{/sha}","git_commits_url":"https://api.github.com/repos/moelsayed/system-tools/git/commits{/sha}","comments_url":"https://api.github.com/repos/moelsayed/system-tools/comments{/number}","issue_comment_url":"https://api.github.com/repos/moelsayed/system-tools/issues/comments{/number}","contents_url":"https://api.github.com/repos/moelsayed/system-tools/contents/{+path}","compare_url":"https://api.github.com/repos/moelsayed/system-tools/compare/{base}...{head}","merges_url":"https://api.github.com/repos/moelsayed/system-tools/merges","archive_url":"https://api.github.com/repos/moelsayed/system-tools/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/moelsayed/system-tools/downloads","issues_url":"https://api.github.com/repos/moelsayed/system-tools/issues{/number}","pulls_url":"https://api.github.com/repos/moelsayed/system-tools/pulls{/number}","milestones_url":"https://api.github.com/repos/moelsayed/system-tools/milestones{/number}","notifications_url":"https://api.github.com/repos/moelsayed/system-tools/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/moelsayed/system-tools/labels{/name}","releases_url":"https://api.github.com/repos/moelsayed/system-tools/releases{/id}","deployments_url":"https://api.github.com/repos/moelsayed/system-tools/deployments","created_at":"2018-09-06T18:30:57Z","updated_at":"2018-09-10T17:45:19Z","pushed_at":"2018-09-14T18:29:19Z","git_url":"git://github.com/moelsayed/system-tools.git","ssh_url":"git@github.com:moelsayed/system-tools.git","clone_url":"https://github.com/moelsayed/system-tools.git","svn_url":"https://github.com/moelsayed/system-tools","homepage":null,"size":2972,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"rancher:master","ref":"master","sha":"f2863dbd1f5137310b519bc0dc9e25668ce65400","user":{"login":"rancher","id":9343010,"node_id":"MDEyOk9yZ2FuaXphdGlvbjkzNDMwMTA=","avatar_url":"https://avatars3.githubusercontent.com/u/9343010?v=4","gravatar_id":"","url":"https://api.github.com/users/rancher","html_url":"https://github.com/rancher","followers_url":"https://api.github.com/users/rancher/followers","following_url":"https://api.github.com/users/rancher/following{/other_user}","gists_url":"https://api.github.com/users/rancher/gists{/gist_id}","starred_url":"https://api.github.com/users/rancher/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rancher/subscriptions","organizations_url":"https://api.github.com/users/rancher/orgs","repos_url":"https://api.github.com/users/rancher/repos","events_url":"https://api.github.com/users/rancher/events{/privacy}","received_events_url":"https://api.github.com/users/rancher/received_events","type":"Organization","site_admin":false},"repo":{"id":147587893,"node_id":"MDEwOlJlcG9zaXRvcnkxNDc1ODc4OTM=","name":"system-tools","full_name":"rancher/system-tools","owner":{"login":"rancher","id":9343010,"node_id":"MDEyOk9yZ2FuaXphdGlvbjkzNDMwMTA=","avatar_url":"https://avatars3.githubusercontent.com/u/9343010?v=4","gravatar_id":"","url":"https://api.github.com/users/rancher","html_url":"https://github.com/rancher","followers_url":"https://api.github.com/users/rancher/followers","following_url":"https://api.github.com/users/rancher/following{/other_user}","gists_url":"https://api.github.com/users/rancher/gists{/gist_id}","starred_url":"https://api.github.com/users/rancher/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rancher/subscriptions","organizations_url":"https://api.github.com/users/rancher/orgs","repos_url":"https://api.github.com/users/rancher/repos","events_url":"https://api.github.com/users/rancher/events{/privacy}","received_events_url":"https://api.github.com/users/rancher/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rancher/system-tools","description":"This repo is for tools helping with various cleanup tasks for rancher projects. Example: rancher installation cleanup","fork":false,"url":"https://api.github.com/repos/rancher/system-tools","forks_url":"https://api.github.com/repos/rancher/system-tools/forks","keys_url":"https://api.github.com/repos/rancher/system-tools/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rancher/system-tools/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rancher/system-tools/teams","hooks_url":"https://api.github.com/repos/rancher/system-tools/hooks","issue_events_url":"https://api.github.com/repos/rancher/system-tools/issues/events{/number}","events_url":"https://api.github.com/repos/rancher/system-tools/events","assignees_url":"https://api.github.com/repos/rancher/system-tools/assignees{/user}","branches_url":"https://api.github.com/repos/rancher/system-tools/branches{/branch}","tags_url":"https://api.github.com/repos/rancher/system-tools/tags","blobs_url":"https://api.github.com/repos/rancher/system-tools/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rancher/system-tools/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rancher/system-tools/git/refs{/sha}","trees_url":"https://api.github.com/repos/rancher/system-tools/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rancher/system-tools/statuses/{sha}","languages_url":"https://api.github.com/repos/rancher/system-tools/languages","stargazers_url":"https://api.github.com/repos/rancher/system-tools/stargazers","contributors_url":"https://api.github.com/repos/rancher/system-tools/contributors","subscribers_url":"https://api.github.com/repos/rancher/system-tools/subscribers","subscription_url":"https://api.github.com/repos/rancher/system-tools/subscription","commits_url":"https://api.github.com/repos/rancher/system-tools/commits{/sha}","git_commits_url":"https://api.github.com/repos/rancher/system-tools/git/commits{/sha}","comments_url":"https://api.github.com/repos/rancher/system-tools/comments{/number}","issue_comment_url":"https://api.github.com/repos/rancher/system-tools/issues/comments{/number}","contents_url":"https://api.github.com/repos/rancher/system-tools/contents/{+path}","compare_url":"https://api.github.com/repos/rancher/system-tools/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rancher/system-tools/merges","archive_url":"https://api.github.com/repos/rancher/system-tools/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rancher/system-tools/downloads","issues_url":"https://api.github.com/repos/rancher/system-tools/issues{/number}","pulls_url":"https://api.github.com/repos/rancher/system-tools/pulls{/number}","milestones_url":"https://api.github.com/repos/rancher/system-tools/milestones{/number}","notifications_url":"https://api.github.com/repos/rancher/system-tools/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rancher/system-tools/labels{/name}","releases_url":"https://api.github.com/repos/rancher/system-tools/releases{/id}","deployments_url":"https://api.github.com/repos/rancher/system-tools/deployments","created_at":"2018-09-05T22:44:41Z","updated_at":"2018-09-10T16:38:13Z","pushed_at":"2018-09-14T18:30:09Z","git_url":"git://github.com/rancher/system-tools.git","ssh_url":"git@github.com:rancher/system-tools.git","clone_url":"https://github.com/rancher/system-tools.git","svn_url":"https://github.com/rancher/system-tools","homepage":null,"size":0,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"archived":false,"open_issues_count":1,"license":null,"forks":1,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rancher/system-tools/pulls/2"},"html":{"href":"https://github.com/rancher/system-tools/pull/2"},"issue":{"href":"https://api.github.com/repos/rancher/system-tools/issues/2"},"comments":{"href":"https://api.github.com/repos/rancher/system-tools/issues/2/comments"},"review_comments":{"href":"https://api.github.com/repos/rancher/system-tools/pulls/2/comments"},"review_comment":{"href":"https://api.github.com/repos/rancher/system-tools/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rancher/system-tools/pulls/2/commits"},"statuses":{"href":"https://api.github.com/repos/rancher/system-tools/statuses/32f2e68c022917912078b176382badc00b36bec9"}},"author_association":"MEMBER"}}
{"actor":{"display_login":"alena1108"}}