/
service_control.go
109 lines (94 loc) · 3.41 KB
/
service_control.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
100
101
102
103
104
105
106
107
108
109
package controller
import (
"context"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
rapi "github.com/IBM/operator-for-redis-cluster/api/v1alpha1"
"github.com/IBM/operator-for-redis-cluster/pkg/controller/pod"
)
// ServicesControlInterface inferface for the ServicesControl
type ServicesControlInterface interface {
// CreateRedisClusterService used to create the Kubernetes Service needed to access the Redis Cluster
CreateRedisClusterService(redisCluster *rapi.RedisCluster) (*v1.Service, error)
// DeleteRedisClusterService used to delete the Kubernetes Service linked to the Redis Cluster
DeleteRedisClusterService(redisCluster *rapi.RedisCluster) error
// GetRedisClusterService used to retrieve the Kubernetes Service associated to the RedisCluster
GetRedisClusterService(redisCluster *rapi.RedisCluster) (*v1.Service, error)
}
// ServicesControl contains all information for managing Kube Services
type ServicesControl struct {
KubeClient client.Client
Recorder record.EventRecorder
}
// NewServicesControl builds and returns new ServicesControl instance
func NewServicesControl(client client.Client, rec record.EventRecorder) *ServicesControl {
ctrl := &ServicesControl{
KubeClient: client,
Recorder: rec,
}
return ctrl
}
// GetRedisClusterService used to retrieve the Kubernetes Service associated to the RedisCluster
func (s *ServicesControl) GetRedisClusterService(redisCluster *rapi.RedisCluster) (*v1.Service, error) {
serviceName := types.NamespacedName{
Name: getServiceName(redisCluster),
Namespace: redisCluster.Namespace,
}
svc := &v1.Service{}
err := s.KubeClient.Get(context.Background(), serviceName, svc)
if err != nil {
return nil, err
}
return svc, nil
}
// CreateRedisClusterService used to create the Kubernetes Service needed to access the Redis Cluster
func (s *ServicesControl) CreateRedisClusterService(redisCluster *rapi.RedisCluster) (*v1.Service, error) {
serviceName := getServiceName(redisCluster)
desiredLabels, err := pod.GetLabelsSet(redisCluster)
if err != nil {
return nil, err
}
desiredAnnotations, err := pod.GetClusterAnnotationsSet(redisCluster)
if err != nil {
return nil, err
}
newService := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Labels: desiredLabels,
Annotations: desiredAnnotations,
Name: serviceName,
Namespace: redisCluster.Namespace,
OwnerReferences: []metav1.OwnerReference{pod.BuildOwnerReference(redisCluster)},
},
Spec: v1.ServiceSpec{
ClusterIP: "None",
Ports: []v1.ServicePort{{Port: 6379, Name: "redis"}},
Selector: desiredLabels,
},
}
err = s.KubeClient.Create(context.Background(), newService)
if err != nil {
return nil, err
}
return newService, nil
}
// DeleteRedisClusterService used to delete the Kubernetes Service linked to the Redis Cluster
func (s *ServicesControl) DeleteRedisClusterService(redisCluster *rapi.RedisCluster) error {
svc := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: getServiceName(redisCluster),
Namespace: redisCluster.Namespace,
},
}
return s.KubeClient.Delete(context.Background(), svc)
}
func getServiceName(redisCluster *rapi.RedisCluster) string {
serviceName := redisCluster.Name
if redisCluster.Spec.ServiceName != "" {
serviceName = redisCluster.Spec.ServiceName
}
return serviceName
}