-
-
Save lblackstone/fe58ad48eb9ce48bb96000af8c6e0791 to your computer and use it in GitHub Desktop.
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
[0/3] Waiting for Pod "foo-4setj4y6" to be scheduled | |
[1/3] Waiting for Pod "foo-4setj4y6" to be initialized | |
[2/3] Waiting for Pod "foo-4setj4y6" to be ready -- containers with unready status: [nginx] | |
[2/3] Waiting for Pod "foo-4setj4y6" to be ready -- containers with unready status: [nginx] | |
[2/3] Waiting for Pod "foo-4setj4y6" to be ready -- containers with unready status: [nginx] | |
✅ Pod ready |
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
type Condition func(s interface{}) (bool, string) | |
type PodState struct { | |
Pod corev1.Pod | |
conditions []Condition | |
} | |
func (s *PodState) Status() string { | |
for i, condition := range s.conditions { | |
done, msg := condition(&s.Pod) | |
if !done { | |
return fmt.Sprintf("[%d/%d] %s\n", i, len(s.conditions), msg) | |
} | |
} | |
return fmt.Sprint("✅ Pod ready") | |
} | |
func NewPodState(conditions ...Condition) *PodState { | |
return &PodState{conditions: conditions} | |
} | |
func podScheduled(obj interface{}) (bool, string) { | |
pod := obj.(*corev1.Pod) | |
defaultMsg := fmt.Sprintf("Waiting for Pod %q to be scheduled", fqName(pod)) | |
for _, condition := range pod.Status.Conditions { | |
if condition.Type == corev1.PodScheduled { | |
if condition.Status == corev1.ConditionTrue { | |
return true, "" | |
} | |
return false, errFromCondition(condition, defaultMsg) | |
} | |
} | |
return false, defaultMsg | |
} | |
func podInitialized(obj interface{}) (bool, string) { | |
pod := obj.(*corev1.Pod) | |
defaultMsg := fmt.Sprintf("Waiting for Pod %q to be initialized", fqName(pod)) | |
for _, condition := range pod.Status.Conditions { | |
if condition.Type == corev1.PodInitialized { | |
if condition.Status == corev1.ConditionTrue { | |
return true, "" | |
} | |
return false, errFromCondition(condition, defaultMsg) | |
} | |
} | |
return false, defaultMsg | |
} | |
func podReady(obj interface{}) (bool, string) { | |
pod := obj.(*corev1.Pod) | |
defaultMsg := fmt.Sprintf("Waiting for Pod %q to be ready", fqName(pod)) | |
for _, condition := range pod.Status.Conditions { | |
if condition.Type == corev1.PodReady { | |
if condition.Status == corev1.ConditionTrue { | |
return true, "" | |
} | |
return false, errFromCondition(condition, defaultMsg) | |
} | |
} | |
return false, defaultMsg | |
} | |
// ... Snip ... | |
func (pia *podInitAwaiter) processPodEvent(event watch.Event) { | |
pod, isUnstructured := event.Object.(*unstructured.Unstructured) | |
if !isUnstructured { | |
glog.V(3).Infof("Pod watch received unknown object type %q", | |
reflect.TypeOf(pod)) | |
return | |
} | |
// Do nothing if this is not the pod we're waiting for. | |
if pod.GetName() != pia.config.currentInputs.GetName() { | |
return | |
} | |
// pia.state created with `NewPodState(podScheduled, podInitialized, podReady)` | |
p, _ := clients.PodFromUnstructured(pod) | |
pia.state.Pod = *p | |
output := pia.state.Status() | |
fmt.Println(output) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment