-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathuploader_service.go
More file actions
333 lines (287 loc) · 10.5 KB
/
Copy pathuploader_service.go
File metadata and controls
333 lines (287 loc) · 10.5 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
Copyright 2024 Flant JSC
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 service
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/deckhouse/virtualization-controller/pkg/common/annotations"
"github.com/deckhouse/virtualization-controller/pkg/common/datasource"
networkpolicy "github.com/deckhouse/virtualization-controller/pkg/common/network_policy"
"github.com/deckhouse/virtualization-controller/pkg/controller/supplements"
"github.com/deckhouse/virtualization-controller/pkg/controller/uploader"
"github.com/deckhouse/virtualization-controller/pkg/dvcr"
"github.com/deckhouse/virtualization-controller/pkg/logger"
)
type UploaderService struct {
client client.Client
dvcrSettings *dvcr.Settings
protection *ProtectionService
image string
pullPolicy string
verbose string
controllerName string
requirements corev1.ResourceRequirements
}
func NewUploaderService(
dvcrSettings *dvcr.Settings,
client client.Client,
image string,
requirements corev1.ResourceRequirements,
pullPolicy string,
verbose string,
controllerName string,
protection *ProtectionService,
) *UploaderService {
return &UploaderService{
dvcrSettings: dvcrSettings,
client: client,
image: image,
requirements: requirements,
pullPolicy: pullPolicy,
verbose: verbose,
controllerName: controllerName,
protection: protection,
}
}
func (s UploaderService) Start(
ctx context.Context,
settings *uploader.Settings,
obj client.Object,
sup supplements.Generator,
caBundle *datasource.CABundle,
opts ...Option,
) error {
options := newGenericOptions(opts...)
ownerRef := metav1.NewControllerRef(obj, obj.GetObjectKind().GroupVersionKind())
settings.Verbose = s.verbose
podSettings := s.getPodSettings(ownerRef, sup)
if options.nodePlacement != nil {
podSettings.NodePlacement = options.nodePlacement
}
pod, err := uploader.NewPod(podSettings, settings).GetOrCreate(ctx, s.client)
if err != nil {
return err
}
err = networkpolicy.CreateNetworkPolicy(ctx, s.client, pod, sup, s.protection.GetFinalizer())
if err != nil {
return fmt.Errorf("failed to create NetworkPolicy: %w", err)
}
err = supplements.EnsureForPod(ctx, s.client, sup, pod, caBundle, s.dvcrSettings)
if err != nil {
return err
}
_, err = uploader.NewService(s.getServiceSettings(ownerRef, sup)).Create(ctx, s.client)
if err != nil && !k8serrors.IsAlreadyExists(err) {
return err
}
ing, err := uploader.NewIngress(s.getIngressSettings(ownerRef, sup)).Apply(ctx, s.client)
if err != nil {
return err
}
return supplements.EnsureForIngress(ctx, s.client, sup, ing, s.dvcrSettings)
}
func (s UploaderService) CleanUp(ctx context.Context, sup supplements.Generator) (bool, error) {
return s.CleanUpSupplements(ctx, sup)
}
func (s UploaderService) CleanUpSupplements(ctx context.Context, sup supplements.Generator) (bool, error) {
pod, err := s.GetPod(ctx, sup)
if err != nil {
return false, err
}
svc, err := s.GetService(ctx, sup)
if err != nil {
return false, err
}
ing, err := s.GetIngress(ctx, sup)
if err != nil {
return false, err
}
networkPolicy, err := networkpolicy.GetNetworkPolicy(ctx, s.client, sup.LegacyUploaderPod(), sup)
if err != nil {
return false, err
}
err = s.protection.RemoveProtection(ctx, pod, svc, ing, networkPolicy)
if err != nil {
return false, err
}
var haveDeleted bool
if pod != nil {
haveDeleted = true
err = s.client.Delete(ctx, pod)
if err != nil && !k8serrors.IsNotFound(err) {
return false, err
}
}
if networkPolicy != nil {
haveDeleted = true
err = s.client.Delete(ctx, networkPolicy)
if err != nil && !k8serrors.IsNotFound(err) {
return false, err
}
}
if svc != nil {
haveDeleted = true
err = s.client.Delete(ctx, svc)
if err != nil && !k8serrors.IsNotFound(err) {
return false, err
}
}
if ing != nil {
haveDeleted = true
err = s.client.Delete(ctx, ing)
if err != nil && !k8serrors.IsNotFound(err) {
return false, err
}
}
return haveDeleted, nil
}
func (s UploaderService) Protect(ctx context.Context, sup supplements.Generator, pod *corev1.Pod, svc *corev1.Service, ing *netv1.Ingress) (err error) {
var networkPolicy *netv1.NetworkPolicy
if pod != nil {
networkPolicy, err = networkpolicy.GetNetworkPolicyFromObject(ctx, s.client, pod, sup)
if err != nil {
return fmt.Errorf("failed to get networkPolicy for removing importer's supplements protection: %w", err)
}
}
err = s.protection.AddProtection(ctx, networkPolicy, pod, svc, ing)
if err != nil {
return fmt.Errorf("failed to add protection for uploader's supplements: %w", err)
}
return nil
}
func (s UploaderService) Unprotect(ctx context.Context, sup supplements.Generator, pod *corev1.Pod, svc *corev1.Service, ing *netv1.Ingress) (err error) {
var networkPolicy *netv1.NetworkPolicy
if pod != nil {
networkPolicy, err = networkpolicy.GetNetworkPolicyFromObject(ctx, s.client, pod, sup)
if err != nil {
return fmt.Errorf("failed to get networkPolicy for removing importer's supplements protection: %w", err)
}
}
err = s.protection.RemoveProtection(ctx, networkPolicy, pod, svc, ing)
if err != nil {
return fmt.Errorf("failed to remove protection for uploader's supplements: %w", err)
}
return nil
}
func (s UploaderService) GetPod(ctx context.Context, sup supplements.Generator) (*corev1.Pod, error) {
pod := &corev1.Pod{}
return supplements.FetchSupplement(ctx, s.client, sup, supplements.SupplementUploaderPod, pod)
}
func (s UploaderService) GetService(ctx context.Context, sup supplements.Generator) (*corev1.Service, error) {
svc := &corev1.Service{}
return supplements.FetchSupplement(ctx, s.client, sup, supplements.SupplementUploaderService, svc)
}
func (s UploaderService) GetIngress(ctx context.Context, sup supplements.Generator) (*netv1.Ingress, error) {
ing := &netv1.Ingress{}
return supplements.FetchSupplement(ctx, s.client, sup, supplements.SupplementUploaderIngress, ing)
}
func (s UploaderService) GetExternalURL(ctx context.Context, ing *netv1.Ingress) string {
url := ing.Annotations[annotations.AnnUploadURL]
if url == "" {
// Fallback to deprecated annotation.
url = ing.Annotations[annotations.AnnUploadURLDeprecated]
}
if url == "" {
logger.FromContext(ctx).Error("unexpected empty upload url, please report a bug")
return ""
}
return url
}
func (s UploaderService) GetInClusterURL(ctx context.Context, svc *corev1.Service) string {
if svc.Spec.ClusterIP == "" {
logger.FromContext(ctx).Error("unexpected empty cluster ip, please report a bug", "clusterIP", svc.Spec.ClusterIP)
return ""
}
return fmt.Sprintf("http://%s/upload", svc.Spec.ClusterIP)
}
func (s UploaderService) getPodSettings(ownerRef *metav1.OwnerReference, sup supplements.Generator) *uploader.PodSettings {
uploaderPod := sup.UploaderPod()
uploaderSvc := sup.UploaderService()
return &uploader.PodSettings{
Name: uploaderPod.Name,
Image: s.image,
PullPolicy: s.pullPolicy,
Namespace: uploaderPod.Namespace,
OwnerReference: *ownerRef,
ControllerName: s.controllerName,
InstallerLabels: map[string]string{},
ServiceName: uploaderSvc.Name,
ResourceRequirements: &s.requirements,
}
}
func (s UploaderService) getServiceSettings(ownerRef *metav1.OwnerReference, sup supplements.Generator) *uploader.ServiceSettings {
uploaderSvc := sup.UploaderService()
return &uploader.ServiceSettings{
Name: uploaderSvc.Name,
Namespace: uploaderSvc.Namespace,
OwnerReference: *ownerRef,
}
}
// EnsureIngress reconciles the uploader Ingress with server-side apply and
// returns the fresh object. It is cheap: callers should invoke it only when the
// already-fetched Ingress host drifted from the configured UPLOADER_INGRESS_HOST
// (e.g. after publicDomainTemplate change). When there is no drift, skip the call:
// Apply is a no-op only when managed fields already match, but avoiding the
// round-trip entirely keeps steady-state reconcile free of extra writes.
func (s UploaderService) EnsureIngress(ctx context.Context, obj client.Object, sup supplements.Generator) (*netv1.Ingress, error) {
ownerRef := metav1.NewControllerRef(obj, obj.GetObjectKind().GroupVersionKind())
ing, err := uploader.NewIngress(s.getIngressSettings(ownerRef, sup)).Apply(ctx, s.client)
if err != nil {
return nil, err
}
if err := supplements.EnsureForIngress(ctx, s.client, sup, ing, s.dvcrSettings); err != nil {
return nil, err
}
return ing, nil
}
// ExpectedIngressHost returns the host the uploader Ingress should expose,
// derived from UPLOADER_INGRESS_HOST (i.e. the rendered publicDomainTemplate).
func (s UploaderService) ExpectedIngressHost() string {
return s.dvcrSettings.UploaderIngressSettings.Host
}
// IngressHostDrifted reports whether the given Ingress host differs from the
// configured one. An absent Ingress (nil) or an Ingress without rules is
// treated as drift so callers fall back to EnsureIngress / Start.
func (s UploaderService) IngressHostDrifted(ing *netv1.Ingress) bool {
expected := s.ExpectedIngressHost()
if ing == nil || len(ing.Spec.Rules) == 0 {
return true
}
return ing.Spec.Rules[0].Host != expected
}
func (s UploaderService) getIngressSettings(ownerRef *metav1.OwnerReference, sup supplements.Generator) *uploader.IngressSettings {
uploaderIng := sup.UploaderIngress()
uploaderSvc := sup.UploaderService()
secretName := s.dvcrSettings.UploaderIngressSettings.TLSSecret
if supplements.ShouldCopyUploaderTLSSecret(s.dvcrSettings, sup) {
secretName = sup.UploaderTLSSecretForIngress().Name
}
var class *string
if c := s.dvcrSettings.UploaderIngressSettings.Class; c != "" {
class = &c
}
return &uploader.IngressSettings{
Name: uploaderIng.Name,
Namespace: uploaderIng.Namespace,
Host: s.dvcrSettings.UploaderIngressSettings.Host,
TLSSecretName: secretName,
ServiceName: uploaderSvc.Name,
ClassName: class,
OwnerReference: *ownerRef,
}
}