@@ -27,43 +27,107 @@ import (
27
27
28
28
const powershellExe = "PowerShell.exe"
29
29
30
- // getLoggingCmd returns the powershell cmd and arguments for the given nodeLogQuery and boot
31
- func getLoggingCmd (n * nodeLogQuery , services []string ) (string , []string , error ) {
32
- args := []string {
30
+ // getLoggingCmd returns the powershell cmd, arguments, and environment variables for the given nodeLogQuery and boot.
31
+ // All string inputs are environment variables to stop subcommands expressions from being executed.
32
+ // The return values are:
33
+ // - cmd: the command to be executed
34
+ // - args: arguments to the command
35
+ // - cmdEnv: environment variables when the command will be executed
36
+ func getLoggingCmd (n * nodeLogQuery , services []string ) (cmd string , args []string , cmdEnv []string , err error ) {
37
+ cmdEnv = getLoggingCmdEnv (n , services )
38
+
39
+ var includeSinceTime , includeUntilTime , includeTailLines , includePattern bool
40
+ if n .SinceTime != nil {
41
+ includeSinceTime = true
42
+ }
43
+ if n .UntilTime != nil {
44
+ includeUntilTime = true
45
+ }
46
+ if n .TailLines != nil {
47
+ includeTailLines = true
48
+ }
49
+ if len (n .Pattern ) > 0 {
50
+ includePattern = true
51
+ }
52
+
53
+ var includeServices []bool
54
+ for _ , service := range services {
55
+ includeServices = append (includeServices , len (service ) > 0 )
56
+ }
57
+
58
+ args = getLoggingCmdArgs (includeSinceTime , includeUntilTime , includeTailLines , includePattern , includeServices )
59
+
60
+ return powershellExe , args , cmdEnv , nil
61
+ }
62
+
63
+ // getLoggingCmdArgs returns arguments that need to be passed to powershellExe
64
+ func getLoggingCmdArgs (includeSinceTime , includeUntilTime , includeTailLines , includePattern bool , services []bool ) (args []string ) {
65
+ args = []string {
33
66
"-NonInteractive" ,
34
67
"-ExecutionPolicy" , "Bypass" ,
35
68
"-Command" ,
36
69
}
37
70
38
- psCmd := "Get-WinEvent -FilterHashtable @{LogName='Application'"
39
- if n .SinceTime != nil {
40
- psCmd += fmt .Sprintf ("; StartTime='%s'" , n .SinceTime .Format (dateLayout ))
71
+ psCmd := `Get-WinEvent -FilterHashtable @{LogName='Application'`
72
+
73
+ if includeSinceTime {
74
+ psCmd += fmt .Sprintf (`; StartTime="$Env:kubelet_sinceTime"` )
41
75
}
42
- if n . UntilTime != nil {
43
- psCmd += fmt .Sprintf (" ; EndTime='%s'" , n . UntilTime . Format ( dateLayout ) )
76
+ if includeUntilTime {
77
+ psCmd += fmt .Sprintf (` ; EndTime="$Env:kubelet_untilTime"` )
44
78
}
79
+
45
80
var providers []string
46
- for _ , service := range services {
47
- if len ( service ) > 0 {
48
- providers = append (providers , "'" + service + "'" )
81
+ for i := range services {
82
+ if services [ i ] {
83
+ providers = append (providers , fmt . Sprintf ( "$Env:kubelet_provider%d" , i ) )
49
84
}
50
85
}
86
+
51
87
if len (providers ) > 0 {
52
88
psCmd += fmt .Sprintf ("; ProviderName=%s" , strings .Join (providers , "," ))
53
89
}
54
- psCmd += "}"
55
- if n .TailLines != nil {
56
- psCmd += fmt .Sprintf (" -MaxEvents %d" , * n .TailLines )
90
+
91
+ psCmd += `}`
92
+ if includeTailLines {
93
+ psCmd += fmt .Sprint (` -MaxEvents $Env:kubelet_tailLines` )
57
94
}
58
- psCmd += " | Sort-Object TimeCreated"
59
- if len (n .Pattern ) > 0 {
60
- psCmd += fmt .Sprintf (" | Where-Object -Property Message -Match '%s'" , n .Pattern )
95
+ psCmd += ` | Sort-Object TimeCreated`
96
+
97
+ if includePattern {
98
+ psCmd += fmt .Sprintf (` | Where-Object -Property Message -Match "$Env:kubelet_pattern"` )
61
99
}
62
- psCmd += " | Format-Table -AutoSize -Wrap"
100
+ psCmd += ` | Format-Table -AutoSize -Wrap`
63
101
64
102
args = append (args , psCmd )
65
103
66
- return powershellExe , args , nil
104
+ return args
105
+ }
106
+
107
+ // getLoggingCmdEnv returns the environment variables that will be present when powershellExe is executed
108
+ func getLoggingCmdEnv (n * nodeLogQuery , services []string ) (cmdEnv []string ) {
109
+ if n .SinceTime != nil {
110
+ cmdEnv = append (cmdEnv , fmt .Sprintf ("kubelet_sinceTime=%s" , n .SinceTime .Format (dateLayout )))
111
+ }
112
+ if n .UntilTime != nil {
113
+ cmdEnv = append (cmdEnv , fmt .Sprintf ("kubelet_untilTime=%s" , n .UntilTime .Format (dateLayout )))
114
+ }
115
+
116
+ for i , service := range services {
117
+ if len (service ) > 0 {
118
+ cmdEnv = append (cmdEnv , fmt .Sprintf ("kubelet_provider%d=%s" , i , service ))
119
+ }
120
+ }
121
+
122
+ if n .TailLines != nil {
123
+ cmdEnv = append (cmdEnv , fmt .Sprintf ("kubelet_tailLines=%d" , * n .TailLines ))
124
+ }
125
+
126
+ if len (n .Pattern ) > 0 {
127
+ cmdEnv = append (cmdEnv , fmt .Sprintf ("kubelet_pattern=%s" , n .Pattern ))
128
+ }
129
+
130
+ return cmdEnv
67
131
}
68
132
69
133
// checkForNativeLogger always returns true for Windows
0 commit comments