forked from LaconicNetwork/kompose
Support assign nodeport port in labels (#1210)
This commit is contained in:
@@ -96,6 +96,7 @@ type ServiceConfig struct {
|
||||
User string `compose:"user"`
|
||||
VolumesFrom []string `compose:"volumes_from"`
|
||||
ServiceType string `compose:"kompose.service.type"`
|
||||
NodePortPort int32 `compose:"kompose.service.nodeport.port"`
|
||||
StopGracePeriod string `compose:"stop_grace_period"`
|
||||
Build string `compose:"build"`
|
||||
BuildArgs map[string]*string `compose:"build-args"`
|
||||
|
||||
@@ -31,6 +31,8 @@ import (
|
||||
const (
|
||||
// LabelServiceType defines the type of service to be created
|
||||
LabelServiceType = "kompose.service.type"
|
||||
// LabelNodePortPort defines the port value for NodePort service
|
||||
LabelNodePortPort = "kompose.service.nodeport.port"
|
||||
// LabelServiceExpose defines if the service needs to be made accessible from outside the cluster or not
|
||||
LabelServiceExpose = "kompose.service.expose"
|
||||
// LabelServiceExposeTLSSecret provides the name of the TLS secret to use with the Kubernetes ingress controller
|
||||
|
||||
@@ -18,6 +18,7 @@ package compose
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cast"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -245,6 +246,8 @@ func libComposeToKomposeMapping(composeObject *project.Project) (kobject.Kompose
|
||||
serviceConfig.ExposeService = strings.Trim(strings.ToLower(value), " ,")
|
||||
case LabelServiceExposeTLSSecret:
|
||||
serviceConfig.ExposeServiceTLS = value
|
||||
case LabelNodePortPort:
|
||||
serviceConfig.NodePortPort = cast.ToInt32(value)
|
||||
case LabelImagePullSecret:
|
||||
serviceConfig.ImagePullSecret = value
|
||||
case LabelImagePullPolicy:
|
||||
@@ -256,6 +259,15 @@ func libComposeToKomposeMapping(composeObject *project.Project) (kobject.Kompose
|
||||
if serviceConfig.ExposeService == "" && serviceConfig.ExposeServiceTLS != "" {
|
||||
return kobject.KomposeObject{}, errors.New("kompose.service.expose.tls-secret was specified without kompose.service.expose")
|
||||
}
|
||||
|
||||
if serviceConfig.ServiceType != string(api.ServiceTypeNodePort) && serviceConfig.NodePortPort != 0 {
|
||||
return kobject.KomposeObject{}, errors.New("kompose.service.type must be nodeport when assign node port value")
|
||||
}
|
||||
|
||||
if len(serviceConfig.Port) > 1 && serviceConfig.NodePortPort != 0 {
|
||||
return kobject.KomposeObject{}, errors.New("cannnot set kompose.service.nodeport.port when service has multiple ports")
|
||||
}
|
||||
|
||||
err = checkLabelsPorts(len(serviceConfig.Port), composeServiceConfig.Labels[LabelServiceType], name)
|
||||
if err != nil {
|
||||
return kobject.KomposeObject{}, errors.Wrap(err, "kompose.service.type can't be set if service doesn't expose any ports.")
|
||||
|
||||
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package compose
|
||||
|
||||
import (
|
||||
"github.com/spf13/cast"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -424,6 +425,8 @@ func dockerComposeToKomposeMapping(composeObject *types.Config) (kobject.Kompose
|
||||
serviceConfig.ServiceType = serviceType
|
||||
case LabelServiceExpose:
|
||||
serviceConfig.ExposeService = strings.Trim(strings.ToLower(value), " ,")
|
||||
case LabelNodePortPort:
|
||||
serviceConfig.NodePortPort = cast.ToInt32(value)
|
||||
case LabelServiceExposeTLSSecret:
|
||||
serviceConfig.ExposeServiceTLS = value
|
||||
case LabelImagePullSecret:
|
||||
@@ -437,6 +440,14 @@ func dockerComposeToKomposeMapping(composeObject *types.Config) (kobject.Kompose
|
||||
return kobject.KomposeObject{}, errors.New("kompose.service.expose.tls-secret was specified without kompose.service.expose")
|
||||
}
|
||||
|
||||
if serviceConfig.ServiceType != string(api.ServiceTypeNodePort) && serviceConfig.NodePortPort != 0 {
|
||||
return kobject.KomposeObject{}, errors.New("kompose.service.type must be nodeport when assign node port value")
|
||||
}
|
||||
|
||||
if len(serviceConfig.Port) > 1 && serviceConfig.NodePortPort != 0 {
|
||||
return kobject.KomposeObject{}, errors.New("cannot set kompose.service.nodeport.port when service has multiple ports")
|
||||
}
|
||||
|
||||
// Log if the name will been changed
|
||||
if normalizeServiceNames(name) != name {
|
||||
log.Infof("Service name in docker-compose has been changed from %q to %q", name, normalizeServiceNames(name))
|
||||
|
||||
@@ -525,6 +525,11 @@ func (k *Kubernetes) ConfigServicePorts(name string, service kobject.ServiceConf
|
||||
Port: port.HostPort,
|
||||
TargetPort: targetPort,
|
||||
}
|
||||
|
||||
if service.ServiceType == string(api.ServiceTypeNodePort) && service.NodePortPort != 0 {
|
||||
servicePort.NodePort = service.NodePortPort
|
||||
}
|
||||
|
||||
// If the default is already TCP, no need to include it.
|
||||
if port.Protocol != api.ProtocolTCP {
|
||||
servicePort.Protocol = port.Protocol
|
||||
|
||||
Reference in New Issue
Block a user