Skip to content

Commit

Permalink
Merge branch 'main' into dev/K8SPSMDB-876
Browse files Browse the repository at this point in the history
  • Loading branch information
pooknull authored Aug 31, 2023
2 parents 6d70681 + 3d4ed9c commit 3843f9b
Show file tree
Hide file tree
Showing 55 changed files with 400 additions and 2,132 deletions.
84 changes: 69 additions & 15 deletions cmd/mongodb-healthcheck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ package main
import (
"context"
"os"
"strconv"
"strings"

uzap "go.uber.org/zap"
"go.uber.org/zap/zapcore"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/percona/percona-server-mongodb-operator/healthcheck"
"github.com/percona/percona-server-mongodb-operator/healthcheck/pkg"
"github.com/percona/percona-server-mongodb-operator/healthcheck/tools/db"
"github.com/percona/percona-server-mongodb-operator/healthcheck/tools/tool"
log "github.com/sirupsen/logrus"
)

var (
Expand All @@ -31,17 +36,24 @@ var (
)

func main() {
app, _ := tool.New("Performs health and readiness checks for MongoDB", GitCommit, GitBranch)
app := tool.New("Performs health and readiness checks for MongoDB", GitCommit, GitBranch)

k8sCmd := app.Command("k8s", "Performs liveness check for MongoDB on Kubernetes")
livenessCmd := k8sCmd.Command("liveness", "Run a liveness check of MongoDB").Default()
_ = k8sCmd.Command("readiness", "Run a readiness check of MongoDB")
startupDelaySeconds := livenessCmd.Flag("startupDelaySeconds", "").Default("7200").Uint64()
component := k8sCmd.Flag("component", "").Default("mongod").String()

opts := zap.Options{
Encoder: getLogEncoder(),
Level: getLogLevel(),
}
log := zap.New(zap.UseFlagOptions(&opts))

restoreInProgress, err := fileExists("/opt/percona/restore-in-progress")
if err != nil {
log.Fatalf("check if restore in progress: %v", err)
log.Error(err, "check if restore in progress")
os.Exit(1)
}

if restoreInProgress {
Expand All @@ -53,65 +65,70 @@ func main() {
pkg.EnvMongoDBClusterMonitorUser,
pkg.EnvMongoDBClusterMonitorPassword,
)

if err != nil {
log.Fatalf("new cfg: %s", err)
log.Error(err, "new cfg")
os.Exit(1)
}

command, err := app.Parse(os.Args[1:])
if err != nil {
log.Fatalf("Cannot parse command line: %s", err)
log.Error(err, "Cannot parse command line")
os.Exit(1)
}

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

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

switch command {

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

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

case "mongos":
err := healthcheck.HealthCheckMongosLiveness(client)
if err != nil {
client.Disconnect(context.TODO()) // nolint:golint,errcheck
log.Errorf("Member failed Kubernetes liveness check: %s", err.Error())
log.Error(err, "Member failed Kubernetes liveness check")
os.Exit(1)
}
log.Infof("Member passed Kubernetes liveness check")
log.Info("Member passed Kubernetes liveness check")
}

case "k8s readiness":
log.Infof("Running Kubernetes readiness check for %s", *component)
log.Info("Running Kubernetes readiness check for component", "component", component)
switch *component {

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

case "mongos":
err := healthcheck.MongosReadinessCheck(client)
if err != nil {
client.Disconnect(context.TODO()) // nolint:golint,errcheck
log.Errorf("Member failed Kubernetes readiness check: %s", err.Error())
log.Error(err, "Member failed Kubernetes readiness check")
os.Exit(1)
}
}
Expand All @@ -128,3 +145,40 @@ func fileExists(name string) (bool, error) {
}
return true, nil
}

func getLogEncoder() zapcore.Encoder {
consoleEnc := zapcore.NewConsoleEncoder(uzap.NewDevelopmentEncoderConfig())

s, found := os.LookupEnv("LOG_STRUCTURED")
if !found {
return consoleEnc
}

useJson, err := strconv.ParseBool(s)
if err != nil {
return consoleEnc
}
if !useJson {
return consoleEnc
}

return zapcore.NewJSONEncoder(uzap.NewProductionEncoderConfig())
}

func getLogLevel() zapcore.LevelEnabler {
l, found := os.LookupEnv("LOG_LEVEL")
if !found {
return zapcore.InfoLevel
}

switch strings.ToUpper(l) {
case "DEBUG":
return zapcore.DebugLevel
case "INFO":
return zapcore.InfoLevel
case "ERROR":
return zapcore.ErrorLevel
default:
return zapcore.InfoLevel
}
}
145 changes: 33 additions & 112 deletions config/crd/bases/psmdb.percona.com_perconaservermongodbs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -389,109 +389,6 @@ spec:
type: object
initImage:
type: string
mongod:
properties:
auditLog:
properties:
destination:
type: string
filter:
type: string
format:
type: string
type: object
net:
properties:
hostPort:
format: int32
type: integer
port:
format: int32
type: integer
type: object
operationProfiling:
properties:
mode:
type: string
rateLimit:
type: integer
slowOpThresholdMs:
type: integer
type: object
replication:
properties:
oplogSizeMB:
type: integer
type: object
security:
properties:
enableEncryption:
type: boolean
encryptionCipherMode:
type: string
encryptionKeySecret:
type: string
redactClientLogData:
type: boolean
type: object
setParameter:
properties:
cursorTimeoutMillis:
type: integer
ttlMonitorSleepSecs:
type: integer
wiredTigerConcurrentReadTransactions:
type: integer
wiredTigerConcurrentWriteTransactions:
type: integer
type: object
storage:
properties:
directoryPerDB:
type: boolean
engine:
type: string
inMemory:
properties:
engineConfig:
properties:
inMemorySizeRatio:
type: number
type: object
type: object
mmapv1:
properties:
nsSize:
type: integer
smallfiles:
type: boolean
type: object
syncPeriodSecs:
type: integer
wiredTiger:
properties:
collectionConfig:
properties:
blockCompressor:
type: string
type: object
engineConfig:
properties:
cacheSizeRatio:
type: number
directoryForIndexes:
type: boolean
journalCompressor:
type: string
type: object
indexConfig:
properties:
prefixCompression:
type: boolean
type: object
type: object
type: object
type: object
multiCluster:
properties:
DNSSuffix:
Expand Down Expand Up @@ -2977,6 +2874,17 @@ spec:
- votes
type: object
type: array
hostAliases:
items:
properties:
hostnames:
items:
type: string
type: array
ip:
type: string
type: object
type: array
labels:
additionalProperties:
type: string
Expand Down Expand Up @@ -9740,6 +9648,17 @@ spec:
- votes
type: object
type: array
hostAliases:
items:
properties:
hostnames:
items:
type: string
type: array
ip:
type: string
type: object
type: array
labels:
additionalProperties:
type: string
Expand Down Expand Up @@ -14438,15 +14357,6 @@ spec:
additionalProperties:
type: string
type: object
auditLog:
properties:
destination:
type: string
filter:
type: string
format:
type: string
type: object
configuration:
type: string
containerSecurityContext:
Expand Down Expand Up @@ -14529,6 +14439,17 @@ spec:
servicePerPod:
type: boolean
type: object
hostAliases:
items:
properties:
hostnames:
items:
type: string
type: array
ip:
type: string
type: object
type: array
hostPort:
format: int32
type: integer
Expand Down
Loading

0 comments on commit 3843f9b

Please sign in to comment.