@@ -1091,12 +1091,13 @@ $STACKDRIVER_VERSION = 'v1-9'
1091
1091
$STACKDRIVER_ROOT = ' C:\Program Files (x86)\Stackdriver'
1092
1092
1093
1093
1094
- # Restart the Stackdriver logging agent
1095
- # `Restart-Service StackdriverLogging` may fail because StackdriverLogging
1096
- # sometimes is unstoppable, so we work around it by killing the processes.
1097
- function Restart-StackdriverLoggingAgent {
1094
+ # Restarts the Stackdriver logging agent, or starts it if it is not currently
1095
+ # running. A standard `Restart-Service StackdriverLogging` may fail because
1096
+ # StackdriverLogging sometimes is unstoppable, so this function works around it
1097
+ # by killing the processes.
1098
+ function Restart-LoggingAgent {
1098
1099
Stop-Service - NoWait - ErrorAction Ignore StackdriverLogging
1099
-
1100
+
1100
1101
# Wait (if necessary) for service to stop.
1101
1102
$timeout = 10
1102
1103
$stopped = (Get-service StackdriverLogging).Status -eq ' Stopped'
@@ -1132,13 +1133,13 @@ function Restart-StackdriverLoggingAgent {
1132
1133
Start-Service StackdriverLogging
1133
1134
}
1134
1135
1135
- # Install and start the Stackdriver logging agent according to
1136
+ # Installs the Stackdriver logging agent according to
1136
1137
# https://cloud.google.com/logging/docs/agent/installation.
1137
1138
# TODO(yujuhong): Update to a newer Stackdriver agent once it is released to
1138
1139
# support kubernetes metadata properly. The current version does not recognizes
1139
1140
# the local resource key "logging.googleapis.com/local_resource_id", and fails
1140
1141
# to label namespace, pod and container names on the logs.
1141
- function InstallAndStart -LoggingAgent {
1142
+ function Install -LoggingAgent {
1142
1143
# Remove the existing storage.json file if it exists. This is a workaround
1143
1144
# for the bug where the logging agent cannot start up if the file is
1144
1145
# corrupted.
@@ -1156,9 +1157,7 @@ function InstallAndStart-LoggingAgent {
1156
1157
# well.
1157
1158
Log- Output (" Skip: $STACKDRIVER_ROOT is already present, assuming that " +
1158
1159
" Stackdriver logging agent is already installed" )
1159
- # Restart-Service restarts a running service or starts a not-running
1160
- # service.
1161
- Restart-StackdriverLoggingAgent
1160
+ Restart-LoggingAgent
1162
1161
return
1163
1162
}
1164
1163
@@ -1174,25 +1173,35 @@ function InstallAndStart-LoggingAgent {
1174
1173
Log- Output ' Invoking Stackdriver installer'
1175
1174
Start-Process $installer_file - ArgumentList " /S" - Wait
1176
1175
1176
+ # Install the record-reformer plugin.
1177
1177
Start-Process " $STACKDRIVER_ROOT \LoggingAgent\Main\bin\fluent-gem" `
1178
1178
- ArgumentList " install" , " fluent-plugin-record-reformer" `
1179
1179
- Wait
1180
1180
1181
+ Remove-Item - Force - Recurse $tmp_dir
1182
+ }
1183
+
1184
+ # Writes the logging configuration file for Stackdriver. Restart-LoggingAgent
1185
+ # should then be called to pick up the new configuration.
1186
+ function Configure-LoggingAgent {
1187
+ $fluentd_config_dir = " $STACKDRIVER_ROOT \LoggingAgent\config.d"
1188
+ $fluentd_config_file = " $fluentd_config_dir \k8s_containers.conf"
1189
+ if (-not (ShouldWrite- File $fluentd_config_file )) {
1190
+ Log- Output (" Skip: fluentd logging config $fluentd_config_file already " +
1191
+ " exists" )
1192
+ return
1193
+ }
1194
+
1181
1195
# Create a configuration file for kubernetes containers.
1182
1196
# The config.d directory should have already been created automatically, but
1183
1197
# try creating again just in case.
1184
- New-Item " $STACKDRIVER_ROOT \LoggingAgent\config.d" `
1185
- - ItemType ' directory' `
1186
- - Force | Out-Null
1187
- $FLUENTD_CONFIG | Out-File `
1188
- - FilePath " $STACKDRIVER_ROOT \LoggingAgent\config.d\k8s_containers.conf" `
1189
- - Encoding ASCII
1190
-
1191
- # Restart the service to pick up the new configurations.
1192
- Restart-StackdriverLoggingAgent
1193
- Remove-Item - Force - Recurse $tmp_dir
1198
+ New-Item $fluentd_config_dir - ItemType ' directory' - Force | Out-Null
1199
+ $config = $FLUENTD_CONFIG.replace (' NODE_NAME' , (hostname))
1200
+ $config | Out-File - FilePath $fluentd_config_file - Encoding ASCII
1201
+ Log- Output " Wrote fluentd logging config to $fluentd_config_file "
1194
1202
}
1195
1203
1204
+ # The NODE_NAME placeholder must be replaced with the node's name (hostname).
1196
1205
$FLUENTD_CONFIG = @'
1197
1206
# This configuration file for Fluentd is used to watch changes to kubernetes
1198
1207
# container logs in the directory /var/lib/docker/containers/ and submit the
@@ -1344,7 +1353,7 @@ $FLUENTD_CONFIG = @'
1344
1353
"logging.googleapis.com/local_resource_id" ${"k8s_node.NODE_NAME"}
1345
1354
</record>
1346
1355
</filter>
1347
- '@ .replace( ' NODE_NAME ' , (hostname))
1356
+ '@
1348
1357
1349
1358
1350
1359
# Export all public functions:
0 commit comments