Skip to content
network.go 4.22 KiB
Newer Older
Timothee Gosselin's avatar
Timothee Gosselin committed
/*

Timothee Gosselin's avatar
Timothee Gosselin committed
Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 (the "License");
Timothee Gosselin's avatar
Timothee Gosselin committed
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

Timothee Gosselin's avatar
Timothee Gosselin committed
    https://www.gnu.org/licenses/agpl-3.0.html
Timothee Gosselin's avatar
Timothee Gosselin committed

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package components

import (
	"fmt"
	"strings"

	corev1 "k8s.io/api/core/v1"
	networkingv1beta1 "k8s.io/api/networking/v1beta1"
	"k8s.io/apimachinery/pkg/util/intstr"

Timothee Gosselin's avatar
Timothee Gosselin committed
	"k8s.libre.sh/interfaces"
Timothee Gosselin's avatar
Timothee Gosselin committed
	meta "k8s.libre.sh/meta"
	ingress "k8s.libre.sh/objects/ingress"
	service "k8s.libre.sh/objects/service"
)

Timothee Gosselin's avatar
Timothee Gosselin committed
// +kubebuilder:object:generate=true
Timothee Gosselin's avatar
Timothee Gosselin committed
type Network struct {
	IngressMeta *meta.ObjectMeta `json:"ingressMeta,omitempty"`
	ServiceMeta *meta.ObjectMeta `json:"serviceMeta,omitempty"`
Timothee Gosselin's avatar
Timothee Gosselin committed
	*Backend    `json:",inline"`
Timothee Gosselin's avatar
Timothee Gosselin committed
}

type Service struct {
	*meta.ObjectMeta `json:"meta,omitempty"`
Timothee Gosselin's avatar
Timothee Gosselin committed
	*Backend         `json:"backend,omitempty"`
Timothee Gosselin's avatar
Timothee Gosselin committed
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (s *Service) Mutate(obj interfaces.Object) error {
Timothee Gosselin's avatar
Timothee Gosselin committed
	service.MutateService(s, obj.(*corev1.Service), s.ObjectMeta)
	meta.MutateMeta(s, obj)
	return nil
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (s *Service) GetObject() interfaces.Object {
Timothee Gosselin's avatar
Timothee Gosselin committed
	return &corev1.Service{}
}

Timothee Gosselin's avatar
Timothee Gosselin committed
type Ingress struct {
Timothee Gosselin's avatar
Timothee Gosselin committed
	*meta.ObjectMeta `json:"commonMeta,omitempty"`
	Backends         []Backend `json:"backends,omitempty"`
Timothee Gosselin's avatar
Timothee Gosselin committed
}

// +kubebuilder:object:generate=true
Timothee Gosselin's avatar
Timothee Gosselin committed
type Backend struct {
	ServiceName  string             `json:"name,omitempty"`
	Hostname     string             `json:"hostname,omitempty"`
	Paths        []string           `json:"routes,omitempty"`
	TLS          bool               `json:"tls,omitempty"`
	TLSSecretRef string             `json:"tlsSecretRef,omitempty"`
	Port         service.Port       `json:"port,omitempty"`
	ServiceType  corev1.ServiceType `json:"type,omitempty"`
Timothee Gosselin's avatar
Timothee Gosselin committed
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (i *Ingress) Mutate(obj interfaces.Object) error {
Timothee Gosselin's avatar
Timothee Gosselin committed
	ingress.MutateIngress(i, obj.(*networkingv1beta1.Ingress))
	meta.MutateMeta(i, obj)
	return nil
Timothee Gosselin's avatar
Timothee Gosselin committed
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (i *Ingress) GetObject() interfaces.Object {
Timothee Gosselin's avatar
Timothee Gosselin committed
	return &networkingv1beta1.Ingress{}
}

func (b *Backend) GetServiceType() corev1.ServiceType { return b.ServiceType }

func (b *Backend) GetServicePorts() []corev1.ServicePort {

	ports := []corev1.ServicePort{}
	ports = append(ports, b.GetServicePort())
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	return ports
Timothee Gosselin's avatar
Timothee Gosselin committed
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (b *Backend) GetServicePort() corev1.ServicePort {

	port := corev1.ServicePort{
		TargetPort: intstr.FromString(b.Port.Name),
		Port:       b.Port.Port,
		Name:       b.Port.Name,
	}

	if len(b.Port.Protocol) > 0 {
		port.Protocol = b.Port.Protocol
	}

	return port
Timothee Gosselin's avatar
Timothee Gosselin committed
func (b *Backend) GetIngressBackendPaths() []networkingv1beta1.HTTPIngressPath {
Timothee Gosselin's avatar
Timothee Gosselin committed

	bkpaths := []networkingv1beta1.HTTPIngressPath{}

Timothee Gosselin's avatar
Timothee Gosselin committed
	for _, p := range b.Paths {
Timothee Gosselin's avatar
Timothee Gosselin committed
		path := networkingv1beta1.HTTPIngressPath{
			Path: p,
			Backend: networkingv1beta1.IngressBackend{
Timothee Gosselin's avatar
Timothee Gosselin committed
				// TODO TO FIX
				ServiceName: b.ServiceName,
				ServicePort: intstr.FromString(b.Port.Name),
Timothee Gosselin's avatar
Timothee Gosselin committed
			},
		}

		bkpaths = append(bkpaths, path)
	}
	return bkpaths
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (b *Backend) GetIngressRule() networkingv1beta1.IngressRule {
Timothee Gosselin's avatar
Timothee Gosselin committed

	rule := networkingv1beta1.IngressRule{
Timothee Gosselin's avatar
Timothee Gosselin committed
		Host: b.Hostname,
Timothee Gosselin's avatar
Timothee Gosselin committed
		IngressRuleValue: networkingv1beta1.IngressRuleValue{
			HTTP: &networkingv1beta1.HTTPIngressRuleValue{
Timothee Gosselin's avatar
Timothee Gosselin committed
				Paths: b.GetIngressBackendPaths(),
Timothee Gosselin's avatar
Timothee Gosselin committed
	return rule
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (i *Ingress) GetIngressRules() []networkingv1beta1.IngressRule {
Timothee Gosselin's avatar
Timothee Gosselin committed

	rules := []networkingv1beta1.IngressRule{}

	for _, ing := range i.Backends {
		rules = append(rules, ing.GetIngressRule())
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

	return rules
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (b *Backend) GetIngressTLS() networkingv1beta1.IngressTLS {
Timothee Gosselin's avatar
Timothee Gosselin committed
	tls := networkingv1beta1.IngressTLS{}
Timothee Gosselin's avatar
Timothee Gosselin committed
	if b.TLS {
		if len(b.TLSSecretRef) == 0 {
			b.TLSSecretRef = fmt.Sprintf("%v-%v", strings.Replace(b.Hostname, ".", "-", -1), "tls")
Timothee Gosselin's avatar
Timothee Gosselin committed
		}
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
		tls = networkingv1beta1.IngressTLS{
Timothee Gosselin's avatar
Timothee Gosselin committed
			SecretName: b.TLSSecretRef,
Timothee Gosselin's avatar
Timothee Gosselin committed
		}
Timothee Gosselin's avatar
Timothee Gosselin committed

		tls.Hosts = append(tls.Hosts, b.Hostname)
	}
	return tls
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (i *Ingress) GetIngressTLS() []networkingv1beta1.IngressTLS {
Timothee Gosselin's avatar
Timothee Gosselin committed

	tlsList := []networkingv1beta1.IngressTLS{}

	for _, ing := range i.Backends {
		tlsList = append(tlsList, ing.GetIngressTLS())
Timothee Gosselin's avatar
Timothee Gosselin committed
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	return tlsList
}