Skip to content

Commit 9282e48

Browse files
committed
Write the Stackdriver config separately from the installation.
This will let us preinstall the Stackdriver logging agent but still configure it correctly when bringing up new Windows nodes. The hostname in the config file looks the same before-and-after: "logging.googleapis.com/local_resource_id" ${"k8s_node.e2e-test-peterhornyack-windows-node-group-6tw6"} "logging.googleapis.com/local_resource_id" ${"k8s_node.e2e-test-peterhornyack-windows-node-group-mf5r"}
1 parent 41049fd commit 9282e48

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

cluster/gce/windows/configure.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ try {
111111
Set-EnvironmentVars
112112
Create-Directories
113113
Download-HelperScripts
114-
InstallAndStart-LoggingAgent
114+
115+
Install-LoggingAgent
116+
Configure-LoggingAgent
117+
Restart-LoggingAgent
115118

116119
Create-DockerRegistryKey
117120
Configure-Dockerd

cluster/gce/windows/k8s-node-setup.psm1

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,12 +1091,13 @@ $STACKDRIVER_VERSION = 'v1-9'
10911091
$STACKDRIVER_ROOT = 'C:\Program Files (x86)\Stackdriver'
10921092

10931093

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 {
10981099
Stop-Service -NoWait -ErrorAction Ignore StackdriverLogging
1099-
1100+
11001101
# Wait (if necessary) for service to stop.
11011102
$timeout = 10
11021103
$stopped = (Get-service StackdriverLogging).Status -eq 'Stopped'
@@ -1132,13 +1133,13 @@ function Restart-StackdriverLoggingAgent {
11321133
Start-Service StackdriverLogging
11331134
}
11341135

1135-
# Install and start the Stackdriver logging agent according to
1136+
# Installs the Stackdriver logging agent according to
11361137
# https://cloud.google.com/logging/docs/agent/installation.
11371138
# TODO(yujuhong): Update to a newer Stackdriver agent once it is released to
11381139
# support kubernetes metadata properly. The current version does not recognizes
11391140
# the local resource key "logging.googleapis.com/local_resource_id", and fails
11401141
# to label namespace, pod and container names on the logs.
1141-
function InstallAndStart-LoggingAgent {
1142+
function Install-LoggingAgent {
11421143
# Remove the existing storage.json file if it exists. This is a workaround
11431144
# for the bug where the logging agent cannot start up if the file is
11441145
# corrupted.
@@ -1156,9 +1157,7 @@ function InstallAndStart-LoggingAgent {
11561157
# well.
11571158
Log-Output ("Skip: $STACKDRIVER_ROOT is already present, assuming that " +
11581159
"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
11621161
return
11631162
}
11641163

@@ -1174,25 +1173,35 @@ function InstallAndStart-LoggingAgent {
11741173
Log-Output 'Invoking Stackdriver installer'
11751174
Start-Process $installer_file -ArgumentList "/S" -Wait
11761175

1176+
# Install the record-reformer plugin.
11771177
Start-Process "$STACKDRIVER_ROOT\LoggingAgent\Main\bin\fluent-gem" `
11781178
-ArgumentList "install","fluent-plugin-record-reformer" `
11791179
-Wait
11801180

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+
11811195
# Create a configuration file for kubernetes containers.
11821196
# The config.d directory should have already been created automatically, but
11831197
# 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"
11941202
}
11951203

1204+
# The NODE_NAME placeholder must be replaced with the node's name (hostname).
11961205
$FLUENTD_CONFIG = @'
11971206
# This configuration file for Fluentd is used to watch changes to kubernetes
11981207
# container logs in the directory /var/lib/docker/containers/ and submit the
@@ -1344,7 +1353,7 @@ $FLUENTD_CONFIG = @'
13441353
"logging.googleapis.com/local_resource_id" ${"k8s_node.NODE_NAME"}
13451354
</record>
13461355
</filter>
1347-
'@.replace('NODE_NAME', (hostname))
1356+
'@
13481357

13491358

13501359
# Export all public functions:

0 commit comments

Comments
 (0)