@@ -19,12 +19,10 @@ package main
1919import (
2020 "context"
2121 "encoding/json"
22- "errors"
2322 "fmt"
2423 "io"
2524 "os"
2625 "path/filepath"
27- "slices"
2826 "strings"
2927
3028 "github.com/spf13/cobra"
@@ -36,7 +34,6 @@ import (
3634 "k8s.io/client-go/kubernetes"
3735 "k8s.io/klog/v2"
3836 "k8s.io/kops/pkg/apis/kops"
39- "k8s.io/kops/pkg/apis/kops/util"
4037 "k8s.io/kops/pkg/commands/commandutils"
4138 "k8s.io/kops/pkg/dump"
4239 "k8s.io/kops/pkg/resources"
@@ -195,11 +192,6 @@ func RunToolboxDump(ctx context.Context, f commandutils.Factory, out io.Writer,
195192 }
196193 }
197194
198- err = truncateNodeList (& nodes , options .MaxNodes )
199- if err != nil {
200- klog .Warningf ("not limiting number of nodes dumped: %v" , err )
201- }
202-
203195 sshConfig := & ssh.ClientConfig {
204196 Config : ssh.Config {},
205197 User : options .SSHUser ,
@@ -239,24 +231,19 @@ func RunToolboxDump(ctx context.Context, f commandutils.Factory, out io.Writer,
239231 }
240232 }
241233 }
242- }
243- dumper := dump .NewLogDumper (bastionAddress , sshConfig , keyRing , options .Dir )
244-
245- var additionalIPs []string
246- var additionalPrivateIPs []string
247- if cloudResources != nil {
248- for _ , instance := range cloudResources .Instances {
249- if len (instance .PublicAddresses ) != 0 {
250- additionalIPs = append (additionalIPs , instance .PublicAddresses [0 ])
251- } else if len (instance .PrivateAddresses ) != 0 {
252- additionalPrivateIPs = append (additionalPrivateIPs , instance .PrivateAddresses [0 ])
253- } else {
254- klog .Warningf ("no IP for instance %q" , instance .Name )
234+ // If we don't have a bastion, use a control plane instance that has public IPs
235+ if bastionAddress == "" {
236+ for _ , instance := range cloudResources .Instances {
237+ if strings .Contains (instance .Name , "control-plane" ) && len (instance .PublicAddresses ) > 0 {
238+ bastionAddress = instance .PublicAddresses [0 ]
239+ }
255240 }
256241 }
257242 }
258243
259- if err := dumper .DumpAllNodes (ctx , nodes , options .MaxNodes , additionalIPs , additionalPrivateIPs ); err != nil {
244+ dumper := dump .NewLogDumper (bastionAddress , sshConfig , keyRing , options .Dir )
245+
246+ if err := dumper .DumpAllNodes (ctx , nodes , options .MaxNodes , cloudResources ); err != nil {
260247 klog .Warningf ("error dumping nodes: %v" , err )
261248 }
262249
@@ -309,20 +296,3 @@ func RunToolboxDump(ctx context.Context, f commandutils.Factory, out io.Writer,
309296 }
310297 return nil
311298}
312-
313- func truncateNodeList (nodes * corev1.NodeList , max int ) error {
314- if max < 0 {
315- return errors .New ("--max-nodes must be greater than zero" )
316- }
317- // Move control plane nodes to the start of the list and truncate the remainder
318- slices .SortFunc [[]corev1.Node ](nodes .Items , func (a corev1.Node , e corev1.Node ) int {
319- if role := util .GetNodeRole (& a ); role == "control-plane" || role == "apiserver" {
320- return - 1
321- }
322- return 1
323- })
324- if len (nodes .Items ) > max {
325- nodes .Items = nodes .Items [:max ]
326- }
327- return nil
328- }
0 commit comments