Skip to content

Commit 8addd49

Browse files
committed
added verbose debugging to kubelet param search
1 parent 11b89fc commit 8addd49

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

config.go

+30-16
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,21 @@ func ImportPodServiceAccountToken() ServerInfo {
7878
}
7979

8080
func getKubeletKubeconfigPath() (string, error) {
81+
8182
// 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+
8286
dir, err := os.Open("/proc")
8387
if err != nil {
84-
fmt.Println("DEBUG: Error opening /proc:", err)
88+
printIfVerbose("DEBUG: Error opening /proc: "+err.Error(), Verbose)
8589
return "", err
8690
}
8791
defer dir.Close()
8892

8993
// List all entries in the /proc directory
94+
printIfVerbose("DEBUG: Reading directory contents from /proc.", Verbose)
95+
9096
procs, err := dir.Readdirnames(-1)
9197
if err != nil {
9298
fmt.Println("DEBUG: Error reading /proc:", err)
@@ -95,23 +101,28 @@ func getKubeletKubeconfigPath() (string, error) {
95101

96102
// Look for the kubelet process by checking cmdline for each process
97103
for _, pid := range procs {
104+
printIfVerbose("DEBUG: Checking PID "+pid+" for kubelet.", Verbose)
105+
98106
args, err := getCmdLine(pid)
99107
if err != nil || len(args) == 0 {
100108
continue
101109
}
102110

103111
// Find the "kubelet" process and return its kubeconfig path
104112
if strings.Contains(args[0], "kubelet") {
113+
printIfVerbose("DEBUG: Found kubelet process with PID "+pid, Verbose)
114+
105115
kubeConfig := findFlagValue(args, "--kubeconfig")
106116
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)
110118
return kubeConfig, nil
119+
} else {
120+
printIfVerbose("DEBUG: Kubelet is running with PID "+pid+" but no kubeconfig found.", Verbose)
111121
}
112122
}
113123
}
114124

125+
printIfVerbose("DEBUG: Kubelet config file command line parameter not found.", Verbose)
115126
return "", errors.New("kubelet not found")
116127
}
117128

@@ -415,14 +426,10 @@ func gatherPodCredentials(serviceAccounts *[]ServiceAccount, interactive bool, r
415426
out, err := cmd.CombinedOutput()
416427

417428
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)
421430
continue
422431
}
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)
426433

427434
// Now find a Subject line:
428435

@@ -433,9 +440,9 @@ func gatherPodCredentials(serviceAccounts *[]ServiceAccount, interactive bool, r
433440
line := strings.TrimSpace(line)
434441
subjectValue := strings.TrimPrefix(line, "Subject: ")
435442
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+
439446
break
440447

441448
}
@@ -588,9 +595,7 @@ func getPodName(kubeletPodsDir, podDirName string) string {
588595
podName = strings.Fields(line)[1]
589596
break
590597
} else {
591-
if Verbose {
592-
println("DEBUG: unexpected line type: " + line)
593-
}
598+
printIfVerbose("DEBUG: unexpected line type: "+line, Verbose)
594599
}
595600

596601
}
@@ -698,13 +703,22 @@ func getCmdLine(pid string) ([]string, error) {
698703

699704
// Function to find the value for a specific flag in the command line arguments
700705
func findFlagValue(args []string, flag string) string {
706+
printIfVerbose("DEBUG: Checking a process' arguments, looking for flag "+flag, Verbose)
701707
for _, arg := range args {
708+
709+
printIfVerbose("DEBUG: Checking argument: "+arg, Verbose)
710+
702711
if strings.HasPrefix(arg, flag) {
712+
printIfVerbose("DEBUG: Found flag in: "+arg, Verbose)
703713
// Split the argument on "=" to separate the flag from its value
704714
parts := strings.SplitN(arg, "=", 2)
705715
if len(parts) == 2 {
716+
printIfVerbose("Found value for flag of "+parts[1], Verbose)
706717
return parts[1]
718+
} else {
719+
printIfVerbose("ERROR: incorrect number of = signs", Verbose)
707720
}
721+
708722
}
709723
}
710724
return ""

output_to_user.go

+6
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ func outputToUser(kubectlOutputString string, logToFile bool, outputFileName str
2828
}
2929

3030
}
31+
32+
func printIfVerbose(message string, verbose bool) {
33+
if verbose {
34+
println(message)
35+
}
36+
}

0 commit comments

Comments
 (0)