@@ -78,15 +78,21 @@ func ImportPodServiceAccountToken() ServerInfo {
78
78
}
79
79
80
80
func getKubeletKubeconfigPath () (string , error ) {
81
+
81
82
// Open the /proc directory
83
+ printIfVerbose ("DEBUG: Reading /proc to determine if a kubelet is visible." , Verbose )
84
+ printIfVerbose ("DEBUG: Opening /proc directory for reading..." , Verbose )
85
+
82
86
dir , err := os .Open ("/proc" )
83
87
if err != nil {
84
- fmt . Println ("DEBUG: Error opening /proc:" , err )
88
+ printIfVerbose ("DEBUG: Error opening /proc: " + err . Error (), Verbose )
85
89
return "" , err
86
90
}
87
91
defer dir .Close ()
88
92
89
93
// List all entries in the /proc directory
94
+ printIfVerbose ("DEBUG: Reading directory contents from /proc." , Verbose )
95
+
90
96
procs , err := dir .Readdirnames (- 1 )
91
97
if err != nil {
92
98
fmt .Println ("DEBUG: Error reading /proc:" , err )
@@ -95,23 +101,28 @@ func getKubeletKubeconfigPath() (string, error) {
95
101
96
102
// Look for the kubelet process by checking cmdline for each process
97
103
for _ , pid := range procs {
104
+ printIfVerbose ("DEBUG: Checking PID " + pid + " for kubelet." , Verbose )
105
+
98
106
args , err := getCmdLine (pid )
99
107
if err != nil || len (args ) == 0 {
100
108
continue
101
109
}
102
110
103
111
// Find the "kubelet" process and return its kubeconfig path
104
112
if strings .Contains (args [0 ], "kubelet" ) {
113
+ printIfVerbose ("DEBUG: Found kubelet process with PID " + pid , Verbose )
114
+
105
115
kubeConfig := findFlagValue (args , "--kubeconfig" )
106
116
if kubeConfig != "" {
107
- if Verbose {
108
- fmt .Printf ("DEBUG: Kubelet is running with PID %s and uses kubeconfig: %s\n " , pid , kubeConfig )
109
- }
117
+ printIfVerbose (fmt .Sprintf ("DEBUG: Kubelet is running with PID %s and uses kubeconfig: %s\n " , pid , kubeConfig ), Verbose )
110
118
return kubeConfig , nil
119
+ } else {
120
+ printIfVerbose ("DEBUG: Kubelet is running with PID " + pid + " but no kubeconfig found." , Verbose )
111
121
}
112
122
}
113
123
}
114
124
125
+ printIfVerbose ("DEBUG: Kubelet config file command line parameter not found." , Verbose )
115
126
return "" , errors .New ("kubelet not found" )
116
127
}
117
128
@@ -415,14 +426,10 @@ func gatherPodCredentials(serviceAccounts *[]ServiceAccount, interactive bool, r
415
426
out , err := cmd .CombinedOutput ()
416
427
417
428
if err != nil {
418
- if Verbose {
419
- println ("DEBUG: running command failed with " + err .Error ())
420
- }
429
+ printIfVerbose ("DEBUG: running command failed with " + err .Error (), Verbose )
421
430
continue
422
431
}
423
- if Verbose {
424
- fmt .Printf ("DEBUG: Certificate is \n %s\n " , string (out ))
425
- }
432
+ printIfVerbose (fmt .Sprintf ("DEBUG: Certificate is \n %s\n " , string (out )), Verbose )
426
433
427
434
// Now find a Subject line:
428
435
@@ -433,9 +440,9 @@ func gatherPodCredentials(serviceAccounts *[]ServiceAccount, interactive bool, r
433
440
line := strings .TrimSpace (line )
434
441
subjectValue := strings .TrimPrefix (line , "Subject: " )
435
442
certNameFound = strings .TrimSpace (subjectValue )
436
- if Verbose {
437
- println ("DEBUG: subject value was : " + subjectValue )
438
- }
443
+
444
+ printIfVerbose ("DEBUG: subject value was : " + subjectValue , Verbose )
445
+
439
446
break
440
447
441
448
}
@@ -588,9 +595,7 @@ func getPodName(kubeletPodsDir, podDirName string) string {
588
595
podName = strings .Fields (line )[1 ]
589
596
break
590
597
} else {
591
- if Verbose {
592
- println ("DEBUG: unexpected line type: " + line )
593
- }
598
+ printIfVerbose ("DEBUG: unexpected line type: " + line , Verbose )
594
599
}
595
600
596
601
}
@@ -698,13 +703,22 @@ func getCmdLine(pid string) ([]string, error) {
698
703
699
704
// Function to find the value for a specific flag in the command line arguments
700
705
func findFlagValue (args []string , flag string ) string {
706
+ printIfVerbose ("DEBUG: Checking a process' arguments, looking for flag " + flag , Verbose )
701
707
for _ , arg := range args {
708
+
709
+ printIfVerbose ("DEBUG: Checking argument: " + arg , Verbose )
710
+
702
711
if strings .HasPrefix (arg , flag ) {
712
+ printIfVerbose ("DEBUG: Found flag in: " + arg , Verbose )
703
713
// Split the argument on "=" to separate the flag from its value
704
714
parts := strings .SplitN (arg , "=" , 2 )
705
715
if len (parts ) == 2 {
716
+ printIfVerbose ("Found value for flag of " + parts [1 ], Verbose )
706
717
return parts [1 ]
718
+ } else {
719
+ printIfVerbose ("ERROR: incorrect number of = signs" , Verbose )
707
720
}
721
+
708
722
}
709
723
}
710
724
return ""
0 commit comments