Skip to content

Commit 9d7f2fa

Browse files
committed
ensure exit-code from upgrade-skript (container) was fetched correct and output is correct (event and logs)
1 parent 7e1528c commit 9d7f2fa

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

pkg/cluster/majorversionupgrade.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,35 +265,47 @@ func (c *Cluster) majorVersionUpgrade() error {
265265
podName := &spec.NamespacedName{Namespace: masterPod.Namespace, Name: masterPod.Name}
266266
c.logger.Infof("triggering major version upgrade on pod %s of %d pods", masterPod.Name, numberOfPods)
267267
c.eventRecorder.Eventf(c.GetReference(), v1.EventTypeNormal, "Major Version Upgrade", "starting major version upgrade on pod %s of %d pods", masterPod.Name, numberOfPods)
268-
upgradeCommand := fmt.Sprintf("set -o pipefail && /usr/local/bin/python3 /scripts/inplace_upgrade.py %d 2>&1 | tee last_upgrade.log", numberOfPods)
269-
268+
upgradeCommand := fmt.Sprintf("/usr/local/bin/python3 /scripts/inplace_upgrade.py %d 2>&1", numberOfPods)
270269
c.logger.Debug("checking if the container runs with root or non-root (check for user id=0)")
271270
resultIdCheck, errIdCheck := c.ExecCommand(podName, "/bin/bash", "-c", "/usr/bin/id -u")
272271
if errIdCheck != nil {
273272
c.eventRecorder.Eventf(c.GetReference(), v1.EventTypeWarning, "Major Version Upgrade", "checking user id to run upgrade from %d to %d FAILED: %v", c.currentMajorVersion, desiredVersion, errIdCheck)
274273
}
275274

276275
resultIdCheck = strings.TrimSuffix(resultIdCheck, "\n")
277-
var result, scriptErrMsg string
276+
var result string
277+
278278
if resultIdCheck != "0" {
279279
c.logger.Infof("user id was identified as: %s, hence default user is non-root already", resultIdCheck)
280280
result, err = c.ExecCommand(podName, "/bin/bash", "-c", upgradeCommand)
281-
scriptErrMsg, _ = c.ExecCommand(podName, "/bin/bash", "-c", "tail -n 1 last_upgrade.log")
282281
} else {
283282
c.logger.Infof("user id was identified as: %s, using su to reach the postgres user", resultIdCheck)
284283
result, err = c.ExecCommand(podName, "/bin/su", "postgres", "-c", upgradeCommand)
285-
scriptErrMsg, _ = c.ExecCommand(podName, "/bin/bash", "-c", "tail -n 1 last_upgrade.log")
286284
}
285+
287286
if err != nil {
288287
isUpgradeSuccess = false
289288
c.annotatePostgresResource(isUpgradeSuccess)
290-
c.eventRecorder.Eventf(c.GetReference(), v1.EventTypeWarning, "Major Version Upgrade", "upgrade from %d to %d FAILED: %v", c.currentMajorVersion, desiredVersion, scriptErrMsg)
291-
return fmt.Errorf("%s", scriptErrMsg)
289+
290+
finalErrorMsg := strings.TrimSpace(result)
291+
if finalErrorMsg == "" {
292+
finalErrorMsg = err.Error()
293+
}
294+
295+
lines := strings.Split(finalErrorMsg, "\n")
296+
if len(lines) > 5 {
297+
finalErrorMsg = strings.Join(lines[len(lines)-5:], " | ")
298+
}
299+
300+
c.logger.Errorf("Major upgrade failed: %v", err)
301+
c.eventRecorder.Eventf(c.GetReference(), v1.EventTypeWarning, "Major Version Upgrade", "upgrade from %d to %d FAILED: %s", c.currentMajorVersion, desiredVersion, finalErrorMsg)
302+
303+
return fmt.Errorf("upgrade script failed: %s", finalErrorMsg)
292304
}
293305

294306
c.annotatePostgresResource(isUpgradeSuccess)
295307
c.logger.Infof("upgrade action triggered and command completed: %s", result[:100])
296-
c.eventRecorder.Eventf(c.GetReference(), v1.EventTypeNormal, "Major Version Upgrade", "upgrade from %d to %d finished", c.currentMajorVersion, desiredVersion)
308+
c.eventRecorder.Eventf(c.GetReference(), v1.EventTypeNormal, "Major Version Upgrade", "major version upgrade from version %d to version %d was successfully completed.", c.currentMajorVersion, desiredVersion)
297309
}
298310
}
299311

0 commit comments

Comments
 (0)