-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
41 lines (37 loc) · 1.29 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package k8sclient
import (
apiv1 "k8s.io/api/core/v1"
v1beta1 "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
)
// GetServiceIngresses returns a ServiceList whose selectors match the labels on passed deployment
func GetServiceIngresses(clientset *kubernetes.Clientset, service apiv1.Service) (*v1beta1.IngressList, error) {
// get all services
ingressClient := clientset.ExtensionsV1beta1().Ingresses(service.Namespace)
ingList, err := ingressClient.List(metav1.ListOptions{FieldSelector: ""})
if err != nil {
return nil, err
}
result := &v1beta1.IngressList{Items: []v1beta1.Ingress{}}
for _, ing := range ingList.Items {
for _, rule := range ing.Spec.Rules {
for _, path := range rule.HTTP.Paths {
if service.Name == path.Backend.ServiceName && ServicePortsContains(service.Spec.Ports, path.Backend.ServicePort) {
result.Items = append(result.Items, ing)
}
}
}
}
return result, nil
}
// ServicePortsContains is a helper for determining if a Port exists in a slice of ServicePorts
func ServicePortsContains(servicePorts []apiv1.ServicePort, port intstr.IntOrString) bool {
for _, p := range servicePorts {
if port.IntVal == p.Port {
return true
}
}
return false
}