-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(k8s): make sure Service Endpoints are ready at end of status checks
This step is put in to give the cluster a moment to update its network routing. For example, when a Deployment passes its health check, Kubernetes doesn't instantly route Service traffic to it (particularly if the master is under high load). We need to account for this so that dependant tasks, tests and services can reliably run after the resource status check resolves.
- Loading branch information
Showing
3 changed files
with
82 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (C) 2018 Garden Technologies, Inc. <[email protected]> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import * as Bluebird from "bluebird" | ||
import { flatten } from "lodash" | ||
import { KubeApi } from "../api" | ||
import { KubernetesServerResource } from "../types" | ||
import { TimeoutError } from "../../../exceptions" | ||
import { getPods } from "../util" | ||
import { sleep } from "../../../util/util" | ||
import { LogEntry } from "../../../logger/log-entry" | ||
import { checkPodStatus } from "./pod" | ||
|
||
// There's something strange going on if this takes more than 10 seconds to resolve | ||
const timeout = 10000 | ||
|
||
/** | ||
* Wait until Service Endpoints are correctly routing to the correct Pods. | ||
* Note: This assumes that the Service and Pod/workload statuses have previously been cleared as ready. | ||
*/ | ||
export async function waitForServiceEndpoints( | ||
api: KubeApi, log: LogEntry, namespace: string, resources: KubernetesServerResource[], | ||
) { | ||
const services = resources.filter(r => r.apiVersion === "v1" && r.kind === "Service") | ||
const start = new Date().getTime() | ||
|
||
return Bluebird.map(services, async (service) => { | ||
const selector = service.spec.selector | ||
|
||
if (!selector) { | ||
return | ||
} | ||
|
||
const serviceName = service.metadata.name | ||
const serviceNamespace = service.metadata.namespace || namespace | ||
|
||
const pods = await getPods(api, serviceNamespace, selector) | ||
const readyPodNames = pods | ||
.filter(p => checkPodStatus(p, [p]).state === "ready") | ||
.map(p => p.metadata.name) | ||
|
||
while (true) { | ||
const endpoints = await api.core.readNamespacedEndpoints(serviceName, serviceNamespace) | ||
|
||
const addresses = flatten(endpoints.subsets!.map(subset => subset.addresses || [])) | ||
const routedPods = addresses | ||
.filter(a => a.targetRef!.kind === "Pod" && readyPodNames.includes(a.targetRef!.name!)) | ||
|
||
if (routedPods.length === readyPodNames.length) { | ||
// All endpoints routing nicely! | ||
break | ||
} | ||
|
||
if (new Date().getTime() - start > timeout) { | ||
throw new TimeoutError( | ||
`Timed out waiting for Service '${serviceName}' Endpoints to resolve to correct Pods`, | ||
{ service, pods }, | ||
) | ||
} | ||
|
||
log.setState({ symbol: "warning", msg: `Waiting for Service '${serviceName}' Endpoints to resolve...` }) | ||
await sleep(1000) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters