2
2
const Command = require ( '../../Command' ) ;
3
3
const runnerRoot = require ( '../root/runner.cmd' ) ;
4
4
const inquirer = require ( 'inquirer' ) ;
5
- const { getAllKubeContexts, getKubeContext } = require ( '../../helpers/kubernetes' ) ;
5
+ const { getAllKubeContexts, getKubeContext, getAllNamespaces } = require ( '../../helpers/kubernetes' ) ;
6
6
const unInstallRuntime = require ( '../runtimeEnvironments/uninstall.cmd' ) ;
7
7
const unInstallAgent = require ( '../agent/uninstall.cmd' ) ;
8
8
const unInstallMonitor = require ( '../monitor/uninstall.cmd' ) ;
@@ -25,6 +25,41 @@ async function handleError(error, message) {
25
25
process . exit ( 1 ) ;
26
26
}
27
27
28
+ // Try to get the most relevant namespaces
29
+ async function getRelatedNamespaces ( kubeConfigPath , kubeContextName , runtimes ) {
30
+ const [ , namespacesOnCluster ] = await to ( getAllNamespaces ( kubeConfigPath , kubeContextName ) ) ;
31
+ const nsOnCluster = new Set ( namespacesOnCluster || [ ] ) ;
32
+
33
+ return _ ( runtimes )
34
+ . filter ( re => nsOnCluster . has ( _ . get ( re , 'runtimeScheduler.cluster.namespace' ) ) )
35
+ . map ( re => _ . get ( re , 'runtimeScheduler.cluster.namespace' ) )
36
+ . uniq ( )
37
+ . value ( ) ;
38
+ }
39
+
40
+ async function getRelatedAgents ( kubeNamespace , runtimes ) {
41
+ const [ listAgentsErr , agents ] = await to ( sdk . agents . list ( { } ) ) ;
42
+ await handleError ( listAgentsErr , 'Failed to get agents' ) ;
43
+
44
+ const relatedREs = new Set ( ) ;
45
+ _ . forEach ( runtimes , ( r ) => {
46
+ if ( _ . get ( r , 'runtimeScheduler.cluster.namespace' ) === kubeNamespace ) {
47
+ relatedREs . add ( r . metadata . name ) ;
48
+ }
49
+ } ) ;
50
+
51
+ const relatedAgents = [ ] ;
52
+ _ . forEach ( agents , ( a ) => {
53
+ _ . forEach ( _ . get ( a , 'runtimes' , [ ] ) , ( r ) => {
54
+ if ( relatedREs . has ( r ) ) {
55
+ relatedAgents . push ( a ) ;
56
+ }
57
+ } ) ;
58
+ } ) ;
59
+
60
+ return relatedAgents ;
61
+ }
62
+
28
63
const deleteCmd = new Command ( {
29
64
root : false ,
30
65
parent : runnerRoot ,
@@ -68,50 +103,73 @@ const deleteCmd = new Command({
68
103
name : agentName ,
69
104
} = argv ;
70
105
71
- const questions = [ ] ;
106
+ const [ listReErr , runtimes ] = await to ( sdk . runtimeEnvs . list ( { } ) ) ;
107
+ await handleError ( listReErr , 'Failed to get runtime environments' ) ;
108
+
109
+ console . log ( colors . green ( 'This uninstaller will guide you through the runner uninstallation process' ) ) ;
110
+
72
111
if ( ! kubeContextName ) {
73
112
const contexts = getAllKubeContexts ( kubeConfigPath ) ;
74
113
const currentKubeContext = getKubeContext ( kubeConfigPath ) ;
75
114
76
- questions . push ( {
115
+ const answer = await inquirer . prompt ( {
77
116
type : 'list' ,
78
117
name : 'context' ,
79
118
message : 'Name of Kubernetes context to use' ,
80
119
default : currentKubeContext ,
81
120
choices : contexts ,
82
121
} ) ;
122
+
123
+ kubeContextName = answer . context ;
83
124
}
125
+
84
126
if ( ! kubeNamespace ) {
85
- questions . push ( {
86
- type : 'input' ,
87
- name : 'namespace' ,
88
- default : defaultNamespace ,
89
- message : 'Kubernetes namespace to remove Codefresh Runner components from ' ,
90
- validate : value => ( value !== undefined && value !== '' ) || 'Please enter namespace\'s name' ,
91
- } ) ;
127
+ const relatedNamespaces = await getRelatedNamespaces ( kubeConfigPath , kubeContextName , runtimes ) ;
128
+ let answer ;
129
+ if ( ! relatedNamespaces . length ) {
130
+ answer = await inquirer . prompt ( {
131
+ type : 'input' ,
132
+ name : 'namespace' ,
133
+ default : defaultNamespace ,
134
+ message : 'Kubernetes namespace to remove Codefresh Runner components from' ,
135
+ validate : value => ( value !== undefined && value !== '' ) || 'Please enter namespace\'s name' ,
136
+ } ) ;
137
+ } else {
138
+ answer = await inquirer . prompt ( {
139
+ type : 'list' ,
140
+ name : 'namespace' ,
141
+ default : defaultNamespace ,
142
+ message : 'Kubernetes namespace to remove Codefresh Runner components from ' ,
143
+ choices : relatedNamespaces ,
144
+ } ) ;
145
+ }
146
+
147
+ kubeNamespace = answer . namespace ;
92
148
}
93
- const [ listAgentsErr , agents ] = await to ( sdk . agents . list ( { } ) ) ;
94
- await handleError ( listAgentsErr , 'Failed to get agents' ) ;
149
+
150
+ const agents = await getRelatedAgents ( kubeNamespace , runtimes ) ;
151
+ if ( ! agents . length ) {
152
+ console . log ( 'No agents related to the specified kubernetes cluster and namespace were found' ) ;
153
+ process . exit ( 1 ) ;
154
+ }
155
+
95
156
if ( ! agentName ) {
96
- questions . push ( {
157
+ const answer = await inquirer . prompt ( {
97
158
type : 'list' ,
98
159
name : 'name' ,
99
160
message : 'Agent name to uninstall' ,
100
161
choices : agents ,
101
162
} ) ;
163
+ agentName = answer . name ;
102
164
}
103
- console . log ( colors . green ( 'This uninstaller will guide you through the runner uninstallation process' ) ) ;
104
- const answers = await inquirer . prompt ( questions ) ;
105
- kubeContextName = kubeContextName || answers . context ;
106
- kubeNamespace = kubeNamespace || answers . namespace ;
107
- agentName = agentName || answers . name ;
165
+
108
166
// check that agent exists
109
167
const agent = _ . find ( agents , curr => curr . name === agentName ) ;
110
168
if ( ! agent ) {
111
169
console . log ( colors . red ( `Agent with name ${ agentName } doesn't exists` ) ) ;
112
170
return ;
113
171
}
114
- if ( agent . runtimes && agent . runtimes > 1 ) {
172
+ if ( agent . runtimes && agent . runtimes . length > 1 ) {
115
173
console . log ( 'Can\'t delete runner with more than one runtime, use runtime delete command' ) ;
116
174
return ;
117
175
}
0 commit comments