Skip to content

Commit

Permalink
toolbox dump: support dumping only k8s resources
Browse files Browse the repository at this point in the history
Because metal does not support cloud-resource discovery, we need to
skip this in our metal tests.
  • Loading branch information
justinsb committed Nov 10, 2024
1 parent 158f130 commit f7b63e4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
64 changes: 36 additions & 28 deletions cmd/kops/toolbox_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ type ToolboxDumpOptions struct {
SSHUser string
MaxNodes int
K8sResources bool

// CloudResources controls whether we dump the cloud resources
CloudResources bool
}

func (o *ToolboxDumpOptions) InitDefaults() {
Expand All @@ -78,6 +81,7 @@ func (o *ToolboxDumpOptions) InitDefaults() {
o.SSHUser = "ubuntu"
o.MaxNodes = 500
o.K8sResources = k8sResources != ""
o.CloudResources = true
}

func NewCmdToolboxDump(f commandutils.Factory, out io.Writer) *cobra.Command {
Expand All @@ -104,6 +108,7 @@ func NewCmdToolboxDump(f commandutils.Factory, out io.Writer) *cobra.Command {
cmd.Flags().StringVar(&options.Dir, "dir", options.Dir, "Target directory; if specified will collect logs and other information.")
cmd.MarkFlagDirname("dir")
cmd.Flags().BoolVar(&options.K8sResources, "k8s-resources", options.K8sResources, "Include k8s resources in the dump")
cmd.Flags().BoolVar(&options.CloudResources, "cloud-resources", options.CloudResources, "Include cloud resources in the dump")
cmd.Flags().IntVar(&options.MaxNodes, "max-nodes", options.MaxNodes, "The maximum number of nodes from which to dump logs")
cmd.Flags().StringVar(&options.PrivateKey, "private-key", options.PrivateKey, "File containing private key to use for SSH access to instances")
cmd.Flags().StringVar(&options.SSHUser, "ssh-user", options.SSHUser, "The remote user for SSH access to instances")
Expand Down Expand Up @@ -132,15 +137,6 @@ func RunToolboxDump(ctx context.Context, f commandutils.Factory, out io.Writer,
return err
}

resourceMap, err := resourceops.ListResources(cloud, cluster)
if err != nil {
return err
}
d, err := resources.BuildDump(ctx, cloud, resourceMap)
if err != nil {
return err
}

if options.Dir != "" {
privateKeyPath := options.PrivateKey
if strings.HasPrefix(privateKeyPath, "~/") {
Expand Down Expand Up @@ -262,32 +258,44 @@ func RunToolboxDump(ctx context.Context, f commandutils.Factory, out io.Writer,
}
}

switch options.Output {
case OutputYaml:
b, err := kops.ToRawYaml(d)
if options.CloudResources {
resourceMap, err := resourceops.ListResources(cloud, cluster)
if err != nil {
return fmt.Errorf("error marshaling yaml: %v", err)
return err
}
_, err = out.Write(b)
d, err := resources.BuildDump(ctx, cloud, resourceMap)
if err != nil {
return fmt.Errorf("error writing to stdout: %v", err)
return err
}
return nil

case OutputJSON:
b, err := json.MarshalIndent(d, "", " ")
if err != nil {
return fmt.Errorf("error marshaling json: %v", err)
}
_, err = out.Write(b)
if err != nil {
return fmt.Errorf("error writing to stdout: %v", err)
}
return nil
switch options.Output {
case OutputYaml:
b, err := kops.ToRawYaml(d)
if err != nil {
return fmt.Errorf("error marshaling yaml: %v", err)
}
_, err = out.Write(b)
if err != nil {
return fmt.Errorf("error writing to stdout: %v", err)
}
return nil

default:
return fmt.Errorf("unsupported output format: %q", options.Output)
case OutputJSON:
b, err := json.MarshalIndent(d, "", " ")
if err != nil {
return fmt.Errorf("error marshaling json: %v", err)
}
_, err = out.Write(b)
if err != nil {
return fmt.Errorf("error writing to stdout: %v", err)
}
return nil

default:
return fmt.Errorf("unsupported output format: %q", options.Output)
}
}
return nil
}

func truncateNodeList(nodes *corev1.NodeList, max int) error {
Expand Down
2 changes: 2 additions & 0 deletions tests/e2e/scenarios/bare-metal/dump-artifacts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ for ns in kube-system; do
done

# Use `kops toolbox dump` to dump a lot of useful information
# We pass --cloud-resources=false because dumping cloud resources is not implemented on metal
mkdir -p ${ARTIFACTS}/dump
${KOPS} toolbox dump \
--dir ${ARTIFACTS}/dump \
--k8s-resources \
--cloud-resources=false \
--private-key ${REPO_ROOT}/.build/.ssh/id_ed25519 \
--ssh-user root \
--name metal.k8s.local

0 comments on commit f7b63e4

Please sign in to comment.