/
service.go
64 lines (59 loc) · 1.34 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package boots
import (
"context"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
func CreateService(ctx context.Context, client ctrlruntimeclient.Client, ns string) error {
service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "boots",
Namespace: ns,
Labels: map[string]string{
"app": "boots",
},
},
Spec: corev1.ServiceSpec{
ClusterIP: "None",
Selector: map[string]string{
"app": "boots",
},
Ports: []corev1.ServicePort{
{
Name: "boots-dhcp",
Port: 67,
TargetPort: intstr.FromInt(67),
Protocol: corev1.ProtocolUDP,
},
{
Name: "boots-http",
Port: 80,
TargetPort: intstr.FromInt(80),
Protocol: corev1.ProtocolTCP,
},
{
Name: "boots-syslog",
Port: 514,
TargetPort: intstr.FromInt(514),
Protocol: corev1.ProtocolUDP,
},
{
Name: "boots-tftp",
Port: 69,
TargetPort: intstr.FromInt(69),
Protocol: corev1.ProtocolUDP,
},
},
},
}
if err := client.Create(ctx, service); err != nil {
if kerrors.IsAlreadyExists(err) {
return nil
}
return err
}
return nil
}