/
service.go
185 lines (159 loc) · 6.33 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
Copyright 2023 The RedisOperator Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package clientset
import (
"context"
"reflect"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"github.com/go-logr/logr"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// Service the client that knows how to interact with kubernetes to manage them
type Service interface {
// GetService get service from kubernetes with namespace and name
GetService(ctx context.Context, namespace string, name string) (*corev1.Service, error)
// CreateService will create the given service
CreateService(ctx context.Context, namespace string, service *corev1.Service) error
//CreateIfNotExistsService create service if it does not exist
CreateIfNotExistsService(ctx context.Context, namespace string, service *corev1.Service) error
// UpdateService will update the given service
UpdateService(ctx context.Context, namespace string, service *corev1.Service) error
// CreateOrUpdateService will update the given service or create it if does not exist
CreateOrUpdateService(ctx context.Context, namespace string, service *corev1.Service) error
// DeleteService will delete the given service
DeleteService(ctx context.Context, namespace string, name string) error
// ListServices get set of service on a given namespace
ListServices(ctx context.Context, namespace string) (*corev1.ServiceList, error)
GetServiceByLabels(ctx context.Context, namespace string, labelsMap map[string]string) (*corev1.ServiceList, error)
UpdateIfSelectorChangedService(ctx context.Context, namespace string, service *corev1.Service) error
}
// ServiceOption is the service client implementation using API calls to kubernetes.
type ServiceOption struct {
client client.Client
logger logr.Logger
}
// NewService returns a new Service client.
func NewService(kubeClient client.Client, logger logr.Logger) Service {
logger = logger.WithValues("service", "k8s.service")
return &ServiceOption{
client: kubeClient,
logger: logger,
}
}
func (s *ServiceOption) GetServiceByLabels(ctx context.Context, namespace string, labelsMap map[string]string) (*corev1.ServiceList, error) {
labelSelector := labels.Set(labelsMap).AsSelector()
services := &corev1.ServiceList{}
err := s.client.List(context.TODO(), services, &client.ListOptions{
Namespace: namespace,
LabelSelector: labelSelector,
})
if err != nil {
return nil, err
}
return services, err
}
// GetService implement the Service.Interface
func (s *ServiceOption) GetService(ctx context.Context, namespace string, name string) (*corev1.Service, error) {
service := &corev1.Service{}
err := s.client.Get(ctx, types.NamespacedName{
Name: name,
Namespace: namespace,
}, service)
if err != nil {
return nil, err
}
return service, err
}
// CreateService implement the Service.Interface
func (s *ServiceOption) CreateService(ctx context.Context, namespace string, service *corev1.Service) error {
err := s.client.Create(ctx, service)
if err != nil {
return err
}
s.logger.WithValues("namespace", namespace, "serviceName", service.Name).Info("service created")
return nil
}
// CreateIfNotExistsService implement the Service.Interface
func (s *ServiceOption) CreateIfNotExistsService(ctx context.Context, namespace string, service *corev1.Service) error {
if _, err := s.GetService(ctx, namespace, service.Name); err != nil {
// If no resource we need to create.
if errors.IsNotFound(err) {
return s.CreateService(ctx, namespace, service)
}
return err
}
return nil
}
func (s *ServiceOption) UpdateIfSelectorChangedService(ctx context.Context, namespace string, service *corev1.Service) error {
old_service, err := s.GetService(ctx, namespace, service.Name)
if err != nil {
// If no resource we need to create.
if errors.IsNotFound(err) {
return s.CreateService(ctx, namespace, service)
}
return err
}
if !reflect.DeepEqual(old_service.Spec.Selector, service.Spec.Selector) {
return s.UpdateService(ctx, namespace, service)
}
return nil
}
// UpdateService implement the Service.Interface
func (s *ServiceOption) UpdateService(ctx context.Context, namespace string, service *corev1.Service) error {
err := s.client.Update(ctx, service)
if err != nil {
return err
}
s.logger.WithValues("namespace", namespace, "serviceName", service.Name).Info("service updated")
return nil
}
// CreateOrUpdateService implement the Service.Interface
func (s *ServiceOption) CreateOrUpdateService(ctx context.Context, namespace string, service *corev1.Service) error {
storedService, err := s.GetService(ctx, namespace, service.Name)
if err != nil {
// If no resource we need to create.
if errors.IsNotFound(err) {
return s.CreateService(ctx, namespace, service)
}
return err
}
// Already exists, need to Update.
// Set the correct resource version to ensure we are on the latest version. This way the only valid
// namespace is our spec(https://github.com/kubernetes/community/blob/master/contributors/devel/api-conventions.md#concurrency-control-and-consistency),
// we will replace the current namespace state.
service.ResourceVersion = storedService.ResourceVersion
return s.UpdateService(ctx, namespace, service)
}
// DeleteService implement the Service.Interface
func (s *ServiceOption) DeleteService(ctx context.Context, namespace string, name string) error {
service := &corev1.Service{}
if err := s.client.Get(ctx, types.NamespacedName{
Name: name,
Namespace: namespace,
}, service); err != nil {
return err
}
return s.client.Delete(ctx, service)
}
// ListServices implement the Service.Interface
func (s *ServiceOption) ListServices(ctx context.Context, namespace string) (*corev1.ServiceList, error) {
services := &corev1.ServiceList{}
listOps := &client.ListOptions{
Namespace: namespace,
}
err := s.client.List(ctx, services, listOps)
return services, err
}