|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "net" |
| 10 | + "os" |
| 11 | + "os/signal" |
| 12 | + "syscall" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/microsoft/tyger/cli/internal/cmd" |
| 16 | + "github.com/rs/zerolog" |
| 17 | + "github.com/rs/zerolog/log" |
| 18 | + "github.com/spf13/cobra" |
| 19 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 20 | + "k8s.io/apimachinery/pkg/fields" |
| 21 | + "k8s.io/client-go/kubernetes" |
| 22 | + "k8s.io/client-go/rest" |
| 23 | +) |
| 24 | + |
| 25 | +var ( |
| 26 | + // set during build |
| 27 | + version = "" |
| 28 | +) |
| 29 | + |
| 30 | +func main() { |
| 31 | + rootCmd := newRootCommand() |
| 32 | + if err := rootCmd.Execute(); err != nil { |
| 33 | + os.Exit(1) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func newRootCommand() *cobra.Command { |
| 38 | + rootCommand := cmd.NewCommonRootCommand(version) |
| 39 | + rootCommand.Use = "worker-waiter" |
| 40 | + rootCommand.Short = "Waits for worker pods to be ready and DNS to resolve" |
| 41 | + |
| 42 | + var labelSelector string |
| 43 | + var hostnames []string |
| 44 | + var namespace string |
| 45 | + var pollInterval time.Duration |
| 46 | + |
| 47 | + rootCommand.Flags().StringVar(&labelSelector, "label-selector", "", "The label selector for worker pods (e.g., tyger-worker=123)") |
| 48 | + rootCommand.Flags().StringSliceVar(&hostnames, "hostname", nil, "Hostnames to wait for DNS resolution (can be specified multiple times)") |
| 49 | + rootCommand.Flags().StringVar(&namespace, "namespace", "", "The Kubernetes namespace to watch pods in") |
| 50 | + rootCommand.Flags().DurationVar(&pollInterval, "poll-interval", 1*time.Second, "Interval between DNS resolution attempts") |
| 51 | + |
| 52 | + rootCommand.MarkFlagRequired("label-selector") |
| 53 | + rootCommand.MarkFlagRequired("namespace") |
| 54 | + |
| 55 | + rootCommand.RunE = func(cmd *cobra.Command, args []string) error { |
| 56 | + ctx, cancel := signal.NotifyContext(cmd.Context(), syscall.SIGINT, syscall.SIGTERM) |
| 57 | + defer cancel() |
| 58 | + |
| 59 | + return waitForWorkers(ctx, namespace, labelSelector, hostnames, pollInterval) |
| 60 | + } |
| 61 | + |
| 62 | + return rootCommand |
| 63 | +} |
| 64 | + |
| 65 | +func waitForWorkers(ctx context.Context, namespace, labelSelector string, hostnames []string, pollInterval time.Duration) error { |
| 66 | + log.Info(). |
| 67 | + Str("namespace", namespace). |
| 68 | + Str("labelSelector", labelSelector). |
| 69 | + Strs("hostnames", hostnames). |
| 70 | + Msg("Starting worker waiter") |
| 71 | + |
| 72 | + // Wait for pods to be ready |
| 73 | + if err := waitForPodsReady(ctx, namespace, labelSelector, len(hostnames), pollInterval); err != nil { |
| 74 | + return fmt.Errorf("failed waiting for pods: %w", err) |
| 75 | + } |
| 76 | + |
| 77 | + // Wait for DNS resolution |
| 78 | + for _, hostname := range hostnames { |
| 79 | + if err := waitForDNS(ctx, hostname, pollInterval); err != nil { |
| 80 | + return fmt.Errorf("failed waiting for DNS resolution of %s: %w", hostname, err) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + log.Info().Msg("All workers ready and DNS resolved") |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func waitForPodsReady(ctx context.Context, namespace, labelSelector string, expectedCount int, pollInterval time.Duration) error { |
| 89 | + config, err := rest.InClusterConfig() |
| 90 | + if err != nil { |
| 91 | + return fmt.Errorf("failed to get in-cluster config: %w", err) |
| 92 | + } |
| 93 | + |
| 94 | + clientset, err := kubernetes.NewForConfig(config) |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("failed to create kubernetes client: %w", err) |
| 97 | + } |
| 98 | + |
| 99 | + for { |
| 100 | + if err := ctx.Err(); err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + pods, err := clientset.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{ |
| 105 | + LabelSelector: labelSelector, |
| 106 | + FieldSelector: fields.Everything().String(), |
| 107 | + }) |
| 108 | + if err != nil { |
| 109 | + log.Warn().Err(err).Msg("Error listing pods, retrying...") |
| 110 | + time.Sleep(pollInterval) |
| 111 | + continue |
| 112 | + } |
| 113 | + |
| 114 | + if len(pods.Items) < expectedCount { |
| 115 | + log.Info(). |
| 116 | + Int("found", len(pods.Items)). |
| 117 | + Int("expected", expectedCount). |
| 118 | + Msg("Waiting for worker pods to be created...") |
| 119 | + time.Sleep(pollInterval) |
| 120 | + continue |
| 121 | + } |
| 122 | + |
| 123 | + readyCount := 0 |
| 124 | + for _, pod := range pods.Items { |
| 125 | + for _, condition := range pod.Status.Conditions { |
| 126 | + if condition.Type == "Ready" && condition.Status == "True" { |
| 127 | + readyCount++ |
| 128 | + break |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if readyCount >= expectedCount { |
| 134 | + log.Info().Int("count", readyCount).Msg("All worker pods are ready") |
| 135 | + return nil |
| 136 | + } |
| 137 | + |
| 138 | + log.Info(). |
| 139 | + Int("ready", readyCount). |
| 140 | + Int("expected", expectedCount). |
| 141 | + Msg("Waiting for worker pods to be ready") |
| 142 | + time.Sleep(pollInterval) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func waitForDNS(ctx context.Context, hostname string, pollInterval time.Duration) error { |
| 147 | + log.Info().Str("hostname", hostname).Msg("Waiting for hostname to resolve") |
| 148 | + |
| 149 | + resolver := &net.Resolver{} |
| 150 | + |
| 151 | + for { |
| 152 | + if err := ctx.Err(); err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + |
| 156 | + addrs, err := resolver.LookupHost(ctx, hostname) |
| 157 | + if err == nil && len(addrs) > 0 { |
| 158 | + log.Info(). |
| 159 | + Str("hostname", hostname). |
| 160 | + Strs("addresses", addrs). |
| 161 | + Msg("Hostname resolved successfully") |
| 162 | + return nil |
| 163 | + } |
| 164 | + |
| 165 | + if err != nil { |
| 166 | + if log.Logger.GetLevel() <= zerolog.DebugLevel { |
| 167 | + log.Debug().Err(err).Str("hostname", hostname).Msg("DNS lookup failed, retrying...") |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + time.Sleep(pollInterval) |
| 172 | + } |
| 173 | +} |
0 commit comments