@@ -141,6 +141,7 @@ var (
141
141
KeyRemapUIDSize : true ,
142
142
KeyNetwork : true ,
143
143
KeyConfigMap : true ,
144
+ KeyPublishPort : true ,
144
145
}
145
146
)
146
147
@@ -454,63 +455,8 @@ func ConvertContainer(container *parser.UnitFile, isUser bool) (*parser.UnitFile
454
455
podman .addf ("--expose=%s" , exposedPort )
455
456
}
456
457
457
- publishPorts := container .LookupAll (ContainerGroup , KeyPublishPort )
458
- for _ , publishPort := range publishPorts {
459
- publishPort = strings .TrimSpace (publishPort ) // Allow whitespace after
460
-
461
- // IP address could have colons in it. For example: "[::]:8080:80/tcp, so use custom splitter
462
- parts := splitPorts (publishPort )
463
-
464
- var containerPort string
465
- ip := ""
466
- hostPort := ""
467
-
468
- // format (from podman run):
469
- // ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort
470
- //
471
- // ip could be IPv6 with minimum of these chars "[::]"
472
- // containerPort can have a suffix of "/tcp" or "/udp"
473
- //
474
-
475
- switch len (parts ) {
476
- case 1 :
477
- containerPort = parts [0 ]
478
-
479
- case 2 :
480
- hostPort = parts [0 ]
481
- containerPort = parts [1 ]
482
-
483
- case 3 :
484
- ip = parts [0 ]
485
- hostPort = parts [1 ]
486
- containerPort = parts [2 ]
487
-
488
- default :
489
- return nil , fmt .Errorf ("invalid published port '%s'" , publishPort )
490
- }
491
-
492
- if ip == "0.0.0.0" {
493
- ip = ""
494
- }
495
-
496
- if len (hostPort ) > 0 && ! isPortRange (hostPort ) {
497
- return nil , fmt .Errorf ("invalid port format '%s'" , hostPort )
498
- }
499
-
500
- if len (containerPort ) > 0 && ! isPortRange (containerPort ) {
501
- return nil , fmt .Errorf ("invalid port format '%s'" , containerPort )
502
- }
503
-
504
- switch {
505
- case len (ip ) > 0 && len (hostPort ) > 0 :
506
- podman .addf ("-p=%s:%s:%s" , ip , hostPort , containerPort )
507
- case len (ip ) > 0 :
508
- podman .addf ("-p=%s::%s" , ip , containerPort )
509
- case len (hostPort ) > 0 :
510
- podman .addf ("-p=%s:%s" , hostPort , containerPort )
511
- default :
512
- podman .addf ("-p=%s" , containerPort )
513
- }
458
+ if err := handlePublishPorts (container , ContainerGroup , podman ); err != nil {
459
+ return nil , err
514
460
}
515
461
516
462
podman .addEnv (podmanEnv )
@@ -775,6 +721,10 @@ func ConvertKube(kube *parser.UnitFile, isUser bool) (*parser.UnitFile, error) {
775
721
execStart .add ("--configmap" , configMapPath )
776
722
}
777
723
724
+ if err := handlePublishPorts (kube , KubeGroup , execStart ); err != nil {
725
+ return nil , err
726
+ }
727
+
778
728
execStart .add (yamlPath )
779
729
780
730
service .AddCmdline (ServiceGroup , "ExecStart" , execStart .Args )
@@ -876,3 +826,67 @@ func getAbsolutePath(quadletUnitFile *parser.UnitFile, filePath string) (string,
876
826
}
877
827
return filePath , nil
878
828
}
829
+
830
+ func handlePublishPorts (unitFile * parser.UnitFile , groupName string , podman * PodmanCmdline ) error {
831
+ publishPorts := unitFile .LookupAll (groupName , KeyPublishPort )
832
+ for _ , publishPort := range publishPorts {
833
+ publishPort = strings .TrimSpace (publishPort ) // Allow whitespace after
834
+
835
+ // IP address could have colons in it. For example: "[::]:8080:80/tcp, so use custom splitter
836
+ parts := splitPorts (publishPort )
837
+
838
+ var containerPort string
839
+ ip := ""
840
+ hostPort := ""
841
+
842
+ // format (from podman run):
843
+ // ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort
844
+ //
845
+ // ip could be IPv6 with minimum of these chars "[::]"
846
+ // containerPort can have a suffix of "/tcp" or "/udp"
847
+ //
848
+
849
+ switch len (parts ) {
850
+ case 1 :
851
+ containerPort = parts [0 ]
852
+
853
+ case 2 :
854
+ hostPort = parts [0 ]
855
+ containerPort = parts [1 ]
856
+
857
+ case 3 :
858
+ ip = parts [0 ]
859
+ hostPort = parts [1 ]
860
+ containerPort = parts [2 ]
861
+
862
+ default :
863
+ return fmt .Errorf ("invalid published port '%s'" , publishPort )
864
+ }
865
+
866
+ if ip == "0.0.0.0" {
867
+ ip = ""
868
+ }
869
+
870
+ if len (hostPort ) > 0 && ! isPortRange (hostPort ) {
871
+ return fmt .Errorf ("invalid port format '%s'" , hostPort )
872
+ }
873
+
874
+ if len (containerPort ) > 0 && ! isPortRange (containerPort ) {
875
+ return fmt .Errorf ("invalid port format '%s'" , containerPort )
876
+ }
877
+
878
+ podman .add ("--publish" )
879
+ switch {
880
+ case len (ip ) > 0 && len (hostPort ) > 0 :
881
+ podman .addf ("%s:%s:%s" , ip , hostPort , containerPort )
882
+ case len (ip ) > 0 :
883
+ podman .addf ("%s::%s" , ip , containerPort )
884
+ case len (hostPort ) > 0 :
885
+ podman .addf ("%s:%s" , hostPort , containerPort )
886
+ default :
887
+ podman .addf ("%s" , containerPort )
888
+ }
889
+ }
890
+
891
+ return nil
892
+ }
0 commit comments