@@ -287,6 +287,22 @@ func (o *Options) Complete(ctx context.Context, rootDir string) (*CompletedOptio
287287 }
288288 }
289289
290+ // ExternalAddress is the address used e.g. when generating
291+ // kubeconfigs. It defaults to the default interface, usually the
292+ // first non-loopback interface, e.g. 192.168.0.1.
293+ // BindAddress is the address the server binds to, it defaults to
294+ // 0.0.0.0 or ::.
295+ //
296+ // If BindAddress is set to a specific address, e.g. the loopback
297+ // 127.0.0.1 the ExternalAddress is invalid and all URLs generated
298+ // from it will not work.
299+ //
300+ // To prevent this ExternalAddress is set to the value of
301+ // BindAddress if it wasn't set to a specific address.
302+ if o .GenericControlPlane .GenericServerRunOptions .ExternalHost == "" && ! o .GenericControlPlane .SecureServing .BindAddress .IsUnspecified () {
303+ o .GenericControlPlane .GenericServerRunOptions .ExternalHost = o .GenericControlPlane .SecureServing .BindAddress .String ()
304+ }
305+
290306 if o .Extra .ExperimentalBindFreePort {
291307 listener , _ , err := genericapiserveroptions .CreateListener ("tcp" , fmt .Sprintf ("%s:0" , o .GenericControlPlane .SecureServing .BindAddress ), net.ListenConfig {})
292308 if err != nil {
@@ -311,7 +327,34 @@ func (o *Options) Complete(ctx context.Context, rootDir string) (*CompletedOptio
311327 o .GenericControlPlane .ServiceAccountSigningKeyFile = o .Controllers .SAController .ServiceAccountKeyFile
312328 }
313329
314- completedGenericOptions , err := o .GenericControlPlane .Complete (ctx , nil , nil )
330+ // o.GenericControlPlane.Complete creates self-signed certificates
331+ // with the advertise address by default. This can cause spurious
332+ // errors if the server binds on multiple interfaces.
333+ possibleIPs := []net.IP {
334+ o .GenericControlPlane .GenericServerRunOptions .AdvertiseAddress ,
335+ o .GenericControlPlane .SecureServing .BindAddress ,
336+ o .GenericControlPlane .SecureServing .ExternalAddress ,
337+ }
338+ if o .GenericControlPlane .SecureServing .Listener != nil {
339+ host , _ , err := net .SplitHostPort (o .GenericControlPlane .SecureServing .Listener .Addr ().String ())
340+ if err != nil {
341+ return nil , err
342+ }
343+ possibleIPs = append (possibleIPs , net .ParseIP (host ))
344+ }
345+
346+ alternateIPs := []net.IP {}
347+ alternateDNS := []string {}
348+
349+ for _ , ip := range possibleIPs {
350+ if ip == nil || ip .IsUnspecified () {
351+ continue
352+ }
353+ alternateIPs = append (alternateIPs , ip )
354+ alternateDNS = append (alternateDNS , ip .String ())
355+ }
356+
357+ completedGenericOptions , err := o .GenericControlPlane .Complete (ctx , alternateDNS , alternateIPs )
315358 if err != nil {
316359 return nil , err
317360 }
0 commit comments