-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathservices.go
42 lines (39 loc) · 1.1 KB
/
services.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
package resource
import (
"github.com/dev4devs-com/postgresql-operator/pkg/apis/postgresql/v1alpha1"
"github.com/dev4devs-com/postgresql-operator/pkg/utils"
"k8s.io/apimachinery/pkg/util/intstr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
// Returns the service object for the Database
func NewDatabaseService(db *v1alpha1.Database, scheme *runtime.Scheme) *corev1.Service {
ls := utils.GetLabels(db.Name)
ser := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: db.Name,
Namespace: db.Namespace,
Labels: ls,
},
Spec: corev1.ServiceSpec{
Selector: ls,
Type: corev1.ServiceTypeClusterIP,
Ports: []corev1.ServicePort{
{
Name: db.Name,
TargetPort: intstr.IntOrString{
Type: intstr.Int,
IntVal: db.Spec.DatabasePort,
},
Port: db.Spec.DatabasePort,
Protocol: "TCP",
},
},
},
}
// Set Database db as the owner and controller
controllerutil.SetControllerReference(db, ser, scheme)
return ser
}