/
main.go
182 lines (155 loc) · 4.49 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2018 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
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"
)
var (
GitCommit string
GitBranch string
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt)
defer stop()
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.Error(err, "check if restore in progress")
os.Exit(1)
}
if restoreInProgress {
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,
pkg.EnvMongoDBClusterMonitorPassword,
)
if err != nil {
log.Error(err, "new cfg")
os.Exit(1)
}
command, err := app.Parse(os.Args[1:])
if err != nil {
log.Error(err, "Cannot parse command line")
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(ctx, cnf, int64(*startupDelaySeconds))
if err != nil {
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(ctx, cnf)
if err != nil {
log.Error(err, "Member failed Kubernetes liveness check")
os.Exit(1)
}
log.Info("Member passed Kubernetes liveness check")
}
case "k8s readiness":
log.Info("Running Kubernetes readiness check for component", "component", component)
switch *component {
case "mongod":
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, cnf)
if err != nil {
log.Error(err, "Member failed Kubernetes readiness check")
os.Exit(1)
}
}
}
}
func fileExists(name string) (bool, error) {
_, err := os.Stat(name)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
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
}
}