Skip to content

Commit

Permalink
Refactor toolbox dump & dump structured instances
Browse files Browse the repository at this point in the history
This will enable log collection even if nodes don't register.

AWS: Dumps ids & addresses
GCE: Dumps names - addresses to follow
Others: Not yet!
  • Loading branch information
justinsb committed Oct 28, 2017
1 parent e38f2a5 commit a18363f
Show file tree
Hide file tree
Showing 19 changed files with 367 additions and 276 deletions.
2 changes: 1 addition & 1 deletion cmd/kops/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ go_library(
"//pkg/kubeconfig:go_default_library",
"//pkg/pretty:go_default_library",
"//pkg/resources:go_default_library",
"//pkg/resources/tracker:go_default_library",
"//pkg/resources/utils:go_default_library",
"//pkg/util/templater:go_default_library",
"//pkg/validation:go_default_library",
"//upup/pkg/fi:go_default_library",
Expand Down
20 changes: 8 additions & 12 deletions cmd/kops/delete_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
api "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/kubeconfig"
"k8s.io/kops/pkg/resources"
"k8s.io/kops/pkg/resources/tracker"
resourceutils "k8s.io/kops/pkg/resources/utils"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
Expand Down Expand Up @@ -131,16 +131,12 @@ func RunDeleteCluster(f *util.Factory, out io.Writer, options *DeleteClusterOpti
}
}

d := &resources.ClusterResources{}
d.ClusterName = clusterName
d.Cloud = cloud

allResources, err := d.ListResources()
allResources, err := resourceutils.ListResources(cloud, clusterName, options.Region)
if err != nil {
return err
}

clusterResources := make(map[string]*tracker.Resource)
clusterResources := make(map[string]*resources.Resource)
for k, resource := range allResources {
if resource.Shared {
continue
Expand All @@ -154,16 +150,16 @@ func RunDeleteCluster(f *util.Factory, out io.Writer, options *DeleteClusterOpti
wouldDeleteCloudResources = true

t := &tables.Table{}
t.AddColumn("TYPE", func(r *tracker.Resource) string {
t.AddColumn("TYPE", func(r *resources.Resource) string {
return r.Type
})
t.AddColumn("ID", func(r *tracker.Resource) string {
t.AddColumn("ID", func(r *resources.Resource) string {
return r.ID
})
t.AddColumn("NAME", func(r *tracker.Resource) string {
t.AddColumn("NAME", func(r *resources.Resource) string {
return r.Name
})
var l []*tracker.Resource
var l []*resources.Resource
for _, v := range clusterResources {
l = append(l, v)
}
Expand All @@ -180,7 +176,7 @@ func RunDeleteCluster(f *util.Factory, out io.Writer, options *DeleteClusterOpti

fmt.Fprintf(out, "\n")

err = d.DeleteResources(clusterResources)
err = resources.DeleteResources(cloud, clusterResources)
if err != nil {
return err
}
Expand Down
35 changes: 8 additions & 27 deletions cmd/kops/toolbox_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"fmt"
"io"

"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/resources"
resourceutils "k8s.io/kops/pkg/resources/utils"
"k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
Expand Down Expand Up @@ -107,38 +107,19 @@ func RunToolboxDump(f *util.Factory, out io.Writer, options *ToolboxDumpOptions)
return err
}

// Todo lets make this smart enough to detect the cloud and switch on the ClusterResources interface
d := &resources.ClusterResources{}
d.ClusterName = options.ClusterName
d.Cloud = cloud

resources, err := d.ListResources()
region := "" // Use default
resourceMap, err := resourceutils.ListResources(cloud, options.ClusterName, region)
if err != nil {
return err
}

data := make(map[string]interface{})

dumpedResources := []interface{}{}
for k, r := range resources {
if r.Dumper == nil {
glog.V(8).Infof("skipping dump of %q (no Dumper)", k)
continue
}

o, err := r.Dumper(r)
if err != nil {
return fmt.Errorf("error dumping %q: %v", k, err)
}
if o != nil {
dumpedResources = append(dumpedResources, o)
}
dump, err := resources.BuildDump(resourceMap)
if err != nil {
return err
}
data["resources"] = dumpedResources

switch options.Output {
case OutputYaml:
b, err := kops.ToRawYaml(data)
b, err := kops.ToRawYaml(dump)
if err != nil {
return fmt.Errorf("error marshaling yaml: %v", err)
}
Expand All @@ -149,7 +130,7 @@ func RunToolboxDump(f *util.Factory, out io.Writer, options *ToolboxDumpOptions)
return nil

case OutputJSON:
b, err := json.MarshalIndent(data, "", " ")
b, err := json.MarshalIndent(dump, "", " ")
if err != nil {
return fmt.Errorf("error marshaling json: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion hack/.packages
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ k8s.io/kops/pkg/pretty
k8s.io/kops/pkg/resources
k8s.io/kops/pkg/resources/digitalocean
k8s.io/kops/pkg/resources/digitalocean/dns
k8s.io/kops/pkg/resources/tracker
k8s.io/kops/pkg/resources/utils
k8s.io/kops/pkg/systemd
k8s.io/kops/pkg/templates
k8s.io/kops/pkg/testutils
Expand Down
8 changes: 3 additions & 5 deletions pkg/resources/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ go_library(
srcs = [
"aws.go",
"cluster_resources.go",
"do.go",
"dump.go",
"dumpmodel.go",
"gce.go",
"tracker.go",
"vsphere.go",
],
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/kops:go_default_library",
"//pkg/dns:go_default_library",
"//pkg/resources/digitalocean:go_default_library",
"//pkg/resources/tracker:go_default_library",
"//upup/pkg/fi:go_default_library",
"//upup/pkg/fi/cloudup/awsup:go_default_library",
"//upup/pkg/fi/cloudup/gce:go_default_library",
Expand Down Expand Up @@ -45,7 +44,6 @@ go_test(
library = ":go_default_library",
deps = [
"//cloudmock/aws/mockec2:go_default_library",
"//pkg/resources/tracker:go_default_library",
"//upup/pkg/fi/cloudup/awsup:go_default_library",
"//vendor/github.com/aws/aws-sdk-go/aws:go_default_library",
"//vendor/github.com/aws/aws-sdk-go/service/ec2:go_default_library",
Expand Down
Loading

0 comments on commit a18363f

Please sign in to comment.