Skip to content

Instantly share code, notes, and snippets.

@timstclair
Last active September 9, 2015 20:24
Show Gist options
  • Save timstclair/a4582cd48bc567c8576a to your computer and use it in GitHub Desktop.
Save timstclair/a4582cd48bc567c8576a to your computer and use it in GitHub Desktop.
/*
Copyright 2014 The Kubernetes Authors All rights reserved.
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.
*/
// run periodically probes the container.
func run(pm *probeManager, w *probeWorker) {
container, ok := <-w.targets
probeTicker := time.Tick(w.probePeriod)
// Handle new target container.
newContainer:
if !ok {
// No more targets, shutdown the worker.
if w.probeType == readinessProbe {
pm.readinessCache.remove(container.ID)
} else {
pm.livenessCache.remove(container.ID)
}
return
}
remainingDelay := time.Now().Unix() - (container.Created + w.spec.InitialDelaySeconds)
if remainingDelay > 0 {
if w.probeType == readinessProbe {
// Readiness probes default to Failure during initial delay.
pm.reportReadiness(container.ID, Failure)
}
select {
case container, ok = <-targets:
// Container restarted during initial delay.
goto newContainer
case <-time.After(remainingDelay):
// Carry on.
}
}
for {
select {
case container, ok = <-targets:
// Restart the probe loop with the new target.
goto newContainer
case <-probeTicker:
doProbe(pm, w, container.ID)
}
}
}
// doProbe probes the container once and records the result.
func doProbe(pm *probeManager, w *probeWorker, containerID types.UID) {
status := pm.statusCache.GetPodStatus(w.pod.UID)
if w.probeType == readinessProbe {
// TODO: Move error handling out of prober.
result, _ := pm.prober.probeReadiness(w.pod, status, w.container, containerID)
pm.readinessCache.set(containerID, result)
} else { // probeType == livenessProbe
result, err := pm.prober.probeLiveness(w.pod, status, w.container, containerID)
// If the liveness probe failed, skip reporting (state unchanged).
if err == nil {
prevResult = pm.livenessCache.get(containerID)
pm.livenessCache.set(containerID, result)
if result != prevResult {
// Trigger a (non-blocking) sync if the probe result changed.
select {
case pm.syncChan <- struct{}{}:
default:
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment