@@ -49,6 +49,7 @@ func (p *Pod) GenerateForKube() (*v1.Pod, []v1.ServicePort, error) {
49
49
}
50
50
51
51
extraHost := make ([]v1.HostAlias , 0 )
52
+ hostNetwork := false
52
53
if p .HasInfraContainer () {
53
54
infraContainer , err := p .getInfraContainer ()
54
55
if err != nil {
@@ -69,9 +70,9 @@ func (p *Pod) GenerateForKube() (*v1.Pod, []v1.ServicePort, error) {
69
70
return nil , servicePorts , err
70
71
}
71
72
servicePorts = containerPortsToServicePorts (ports )
72
-
73
+ hostNetwork = p . config . InfraContainer . HostNetwork
73
74
}
74
- pod , err := p .podWithContainers (allContainers , ports )
75
+ pod , err := p .podWithContainers (allContainers , ports , hostNetwork )
75
76
if err != nil {
76
77
return nil , servicePorts , err
77
78
}
@@ -167,13 +168,14 @@ func containersToServicePorts(containers []v1.Container) []v1.ServicePort {
167
168
return sps
168
169
}
169
170
170
- func (p * Pod ) podWithContainers (containers []* Container , ports []v1.ContainerPort ) (* v1.Pod , error ) {
171
+ func (p * Pod ) podWithContainers (containers []* Container , ports []v1.ContainerPort , hostNetwork bool ) (* v1.Pod , error ) {
171
172
deDupPodVolumes := make (map [string ]* v1.Volume )
172
173
first := true
173
174
podContainers := make ([]v1.Container , 0 , len (containers ))
175
+ dnsInfo := v1.PodDNSConfig {}
174
176
for _ , ctr := range containers {
175
177
if ! ctr .IsInfra () {
176
- ctr , volumes , err := containerToV1Container (ctr )
178
+ ctr , volumes , _ , err := containerToV1Container (ctr )
177
179
if err != nil {
178
180
return nil , err
179
181
}
@@ -196,17 +198,33 @@ func (p *Pod) podWithContainers(containers []*Container, ports []v1.ContainerPor
196
198
vol := vol
197
199
deDupPodVolumes [vol .Name ] = & vol
198
200
}
201
+ } else {
202
+ _ , _ , infraDNS , err := containerToV1Container (ctr )
203
+ if err != nil {
204
+ return nil , err
205
+ }
206
+ if infraDNS != nil {
207
+ if servers := infraDNS .Nameservers ; len (servers ) > 0 {
208
+ dnsInfo .Nameservers = servers
209
+ }
210
+ if searches := infraDNS .Searches ; len (searches ) > 0 {
211
+ dnsInfo .Searches = searches
212
+ }
213
+ if options := infraDNS .Options ; len (options ) > 0 {
214
+ dnsInfo .Options = options
215
+ }
216
+ }
199
217
}
200
218
}
201
219
podVolumes := make ([]v1.Volume , 0 , len (deDupPodVolumes ))
202
220
for _ , vol := range deDupPodVolumes {
203
221
podVolumes = append (podVolumes , * vol )
204
222
}
205
223
206
- return addContainersAndVolumesToPodObject (podContainers , podVolumes , p .Name ()), nil
224
+ return addContainersAndVolumesToPodObject (podContainers , podVolumes , p .Name (), & dnsInfo , hostNetwork ), nil
207
225
}
208
226
209
- func addContainersAndVolumesToPodObject (containers []v1.Container , volumes []v1.Volume , podName string ) * v1.Pod {
227
+ func addContainersAndVolumesToPodObject (containers []v1.Container , volumes []v1.Volume , podName string , dnsOptions * v1. PodDNSConfig , hostNetwork bool ) * v1.Pod {
210
228
tm := v12.TypeMeta {
211
229
Kind : "Pod" ,
212
230
APIVersion : "v1" ,
@@ -225,8 +243,12 @@ func addContainersAndVolumesToPodObject(containers []v1.Container, volumes []v1.
225
243
CreationTimestamp : v12 .Now (),
226
244
}
227
245
ps := v1.PodSpec {
228
- Containers : containers ,
229
- Volumes : volumes ,
246
+ Containers : containers ,
247
+ Volumes : volumes ,
248
+ HostNetwork : hostNetwork ,
249
+ }
250
+ if dnsOptions != nil {
251
+ ps .DNSConfig = dnsOptions
230
252
}
231
253
p := v1.Pod {
232
254
TypeMeta : tm ,
@@ -241,75 +263,111 @@ func addContainersAndVolumesToPodObject(containers []v1.Container, volumes []v1.
241
263
func simplePodWithV1Containers (ctrs []* Container ) (* v1.Pod , error ) {
242
264
kubeCtrs := make ([]v1.Container , 0 , len (ctrs ))
243
265
kubeVolumes := make ([]v1.Volume , 0 )
266
+ hostNetwork := true
267
+ podDNS := v1.PodDNSConfig {}
244
268
for _ , ctr := range ctrs {
245
- kubeCtr , kubeVols , err := containerToV1Container (ctr )
269
+ if ! ctr .HostNetwork () {
270
+ hostNetwork = false
271
+ }
272
+ kubeCtr , kubeVols , ctrDNS , err := containerToV1Container (ctr )
246
273
if err != nil {
247
274
return nil , err
248
275
}
249
276
kubeCtrs = append (kubeCtrs , kubeCtr )
250
277
kubeVolumes = append (kubeVolumes , kubeVols ... )
251
- }
252
- return addContainersAndVolumesToPodObject (kubeCtrs , kubeVolumes , strings .ReplaceAll (ctrs [0 ].Name (), "_" , "" )), nil
253
278
279
+ // Combine DNS information in sum'd structure
280
+ if ctrDNS != nil {
281
+ // nameservers
282
+ if servers := ctrDNS .Nameservers ; servers != nil {
283
+ if podDNS .Nameservers == nil {
284
+ podDNS .Nameservers = make ([]string , 0 )
285
+ }
286
+ for _ , s := range servers {
287
+ if ! util .StringInSlice (s , podDNS .Nameservers ) { // only append if it does not exist
288
+ podDNS .Nameservers = append (podDNS .Nameservers , s )
289
+ }
290
+ }
291
+ }
292
+ // search domains
293
+ if domains := ctrDNS .Searches ; domains != nil {
294
+ if podDNS .Searches == nil {
295
+ podDNS .Searches = make ([]string , 0 )
296
+ }
297
+ for _ , d := range domains {
298
+ if ! util .StringInSlice (d , podDNS .Searches ) { // only append if it does not exist
299
+ podDNS .Searches = append (podDNS .Searches , d )
300
+ }
301
+ }
302
+ }
303
+ // dns options
304
+ if options := ctrDNS .Options ; options != nil {
305
+ if podDNS .Options == nil {
306
+ podDNS .Options = make ([]v1.PodDNSConfigOption , 0 )
307
+ }
308
+ podDNS .Options = append (podDNS .Options , options ... )
309
+ }
310
+ } // end if ctrDNS
311
+ }
312
+ return addContainersAndVolumesToPodObject (kubeCtrs , kubeVolumes , strings .ReplaceAll (ctrs [0 ].Name (), "_" , "" ), & podDNS , hostNetwork ), nil
254
313
}
255
314
256
315
// containerToV1Container converts information we know about a libpod container
257
316
// to a V1.Container specification.
258
- func containerToV1Container (c * Container ) (v1.Container , []v1.Volume , error ) {
317
+ func containerToV1Container (c * Container ) (v1.Container , []v1.Volume , * v1. PodDNSConfig , error ) {
259
318
kubeContainer := v1.Container {}
260
319
kubeVolumes := []v1.Volume {}
261
320
kubeSec , err := generateKubeSecurityContext (c )
262
321
if err != nil {
263
- return kubeContainer , kubeVolumes , err
322
+ return kubeContainer , kubeVolumes , nil , err
264
323
}
265
324
266
325
if len (c .config .Spec .Linux .Devices ) > 0 {
267
326
// TODO Enable when we can support devices and their names
268
327
kubeContainer .VolumeDevices = generateKubeVolumeDeviceFromLinuxDevice (c .Spec ().Linux .Devices )
269
- return kubeContainer , kubeVolumes , errors .Wrapf (define .ErrNotImplemented , "linux devices" )
328
+ return kubeContainer , kubeVolumes , nil , errors .Wrapf (define .ErrNotImplemented , "linux devices" )
270
329
}
271
330
272
331
if len (c .config .UserVolumes ) > 0 {
273
332
// TODO When we until we can resolve what the volume name should be, this is disabled
274
333
// Volume names need to be coordinated "globally" in the kube files.
275
334
volumeMounts , volumes , err := libpodMountsToKubeVolumeMounts (c )
276
335
if err != nil {
277
- return kubeContainer , kubeVolumes , err
336
+ return kubeContainer , kubeVolumes , nil , err
278
337
}
279
338
kubeContainer .VolumeMounts = volumeMounts
280
339
kubeVolumes = append (kubeVolumes , volumes ... )
281
340
}
282
341
283
342
envVariables , err := libpodEnvVarsToKubeEnvVars (c .config .Spec .Process .Env )
284
343
if err != nil {
285
- return kubeContainer , kubeVolumes , err
344
+ return kubeContainer , kubeVolumes , nil , err
286
345
}
287
346
288
347
portmappings , err := c .PortMappings ()
289
348
if err != nil {
290
- return kubeContainer , kubeVolumes , err
349
+ return kubeContainer , kubeVolumes , nil , err
291
350
}
292
351
ports , err := ocicniPortMappingToContainerPort (portmappings )
293
352
if err != nil {
294
- return kubeContainer , kubeVolumes , err
353
+ return kubeContainer , kubeVolumes , nil , err
295
354
}
296
355
297
- containerCommands := c .Command ()
298
- kubeContainer .Name = removeUnderscores (c .Name ())
356
+ // Handle command and arguments.
357
+ if ep := c .Entrypoint (); len (ep ) > 0 {
358
+ // If we have an entrypoint, set the container's command as
359
+ // arguments.
360
+ kubeContainer .Command = ep
361
+ kubeContainer .Args = c .Command ()
362
+ } else {
363
+ kubeContainer .Command = c .Command ()
364
+ }
299
365
366
+ kubeContainer .Name = removeUnderscores (c .Name ())
300
367
_ , image := c .Image ()
301
368
kubeContainer .Image = image
302
369
kubeContainer .Stdin = c .Stdin ()
303
370
304
- // prepend the entrypoint of the container to command
305
- if ep := c .Entrypoint (); len (c .Entrypoint ()) > 0 {
306
- ep = append (ep , containerCommands ... )
307
- containerCommands = ep
308
- }
309
- kubeContainer .Command = containerCommands
310
- // TODO need to figure out how we handle command vs entry point. Kube appears to prefer entrypoint.
311
- // right now we just take the container's command
312
- //container.Args = args
313
371
kubeContainer .WorkingDir = c .WorkingDir ()
314
372
kubeContainer .Ports = ports
315
373
// This should not be applicable
@@ -355,7 +413,38 @@ func containerToV1Container(c *Container) (v1.Container, []v1.Volume, error) {
355
413
}
356
414
}
357
415
358
- return kubeContainer , kubeVolumes , nil
416
+ // Obtain the DNS entries from the container
417
+ dns := v1.PodDNSConfig {}
418
+
419
+ // DNS servers
420
+ if servers := c .config .DNSServer ; len (servers ) > 0 {
421
+ dnsServers := make ([]string , 0 )
422
+ for _ , server := range servers {
423
+ dnsServers = append (dnsServers , server .String ())
424
+ }
425
+ dns .Nameservers = dnsServers
426
+ }
427
+
428
+ // DNS search domains
429
+ if searches := c .config .DNSSearch ; len (searches ) > 0 {
430
+ dns .Searches = searches
431
+ }
432
+
433
+ // DNS options
434
+ if options := c .config .DNSOption ; len (options ) > 0 {
435
+ dnsOptions := make ([]v1.PodDNSConfigOption , 0 )
436
+ for _ , option := range options {
437
+ // the option can be "k:v" or just "k", no delimiter is required
438
+ opts := strings .SplitN (option , ":" , 2 )
439
+ dnsOpt := v1.PodDNSConfigOption {
440
+ Name : opts [0 ],
441
+ Value : & opts [1 ],
442
+ }
443
+ dnsOptions = append (dnsOptions , dnsOpt )
444
+ }
445
+ dns .Options = dnsOptions
446
+ }
447
+ return kubeContainer , kubeVolumes , & dns , nil
359
448
}
360
449
361
450
// ocicniPortMappingToContainerPort takes an ocicni portmapping and converts
0 commit comments