Skip to content

Commit 0622106

Browse files
committed
Make pods not require IsPodAvailable since that will break when a pod just started (it won't generate a second event)
1 parent 1f5875f commit 0622106

File tree

7 files changed

+91
-40
lines changed

7 files changed

+91
-40
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ It accepts arguments in the following formats:
1717
- `pod,pod-name` using the namespace from the `--namespace`, `-n` flag or `default`
1818
- `pod-name` using the namespace from the `--namespace`, `-n` flag or `default` and the kind `pod`
1919

20-
For pods it waits until the pod is Ready (`k8s.io/kubectl/pkg/util/podutils.IsPodReady`) and Available (`k8s.io/kubectl/pkg/util/podutils.IsPodAvailable`).
20+
For pods it waits until the pod is Ready (`k8s.io/kubectl/pkg/util/podutils.IsPodReady`).
2121

2222
For jobs it wait until the `Completed` condition is true.
2323

24-
For services it will wait until all pods that match the service selector are Ready and Available (like above).
24+
For services it will wait until all pods that match the service selector are Ready (like above).
2525

2626
## Example
2727

cmd/wait.go

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func wait(cmd *cobra.Command, args []string) error {
9292

9393
opts := cache.Options{
9494
Namespaces: namespaces,
95+
SyncPeriod: WaitForConfigFlags.SyncPeriod,
9596
}
9697

9798
conf, err := KubernetesConfigFlags.ToRESTConfig()

flags/flags.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ type ConfigFlags struct {
2929
PrintTree *bool
3030
PrintCollapsedTree *bool
3131

32-
Timeout *time.Duration
32+
Timeout *time.Duration
33+
SyncPeriod *time.Duration
3334
}
3435

3536
func NewConfigFlags() *ConfigFlags {
@@ -38,7 +39,8 @@ func NewConfigFlags() *ConfigFlags {
3839
PrintTree: utilpointer.Bool(true),
3940
PrintCollapsedTree: utilpointer.Bool(true),
4041

41-
Timeout: utilpointer.Duration(time.Duration(600 * time.Second)),
42+
Timeout: utilpointer.Duration(time.Duration(600 * time.Second)),
43+
SyncPeriod: utilpointer.Duration(time.Duration(90 * time.Second)),
4244
}
4345
}
4446

@@ -47,6 +49,10 @@ func (f *ConfigFlags) AddFlags(flags *pflag.FlagSet) {
4749
flags.DurationVarP(f.Timeout, "timeout", "t", *f.Timeout, "The length of time to wait before ending watch, zero means never. Any other values should contain a corresponding time unit (e.g. 1s, 2m, 3h)")
4850
}
4951

52+
if f.Timeout != nil {
53+
flags.DurationVar(f.SyncPeriod, "sync-period", *f.SyncPeriod, "The length of time to pass to the cache to initiate a sync. (e.g. 1s, 2m, 3h)")
54+
}
55+
5056
if f.PrintVersion != nil {
5157
flags.BoolVarP(f.PrintVersion, "version", "v", *f.PrintVersion, "Display version info")
5258
}

pkg/handlers.go

+38-28
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ func (w *Waitables) ProcessEventDeleteService(ctx context.Context, svc *corev1.S
6868
}
6969

7070
func (w *Waitables) ProcessOldPodEvents(ctx context.Context, pod *corev1.Pod) (bool, error) {
71-
if val, ok := w.UnprocessablePodEvents[pod.UID]; ok {
72-
//log.Printf("Running UnprocessablePodEvents for %s/%s of type %v", pod.Namespace, pod.Name, val.EventType)
73-
//defer delete(w.UnprocessablePodEvents, pod.UID)
71+
if val, ok := w.LastPodEvents[pod.UID]; ok {
72+
//log.Printf("Running LastPodEvents for %s/%s of type %v", pod.Namespace, pod.Name, val.EventType)
73+
//defer delete(w.LastPodEvents, pod.UID)
7474
if val.EventType == EventTypeAdd {
7575
return w.ProcessEventAddPod(ctx, pod)
7676
} else if val.EventType == EventTypeUpdate {
@@ -83,54 +83,64 @@ func (w *Waitables) ProcessOldPodEvents(ctx context.Context, pod *corev1.Pod) (b
8383
}
8484

8585
func (w *Waitables) ProcessEventAddPod(ctx context.Context, pod *corev1.Pod) (bool, error) {
86-
processed := false
87-
if w.HasPod(pod.ObjectMeta) {
88-
//log.Printf("Add %T %s %s", pod, pod.Namespace, pod.Name)
86+
// if w.HasPod(pod.ObjectMeta) {
87+
// log.Printf("Add %T %s %s", pod, pod.Namespace, pod.Name)
88+
// }
89+
90+
if w.HasPodDirect(pod.ObjectMeta) {
8991
w.SetPodReadyFromPod(pod)
90-
processed = true
9192
}
93+
9294
if podItems, ok := w.Services.GetPods(pod); ok {
9395
for _, podItem := range podItems {
9496
podItem.WithReadyFromPod(pod)
9597
}
96-
processed = true
97-
}
98-
if processed {
99-
return true, nil
100-
} else {
101-
w.UnprocessablePodEvents[pod.UID] = Event{EventType: EventTypeAdd, Pod: pod}
10298
}
103-
return false, nil
99+
100+
w.LastPodEvents[pod.UID] = Event{EventType: EventTypeAdd, Pod: pod}
101+
102+
return w.HasPod(pod.ObjectMeta), nil
104103
}
105104

106105
func (w *Waitables) ProcessEventUpdatePod(ctx context.Context, pod *corev1.Pod) (bool, error) {
107-
if w.HasPod(pod.ObjectMeta) {
108-
//log.Printf("Update %T %s %s", pod, pod.Namespace, pod.Name)
106+
// if w.HasPod(pod.ObjectMeta) {
107+
// log.Printf("Update %T %s %s", pod, pod.Namespace, pod.Name)
108+
// }
109+
110+
if w.HasPodDirect(pod.ObjectMeta) {
109111
w.SetPodReadyFromPod(pod)
110-
return true, nil
111-
} else if podItems, ok := w.Services.GetPods(pod); ok {
112+
}
113+
114+
if podItems, ok := w.Services.GetPods(pod); ok {
112115
for _, podItem := range podItems {
113116
podItem.WithReadyFromPod(pod)
114117
}
115-
} else {
116-
w.UnprocessablePodEvents[pod.UID] = Event{EventType: EventTypeUpdate, Pod: pod}
117118
}
118-
return false, nil
119+
120+
w.LastPodEvents[pod.UID] = Event{EventType: EventTypeUpdate, Pod: pod}
121+
122+
return w.HasPod(pod.ObjectMeta), nil
119123
}
120124

121125
func (w *Waitables) ProcessEventDeletePod(ctx context.Context, pod *corev1.Pod) (bool, error) {
122-
if w.HasPod(pod.ObjectMeta) {
123-
//log.Printf("Delete %T %s %s", pod, pod.Namespace, pod.Name)
126+
// if w.HasPod(pod.ObjectMeta) {
127+
// log.Printf("Delete %T %s %s", pod, pod.Namespace, pod.Name)
128+
// }
129+
130+
if w.HasPodDirect(pod.ObjectMeta) {
124131
w.UnsetPodReady(pod)
125-
return true, nil
126-
} else if podItems, ok := w.Services.GetPods(pod); ok {
132+
}
133+
134+
if podItems, ok := w.Services.GetPods(pod); ok {
127135
for _, podItem := range podItems {
128136
podItem.WithReady(false)
129137
}
130-
} else {
131-
w.UnprocessablePodEvents[pod.UID] = Event{EventType: EventTypeDelete, Pod: pod}
138+
w.Services.DeletePod(&pod.ObjectMeta)
132139
}
133-
return false, nil
140+
141+
w.LastPodEvents[pod.UID] = Event{EventType: EventTypeDelete, Pod: pod}
142+
143+
return w.HasPod(pod.ObjectMeta), nil
134144
}
135145

136146
func (w *Waitables) ProcessEventAddJob(ctx context.Context, job *batchv1.Job) (bool, error) {

pkg/items/pod.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"k8s.io/kubectl/pkg/util/podutils"
2121

2222
corev1 "k8s.io/api/core/v1"
23-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2423
)
2524

2625
type NamespacedPodCollection map[string]PodCollection
@@ -47,7 +46,7 @@ func (i *PodItem) WithReady(ready bool) *PodItem {
4746
}
4847

4948
func (i *PodItem) WithReadyFromPod(pod *corev1.Pod) *PodItem {
50-
i.ready = podutils.IsPodReady(pod) && podutils.IsPodAvailable(pod, 2, metav1.Now())
49+
i.ready = podutils.IsPodReady(pod)
5150
return i
5251
}
5352

pkg/items/service.go

+31
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,26 @@ func (i *ServiceItem) GetPod(pod ItemInterface) (*PodItem, bool) {
7272
return val, ok
7373
}
7474

75+
func (c ServiceItem) DeletePod(i ItemInterface) {
76+
if c.namespace == i.GetNamespace() {
77+
delete(c.children, i.GetName())
78+
}
79+
}
80+
81+
func (c ServiceCollection) DeletePod(i ItemInterface) {
82+
for _, svc := range c {
83+
svc.DeletePod(i)
84+
}
85+
}
86+
87+
func (c NamespacedServiceCollection) DeletePod(i ItemInterface) {
88+
for ns, nssvcs := range c {
89+
if ns == i.GetNamespace() {
90+
nssvcs.DeletePod(i)
91+
}
92+
}
93+
}
94+
7595
func (c NamespacedServiceCollection) EnsureNamespace(ns string) {
7696
if _, ok := c[ns]; !ok {
7797
c[ns] = ServiceCollection{}
@@ -83,6 +103,17 @@ func (c NamespacedServiceCollection) Contains(i ItemInterface) bool {
83103
return ok
84104
}
85105

106+
func (c NamespacedServiceCollection) ContainsPod(i ItemInterface) bool {
107+
for _, items := range c {
108+
for _, item := range items {
109+
if item.children.Contains(i) {
110+
return true
111+
}
112+
}
113+
}
114+
return false
115+
}
116+
86117
func (c NamespacedServiceCollection) GetPods(i ItemInterface) ([]*PodItem, bool) {
87118

88119
pods := []*PodItem{}

pkg/waitables.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type Waitables struct {
4545
tickerDone chan bool
4646
tickerFinished chan bool
4747

48-
UnprocessablePodEvents map[types.UID]Event
48+
LastPodEvents map[types.UID]Event
4949

5050
Services items.NamespacedServiceCollection
5151
Pods items.NamespacedPodCollection
@@ -89,10 +89,14 @@ func (w *Waitables) addJob(namespace string, name string) *items.JobItem {
8989
return w.Jobs[namespace][name]
9090
}
9191

92-
func (w *Waitables) HasPod(meta metav1.ObjectMeta) bool {
92+
func (w *Waitables) HasPodDirect(meta metav1.ObjectMeta) bool {
9393
return w.Pods.Contains(&meta)
9494
}
9595

96+
func (w *Waitables) HasPod(meta metav1.ObjectMeta) bool {
97+
return w.HasPodDirect(meta) || w.Services.ContainsPod(&meta)
98+
}
99+
96100
func (w *Waitables) HasService(meta metav1.ObjectMeta) bool {
97101
return w.Services.Contains(&meta)
98102
}
@@ -319,10 +323,10 @@ func (w *Waitables) WithCache(c cache.Cache) *Waitables {
319323

320324
func NewWaitables(c *flags.ConfigFlags) *Waitables {
321325
w := &Waitables{
322-
UnprocessablePodEvents: map[types.UID]Event{},
323-
Services: items.NamespacedServiceCollection{},
324-
Pods: items.NamespacedPodCollection{},
325-
Jobs: items.NamespacedJobCollection{},
326+
LastPodEvents: map[types.UID]Event{},
327+
Services: items.NamespacedServiceCollection{},
328+
Pods: items.NamespacedPodCollection{},
329+
Jobs: items.NamespacedJobCollection{},
326330

327331
ticker: time.NewTicker(250 * time.Millisecond),
328332
queuedPrints: 0,

0 commit comments

Comments
 (0)