Skip to content

Commit

Permalink
fix: Fix ports and remove service to ingress (#560)
Browse files Browse the repository at this point in the history
Signed-off-by: Ashok Pon Kumar <[email protected]>
Co-authored-by: seshapad <[email protected]>
  • Loading branch information
ashokponkumar and seshapad authored Jul 15, 2021
1 parent 0964d95 commit f5eefee
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 294 deletions.
4 changes: 3 additions & 1 deletion internal/apiresource/buildconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ func (bc *BuildConfig) getBuildTriggerPolicy(irBuildConfig irtypes.BuildConfig,
if repoURL != "" {
gitRepoURLObj, err := giturls.Parse(repoURL)
if err != nil {
logrus.Warnf("Failed to parse git repo url %s Error: %q", repoURL, err)
if repoURL != "" {
logrus.Warnf("Failed to parse git repo url %s Error: %q", repoURL, err)
}
} else if gitRepoURLObj.Hostname() == "" {
logrus.Warnf("Successfully parsed git repo url %s but the host name is empty: %+v", repoURL, gitRepoURLObj)
} else {
Expand Down
167 changes: 40 additions & 127 deletions internal/apiresource/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/konveyor/move2kube/types/qaengine/commonqa"
okdroutev1 "github.com/openshift/api/route/v1"
"github.com/sirupsen/logrus"
"github.com/spf13/cast"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -78,18 +77,16 @@ func (d *Service) createNewResources(ir irtypes.EnhancedIR, supportedKinds []str
}
continue
}
if !common.IsStringPresent(supportedKinds, common.ServiceKind) {
logrus.Errorf("Could not find a valid resource type in cluster to create a Service")
continue
}
obj := d.createService(service)
objs = append(objs, obj)
}

// Create one ingress for all services
if ingressEnabled {
obj := d.createIngress(ir, targetCluster.Spec)
objs = append(objs, obj)
if obj != nil {
objs = append(objs, obj)
}
}

return objs
Expand All @@ -105,10 +102,7 @@ func (d *Service) convertToClusterSupportedKinds(obj runtime.Object, supportedKi
if ingress, ok := lobj.(*networking.Ingress); ok {
return d.ingressToRoute(*ingress), true
}
if service, ok := lobj.(*core.Service); ok {
if service.Spec.Type == core.ServiceTypeLoadBalancer || service.Spec.Type == core.ServiceTypeNodePort {
return d.serviceToRoutes(*service, ir, targetCluster.Spec), true
}
if _, ok := lobj.(*core.Service); ok {
return []runtime.Object{obj}, true
}
} else if common.IsStringPresent(supportedKinds, common.IngressKind) {
Expand All @@ -118,10 +112,7 @@ func (d *Service) convertToClusterSupportedKinds(obj runtime.Object, supportedKi
if _, ok := lobj.(*networking.Ingress); ok {
return []runtime.Object{obj}, true
}
if service, ok := lobj.(*core.Service); ok {
if service.Spec.Type == core.ServiceTypeLoadBalancer || service.Spec.Type == core.ServiceTypeNodePort {
return d.serviceToIngress(*service, ir, targetCluster.Spec), true
}
if _, ok := lobj.(*core.Service); ok {
return []runtime.Object{obj}, true
}
} else {
Expand Down Expand Up @@ -180,53 +171,6 @@ func (d *Service) ingressToRoute(ingress networking.Ingress) []runtime.Object {
return objs
}

func (d *Service) serviceToRoutes(service core.Service, ir irtypes.EnhancedIR, targetClusterSpec collecttypes.ClusterMetadataSpec) []runtime.Object {
weight := int32(1) //Hard-coded to 1 to avoid Helm v3 errors
ingressArray := []okdroutev1.RouteIngress{} //Hard-coded to empty list to avoid Helm v3 errors
ingressArray = append(ingressArray, okdroutev1.RouteIngress{Host: ""})

objs := []runtime.Object{}
pathPrefix := "/" + service.Name
for _, serviceport := range service.Spec.Ports {
path := pathPrefix
if len(service.Spec.Ports) > 1 {
// All ports cannot be exposed as /svcname because they will clash
path = pathPrefix + "/" + serviceport.Name
if serviceport.Name == "" {
path = pathPrefix + "/" + cast.ToString(serviceport.Port)
}
}
targetPort := intstr.IntOrString{Type: intstr.String, StrVal: serviceport.Name}
if serviceport.Name == "" {
targetPort.Type = intstr.Int
targetPort.IntVal = serviceport.Port
}
route := &okdroutev1.Route{
TypeMeta: metav1.TypeMeta{
Kind: routeKind,
APIVersion: okdroutev1.SchemeGroupVersion.String(),
},
ObjectMeta: service.ObjectMeta,
Spec: okdroutev1.RouteSpec{
Host: targetClusterSpec.Host,
Path: path,
To: okdroutev1.RouteTargetReference{
Kind: common.ServiceKind,
Name: service.Name,
Weight: &weight,
},
Port: &okdroutev1.RoutePort{TargetPort: targetPort},
},
Status: okdroutev1.RouteStatus{Ingress: ingressArray},
}
objs = append(objs, route)
}
service.Spec.Type = core.ServiceTypeClusterIP
objs = append(objs, &service)

return objs
}

func (d *Service) routeToIngress(route okdroutev1.Route, ir irtypes.EnhancedIR, targetClusterSpec collecttypes.ClusterMetadataSpec) []runtime.Object {
targetPort := networking.ServiceBackendPort{}
if route.Spec.Port.TargetPort.Type == intstr.String {
Expand Down Expand Up @@ -268,50 +212,6 @@ func (d *Service) routeToIngress(route okdroutev1.Route, ir irtypes.EnhancedIR,
return []runtime.Object{&ingress}
}

func (d *Service) serviceToIngress(service core.Service, ir irtypes.EnhancedIR, targetClusterSpec collecttypes.ClusterMetadataSpec) []runtime.Object {
rules := []networking.IngressRule{}
pathPrefix := "/" + service.Name
for _, serviceport := range service.Spec.Ports {
path := pathPrefix
if len(service.Spec.Ports) > 1 {
// All ports cannot be exposed as /svcname because they will clash
path = pathPrefix + "/" + serviceport.Name
if serviceport.Name == "" {
path = pathPrefix + "/" + cast.ToString(serviceport.Port)
}
}
rule := networking.IngressRule{
IngressRuleValue: networking.IngressRuleValue{
HTTP: &networking.HTTPIngressRuleValue{
Paths: []networking.HTTPIngressPath{
{
Path: path,
Backend: networking.IngressBackend{
Service: &networking.IngressServiceBackend{
Name: service.Name,
Port: networking.ServiceBackendPort{Number: serviceport.Port},
},
},
},
},
},
},
Host: targetClusterSpec.Host,
}
rules = append(rules, rule)
}
ingress := networking.Ingress{
TypeMeta: metav1.TypeMeta{
Kind: common.IngressKind,
APIVersion: networking.SchemeGroupVersion.String(),
},
ObjectMeta: service.ObjectMeta,
Spec: networking.IngressSpec{Rules: rules},
}
service.Spec.Type = core.ServiceTypeClusterIP
return []runtime.Object{&ingress, &service}
}

func (d *Service) routeToService(route okdroutev1.Route) []runtime.Object {
// TODO: Think through how will the clusterip service that was originally there will behave when merged with this service?
svc := &core.Service{
Expand Down Expand Up @@ -372,7 +272,10 @@ func (d *Service) createRoutes(service irtypes.Service, ir irtypes.EnhancedIR, t
routes := [](*okdroutev1.Route){}
servicePorts, hostPrefixes, relPaths, _ := d.getExposeInfo(service)
for i, servicePort := range servicePorts {
route := d.createRoute(service, servicePort, hostPrefixes[i], relPaths[i], ir, targetClusterSpec)
if relPaths[i] == "" {
continue
}
route := d.createRoute(ir.Name, service, servicePort, hostPrefixes[i], relPaths[i], ir, targetClusterSpec)
routes = append(routes, route)
}
return routes
Expand All @@ -383,15 +286,18 @@ func (d *Service) createRoutes(service irtypes.Service, ir irtypes.EnhancedIR, t
//[https://bugzilla.redhat.com/show_bug.cgi?id=1773682]
// Can't use https because of this https://github.com/openshift/origin/issues/2162
// When service has multiple ports,the route needs a port name. Port number doesn't seem to work.
func (d *Service) createRoute(service irtypes.Service, port core.ServicePort, hostprefix, path string, ir irtypes.EnhancedIR, targetClusterSpec collecttypes.ClusterMetadataSpec) *okdroutev1.Route {
func (d *Service) createRoute(irName string, service irtypes.Service, port core.ServicePort, hostprefix, path string, ir irtypes.EnhancedIR, targetClusterSpec collecttypes.ClusterMetadataSpec) *okdroutev1.Route {
weight := int32(1) //Hard-coded to 1 to avoid Helm v3 errors
ingressArray := []okdroutev1.RouteIngress{{Host: ""}} //Hard-coded to empty string to avoid Helm v3 errors

host := targetClusterSpec.Host
if host == "" {
host = commonqa.IngressHost(host)
host = commonqa.IngressHost(d.getHostName(irName))
}
ph := host
if hostprefix != "" {
ph = hostprefix + "." + ph
}

route := &okdroutev1.Route{
TypeMeta: metav1.TypeMeta{
Kind: routeKind,
Expand All @@ -402,7 +308,7 @@ func (d *Service) createRoute(service irtypes.Service, port core.ServicePort, ho
Labels: getServiceLabels(service.Name),
},
Spec: okdroutev1.RouteSpec{
Host: hostprefix + "." + host,
Host: ph,
Path: path,
To: okdroutev1.RouteTargetReference{
Kind: common.ServiceKind,
Expand All @@ -423,18 +329,17 @@ func (d *Service) createRoute(service irtypes.Service, port core.ServicePort, ho
func (d *Service) createIngress(ir irtypes.EnhancedIR, targetClusterSpec collecttypes.ClusterMetadataSpec) *networking.Ingress {
pathType := networking.PathTypePrefix

// Create the fan-out paths
hostHttpIngressPaths := map[string][]networking.HTTPIngressPath{} //[hostprefix]
for _, service := range ir.Services {
if !service.HasValidAnnotation(common.ExposeSelector) {
continue
}
backendServiceName := service.BackendServiceName
if service.BackendServiceName == "" {
backendServiceName = service.Name
}
servicePorts, hostPrefixes, relPaths, _ := d.getExposeInfo(service)
for i, servicePort := range servicePorts {
if relPaths[i] == "" {
continue
}
backendPort := networking.ServiceBackendPort{Name: servicePort.Name}
if servicePort.Name == "" {
backendPort = networking.ServiceBackendPort{Number: servicePort.Port}
Expand All @@ -452,21 +357,26 @@ func (d *Service) createIngress(ir irtypes.EnhancedIR, targetClusterSpec collect
hostHttpIngressPaths[hostPrefixes[i]] = append(hostHttpIngressPaths[hostPrefixes[i]], httpIngressPath)
}
}
if len(hostHttpIngressPaths) == 0 {
return nil
}

// Configure the rule with the above fan-out paths
rules := []networking.IngressRule{}
host := targetClusterSpec.Host
secretName := ""
if len(hostHttpIngressPaths) > 0 {
if host == "" {
host = commonqa.IngressHost(host)
}
defaultSecretName := ""
secretName = qaengine.FetchStringAnswer(common.ConfigIngressTLSKey, "Provide the TLS secret for ingress", []string{"Leave empty to use http"}, defaultSecretName)
if host == "" {
host = commonqa.IngressHost(d.getHostName(ir.Name))
}
defaultSecretName := ""
secretName = qaengine.FetchStringAnswer(common.ConfigIngressTLSKey, "Provide the TLS secret for ingress", []string{"Leave empty to use http"}, defaultSecretName)
for hostprefix, httpIngressPaths := range hostHttpIngressPaths {
ph := host
if hostprefix != "" {
ph = hostprefix + "." + ph
}
rules = append(rules, networking.IngressRule{
Host: hostprefix + `/` + host,
Host: ph,
IngressRuleValue: networking.IngressRuleValue{
HTTP: &networking.HTTPIngressRuleValue{
Paths: httpIngressPaths,
Expand Down Expand Up @@ -533,9 +443,6 @@ func (d *Service) getExposeInfo(service irtypes.Service) (servicePorts []core.Se
hostPrefixes = []string{}
serviceType = core.ServiceTypeClusterIP
for _, forwarding := range service.ServiceToPodPortForwardings {
if forwarding.ServiceRelPath == "" {
continue
}
servicePortName := forwarding.ServicePort.Name
if servicePortName == "" {
servicePortName = fmt.Sprintf("port-%d", forwarding.ServicePort.Number)
Expand All @@ -562,7 +469,7 @@ func (d *Service) getExposeInfo(service irtypes.Service) (servicePorts []core.Se
continue
}
relPaths = append(relPaths, relPath)
hostPrefixes = append(relPaths, hostPrefix)
hostPrefixes = append(hostPrefixes, hostPrefix)
servicePorts = append(servicePorts, servicePort)
}
return servicePorts, hostPrefixes, relPaths, serviceType
Expand All @@ -571,6 +478,7 @@ func (d *Service) getExposeInfo(service irtypes.Service) (servicePorts []core.Se
func (d *Service) parseServiceRelPath(path string) (hostPrefix, relPath string, serviceType core.ServiceType) {
serviceType = core.ServiceTypeClusterIP
relPath = path
hostPrefix = ""
const nodeportSuffix = ":N"
const loadBalancerSuffix = ":L"
const noneSuffix = ":-"
Expand All @@ -586,15 +494,20 @@ func (d *Service) parseServiceRelPath(path string) (hostPrefix, relPath string,
serviceType = ""
relPath = strings.TrimSuffix(relPath, noneSuffix)
}
if !strings.HasPrefix(relPath, `/`) {
if relPath != "" && !strings.HasPrefix(relPath, `/`) {
parts := []string{relPath}
if strings.Contains(relPath, `/`) {
parts = strings.SplitN(relPath, `/`, 2)
}
relPath = `/`
if len(parts) > 1 {
relPath = `/` + parts[1]
relPath += parts[1]
}
hostPrefix = parts[0]
}
return hostPrefix, relPath, serviceType
}

func (d *Service) getHostName(irName string) string {
return irName + ".com"
}
2 changes: 2 additions & 0 deletions internal/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ const (
ConfigServicesKey = BaseKey + d + "services"
//ConfigStoragesKey represents Storages Key
ConfigStoragesKey = BaseKey + d + "storages"
//ConfigMinReplicasKey represents Ingress host Key
ConfigMinReplicasKey = BaseKey + d + "minreplicas"
//ConfigModesKey represents modes Key
ConfigModesKey = BaseKey + d + "modes"
//ConfigSpawmContainersKey represents modes Key
Expand Down
4 changes: 3 additions & 1 deletion internal/irpreprocessor/ingresspreprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (opt *ingressPreprocessor) preprocess(ir irtypes.IR) (irtypes.IR, error) {
for sn, s := range ir.Services {
tempService := ir.Services[sn]
expose := false
for _, pf := range s.ServiceToPodPortForwardings {
for pfi, pf := range s.ServiceToPodPortForwardings {
if pf.ServicePort.Number == 0 {
continue
}
Expand All @@ -54,9 +54,11 @@ func (opt *ingressPreprocessor) preprocess(ir irtypes.IR) (irtypes.IR, error) {
exposedServiceRelPath = "/" + sn
}
exposedServiceRelPath = strings.TrimSpace(qaengine.FetchStringAnswer(key, message, hints, exposedServiceRelPath))
pf.ServiceRelPath = exposedServiceRelPath
if exposedServiceRelPath != "" {
expose = true
}
tempService.ServiceToPodPortForwardings[pfi] = pf
}
if tempService.Annotations == nil {
tempService.Annotations = map[string]string{}
Expand Down
17 changes: 17 additions & 0 deletions internal/irpreprocessor/mergepreprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ func (opt *mergePreprocessor) preprocess(ir irtypes.IR) (irtypes.IR, error) {
service.AddPortForwarding(networking.ServiceBackendPort{Number: p.ContainerPort}, networking.ServiceBackendPort{Number: p.ContainerPort}, "")
}
}
tolerations := service.Tolerations
service.Tolerations = []core.Toleration{}
for _, t := range tolerations {
if (t == core.Toleration{}) {
continue
}
found := false
for _, ot := range service.Tolerations {
if ot == t {
found = true
continue
}
}
if !found {
service.Tolerations = append(service.Tolerations, t)
}
}
ir.Services[serviceName] = service
}
return ir, nil
Expand Down
Loading

0 comments on commit f5eefee

Please sign in to comment.