This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathservice_control.go
209 lines (188 loc) · 7.66 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2019 The Kubeflow 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 control
import (
"context"
"fmt"
"sync"
log "github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/record"
)
const (
// FailedCreateServiceReason is added in an event and in a job controller condition
// when a service for a job is failed to be created.
FailedCreateServiceReason = "FailedCreateService"
// SuccessfulCreateServiceReason is added in an event when a service for a job
// is successfully created.
SuccessfulCreateServiceReason = "SuccessfulCreateService"
// FailedDeleteServiceReason is added in an event and in a job condition
// when a service for a job is failed to be deleted.
FailedDeleteServiceReason = "FailedDeleteService"
// SuccessfulDeleteServiceReason is added in an event when a service for a job
// is successfully deleted.
SuccessfulDeleteServiceReason = "SuccessfulDeleteService"
)
// ServiceControlInterface is an interface that knows how to add or delete Services
// created as an interface to allow testing.
type ServiceControlInterface interface {
// CreateServices creates new Services according to the spec.
CreateServices(namespace string, service *v1.Service, object runtime.Object) error
// CreateServicesWithControllerRef creates new services according to the spec, and sets object as the service's controller.
CreateServicesWithControllerRef(namespace string, service *v1.Service, object runtime.Object, controllerRef *metav1.OwnerReference) error
// PatchService patches the service.
PatchService(namespace, name string, data []byte) error
// DeleteService deletes the service identified by serviceID.
DeleteService(namespace, serviceID string, object runtime.Object) error
}
// RealServiceControl is the default implementation of ServiceControlInterface.
type RealServiceControl struct {
KubeClient clientset.Interface
Recorder record.EventRecorder
}
func (r RealServiceControl) PatchService(namespace, name string, data []byte) error {
_, err := r.KubeClient.CoreV1().Services(namespace).Patch(context.TODO(), name, types.StrategicMergePatchType, data, metav1.PatchOptions{})
return err
}
func (r RealServiceControl) CreateServices(namespace string, service *v1.Service, object runtime.Object) error {
return r.createServices(namespace, service, object, nil)
}
func (r RealServiceControl) CreateServicesWithControllerRef(namespace string, service *v1.Service, controllerObject runtime.Object, controllerRef *metav1.OwnerReference) error {
if err := ValidateControllerRef(controllerRef); err != nil {
return err
}
return r.createServices(namespace, service, controllerObject, controllerRef)
}
func (r RealServiceControl) createServices(namespace string, service *v1.Service, object runtime.Object, controllerRef *metav1.OwnerReference) error {
if labels.Set(service.Labels).AsSelectorPreValidated().Empty() {
return fmt.Errorf("unable to create Services, no labels")
}
serviceWithOwner, err := GetServiceFromTemplate(service, object, controllerRef)
if err != nil {
r.Recorder.Eventf(object, v1.EventTypeWarning, FailedCreateServiceReason, "Error creating: %v", err)
return fmt.Errorf("unable to create services: %v", err)
}
newService, err := r.KubeClient.CoreV1().Services(namespace).Create(context.TODO(), serviceWithOwner, metav1.CreateOptions{})
if err != nil {
r.Recorder.Eventf(object, v1.EventTypeWarning, FailedCreateServiceReason, "Error creating: %v", err)
return fmt.Errorf("unable to create services: %v", err)
}
accessor, err := meta.Accessor(object)
if err != nil {
log.Errorf("parentObject does not have ObjectMeta, %v", err)
return nil
}
log.Infof("Controller %v created service %v", accessor.GetName(), newService.Name)
r.Recorder.Eventf(object, v1.EventTypeNormal, SuccessfulCreateServiceReason, "Created service: %v", newService.Name)
return nil
}
// DeleteService deletes the service identified by serviceID.
func (r RealServiceControl) DeleteService(namespace, serviceID string, object runtime.Object) error {
accessor, err := meta.Accessor(object)
if err != nil {
return fmt.Errorf("object does not have ObjectMeta, %v", err)
}
service, err := r.KubeClient.CoreV1().Services(namespace).Get(context.TODO(), serviceID, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
if service.DeletionTimestamp != nil {
log.Infof("service %s/%s is terminating, skip deleting", service.Namespace, service.Name)
return nil
}
log.Infof("Controller %v deleting service %v/%v", accessor.GetName(), namespace, serviceID)
if err := r.KubeClient.CoreV1().Services(namespace).Delete(context.TODO(), serviceID, metav1.DeleteOptions{}); err != nil {
r.Recorder.Eventf(object, v1.EventTypeWarning, FailedDeleteServiceReason, "Error deleting: %v", err)
return fmt.Errorf("unable to delete service: %v", err)
} else {
r.Recorder.Eventf(object, v1.EventTypeNormal, SuccessfulDeleteServiceReason, "Deleted service: %v", serviceID)
}
return nil
}
type FakeServiceControl struct {
sync.Mutex
Templates []v1.Service
ControllerRefs []metav1.OwnerReference
DeleteServiceName []string
Patches [][]byte
Err error
CreateLimit int
CreateCallCount int
}
var _ ServiceControlInterface = &FakeServiceControl{}
func (f *FakeServiceControl) PatchService(namespace, name string, data []byte) error {
f.Lock()
defer f.Unlock()
f.Patches = append(f.Patches, data)
if f.Err != nil {
return f.Err
}
return nil
}
func (f *FakeServiceControl) CreateServices(namespace string, service *v1.Service, object runtime.Object) error {
f.Lock()
defer f.Unlock()
f.CreateCallCount++
if f.CreateLimit != 0 && f.CreateCallCount > f.CreateLimit {
return fmt.Errorf("not creating service, limit %d already reached (create call %d)", f.CreateLimit, f.CreateCallCount)
}
f.Templates = append(f.Templates, *service)
if f.Err != nil {
return f.Err
}
return nil
}
func (f *FakeServiceControl) CreateServicesWithControllerRef(namespace string, service *v1.Service, object runtime.Object, controllerRef *metav1.OwnerReference) error {
f.Lock()
defer f.Unlock()
f.CreateCallCount++
if f.CreateLimit != 0 && f.CreateCallCount > f.CreateLimit {
return fmt.Errorf("not creating service, limit %d already reached (create call %d)", f.CreateLimit, f.CreateCallCount)
}
f.Templates = append(f.Templates, *service)
f.ControllerRefs = append(f.ControllerRefs, *controllerRef)
if f.Err != nil {
return f.Err
}
return nil
}
func (f *FakeServiceControl) DeleteService(namespace string, serviceID string, object runtime.Object) error {
f.Lock()
defer f.Unlock()
f.DeleteServiceName = append(f.DeleteServiceName, serviceID)
if f.Err != nil {
return f.Err
}
return nil
}
func (f *FakeServiceControl) Clear() {
f.Lock()
defer f.Unlock()
f.DeleteServiceName = []string{}
f.Templates = []v1.Service{}
f.ControllerRefs = []metav1.OwnerReference{}
f.Patches = [][]byte{}
f.CreateLimit = 0
f.CreateCallCount = 0
}