@@ -135,6 +135,58 @@ func (m *Minikube) waitForDefaultServiceAccount() error {
135135 }
136136}
137137
138+ func (m * Minikube ) waitForDNSReady (timeout time.Duration ) error {
139+ ctx , cancel := context .WithTimeout (context .Background (), timeout )
140+ defer cancel ()
141+
142+ start := time .Now ()
143+
144+ // First, wait for CoreDNS pods to be Running
145+ pods , err := m .WaitForDeploymentPhase (
146+ & metav1.ObjectMeta {Name : "coredns" , Namespace : "kube-system" },
147+ corev1 .PodRunning ,
148+ "k8s-app=kube-dns" ,
149+ timeout ,
150+ )
151+ if err != nil {
152+ return fmt .Errorf ("coredns pods not running: %w" , err )
153+ }
154+
155+ // Then wait for CoreDNS pods to be Ready (readiness probe passed)
156+ remaining := timeout - time .Since (start )
157+ if remaining <= 0 {
158+ remaining = 10 * time .Second
159+ }
160+ for _ , pod := range pods {
161+ if err := m .WaitForPodReady (pod .GetObjectMeta (), remaining ); err != nil {
162+ return fmt .Errorf ("coredns pod %s not ready: %w" , pod .Name , err )
163+ }
164+ }
165+
166+ // Finally, wait for kube-dns service to have ready endpoints.
167+ // This ensures kube-proxy has updated iptables rules to route DNS traffic.
168+ for {
169+ select {
170+ case <- ctx .Done ():
171+ return fmt .Errorf ("kube-dns endpoints not ready within %s" , timeout )
172+ case <- time .After (500 * time .Millisecond ):
173+ endpoints , epErr := m .GetClient ().CoreV1 ().Endpoints ("kube-system" ).Get (context .Background (), "kube-dns" , metav1.GetOptions {})
174+ if epErr != nil {
175+ continue
176+ }
177+ for _ , subset := range endpoints .Subsets {
178+ if len (subset .Addresses ) > 0 {
179+ log .Info ().
180+ Int ("endpoints" , len (subset .Addresses )).
181+ TimeDiff ("duration" , time .Now (), start ).
182+ Msg ("kube-dns is ready" )
183+ return nil
184+ }
185+ }
186+ }
187+ }
188+ }
189+
138190func (m * Minikube ) delete () error {
139191 globalMinikubeMutex .Lock ()
140192 defer globalMinikubeMutex .Unlock ()
@@ -263,8 +315,8 @@ func WithMinikube(t *testing.T, mOpts MinikubeOpts, extFactory ExtensionFactory,
263315 }
264316 }
265317
266- if _ , dnsErr := minikube .WaitForDeploymentPhase ( & metav1. ObjectMeta { Name : "coredns" , Namespace : "kube-system" }, corev1 . PodRunning , "k8s-app=kube-dns" , 1 * time .Minute ); dnsErr != nil {
267- log . Warn (). Err ( dnsErr ). Msg ( "coredns not started withing 1 minute." )
318+ if dnsErr := minikube .waitForDNSReady ( 2 * time .Minute ); dnsErr != nil {
319+ t . Fatal ( "DNS not ready:" , dnsErr )
268320 }
269321
270322 wg .Wait ()
0 commit comments