@@ -19,26 +19,23 @@ package cmd
19
19
import (
20
20
"fmt"
21
21
22
- "k8c.io/kubeone/pkg/clientutil"
23
- "k8c.io/kubeone/pkg/kubeconfig"
24
- "k8c.io/kubeone/pkg/state"
25
- "k8c.io/kubeone/pkg/tasks"
26
- clusterv1alpha1 "k8c.io/machine-controller/pkg/apis/cluster/v1alpha1"
27
-
28
22
"github.com/MakeNowJust/heredoc/v2"
29
23
"github.com/spf13/cobra"
30
24
"github.com/spf13/pflag"
31
25
32
- corev1 "k8s.io/api/core/v1"
26
+ "k8c.io/kubeone/pkg/kubeconfig"
27
+ "k8c.io/kubeone/pkg/state"
28
+ "k8c.io/kubeone/pkg/tasks"
29
+ clusterv1alpha1 "k8c.io/machine-controller/pkg/apis/cluster/v1alpha1"
33
30
)
34
31
35
32
type resetOpts struct {
36
33
globalOptions
37
- AutoApprove bool `longflag:"auto-approve" shortflag:"y"`
38
- DestroyWorkers bool `longflag:"destroy-workers"`
39
- CleanupVolumes bool `longflag:"cleanup -volumes"`
40
- CleanupLoadBalancers bool `longflag:"cleanup-load-balancers "`
41
- RemoveBinaries bool `longflag:"remove-binaries"`
34
+ AutoApprove bool `longflag:"auto-approve" shortflag:"y"`
35
+ DestroyWorkers bool `longflag:"destroy-workers"`
36
+ RemoveVolumes bool `longflag:"remove -volumes"`
37
+ RemoveLBServices bool `longflag:"remove-lb-services "`
38
+ RemoveBinaries bool `longflag:"remove-binaries"`
42
39
}
43
40
44
41
func (opts * resetOpts ) BuildState () (* state.State , error ) {
@@ -48,8 +45,8 @@ func (opts *resetOpts) BuildState() (*state.State, error) {
48
45
}
49
46
50
47
s .DestroyWorkers = opts .DestroyWorkers
51
- s .CleanupVolumes = opts .CleanupVolumes
52
- s .CleanupLoadBalancers = opts .CleanupLoadBalancers
48
+ s .RemoveVolumes = opts .RemoveVolumes
49
+ s .RemoveLBServices = opts .RemoveLBServices
53
50
s .RemoveBinaries = opts .RemoveBinaries
54
51
55
52
return s , nil
@@ -101,16 +98,16 @@ func resetCmd(rootFlags *pflag.FlagSet) *cobra.Command {
101
98
"remove kubernetes binaries after resetting the cluster" )
102
99
103
100
cmd .Flags ().BoolVar (
104
- & opts .CleanupVolumes ,
105
- longFlagName (opts , "CleanupVolumes " ),
101
+ & opts .RemoveVolumes ,
102
+ longFlagName (opts , "RemoveVolumes " ),
106
103
true ,
107
- "cleanup all dynamically provisioned and unretained volumes before resetting the cluster" )
104
+ "remove all dynamically provisioned and unretained volumes before resetting the cluster" )
108
105
109
106
cmd .Flags ().BoolVar (
110
- & opts .CleanupLoadBalancers ,
111
- longFlagName (opts , "CleanupLoadBalancers " ),
107
+ & opts .RemoveLBServices ,
108
+ longFlagName (opts , "RemoveLBServices " ),
112
109
true ,
113
- "cleanup all load balancers services before resetting the cluster" )
110
+ "remove all load balancers services before resetting the cluster" )
114
111
115
112
return cmd
116
113
}
@@ -122,15 +119,15 @@ func runReset(opts *resetOpts) error {
122
119
return err
123
120
}
124
121
125
- if opts .DestroyWorkers || opts .CleanupVolumes || opts .CleanupLoadBalancers {
122
+ if opts .DestroyWorkers || opts .RemoveVolumes || opts .RemoveLBServices {
126
123
if cErr := kubeconfig .BuildKubernetesClientset (s ); cErr != nil {
127
124
s .Logger .Errorln ("Failed to build the Kubernetes clientset." )
128
- if opts .CleanupLoadBalancers {
125
+ if opts .RemoveLBServices {
129
126
s .Logger .Warnln ("Unable to list and delete load balancers in the cluster." )
130
127
s .Logger .Warnln ("You can skip this phase by using '--cleanup-load-balancer=false'." )
131
128
s .Logger .Warnln ("If there are load balancers in the cluster, you might have to delete them manually." )
132
129
}
133
- if opts .CleanupVolumes {
130
+ if opts .RemoveVolumes {
134
131
s .Logger .Warnln ("Unable to list and delete dynamically provisioned and unretained volumes in the cluster." )
135
132
s .Logger .Warnln ("You can skip this phase by using '--cleanup-volumes=false'." )
136
133
s .Logger .Warnln ("If there are unretained volumes in the cluster, you might have to delete them manually." )
@@ -140,65 +137,22 @@ func runReset(opts *resetOpts) error {
140
137
s .Logger .Warnln ("You can skip this phase by using '--destroy-workers=false' flag." )
141
138
s .Logger .Warnln ("If there are worker nodes in the cluster, you might have to delete them manually." )
142
139
}
143
- s .Logger .Warnln ("If there are unretained volumes and load balancers in the cluster, you might have to delete them manually." )
144
140
145
141
return cErr
146
142
}
147
143
}
148
144
149
- if opts .CleanupLoadBalancers {
150
- s .Logger .Warnln ("cleanup-load-balancers command will PERMANENTLY delete the load balancers from the cluster." )
151
- svcList := corev1.ServiceList {}
152
- if err = s .DynamicClient .List (s .Context , & svcList ); err != nil {
153
- s .Logger .Errorln ("Failed to list load balancer services." )
154
- s .Logger .Warnln ("Load balancer Service might not be deleted. If there are services of type load balancer in the cluster, you might have to delete them manually." )
155
- }
156
- services := []corev1.Service {}
157
- for _ , svc := range svcList .Items {
158
- if svc .Spec .Type == corev1 .ServiceTypeLoadBalancer {
159
- services = append (services , svc )
160
- }
161
- }
162
- if len (services ) > 0 {
163
- fmt .Printf ("\n The following load balancer services will be destroyed:\n " )
164
- for _ , svc := range services {
165
- fmt .Printf ("\t - %s/%s\n " , svc .Namespace , svc .Name )
166
- }
167
- }
145
+ if opts .RemoveLBServices {
146
+ s .Logger .Warnln ("remove-lb-services command will PERMANENTLY delete the load balancers from the cluster." )
168
147
}
169
148
170
- if opts .CleanupVolumes {
171
- s .Logger .Warnln ("cleanup-volumes command will PERMANENTLY delete the unretained volumes from the cluster." )
172
-
173
- pvList := corev1.PersistentVolumeList {}
174
- if err = s .DynamicClient .List (s .Context , & pvList ); err != nil {
175
- s .Logger .Errorln ("Failed to list persistent volumes." )
176
- s .Logger .Warnln ("Volumes might not be deleted. If there are volumes in the cluster, you might have to delete them manually." )
177
- }
178
- pvs := []corev1.PersistentVolume {}
179
- for _ , pv := range pvList .Items {
180
- if pv .Annotations [clientutil .AnnDynamicallyProvisioned ] != "" && pv .Spec .PersistentVolumeReclaimPolicy == corev1 .PersistentVolumeReclaimDelete {
181
- pvs = append (pvs , pv )
182
- }
183
- }
184
- if len (pvs ) > 0 {
185
- fmt .Printf ("\n The following volumes will be destroyed:\n " )
186
- for _ , pv := range pvs {
187
- fmt .Printf ("\t - %s/%s/%s\n " , pv .Spec .ClaimRef .Namespace , pv .Spec .ClaimRef .Name , pv .Name )
188
- }
189
- }
149
+ if opts .RemoveVolumes {
150
+ s .Logger .Warnln ("remove-volumes command will PERMANENTLY delete the unretained volumes from the cluster." )
190
151
}
191
152
192
153
if opts .DestroyWorkers {
193
154
s .Logger .Warnln ("destroy-workers command will PERMANENTLY destroy the Kubernetes cluster running on the following nodes:" )
194
155
195
- for _ , node := range s .Cluster .ControlPlane .Hosts {
196
- fmt .Printf ("\t - reset control plane node %q (%s)\n " , node .Hostname , node .PrivateAddress )
197
- }
198
- for _ , node := range s .Cluster .StaticWorkers .Hosts {
199
- fmt .Printf ("\t - reset static worker nodes %q (%s)\n " , node .Hostname , node .PrivateAddress )
200
- }
201
-
202
156
// Gather information about machine-controller managed nodes
203
157
machines := clusterv1alpha1.MachineList {}
204
158
if err = s .DynamicClient .List (s .Context , & machines ); err != nil {
@@ -217,6 +171,13 @@ func runReset(opts *resetOpts) error {
217
171
s .Logger .Warnln ("If there are worker nodes in the cluster, you might have to delete them manually." )
218
172
}
219
173
174
+ for _ , node := range s .Cluster .ControlPlane .Hosts {
175
+ fmt .Printf ("\t - reset control plane node %q (%s)\n " , node .Hostname , node .PrivateAddress )
176
+ }
177
+ for _ , node := range s .Cluster .StaticWorkers .Hosts {
178
+ fmt .Printf ("\t - reset static worker nodes %q (%s)\n " , node .Hostname , node .PrivateAddress )
179
+ }
180
+
220
181
fmt .Printf ("\n After the command is complete, there's NO way to recover the cluster or its data!\n " )
221
182
222
183
confirm , err := confirmCommand (opts .AutoApprove )
0 commit comments