|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/mongodb/mongodb-kubernetes-operator/pkg/readiness/config" |
| 11 | + "github.com/mongodb/mongodb-kubernetes-operator/pkg/readiness/headless" |
| 12 | + "github.com/mongodb/mongodb-kubernetes-operator/pkg/readiness/health" |
| 13 | + "github.com/mongodb/mongodb-kubernetes-operator/pkg/util/contains" |
| 14 | + |
| 15 | + "k8s.io/client-go/kubernetes" |
| 16 | + "k8s.io/client-go/rest" |
| 17 | + |
| 18 | + "go.uber.org/zap" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + headlessAgent = "HEADLESS_AGENT" |
| 23 | +) |
| 24 | + |
| 25 | +var riskySteps []string |
| 26 | +var logger *zap.SugaredLogger |
| 27 | + |
| 28 | +func init() { |
| 29 | + riskySteps = []string{"WaitAllRsMembersUp", "WaitRsInit"} |
| 30 | + |
| 31 | + // By default we log to the output (convenient for tests) |
| 32 | + cfg := zap.NewDevelopmentConfig() |
| 33 | + log, err := cfg.Build() |
| 34 | + if err != nil { |
| 35 | + panic(err) |
| 36 | + } |
| 37 | + logger = log.Sugar() |
| 38 | +} |
| 39 | + |
| 40 | +// isPodReady main function which makes decision if the pod is ready or not. The decision is based on the information |
| 41 | +// from the AA health status file. |
| 42 | +// The logic depends on if the pod is a standard MongoDB or an AppDB one. |
| 43 | +// - If MongoDB: then just the 'statuses[0].IsInGoalState` field is used to learn if the Agent has reached the goal |
| 44 | +// - if AppDB: the 'mmsStatus[0].lastGoalVersionAchieved' field is compared with the one from mounted automation config |
| 45 | +// Additionally if the previous check hasn't returned 'true' the "deadlock" case is checked to make sure the Agent is |
| 46 | +// not waiting for the other members. |
| 47 | +func isPodReady(conf config.Config) bool { |
| 48 | + fd, err := os.Open(conf.HealthStatusFilePath) |
| 49 | + if err != nil { |
| 50 | + logger.Warn("No health status file exists, assuming the Automation agent is old") |
| 51 | + return true |
| 52 | + } |
| 53 | + defer fd.Close() |
| 54 | + |
| 55 | + health, err := readAgentHealthStatus(fd) |
| 56 | + if err != nil { |
| 57 | + logger.Errorf("Failed to read agent health status file: %s", err) |
| 58 | + // panicking allows to see the problem in the events for the pod (kubectl describe pod ..) |
| 59 | + panic("Failed to read agent health status file: %s") |
| 60 | + } |
| 61 | + |
| 62 | + // The 'statuses' file can be empty only for OM Agents |
| 63 | + if len(health.Healthiness) == 0 && !isHeadlessMode() { |
| 64 | + logger.Info("'statuses' is empty. We assume there is no automation config for the agent yet.") |
| 65 | + return true |
| 66 | + } |
| 67 | + |
| 68 | + // If the agent has reached the goal state - returning true |
| 69 | + ok, err := isInGoalState(health, conf) |
| 70 | + |
| 71 | + if err != nil { |
| 72 | + logger.Errorf("There was problem checking the health status: %s", err) |
| 73 | + panic(err) |
| 74 | + } |
| 75 | + |
| 76 | + if ok { |
| 77 | + logger.Info("Agent has reached goal state") |
| 78 | + return true |
| 79 | + } |
| 80 | + |
| 81 | + // Failback logic: the agent is not in goal state and got stuck in some steps |
| 82 | + if hasDeadlockedSteps(health) { |
| 83 | + return true |
| 84 | + } |
| 85 | + |
| 86 | + return false |
| 87 | +} |
| 88 | + |
| 89 | +func readAgentHealthStatus(file *os.File) (health.Status, error) { |
| 90 | + var health health.Status |
| 91 | + |
| 92 | + data, err := ioutil.ReadAll(file) |
| 93 | + if err != nil { |
| 94 | + return health, err |
| 95 | + } |
| 96 | + |
| 97 | + err = json.Unmarshal(data, &health) |
| 98 | + return health, err |
| 99 | +} |
| 100 | + |
| 101 | +// hasDeadlockedSteps returns true if the agent is stuck on waiting for the other agents |
| 102 | +func hasDeadlockedSteps(health health.Status) bool { |
| 103 | + currentStep := findCurrentStep(health.ProcessPlans) |
| 104 | + if currentStep != nil { |
| 105 | + return isDeadlocked(currentStep) |
| 106 | + } |
| 107 | + return false |
| 108 | +} |
| 109 | + |
| 110 | +// findCurrentStep returns the step which seems to be run by the Agent now. The step is always in the last plan |
| 111 | +// (see https://github.com/10gen/ops-manager-kubernetes/pull/401#discussion_r333071555) so we iterate over all the steps |
| 112 | +// there and find the last step which has "Started" non nil |
| 113 | +// (indeed this is not the perfect logic as sometimes the agent doesn't update the 'Started' as well - see |
| 114 | +// 'health-status-ok.json', but seems it works for finding deadlocks still |
| 115 | +//noinspection GoNilness |
| 116 | +func findCurrentStep(processStatuses map[string]health.MmsDirectorStatus) *health.StepStatus { |
| 117 | + var currentPlan *health.PlanStatus |
| 118 | + if len(processStatuses) == 0 { |
| 119 | + // Seems shouldn't happen but let's check anyway - may be needs to be changed to Info if this happens |
| 120 | + logger.Warnf("There is no information about Agent process plans") |
| 121 | + return nil |
| 122 | + } |
| 123 | + if len(processStatuses) > 1 { |
| 124 | + logger.Errorf("Only one process status is expected but got %d!", len(processStatuses)) |
| 125 | + return nil |
| 126 | + } |
| 127 | + // There is always only one process managed by the Agent - so there will be only one loop |
| 128 | + for k, v := range processStatuses { |
| 129 | + if len(v.Plans) == 0 { |
| 130 | + logger.Errorf("The process %s doesn't contain any plans!", k) |
| 131 | + return nil |
| 132 | + } |
| 133 | + currentPlan = v.Plans[len(v.Plans)-1] |
| 134 | + } |
| 135 | + |
| 136 | + if currentPlan.Completed != nil { |
| 137 | + logger.Debugf("The Agent hasn't reported working on the new config yet, the last plan finished at %s", |
| 138 | + currentPlan.Completed.Format(time.RFC3339)) |
| 139 | + return nil |
| 140 | + } |
| 141 | + |
| 142 | + var lastStartedStep *health.StepStatus |
| 143 | + for _, m := range currentPlan.Moves { |
| 144 | + for _, s := range m.Steps { |
| 145 | + if s.Started != nil { |
| 146 | + lastStartedStep = s |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return lastStartedStep |
| 152 | +} |
| 153 | + |
| 154 | +func isDeadlocked(status *health.StepStatus) bool { |
| 155 | + // Some logic behind 15 seconds: the health status file is dumped each 10 seconds so we are sure that if the agent |
| 156 | + // has been in the the step for 10 seconds - this means it is waiting for the other hosts and they are not available |
| 157 | + fifteenSecondsAgo := time.Now().Add(time.Duration(-15) * time.Second) |
| 158 | + if contains.String(riskySteps, status.Step) && status.Completed == nil && status.Started.Before(fifteenSecondsAgo) { |
| 159 | + logger.Infof("Indicated a possible deadlock, status: %s, started at %s but hasn't finished "+ |
| 160 | + "yet. Marking the probe as ready", status.Step, status.Started.Format(time.RFC3339)) |
| 161 | + return true |
| 162 | + } |
| 163 | + return false |
| 164 | +} |
| 165 | + |
| 166 | +func isInGoalState(health health.Status, conf config.Config) (bool, error) { |
| 167 | + if isHeadlessMode() { |
| 168 | + return headless.PerformCheckHeadlessMode(health, conf) |
| 169 | + } |
| 170 | + return performCheckOMMode(health), nil |
| 171 | +} |
| 172 | + |
| 173 | +// performCheckOMMode does a general check if the Agent has reached the goal state - must be called when Agent is in |
| 174 | +// "OM mode" |
| 175 | +func performCheckOMMode(health health.Status) bool { |
| 176 | + for _, v := range health.Healthiness { |
| 177 | + logger.Debug(v) |
| 178 | + if v.IsInGoalState { |
| 179 | + return true |
| 180 | + } |
| 181 | + } |
| 182 | + return false |
| 183 | +} |
| 184 | + |
| 185 | +func isHeadlessMode() bool { |
| 186 | + return os.Getenv(headlessAgent) == "true" |
| 187 | +} |
| 188 | + |
| 189 | +func kubernetesClientset() (kubernetes.Interface, error) { |
| 190 | + config, err := rest.InClusterConfig() |
| 191 | + if err != nil { |
| 192 | + return nil, fmt.Errorf("failed to get in cluster config: %s", err) |
| 193 | + } |
| 194 | + // creates the clientset |
| 195 | + clientset, err := kubernetes.NewForConfig(config) |
| 196 | + if err != nil { |
| 197 | + return nil, fmt.Errorf("failed to build config: %s", err) |
| 198 | + } |
| 199 | + return clientset, nil |
| 200 | +} |
| 201 | +func main() { |
| 202 | + clientSet, err := kubernetesClientset() |
| 203 | + if err != nil { |
| 204 | + panic(err) |
| 205 | + } |
| 206 | + |
| 207 | + config, err := config.BuildFromEnvVariables(clientSet, isHeadlessMode()) |
| 208 | + if err != nil { |
| 209 | + panic(err) |
| 210 | + } |
| 211 | + cfg := zap.NewDevelopmentConfig() |
| 212 | + // In production we log to the file |
| 213 | + cfg.OutputPaths = []string{ |
| 214 | + config.LogFilePath, |
| 215 | + } |
| 216 | + log, err := cfg.Build() |
| 217 | + if err != nil { |
| 218 | + panic(err) |
| 219 | + } |
| 220 | + logger = log.Sugar() |
| 221 | + if !isPodReady(config) { |
| 222 | + os.Exit(1) |
| 223 | + } |
| 224 | +} |
0 commit comments