From e8008707b53d6e86bfd63b30b58399efb9385680 Mon Sep 17 00:00:00 2001 From: David Sauer Date: Wed, 9 Jun 2021 19:57:30 +0200 Subject: [PATCH] use namespace flag, context namespace, and wedding namespace to look for wedding server --- cmd/d8s/main.go | 109 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 83 insertions(+), 26 deletions(-) diff --git a/cmd/d8s/main.go b/cmd/d8s/main.go index 764bafbf..175bc7c0 100644 --- a/cmd/d8s/main.go +++ b/cmd/d8s/main.go @@ -34,8 +34,9 @@ import ( ) const ( - weddingPort = 2376 - staticPol = chunker.Pol(0x3DA3358B4DC173) + weddingPort = 2376 + staticPol = chunker.Pol(0x3DA3358B4DC173) + ErrPodNotExist = NotFoundError("pod could not be found") ) var ( @@ -80,6 +81,12 @@ func main() { } } +type NotFoundError string + +func (e NotFoundError) Error() string { + return string(e) +} + func version(c *cli.Context) error { _, err := os.Stdout.WriteString(fmt.Sprintf("version: %s\ngit commit: %s", gitRef, gitHash)) if err != nil { @@ -98,16 +105,14 @@ func run(c *cli.Context) error { context := c.String("context") namespace := c.String("namespace") - clientset, config, namespace, err := setupKubernetesClient(context, namespace) + clientset, config, contextNamespace, err := setupKubernetesClient(context, namespace) if err != nil { return fmt.Errorf("setup kubernetes client: %v", err) } - pods := clientset.CoreV1().Pods(namespace) - - pod, err := weddingPod(c.Context, pods) + pod, err := findWeddingPod(c.Context, namespace, contextNamespace, clientset) if err != nil { - return fmt.Errorf("list pods: %v", err) + return fmt.Errorf("find wedding pod: %v", err) } localAddr, stopCh := portForward(pod, config) @@ -156,37 +161,89 @@ func setupKubernetesClient(context, namespace string) (*kubernetes.Clientset, *r return clientset, config, namespace, nil } -func weddingPod(ctx context.Context, pods corev1.PodInterface) (*v1.Pod, error) { +func findWeddingPod(ctx context.Context, definedNamespace, contextNamespace string, clientset *kubernetes.Clientset) (*v1.Pod, error) { for i := 0; i < 60; i++ { - labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{"app": "wedding"}} - listOptions := metav1.ListOptions{ - LabelSelector: labels.Set(labelSelector.MatchLabels).String(), - Limit: 100, + if definedNamespace != "" { + pods, err := weddingPodsInNamespace(ctx, clientset.CoreV1().Pods(definedNamespace)) + if err != nil { + return nil, err + } + + pod := filterReady(pods.Items) + + if pod != nil { + return pod, nil + } } - pods, err := pods.List(ctx, listOptions) - if err != nil { - return nil, fmt.Errorf("list pods: %v", err) + + if definedNamespace != contextNamespace { + pods, err := weddingPodsInNamespace(ctx, clientset.CoreV1().Pods(contextNamespace)) + if err != nil { + return nil, err + } + + pod := filterReady(pods.Items) + + if pod != nil { + return pod, nil + } } - PODS: - for _, pod := range pods.Items { - if pod.Status.Phase != v1.PodRunning { - continue + if definedNamespace != "wedding" && contextNamespace != "wedding" { + pods, err := weddingPodsInNamespace(ctx, clientset.CoreV1().Pods("wedding")) + if err != nil { + return nil, err } - for _, conditions := range pod.Status.Conditions { - if conditions.Status != v1.ConditionTrue { - continue PODS - } + pod := filterReady(pods.Items) + + if pod != nil { + return pod, nil } + } + + select { + case <-ctx.Done(): + return nil, ErrPodNotExist + case <-time.After(time.Second): + // continue + } + } + + return nil, ErrPodNotExist +} + +func weddingPodsInNamespace(ctx context.Context, podsAPI corev1.PodInterface) (*v1.PodList, error) { + labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{"app": "wedding"}} + listOptions := metav1.ListOptions{ + LabelSelector: labels.Set(labelSelector.MatchLabels).String(), + Limit: 100, + } + pods, err := podsAPI.List(ctx, listOptions) + if err != nil { + return nil, fmt.Errorf("list pods: %v", err) + } + + return pods, nil +} - return &pod, nil +func filterReady(pods []v1.Pod) *v1.Pod { +PODS: + for _, pod := range pods { + if pod.Status.Phase != v1.PodRunning { + continue } - time.Sleep(time.Second) + for _, conditions := range pod.Status.Conditions { + if conditions.Status != v1.ConditionTrue { + continue PODS + } + } + + return &pod } - return nil, fmt.Errorf("running wedding server not found") + return nil } func portForward(pod *v1.Pod, cfg *rest.Config) (string, chan struct{}) {