toolbox dump: support dumping only k8s resources

Because metal does not support cloud-resource discovery, we need to
skip this in our metal tests.
This commit is contained in:
justinsb 2024-11-10 18:31:14 -05:00
parent 6c421cc6e7
commit b124625c62
3 changed files with 64 additions and 45 deletions

View File

@ -70,6 +70,9 @@ type ToolboxDumpOptions struct {
SSHUser string SSHUser string
MaxNodes int MaxNodes int
K8sResources bool K8sResources bool
// CloudResources controls whether we dump the cloud resources
CloudResources bool
} }
func (o *ToolboxDumpOptions) InitDefaults() { func (o *ToolboxDumpOptions) InitDefaults() {
@ -78,6 +81,7 @@ func (o *ToolboxDumpOptions) InitDefaults() {
o.SSHUser = "ubuntu" o.SSHUser = "ubuntu"
o.MaxNodes = 500 o.MaxNodes = 500
o.K8sResources = k8sResources != "" o.K8sResources = k8sResources != ""
o.CloudResources = true
} }
func NewCmdToolboxDump(f commandutils.Factory, out io.Writer) *cobra.Command { func NewCmdToolboxDump(f commandutils.Factory, out io.Writer) *cobra.Command {
@ -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.Flags().StringVar(&options.Dir, "dir", options.Dir, "Target directory; if specified will collect logs and other information.")
cmd.MarkFlagDirname("dir") cmd.MarkFlagDirname("dir")
cmd.Flags().BoolVar(&options.K8sResources, "k8s-resources", options.K8sResources, "Include k8s resources in the dump") 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().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.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") cmd.Flags().StringVar(&options.SSHUser, "ssh-user", options.SSHUser, "The remote user for SSH access to instances")
@ -132,13 +137,17 @@ func RunToolboxDump(ctx context.Context, f commandutils.Factory, out io.Writer,
return err return err
} }
resourceMap, err := resourceops.ListResources(cloud, cluster) var cloudResources *resources.Dump
if err != nil { if options.CloudResources {
return err resourceMap, err := resourceops.ListResources(cloud, cluster)
} if err != nil {
d, err := resources.BuildDump(ctx, cloud, resourceMap) return err
if err != nil { }
return err d, err := resources.BuildDump(ctx, cloud, resourceMap)
if err != nil {
return err
}
cloudResources = d
} }
if options.Dir != "" { if options.Dir != "" {
@ -213,15 +222,17 @@ func RunToolboxDump(ctx context.Context, f commandutils.Factory, out io.Writer,
// look for a bastion instance and use it if exists // look for a bastion instance and use it if exists
// Prefer a bastion load balancer if exists // Prefer a bastion load balancer if exists
bastionAddress := "" bastionAddress := ""
for _, lb := range d.LoadBalancers { if cloudResources != nil {
if strings.Contains(lb.Name, "bastion") && lb.DNSName != "" { for _, lb := range cloudResources.LoadBalancers {
bastionAddress = lb.DNSName if strings.Contains(lb.Name, "bastion") && lb.DNSName != "" {
bastionAddress = lb.DNSName
}
} }
} if bastionAddress == "" {
if bastionAddress == "" { for _, instance := range cloudResources.Instances {
for _, instance := range d.Instances { if strings.Contains(instance.Name, "bastion") {
if strings.Contains(instance.Name, "bastion") { bastionAddress = instance.PublicAddresses[0]
bastionAddress = instance.PublicAddresses[0] }
} }
} }
} }
@ -229,13 +240,15 @@ func RunToolboxDump(ctx context.Context, f commandutils.Factory, out io.Writer,
var additionalIPs []string var additionalIPs []string
var additionalPrivateIPs []string var additionalPrivateIPs []string
for _, instance := range d.Instances { if cloudResources != nil {
if len(instance.PublicAddresses) != 0 { for _, instance := range cloudResources.Instances {
additionalIPs = append(additionalIPs, instance.PublicAddresses[0]) if len(instance.PublicAddresses) != 0 {
} else if len(instance.PrivateAddresses) != 0 { additionalIPs = append(additionalIPs, instance.PublicAddresses[0])
additionalPrivateIPs = append(additionalPrivateIPs, instance.PrivateAddresses[0]) } else if len(instance.PrivateAddresses) != 0 {
} else { additionalPrivateIPs = append(additionalPrivateIPs, instance.PrivateAddresses[0])
klog.Warningf("no IP for instance %q", instance.Name) } else {
klog.Warningf("no IP for instance %q", instance.Name)
}
} }
} }
@ -262,32 +275,35 @@ func RunToolboxDump(ctx context.Context, f commandutils.Factory, out io.Writer,
} }
} }
switch options.Output { if cloudResources != nil {
case OutputYaml: switch options.Output {
b, err := kops.ToRawYaml(d) case OutputYaml:
if err != nil { b, err := kops.ToRawYaml(cloudResources)
return fmt.Errorf("error marshaling yaml: %v", err) if err != nil {
} return fmt.Errorf("error marshaling yaml: %v", err)
_, err = out.Write(b) }
if err != nil { _, err = out.Write(b)
return fmt.Errorf("error writing to stdout: %v", err) if err != nil {
} return fmt.Errorf("error writing to stdout: %v", err)
return nil }
return nil
case OutputJSON: case OutputJSON:
b, err := json.MarshalIndent(d, "", " ") b, err := json.MarshalIndent(cloudResources, "", " ")
if err != nil { if err != nil {
return fmt.Errorf("error marshaling json: %v", err) return fmt.Errorf("error marshaling json: %v", err)
} }
_, err = out.Write(b) _, err = out.Write(b)
if err != nil { if err != nil {
return fmt.Errorf("error writing to stdout: %v", err) return fmt.Errorf("error writing to stdout: %v", err)
} }
return nil return nil
default: default:
return fmt.Errorf("unsupported output format: %q", options.Output) return fmt.Errorf("unsupported output format: %q", options.Output)
}
} }
return nil
} }
func truncateNodeList(nodes *corev1.NodeList, max int) error { func truncateNodeList(nodes *corev1.NodeList, max int) error {

View File

@ -23,6 +23,7 @@ kops toolbox dump [CLUSTER] [flags]
### Options ### Options
``` ```
--cloud-resources Include cloud resources in the dump (default true)
--dir string Target directory; if specified will collect logs and other information. --dir string Target directory; if specified will collect logs and other information.
-h, --help help for dump -h, --help help for dump
--k8s-resources Include k8s resources in the dump --k8s-resources Include k8s resources in the dump

View File

@ -73,10 +73,12 @@ for ns in kube-system; do
done done
# Use `kops toolbox dump` to dump a lot of useful information # 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 mkdir -p ${ARTIFACTS}/dump
${KOPS} toolbox dump \ ${KOPS} toolbox dump \
--dir ${ARTIFACTS}/dump \ --dir ${ARTIFACTS}/dump \
--k8s-resources \ --k8s-resources \
--cloud-resources=false \
--private-key ${REPO_ROOT}/.build/.ssh/id_ed25519 \ --private-key ${REPO_ROOT}/.build/.ssh/id_ed25519 \
--ssh-user root \ --ssh-user root \
--name metal.k8s.local --name metal.k8s.local