github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/pkg/kubernetes/resources/ingress/adapt.go (about)

     1  package ingress
     2  
     3  import (
     4  	"github.com/caos/orbos/pkg/kubernetes"
     5  	"github.com/caos/orbos/pkg/kubernetes/resources"
     6  	"github.com/caos/orbos/pkg/labels"
     7  	"k8s.io/api/extensions/v1beta1"
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  	"k8s.io/apimachinery/pkg/util/intstr"
    10  )
    11  
    12  type Arguments struct {
    13  	Namespace   string
    14  	Id          labels.IDLabels
    15  	Host        string
    16  	Prefix      string
    17  	Service     string
    18  	ServicePort uint16
    19  	Annotations map[string]string
    20  }
    21  
    22  func AdaptFuncToEnsure(params *Arguments) (resources.QueryFunc, error) {
    23  
    24  	ingress := &v1beta1.Ingress{
    25  		ObjectMeta: metav1.ObjectMeta{
    26  			Name:        params.Id.Name(),
    27  			Namespace:   params.Namespace,
    28  			Labels:      labels.MustK8sMap(params.Id),
    29  			Annotations: params.Annotations,
    30  		},
    31  		Spec: v1beta1.IngressSpec{
    32  			Rules: []v1beta1.IngressRule{{
    33  				Host: params.Host,
    34  				IngressRuleValue: v1beta1.IngressRuleValue{HTTP: &v1beta1.HTTPIngressRuleValue{Paths: []v1beta1.HTTPIngressPath{{
    35  					Path:     params.Prefix,
    36  					PathType: pathTypePtr(v1beta1.PathTypePrefix),
    37  					Backend: v1beta1.IngressBackend{
    38  						ServiceName: params.Service,
    39  						ServicePort: intstr.FromInt(int(params.ServicePort)),
    40  						Resource:    nil,
    41  					},
    42  				}}}},
    43  			}},
    44  		},
    45  	}
    46  	return func(_ kubernetes.ClientInt, _ map[string]interface{}) (resources.EnsureFunc, error) {
    47  		return func(k8sClient kubernetes.ClientInt) error {
    48  			return k8sClient.ApplyIngress(ingress)
    49  		}, nil
    50  	}, nil
    51  }
    52  
    53  func pathTypePtr(pathType v1beta1.PathType) *v1beta1.PathType {
    54  	return &pathType
    55  }
    56  
    57  func AdaptFuncToDestroy(namespace, name string) (resources.DestroyFunc, error) {
    58  	return func(k8sClient kubernetes.ClientInt) error {
    59  		return k8sClient.DeleteIngress(namespace, name)
    60  	}, nil
    61  }