-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
delete.go
161 lines (134 loc) · 6.94 KB
/
delete.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
/*
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 cmd
import (
"context"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client"
)
type deleteOptions struct {
kubeconfig string
kubeconfigContext string
coreProvider string
bootstrapProviders []string
controlPlaneProviders []string
infrastructureProviders []string
ipamProviders []string
runtimeExtensionProviders []string
addonProviders []string
includeNamespace bool
includeCRDs bool
deleteAll bool
}
var dd = &deleteOptions{}
var deleteCmd = &cobra.Command{
Use: "delete [providers]",
GroupID: groupManagement,
Short: "Delete one or more providers from the management cluster",
Long: LongDesc(`
Delete one or more providers from the management cluster.`),
Example: Examples(`
# Deletes the AWS provider
# Please note that this implies the deletion of all provider components except the hosting namespace
# and the CRDs.
clusterctl delete --infrastructure aws
# Deletes all the providers
# Important! As a consequence of this operation, all the corresponding resources managed by
# Cluster API Providers are orphaned and there might be ongoing costs incurred as a result of this.
clusterctl delete --all
# Delete the AWS infrastructure provider and Core provider. This will leave behind Bootstrap and ControlPlane
# providers
# Important! As a consequence of this operation, all the corresponding resources managed by
# the AWS infrastructure provider and Cluster API Providers are orphaned and there might be
# ongoing costs incurred as a result of this.
clusterctl delete --core cluster-api --infrastructure aws
# Delete the AWS infrastructure provider and related CRDs. Please note that this forces deletion of
# all the related objects (e.g. AWSClusters, AWSMachines etc.).
# Important! As a consequence of this operation, all the corresponding resources managed by
# the AWS infrastructure provider are orphaned and there might be ongoing costs incurred as a result of this.
clusterctl delete --infrastructure aws --include-crd
# Delete the AWS infrastructure provider and its hosting Namespace. Please note that this forces deletion of
# all objects existing in the namespace.
# Important! As a consequence of this operation, all the corresponding resources managed by
# Cluster API Providers are orphaned and there might be ongoing costs incurred as a result of this.
clusterctl delete --infrastructure aws --include-namespace
# Reset the management cluster to its original state
# Important! As a consequence of this operation all the corresponding resources on target clouds
# are "orphaned" and thus there may be ongoing costs incurred as a result of this.
clusterctl delete --all --include-crd --include-namespace`),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runDelete()
},
}
func init() {
deleteCmd.Flags().StringVar(&dd.kubeconfig, "kubeconfig", "",
"Path to the kubeconfig file to use for accessing the management cluster. If unspecified, default discovery rules apply.")
deleteCmd.Flags().StringVar(&dd.kubeconfigContext, "kubeconfig-context", "",
"Context to be used within the kubeconfig file. If empty, current context will be used.")
deleteCmd.Flags().BoolVar(&dd.includeNamespace, "include-namespace", false,
"Forces the deletion of the namespace where the providers are hosted (and of all the contained objects)")
deleteCmd.Flags().BoolVar(&dd.includeCRDs, "include-crd", false,
"Forces the deletion of the provider's CRDs (and of all the related objects)")
deleteCmd.Flags().StringVar(&dd.coreProvider, "core", "",
"Core provider version (e.g. cluster-api:v1.1.5) to delete from the management cluster")
deleteCmd.Flags().StringSliceVarP(&dd.infrastructureProviders, "infrastructure", "i", nil,
"Infrastructure providers and versions (e.g. aws:v0.5.0) to delete from the management cluster")
deleteCmd.Flags().StringSliceVarP(&dd.bootstrapProviders, "bootstrap", "b", nil,
"Bootstrap providers and versions (e.g. kubeadm:v1.1.5) to delete from the management cluster")
deleteCmd.Flags().StringSliceVarP(&dd.controlPlaneProviders, "control-plane", "c", nil,
"ControlPlane providers and versions (e.g. kubeadm:v1.1.5) to delete from the management cluster")
deleteCmd.Flags().StringSliceVar(&dd.ipamProviders, "ipam", nil,
"IPAM providers and versions (e.g. infoblox:v0.0.1) to delete from the management cluster")
deleteCmd.Flags().StringSliceVar(&dd.runtimeExtensionProviders, "runtime-extension", nil,
"Runtime extension providers and versions (e.g. test:v0.0.1) to delete from the management cluster")
deleteCmd.Flags().StringSliceVar(&dd.addonProviders, "addon", nil,
"Add-on providers and versions (e.g. helm:v0.1.0) to delete from the management cluster")
deleteCmd.Flags().BoolVar(&dd.deleteAll, "all", false,
"Force deletion of all the providers")
RootCmd.AddCommand(deleteCmd)
}
func runDelete() error {
ctx := context.Background()
c, err := client.New(ctx, cfgFile)
if err != nil {
return err
}
hasProviderNames := (dd.coreProvider != "") ||
(len(dd.bootstrapProviders) > 0) ||
(len(dd.controlPlaneProviders) > 0) ||
(len(dd.infrastructureProviders) > 0) ||
(len(dd.ipamProviders) > 0) ||
(len(dd.runtimeExtensionProviders) > 0) ||
(len(dd.addonProviders) > 0)
if dd.deleteAll && hasProviderNames {
return errors.New("The --all flag can't be used in combination with --core, --bootstrap, --control-plane, --infrastructure, --ipam, --extension, --addon")
}
if !dd.deleteAll && !hasProviderNames {
return errors.New("At least one of --core, --bootstrap, --control-plane, --infrastructure, --ipam, --extension, --addon should be specified or the --all flag should be set")
}
return c.Delete(ctx, client.DeleteOptions{
Kubeconfig: client.Kubeconfig{Path: dd.kubeconfig, Context: dd.kubeconfigContext},
IncludeNamespace: dd.includeNamespace,
IncludeCRDs: dd.includeCRDs,
CoreProvider: dd.coreProvider,
BootstrapProviders: dd.bootstrapProviders,
InfrastructureProviders: dd.infrastructureProviders,
ControlPlaneProviders: dd.controlPlaneProviders,
IPAMProviders: dd.ipamProviders,
RuntimeExtensionProviders: dd.runtimeExtensionProviders,
AddonProviders: dd.addonProviders,
DeleteAll: dd.deleteAll,
})
}