/
delete_cluster.go
216 lines (181 loc) · 5.42 KB
/
delete_cluster.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"fmt"
"io"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
"k8s.io/kops/cmd/kops/util"
kopsapi "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/kubeconfig"
"k8s.io/kops/pkg/resources"
resourceops "k8s.io/kops/pkg/resources/ops"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/util/pkg/tables"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
type DeleteClusterOptions struct {
Yes bool
Region string
External bool
Unregister bool
ClusterName string
}
var (
deleteClusterLong = templates.LongDesc(i18n.T(`
Deletes a Kubernetes cluster and all associated resources. Resources include instancegroups,
secrets and the state store. There is no "UNDO" for this command.
`))
deleteClusterExample = templates.Examples(i18n.T(`
# Delete a cluster.
# The --yes option runs the command immediately.
kops delete cluster --name=k8s.cluster.site --yes
`))
deleteClusterShort = i18n.T("Delete a cluster.")
)
func NewCmdDeleteCluster(f *util.Factory, out io.Writer) *cobra.Command {
options := &DeleteClusterOptions{}
cmd := &cobra.Command{
Use: "cluster CLUSTERNAME [--yes]",
Short: deleteClusterShort,
Long: deleteClusterLong,
Example: deleteClusterExample,
Run: func(cmd *cobra.Command, args []string) {
ctx := context.TODO()
err := rootCommand.ProcessArgs(args)
if err != nil {
exitWithError(err)
}
// Note _not_ ClusterName(); we only want the --name flag
options.ClusterName = rootCommand.clusterName
err = RunDeleteCluster(ctx, f, out, options)
if err != nil {
exitWithError(err)
}
},
}
cmd.Flags().BoolVarP(&options.Yes, "yes", "y", options.Yes, "Specify --yes to delete the cluster")
cmd.Flags().BoolVar(&options.Unregister, "unregister", options.Unregister, "Don't delete cloud resources, just unregister the cluster")
cmd.Flags().BoolVar(&options.External, "external", options.External, "Delete an external cluster")
cmd.Flags().StringVar(&options.Region, "region", options.Region, "region")
return cmd
}
func RunDeleteCluster(ctx context.Context, f *util.Factory, out io.Writer, options *DeleteClusterOptions) error {
clusterName := options.ClusterName
if clusterName == "" {
return fmt.Errorf("--name is required (for safety)")
}
var cloud fi.Cloud
var cluster *kopsapi.Cluster
var err error
if options.External {
region := options.Region
if region == "" {
return fmt.Errorf("--region is required (when --external)")
}
tags := map[string]string{"KubernetesCluster": clusterName}
cloud, err = awsup.NewAWSCloud(region, tags)
if err != nil {
return fmt.Errorf("error initializing AWS client: %v", err)
}
} else {
cluster, err = GetCluster(ctx, f, clusterName)
if err != nil {
return err
}
}
wouldDeleteCloudResources := false
if !options.Unregister {
if cloud == nil {
cloud, err = cloudup.BuildCloud(cluster)
if err != nil {
return err
}
}
allResources, err := resourceops.ListResources(cloud, cluster, options.Region)
if err != nil {
return err
}
clusterResources := make(map[string]*resources.Resource)
for k, resource := range allResources {
if resource.Shared {
continue
}
clusterResources[k] = resource
}
if len(clusterResources) == 0 {
fmt.Fprintf(out, "No cloud resources to delete\n")
} else {
wouldDeleteCloudResources = true
t := &tables.Table{}
t.AddColumn("TYPE", func(r *resources.Resource) string {
return r.Type
})
t.AddColumn("ID", func(r *resources.Resource) string {
return r.ID
})
t.AddColumn("NAME", func(r *resources.Resource) string {
return r.Name
})
var l []*resources.Resource
for _, v := range clusterResources {
l = append(l, v)
}
err := t.Render(l, out, "TYPE", "NAME", "ID")
if err != nil {
return err
}
if !options.Yes {
fmt.Fprintf(out, "\nMust specify --yes to delete cluster\n")
return nil
}
fmt.Fprintf(out, "\n")
err = resourceops.DeleteResources(cloud, clusterResources)
if err != nil {
return err
}
}
}
if !options.External {
if !options.Yes {
if wouldDeleteCloudResources {
fmt.Fprintf(out, "\nMust specify --yes to delete cloud resources & unregister cluster\n")
} else {
fmt.Fprintf(out, "\nMust specify --yes to unregister the cluster\n")
}
return nil
}
clientset, err := f.Clientset()
if err != nil {
return err
}
err = clientset.DeleteCluster(ctx, cluster)
if err != nil {
return fmt.Errorf("error removing cluster from state store: %v", err)
}
}
b := kubeconfig.NewKubeconfigBuilder()
b.Context = clusterName
err = b.DeleteKubeConfig(clientcmd.NewDefaultPathOptions())
if err != nil {
klog.Warningf("error removing kube config: %v", err)
}
fmt.Fprintf(out, "\nDeleted cluster: %q\n", clusterName)
return nil
}