Skip to content

Commit f8a252f

Browse files
committed
Add ExternalName support
1 parent 582f3e3 commit f8a252f

File tree

5 files changed

+71
-30
lines changed

5 files changed

+71
-30
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ For pods it waits until the pod is Ready (`k8s.io/kubectl/pkg/util/podutils.IsPo
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 (like above).
24+
For services it will wait until all pods that match the service selector are Ready (like above).
25+
If it is an `ExternalName` service it is always assumed to be ready.
2526

2627
## Example
2728

cmd/wait.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,5 +235,4 @@ func handleEvent[V *corev1.Pod | *corev1.Service | *batchv1.Job](ctx context.Con
235235
processCompletion()
236236
}
237237
}
238-
239238
}

pkg/handlers.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func (w *Waitables) ProcessEventAddService(ctx context.Context, svc *corev1.Serv
3737
}
3838

3939
w.SetServiceChildren(&svc.ObjectMeta, pods.Items)
40+
w.SetServiceExternality(&svc.ObjectMeta, svc.Spec.Type == corev1.ServiceTypeExternalName)
4041
return true, nil
4142
}
4243
return false, nil
@@ -52,6 +53,7 @@ func (w *Waitables) ProcessEventUpdateService(ctx context.Context, svc *corev1.S
5253
}
5354

5455
w.SetServiceChildren(&svc.ObjectMeta, pods.Items)
56+
w.SetServiceExternality(&svc.ObjectMeta, svc.Spec.Type == corev1.ServiceTypeExternalName)
5557
return true, nil
5658
}
5759
return false, nil
@@ -62,6 +64,7 @@ func (w *Waitables) ProcessEventDeleteService(ctx context.Context, svc *corev1.S
6264
//log.Printf("Delete %T %s %s", svc, svc.Namespace, svc.Name)
6365

6466
w.SetServiceChildren(&svc.ObjectMeta, nil)
67+
w.SetServiceExternality(&svc.ObjectMeta, svc.Spec.Type == corev1.ServiceTypeExternalName)
6568
return true, nil
6669
}
6770
return false, nil
@@ -71,11 +74,12 @@ func (w *Waitables) ProcessOldPodEvents(ctx context.Context, pod *corev1.Pod) (b
7174
if val, ok := w.LastPodEvents[pod.UID]; ok {
7275
//log.Printf("Running LastPodEvents for %s/%s of type %v", pod.Namespace, pod.Name, val.EventType)
7376
//defer delete(w.LastPodEvents, pod.UID)
74-
if val.EventType == EventTypeAdd {
77+
switch val.EventType {
78+
case EventTypeAdd:
7579
return w.ProcessEventAddPod(ctx, pod)
76-
} else if val.EventType == EventTypeUpdate {
80+
case EventTypeUpdate:
7781
return w.ProcessEventUpdatePod(ctx, pod)
78-
} else if val.EventType == EventTypeDelete {
82+
case EventTypeDelete:
7983
return w.ProcessEventDeletePod(ctx, pod)
8084
}
8185
}

pkg/items/service.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@ type NamespacedServiceCollection map[string]ServiceCollection
2121
type ServiceCollection map[string]*ServiceItem
2222

2323
type ServiceItem struct {
24-
namespace string
25-
name string
26-
children PodCollection
24+
namespace string
25+
name string
26+
children PodCollection
27+
isExternal bool
2728
}
2829

2930
func Service(ns string, n string) *ServiceItem {
3031
return &ServiceItem{
31-
namespace: ns,
32-
name: n,
33-
children: nil,
32+
namespace: ns,
33+
name: n,
34+
children: nil,
35+
isExternal: false,
3436
}
3537
}
3638

@@ -43,8 +45,24 @@ func (i *ServiceItem) WithChildren(children PodCollection) *ServiceItem {
4345
return i
4446
}
4547

48+
func (i *ServiceItem) WithExternal(isExternal bool) *ServiceItem {
49+
i.isExternal = isExternal
50+
if i.isExternal {
51+
i.children = nil
52+
}
53+
return i
54+
}
55+
56+
func (i *ServiceItem) IsExternal() bool {
57+
return i.isExternal
58+
}
59+
4660
func (i *ServiceItem) IsAvailable() bool {
47-
if i.children == nil || len(i.children) == 0 {
61+
if i.isExternal {
62+
return true
63+
}
64+
65+
if len(i.children) == 0 {
4866
return false
4967
}
5068

@@ -57,7 +75,11 @@ func (i *ServiceItem) IsAvailable() bool {
5775
}
5876

5977
func (i *ServiceItem) IsAtLeastOneAvailable() bool {
60-
if i.children == nil || len(i.children) == 0 {
78+
if i.isExternal {
79+
return true
80+
}
81+
82+
if len(i.children) == 0 {
6183
return false
6284
}
6385

pkg/waitables.go

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ type Waitables struct {
5555
}
5656

5757
func (w *Waitables) AddItem(kind string, namespace string, name string) error {
58-
if kind == "pod" {
58+
switch kind {
59+
case "pod":
5960
w.addPod(namespace, name)
60-
} else if kind == "job" {
61+
case "job":
6162
w.addJob(namespace, name)
62-
} else if kind == "service" {
63+
case "service":
6364
w.addService(namespace, name)
64-
} else {
65+
default:
6566
return fmt.Errorf("unsupported kind '%s'", kind)
6667
}
6768
return nil
@@ -180,21 +181,31 @@ func (w *Waitables) getStatusTreeString() string {
180181
status := "Unavailable"
181182
svcIsAvailable := (!w.onlyOnePerServiceRequired && val.IsAvailable()) || (w.onlyOnePerServiceRequired && val.IsAtLeastOneAvailable())
182183
if svcIsAvailable {
183-
status = "Available"
184+
if val.IsExternal() {
185+
status = "External"
186+
} else {
187+
status = "Available"
188+
}
184189
}
185-
svc_branch := branch.AddMetaBranch(TreeStatusUnknown, fmt.Sprintf("service/%s: %s", n, status))
186-
187-
for podname, pod := range *val.GetChildren() {
188-
status := "NotReady"
189-
meta := TreeStatusNotDone
190-
if pod.IsReady() {
191-
status = "Ready"
192-
meta = TreeStatusDone
193-
} else if w.onlyOnePerServiceRequired && svcIsAvailable {
194-
status = "Ignored"
195-
meta = TreeStatusIgnored
190+
var svc_branch treeprint.Tree
191+
192+
if val.IsExternal() {
193+
svc_branch = branch.AddMetaBranch(TreeStatusDone, fmt.Sprintf("service/%s: %s", n, status))
194+
} else {
195+
svc_branch = branch.AddMetaBranch(TreeStatusUnknown, fmt.Sprintf("service/%s: %s", n, status))
196+
197+
for podname, pod := range *val.GetChildren() {
198+
status := "NotReady"
199+
meta := TreeStatusNotDone
200+
if pod.IsReady() {
201+
status = "Ready"
202+
meta = TreeStatusDone
203+
} else if w.onlyOnePerServiceRequired && svcIsAvailable {
204+
status = "Ignored"
205+
meta = TreeStatusIgnored
206+
}
207+
svc_branch.AddMetaNode(meta, fmt.Sprintf("pod/%s: %s", podname, status))
196208
}
197-
svc_branch.AddMetaNode(meta, fmt.Sprintf("pod/%s: %s", podname, status))
198209
}
199210
}
200211
}
@@ -264,6 +275,10 @@ func (w *Waitables) SetServiceChildren(meta *metav1.ObjectMeta, pods []corev1.Po
264275
w.Services[meta.Namespace][meta.Name].WithChildren(podItems)
265276
}
266277

278+
func (w *Waitables) SetServiceExternality(meta *metav1.ObjectMeta, isExternal bool) {
279+
w.Services[meta.Namespace][meta.Name].WithExternal(isExternal)
280+
}
281+
267282
func (w *Waitables) TotalCount() int {
268283
return w.Services.TotalCount() + w.Pods.TotalCount() + w.Jobs.TotalCount()
269284
}

0 commit comments

Comments
 (0)