This repository has been archived by the owner on Jun 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
/
populate.go
99 lines (94 loc) · 2.29 KB
/
populate.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package populate
import (
"fmt"
"strconv"
"github.com/rancher/rio/modules/istio/pkg/parse"
riov1 "github.com/rancher/rio/pkg/apis/rio.cattle.io/v1"
"github.com/rancher/rio/pkg/constructors"
"github.com/rancher/wrangler/pkg/objectset"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func ServiceForExternalService(es *riov1.ExternalService, namespace *corev1.Namespace, os *objectset.ObjectSet) error {
svc := constructors.NewService(namespace.Name, es.Name, v1.Service{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"rio.cattle.io/service": es.Name,
},
},
})
if es.Spec.FQDN != "" {
u, err := parse.TargetURL(es.Spec.FQDN)
if err != nil {
return err
}
svc.Spec = v1.ServiceSpec{
Type: v1.ServiceTypeExternalName,
ExternalName: u.Host,
}
} else if len(es.Spec.IPAddresses) > 0 {
var hosts []string
var ports []int32
for _, ip := range es.Spec.IPAddresses {
u, err := parse.TargetURL(ip)
if err != nil {
return err
}
port := u.Port()
if port == "" {
port = "80"
}
portInt, _ := strconv.Atoi(port)
svc.Spec = v1.ServiceSpec{
Type: v1.ServiceTypeClusterIP,
Ports: []v1.ServicePort{
{
Name: fmt.Sprintf("http-%v-%v", portInt, portInt),
Protocol: v1.ProtocolTCP,
Port: int32(portInt),
},
},
ClusterIP: v1.ClusterIPNone,
}
hosts = append(hosts, u.Hostname())
ports = append(ports, int32(portInt))
}
os.Add(populateEndpoint(svc.Name, svc.Namespace, hosts, ports))
} else if es.Spec.Service != "" {
svc.Spec = v1.ServiceSpec{
Type: v1.ServiceTypeClusterIP,
Ports: []v1.ServicePort{
{
Name: "http-80-80",
Protocol: v1.ProtocolTCP,
Port: 80,
},
},
}
}
os.Add(svc)
return nil
}
func populateEndpoint(name, namespace string, hosts []string, ports []int32) *v1.Endpoints {
var subnet []v1.EndpointSubset
for i, host := range hosts {
subnet = append(subnet, v1.EndpointSubset{
Addresses: []v1.EndpointAddress{
{
IP: host,
},
},
Ports: []v1.EndpointPort{
{
Name: "http-80-80",
Protocol: v1.ProtocolTCP,
Port: ports[i],
},
},
})
}
return constructors.NewEndpoints(namespace, name, v1.Endpoints{
Subsets: subnet,
})
}