Merge pull request #2772 from chrislovecnm/delete-fix

Work on kops delete
This commit is contained in:
Justin Santa Barbara 2017-06-20 01:18:26 -04:00 committed by GitHub
commit f75ea400a3
5 changed files with 52 additions and 47 deletions

View File

@ -21,10 +21,10 @@ import (
"io" "io"
"bytes" "bytes"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/kops/cmd/kops/util" "k8s.io/kops/cmd/kops/util"
kopsapi "k8s.io/kops/pkg/apis/kops" kopsapi "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/kops/v1alpha1" "k8s.io/kops/pkg/apis/kops/v1alpha1"
@ -42,16 +42,22 @@ type DeleteOptions struct {
var ( var (
delete_long = templates.LongDesc(i18n.T(` delete_long = templates.LongDesc(i18n.T(`
Delete clusters, instancegroups, or secrets. Delete Kubernetes clusters, instancegroups, and or secrets.
`)) `))
delete_example = templates.Examples(i18n.T(` delete_example = templates.Examples(i18n.T(`
# Create a cluster using a file # Create a cluster using a manifest file
kops delete -f my-cluster.yaml kops delete -f my-cluster.yaml
# Delete a cluster in AWS. # Delete a cluster in AWS.
kops delete cluster --name=k8s.example.com --state=s3://kops-state-1234 kops delete cluster --name=k8s.example.com --state=s3://kops-state-1234
# Delete an instancegroup for the k8s-cluster.example.com cluster.
# The --yes option runs the command immediately.
kops delete ig --name=k8s-cluster.example.com node-example --yes
`)) `))
delete_short = i18n.T("Delete clusters,instancegroups, or secrets.")
) )
func NewCmdDelete(f *util.Factory, out io.Writer) *cobra.Command { func NewCmdDelete(f *util.Factory, out io.Writer) *cobra.Command {
@ -59,7 +65,7 @@ func NewCmdDelete(f *util.Factory, out io.Writer) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "delete -f FILENAME [--yes]", Use: "delete -f FILENAME [--yes]",
Short: i18n.T("Delete clusters,instancegroups, or secrets."), Short: delete_short,
Long: delete_long, Long: delete_long,
Example: delete_example, Example: delete_example,
SuggestFor: []string{"rm"}, SuggestFor: []string{"rm"},
@ -86,12 +92,11 @@ func NewCmdDelete(f *util.Factory, out io.Writer) *cobra.Command {
func RunDelete(factory *util.Factory, out io.Writer, d *DeleteOptions) error { func RunDelete(factory *util.Factory, out io.Writer, d *DeleteOptions) error {
// Codecs provides access to encoding and decoding for the scheme // Codecs provides access to encoding and decoding for the scheme
codecs := kopsapi.Codecs //serializer.NewCodecFactory(scheme) codec := kopsapi.Codecs.UniversalDecoder(kopsapi.SchemeGroupVersion)
codec := codecs.UniversalDecoder(kopsapi.SchemeGroupVersion) // We could have more than one cluster in a manifest so we are using a set
deletedClusters := sets.NewString()
var sb bytes.Buffer
fmt.Fprintf(&sb, "\n")
for _, f := range d.Filenames { for _, f := range d.Filenames {
contents, err := vfs.Context.ReadFile(f) contents, err := vfs.Context.ReadFile(f)
if err != nil { if err != nil {
@ -111,40 +116,38 @@ func RunDelete(factory *util.Factory, out io.Writer, d *DeleteOptions) error {
switch v := o.(type) { switch v := o.(type) {
case *kopsapi.Cluster: case *kopsapi.Cluster:
options := &DeleteClusterOptions{} options := &DeleteClusterOptions{
options.ClusterName = v.ObjectMeta.Name ClusterName: v.ObjectMeta.Name,
options.Yes = d.Yes Yes: d.Yes,
}
err = RunDeleteCluster(factory, out, options) err = RunDeleteCluster(factory, out, options)
if err != nil { if err != nil {
exitWithError(err) exitWithError(err)
} }
if d.Yes { deletedClusters.Insert(v.ObjectMeta.Name)
fmt.Fprintf(&sb, "Deleted cluster/%s\n", v.ObjectMeta.Name)
}
case *kopsapi.InstanceGroup: case *kopsapi.InstanceGroup:
options := &DeleteInstanceGroupOptions{} options := &DeleteInstanceGroupOptions{
options.GroupName = v.ObjectMeta.Name GroupName: v.ObjectMeta.Name,
options.ClusterName = v.ObjectMeta.Labels[kopsapi.LabelClusterName] ClusterName: v.ObjectMeta.Labels[kopsapi.LabelClusterName],
options.Yes = d.Yes Yes: d.Yes,
}
// If the cluster has been already deleted we cannot delete the ig
if deletedClusters.Has(options.ClusterName) {
glog.V(4).Infof("Skipping instance group %q because cluster %q has been deleted", v.ObjectMeta.Name, options.ClusterName)
continue
}
err := RunDeleteInstanceGroup(factory, out, options) err := RunDeleteInstanceGroup(factory, out, options)
if err != nil { if err != nil {
exitWithError(err) exitWithError(err)
} }
if d.Yes {
fmt.Fprintf(&sb, "Deleted instancegroup/%s\n", v.ObjectMeta.Name)
}
default: default:
glog.V(2).Infof("Type of object was %T", v) glog.V(2).Infof("Type of object was %T", v)
return fmt.Errorf("Unhandled kind %q in %s", gvk, f) return fmt.Errorf("Unhandled kind %q in %s", gvk, f)
} }
} }
} }
{
_, err := out.Write(sb.Bytes())
if err != nil {
return fmt.Errorf("error writing to output: %v", err)
}
}
return nil return nil
} }

View File

@ -46,17 +46,15 @@ type DeleteClusterOptions struct {
var ( var (
delete_cluster_long = templates.LongDesc(i18n.T(` delete_cluster_long = templates.LongDesc(i18n.T(`
Deletes a Kubneretes cluster and all associated resources. Resources include instancegroups, and Deletes a Kubernetes cluster and all associated resources. Resources include instancegroups,
the state store. There is no "UNDO" for this command. secrets and the state store. There is no "UNDO" for this command.
`)) `))
delete_cluster_example = templates.Examples(i18n.T(` delete_cluster_example = templates.Examples(i18n.T(`
# Delete a cluster. # Delete a cluster.
# The --yes option runs the command immediately.
kops delete cluster --name=k8s.cluster.site --yes kops delete cluster --name=k8s.cluster.site --yes
# Delete an instancegroup for the k8s-cluster.example.com cluster.
# The --yes option runs the command immediately.
kops delete ig --name=k8s-cluster.example.com node-example --yes
`)) `))
delete_cluster_short = i18n.T("Delete a cluster.") delete_cluster_short = i18n.T("Delete a cluster.")
@ -176,7 +174,8 @@ func RunDeleteCluster(f *util.Factory, out io.Writer, options *DeleteClusterOpti
} }
if !options.Yes { if !options.Yes {
return fmt.Errorf("Must specify --yes to delete") fmt.Fprintf(out, "\nMust specify --yes to delete cluster\n")
return nil
} }
fmt.Fprintf(out, "\n") fmt.Fprintf(out, "\n")
@ -210,6 +209,6 @@ func RunDeleteCluster(f *util.Factory, out io.Writer, options *DeleteClusterOpti
glog.Warningf("error removing kube config: %v", err) glog.Warningf("error removing kube config: %v", err)
} }
fmt.Fprintf(out, "\nCluster deleted\n") fmt.Fprintf(out, "\nDeleted cluster: %q\n", clusterName)
return nil return nil
} }

View File

@ -119,11 +119,6 @@ func RunDeleteInstanceGroup(f *util.Factory, out io.Writer, options *DeleteInsta
return fmt.Errorf("ClusterName is required") return fmt.Errorf("ClusterName is required")
} }
if !options.Yes {
// Just for sanity / safety
return fmt.Errorf("Yes must be specified")
}
cluster, err := GetCluster(f, clusterName) cluster, err := GetCluster(f, clusterName)
if err != nil { if err != nil {
return err return err
@ -147,6 +142,13 @@ func RunDeleteInstanceGroup(f *util.Factory, out io.Writer, options *DeleteInsta
return err return err
} }
fmt.Fprintf(out, "InstanceGroup %q found for deletion\n", groupName)
if !options.Yes {
fmt.Fprintf(out, "\nMust specify --yes to delete instancegroup\n")
return nil
}
d := &instancegroups.DeleteInstanceGroup{} d := &instancegroups.DeleteInstanceGroup{}
d.Cluster = cluster d.Cluster = cluster
d.Cloud = cloud d.Cloud = cloud
@ -157,7 +159,7 @@ func RunDeleteInstanceGroup(f *util.Factory, out io.Writer, options *DeleteInsta
return err return err
} }
fmt.Fprintf(out, "InstanceGroup %q deleted\n", group.ObjectMeta.Name) fmt.Fprintf(out, "\nDeleted InstanceGroup: %q\n", group.ObjectMeta.Name)
return nil return nil
} }

View File

@ -5,7 +5,7 @@ Delete clusters,instancegroups, or secrets.
### Synopsis ### Synopsis
Delete clusters, instancegroups, or secrets. Delete Kubernetes clusters, instancegroups, and or secrets.
``` ```
kops delete -f FILENAME [--yes] kops delete -f FILENAME [--yes]
@ -14,11 +14,15 @@ kops delete -f FILENAME [--yes]
### Examples ### Examples
``` ```
# Create a cluster using a file # Create a cluster using a manifest file
kops delete -f my-cluster.yaml kops delete -f my-cluster.yaml
# Delete a cluster in AWS. # Delete a cluster in AWS.
kops delete cluster --name=k8s.example.com --state=s3://kops-state-1234 kops delete cluster --name=k8s.example.com --state=s3://kops-state-1234
# Delete an instancegroup for the k8s-cluster.example.com cluster.
# The --yes option runs the command immediately.
kops delete ig --name=k8s-cluster.example.com node-example --yes
``` ```
### Options ### Options

View File

@ -5,7 +5,7 @@ Delete a cluster.
### Synopsis ### Synopsis
Deletes a Kubneretes cluster and all associated resources. Resources include instancegroups, and the state store. There is no "UNDO" for this command. Deletes a Kubernetes cluster and all associated resources. Resources include instancegroups, secrets and the state store. There is no "UNDO" for this command.
``` ```
kops delete cluster CLUSTERNAME [--yes] kops delete cluster CLUSTERNAME [--yes]
@ -15,11 +15,8 @@ kops delete cluster CLUSTERNAME [--yes]
``` ```
# Delete a cluster. # Delete a cluster.
kops delete cluster --name=k8s.cluster.site --yes
# Delete an instancegroup for the k8s-cluster.example.com cluster.
# The --yes option runs the command immediately. # The --yes option runs the command immediately.
kops delete ig --name=k8s-cluster.example.com node-example --yes kops delete cluster --name=k8s.cluster.site --yes
``` ```
### Options ### Options