Skip to content

Commit f5cca1a

Browse files
neelasha-09FxKu
andauthored
major version upgrade for rootless and ocp : solving #1689 (#1770)
* major version upgrade for rootless and ocp : solving #1689 Co-authored-by: Felix Kunde <felix-kunde@gmx.de>
1 parent 2333d53 commit f5cca1a

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

pkg/cluster/majorversionupgrade.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cluster
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/zalando/postgres-operator/pkg/spec"
78
"github.com/zalando/postgres-operator/pkg/util"
@@ -105,14 +106,28 @@ func (c *Cluster) majorVersionUpgrade() error {
105106
c.logger.Infof("triggering major version upgrade on pod %s of %d pods", masterPod.Name, numberOfPods)
106107
c.eventRecorder.Eventf(c.GetReference(), v1.EventTypeNormal, "Major Version Upgrade", "Starting major version upgrade on pod %s of %d pods", masterPod.Name, numberOfPods)
107108
upgradeCommand := fmt.Sprintf("/usr/bin/python3 /scripts/inplace_upgrade.py %d 2>&1 | tee last_upgrade.log", numberOfPods)
109+
110+
c.logger.Debugf("checking if the spilo image runs with root or non-root (check for user id=0)")
111+
resultIdCheck, errIdCheck := c.ExecCommand(podName, "/bin/bash", "-c", "/usr/bin/id -u")
112+
if errIdCheck != nil {
113+
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)
114+
}
108115

109-
result, err := c.ExecCommand(podName, "/bin/su", "postgres", "-c", upgradeCommand)
116+
resultIdCheck = strings.TrimSuffix(resultIdCheck, "\n")
117+
var result string
118+
if resultIdCheck != "0" {
119+
c.logger.Infof("User id was identified as: %s, hence default user is non-root already", resultIdCheck)
120+
result, err = c.ExecCommand(podName, "/bin/bash", "-c", upgradeCommand)
121+
} else {
122+
c.logger.Infof("User id was identified as: %s, using su to reach the postgres user", resultIdCheck)
123+
result, err = c.ExecCommand(podName, "/bin/su", "postgres", "-c", upgradeCommand)
124+
}
110125
if err != nil {
111-
c.eventRecorder.Eventf(c.GetReference(), v1.EventTypeNormal, "Major Version Upgrade", "Upgrade from %d to %d FAILED: %v", c.currentMajorVersion, desiredVersion, err)
126+
c.eventRecorder.Eventf(c.GetReference(), v1.EventTypeWarning, "Major Version Upgrade", "Upgrade from %d to %d FAILED: %v", c.currentMajorVersion, desiredVersion, err)
112127
return err
113128
}
114-
115129
c.logger.Infof("upgrade action triggered and command completed: %s", result[:100])
130+
116131
c.eventRecorder.Eventf(c.GetReference(), v1.EventTypeNormal, "Major Version Upgrade", "Upgrade from %d to %d finished", c.currentMajorVersion, desiredVersion)
117132
}
118133
}

0 commit comments

Comments
 (0)