Skip to content

Commit

Permalink
K8SPSMDB-974: add sleep infinity for mongod pods (#1315)
Browse files Browse the repository at this point in the history
* K8SPSMDB-974: add `sleep infinity` for mongod pods

https://jira.percona.com/browse/K8SPSMDB-974

* fix `golangci-lint` warns and replace `context.TODO()`

* fix tests

* fix tests

* fix tests

---------

Co-authored-by: Ege Güneş <[email protected]>
Co-authored-by: Inel Pandzic <[email protected]>
  • Loading branch information
3 people authored Sep 18, 2023
1 parent f005fe4 commit 9340bef
Show file tree
Hide file tree
Showing 136 changed files with 1,031 additions and 315 deletions.
21 changes: 17 additions & 4 deletions build/ps-entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ fi
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
local def="${2-}"
if [ "${!var-}" ] && [ "${!fileVar-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
if [ "${!var-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
elif [ "${!fileVar-}" ]; then
val="$(<"${!fileVar}")"
fi
export "$var"="$val"
Expand Down Expand Up @@ -245,7 +245,20 @@ _dbPath() {
echo "$dbPath"
}

is_manual_recovery() {
recovery_file='/data/db/sleep-forever'
if [ -f "${recovery_file}" ]; then
echo "The $recovery_file file is detected, node is going to infinity loop"
echo "If you want to exit from infinity loop you need to remove $recovery_file file"
while [ -f "${recovery_file}" ]; do
sleep 1
done
fi
}

if [ "$originalArgOne" = 'mongod' ]; then
is_manual_recovery

file_env 'MONGO_INITDB_ROOT_USERNAME'
file_env 'MONGO_INITDB_ROOT_PASSWORD'
# pre-check a few factors to see if it's even worth bothering with initdb
Expand Down
41 changes: 17 additions & 24 deletions cmd/mongodb-healthcheck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ func main() {
os.Exit(0)
}

sleepForever, err := fileExists("/data/db/sleep-forever")
if err != nil {
log.Error(err, "check if sleep-forever file exists")
os.Exit(1)
}
if sleepForever {
os.Exit(0)
}

cnf, err := db.NewConfig(
app,
pkg.EnvMongoDBClusterMonitorUser,
Expand All @@ -82,38 +91,22 @@ func main() {
os.Exit(1)
}

client, err := db.Dial(ctx, cnf)
if err != nil {
log.Error(err, "connection error")
os.Exit(1)
}

defer func() {
if err := client.Disconnect(ctx); err != nil {
log.Error(err, "failed to disconnect")
os.Exit(1)
}
}()

switch command {

case "k8s liveness":
log.Info("Running Kubernetes liveness check for", "component", component)
switch *component {

case "mongod":
memberState, err := healthcheck.HealthCheckMongodLiveness(client, int64(*startupDelaySeconds))
memberState, err := healthcheck.HealthCheckMongodLiveness(ctx, cnf, int64(*startupDelaySeconds))
if err != nil {
client.Disconnect(ctx) // nolint:golint,errcheck
log.Error(err, "Member failed Kubernetes liveness check")
os.Exit(1)
}
log.Info("Member passed Kubernetes liveness check with replication state", "state", memberState)

case "mongos":
err := healthcheck.HealthCheckMongosLiveness(client)
err := healthcheck.HealthCheckMongosLiveness(ctx, cnf)
if err != nil {
client.Disconnect(ctx) // nolint:golint,errcheck
log.Error(err, "Member failed Kubernetes liveness check")
os.Exit(1)
}
Expand All @@ -125,14 +118,14 @@ func main() {
switch *component {

case "mongod":
client.Disconnect(ctx) // nolint:golint,errcheck
log.Error(err, "readiness check for mongod is not implemented")
os.Exit(1)

err := healthcheck.MongodReadinessCheck(ctx, cnf.Hosts[0])
if err != nil {
log.Error(err, "Member failed Kubernetes readiness check")
os.Exit(1)
}
case "mongos":
err := healthcheck.MongosReadinessCheck(ctx, client)
err := healthcheck.MongosReadinessCheck(ctx, cnf)
if err != nil {
client.Disconnect(ctx) // nolint:golint,errcheck
log.Error(err, "Member failed Kubernetes readiness check")
os.Exit(1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,17 @@ spec:
name: mongodb
protocol: TCP
readinessProbe:
exec:
command:
- /opt/percona/mongodb-healthcheck
- k8s
- readiness
- --component
- mongod
failureThreshold: 8
initialDelaySeconds: 10
periodSeconds: 3
successThreshold: 1
tcpSocket:
port: 27017
timeoutSeconds: 2
resources: {}
securityContext:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,17 @@ spec:
name: mongodb
protocol: TCP
readinessProbe:
exec:
command:
- /opt/percona/mongodb-healthcheck
- k8s
- readiness
- --component
- mongod
failureThreshold: 8
initialDelaySeconds: 10
periodSeconds: 3
successThreshold: 1
tcpSocket:
port: 27017
timeoutSeconds: 2
resources: {}
securityContext:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,17 @@ spec:
name: mongodb
protocol: TCP
readinessProbe:
exec:
command:
- /opt/percona/mongodb-healthcheck
- k8s
- readiness
- --component
- mongod
failureThreshold: 8
initialDelaySeconds: 10
periodSeconds: 3
successThreshold: 1
tcpSocket:
port: 27017
timeoutSeconds: 2
resources: {}
securityContext:
Expand Down
9 changes: 7 additions & 2 deletions e2e-tests/arbiter/compare/statefulset_arbiter-rs0-arbiter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,17 @@ spec:
name: mongodb
protocol: TCP
readinessProbe:
exec:
command:
- /opt/percona/mongodb-healthcheck
- k8s
- readiness
- --component
- mongod
failureThreshold: 8
initialDelaySeconds: 10
periodSeconds: 3
successThreshold: 1
tcpSocket:
port: 27017
timeoutSeconds: 2
resources: {}
securityContext:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,17 @@ spec:
name: mongodb
protocol: TCP
readinessProbe:
exec:
command:
- /opt/percona/mongodb-healthcheck
- k8s
- readiness
- --component
- mongod
failureThreshold: 3
initialDelaySeconds: 10
periodSeconds: 3
successThreshold: 1
tcpSocket:
port: 27017
timeoutSeconds: 2
resources: {}
securityContext:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,17 @@ spec:
name: mongodb
protocol: TCP
readinessProbe:
exec:
command:
- /opt/percona/mongodb-healthcheck
- k8s
- readiness
- --component
- mongod
failureThreshold: 3
initialDelaySeconds: 10
periodSeconds: 3
successThreshold: 1
tcpSocket:
port: 27017
timeoutSeconds: 2
resources: {}
securityContext:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,17 @@ spec:
name: mongodb
protocol: TCP
readinessProbe:
exec:
command:
- /opt/percona/mongodb-healthcheck
- k8s
- readiness
- --component
- mongod
failureThreshold: 8
initialDelaySeconds: 10
periodSeconds: 3
successThreshold: 1
tcpSocket:
port: 27017
timeoutSeconds: 2
resources:
limits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,17 @@ spec:
name: mongodb
protocol: TCP
readinessProbe:
exec:
command:
- /opt/percona/mongodb-healthcheck
- k8s
- readiness
- --component
- mongod
failureThreshold: 8
initialDelaySeconds: 10
periodSeconds: 3
successThreshold: 1
tcpSocket:
port: 27017
timeoutSeconds: 2
resources:
limits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,17 @@ spec:
name: mongodb
protocol: TCP
readinessProbe:
exec:
command:
- /opt/percona/mongodb-healthcheck
- k8s
- readiness
- --component
- mongod
failureThreshold: 3
initialDelaySeconds: 10
periodSeconds: 3
successThreshold: 1
tcpSocket:
port: 27017
timeoutSeconds: 2
resources: {}
securityContext:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,17 @@ spec:
name: mongodb
protocol: TCP
readinessProbe:
exec:
command:
- /opt/percona/mongodb-healthcheck
- k8s
- readiness
- --component
- mongod
failureThreshold: 3
initialDelaySeconds: 10
periodSeconds: 3
successThreshold: 1
tcpSocket:
port: 27017
timeoutSeconds: 2
resources: {}
securityContext:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,17 @@ spec:
name: mongodb
protocol: TCP
readinessProbe:
exec:
command:
- /opt/percona/mongodb-healthcheck
- k8s
- readiness
- --component
- mongod
failureThreshold: 8
initialDelaySeconds: 10
periodSeconds: 3
successThreshold: 1
tcpSocket:
port: 27017
timeoutSeconds: 2
resources: {}
securityContext:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,17 @@ spec:
name: mongodb
protocol: TCP
readinessProbe:
exec:
command:
- /opt/percona/mongodb-healthcheck
- k8s
- readiness
- --component
- mongod
failureThreshold: 8
initialDelaySeconds: 10
periodSeconds: 3
successThreshold: 1
tcpSocket:
port: 27017
timeoutSeconds: 2
resources: {}
securityContext:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,17 @@ spec:
name: mongodb
protocol: TCP
readinessProbe:
exec:
command:
- /opt/percona/mongodb-healthcheck
- k8s
- readiness
- --component
- mongod
failureThreshold: 3
initialDelaySeconds: 10
periodSeconds: 3
successThreshold: 1
tcpSocket:
port: 27017
timeoutSeconds: 2
resources:
limits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,17 @@ spec:
name: mongodb
protocol: TCP
readinessProbe:
exec:
command:
- /opt/percona/mongodb-healthcheck
- k8s
- readiness
- --component
- mongod
failureThreshold: 3
initialDelaySeconds: 10
periodSeconds: 3
successThreshold: 1
tcpSocket:
port: 27017
timeoutSeconds: 2
resources:
limits:
Expand Down
Loading

0 comments on commit 9340bef

Please sign in to comment.