github.com/imran-kn/cilium-fork@v1.6.9/pkg/k8s/ingress.go (about)

     1  // Copyright 2018-2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package k8s
    16  
    17  import (
    18  	"fmt"
    19  	"net"
    20  
    21  	"github.com/cilium/cilium/pkg/k8s/types"
    22  	"github.com/cilium/cilium/pkg/loadbalancer"
    23  )
    24  
    25  func supportV1beta1(ing *types.Ingress) bool {
    26  	// We only support Single Service Ingress for now which means
    27  	// ing.Spec.Backend needs to be different than nil.
    28  	return ing.Spec.Backend != nil
    29  }
    30  
    31  // ParseIngressID parses the service ID from the ingress resource
    32  func ParseIngressID(svc *types.Ingress) ServiceID {
    33  	id := ServiceID{
    34  		Namespace: svc.ObjectMeta.Namespace,
    35  	}
    36  
    37  	if svc.Spec.Backend != nil {
    38  		id.Name = svc.Spec.Backend.ServiceName
    39  	}
    40  
    41  	return id
    42  }
    43  
    44  // ParseIngress parses an ingress resources and returns the Service definition
    45  func ParseIngress(ingress *types.Ingress, host net.IP) (ServiceID, *Service, error) {
    46  	svcID := ParseIngressID(ingress)
    47  
    48  	if !supportV1beta1(ingress) {
    49  		return svcID, nil, fmt.Errorf("only Single Service Ingress is supported for now, ignoring Ingress")
    50  	}
    51  
    52  	ingressPort := ingress.Spec.Backend.ServicePort.IntValue()
    53  	if ingressPort == 0 {
    54  		return svcID, nil, fmt.Errorf("invalid port number")
    55  	}
    56  
    57  	svc := NewService(host, false, nil, nil)
    58  	portName := loadbalancer.FEPortName(ingress.Spec.Backend.ServiceName + "/" + ingress.Spec.Backend.ServicePort.String())
    59  	svc.Ports[portName] = loadbalancer.NewFEPort(loadbalancer.TCP, uint16(ingressPort))
    60  
    61  	return svcID, svc, nil
    62  }