-
Notifications
You must be signed in to change notification settings - Fork 301
/
gcp.go
1061 lines (962 loc) · 31.2 KB
/
gcp.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2018 The Kubernetes 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 fuzz
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
computealpha "google.golang.org/api/compute/v0.alpha"
computebeta "google.golang.org/api/compute/v0.beta"
compute "google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
"k8s.io/klog/v2"
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/filter"
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
"k8s.io/ingress-gce/pkg/utils"
)
const (
NegResourceType = "networkEndpointGroup"
IgResourceType = "instanceGroup"
HttpProtocol = Protocol("HTTP")
HttpsProtocol = Protocol("HTTPS")
targetHTTPProxyResource = "targetHttpProxies"
targetHTTPSProxyResource = "targetHttpsProxies"
kubeSystemNS = "kube-system"
defaultHTTPBackend = "default-http-backend"
)
// Protocol specifies GCE loadbalancer protocol.
type Protocol string
// ForwardingRule is a union of the API version types.
type ForwardingRule struct {
GA *compute.ForwardingRule
Alpha *computealpha.ForwardingRule
Beta *computebeta.ForwardingRule
}
// TargetHTTPProxy is a union of the API version types.
type TargetHTTPProxy struct {
GA *compute.TargetHttpProxy
Alpha *computealpha.TargetHttpProxy
Beta *computebeta.TargetHttpProxy
}
// TargetHTTPSProxy is a union of the API version types.
type TargetHTTPSProxy struct {
GA *compute.TargetHttpsProxy
Alpha *computealpha.TargetHttpsProxy
Beta *computebeta.TargetHttpsProxy
}
// URLMap is a union of the API version types.
type URLMap struct {
GA *compute.UrlMap
Alpha *computealpha.UrlMap
Beta *computebeta.UrlMap
}
// BackendService is a union of the API version types.
type BackendService struct {
GA *compute.BackendService
Alpha *computealpha.BackendService
Beta *computebeta.BackendService
}
// HealthCheck is a union of the API version types.
type HealthCheck struct {
GA *compute.HealthCheck
}
// NetworkEndpointGroup is a union of the API version types.
type NetworkEndpointGroup struct {
GA *compute.NetworkEndpointGroup
Alpha *computealpha.NetworkEndpointGroup
Beta *computebeta.NetworkEndpointGroup
}
// InstanceGroup is a union of the API version types.
type InstanceGroup struct {
GA *compute.InstanceGroup
}
// NetworkEndpoints contains the NEG definition and the network Endpoints in NEG
type NetworkEndpoints struct {
NEG *compute.NetworkEndpointGroup
Endpoints []*compute.NetworkEndpointWithHealthStatus
}
// ServiceAttachment is a union of the API version types.
type ServiceAttachment struct {
GA *compute.ServiceAttachment
}
// GCLB contains the resources for a load balancer.
type GCLB struct {
VIP string
ForwardingRule map[meta.Key]*ForwardingRule
TargetHTTPProxy map[meta.Key]*TargetHTTPProxy
TargetHTTPSProxy map[meta.Key]*TargetHTTPSProxy
URLMap map[meta.Key]*URLMap
BackendService map[meta.Key]*BackendService
NetworkEndpointGroup map[meta.Key]*NetworkEndpointGroup
InstanceGroup map[meta.Key]*InstanceGroup
HealthCheck map[meta.Key]*HealthCheck
}
// NewGCLB returns an empty GCLB.
func NewGCLB(vip string) *GCLB {
return &GCLB{
VIP: vip,
ForwardingRule: map[meta.Key]*ForwardingRule{},
TargetHTTPProxy: map[meta.Key]*TargetHTTPProxy{},
TargetHTTPSProxy: map[meta.Key]*TargetHTTPSProxy{},
URLMap: map[meta.Key]*URLMap{},
BackendService: map[meta.Key]*BackendService{},
NetworkEndpointGroup: map[meta.Key]*NetworkEndpointGroup{},
InstanceGroup: map[meta.Key]*InstanceGroup{},
HealthCheck: map[meta.Key]*HealthCheck{},
}
}
// GCLBDeleteOptions may be provided when cleaning up GCLB resource.
type GCLBDeleteOptions struct {
// SkipDefaultBackend indicates whether to skip checking for the
// system default backend.
SkipDefaultBackend bool
// SkipBackends indicates whether to skip checking for the backends.
// This is enabled only when we know that backends are shared among multiple ingresses
// in which case shared backends are not cleaned up on ingress deletion.
SkipBackends bool
// CheckHttpFrontendResources indicates whether to check just the http
// frontend resources.
CheckHttpFrontendResources bool
// CheckHttpsFrontendResources indicates whether to check just the https
// frontend resources.
CheckHttpsFrontendResources bool
}
// CheckResourceDeletion checks the existence of the resources. Returns nil if
// all of the associated resources no longer exist.
func (g *GCLB) CheckResourceDeletion(ctx context.Context, c cloud.Cloud, options *GCLBDeleteOptions) error {
var resources []meta.Key
for k := range g.ForwardingRule {
var err error
if k.Region != "" {
_, err = c.ForwardingRules().Get(ctx, &k)
} else {
_, err = c.GlobalForwardingRules().Get(ctx, &k)
}
if err != nil {
if err.(*googleapi.Error) == nil || err.(*googleapi.Error).Code != http.StatusNotFound {
return fmt.Errorf("ForwardingRule %s is not deleted/error to get: %s", k.Name, err)
}
} else {
resources = append(resources, k)
}
}
for k := range g.TargetHTTPProxy {
var err error
if k.Region != "" {
// Use beta since GA isn't available yet
_, err = c.BetaRegionTargetHttpProxies().Get(ctx, &k)
} else {
_, err = c.TargetHttpProxies().Get(ctx, &k)
}
if err != nil {
if err.(*googleapi.Error) == nil || err.(*googleapi.Error).Code != http.StatusNotFound {
return fmt.Errorf("TargetHTTPProxy %s is not deleted/error to get: %s", k.Name, err)
}
} else {
resources = append(resources, k)
}
}
for k := range g.TargetHTTPSProxy {
var err error
if k.Region != "" {
// Use beta since GA isn't available yet
_, err = c.BetaRegionTargetHttpsProxies().Get(ctx, &k)
} else {
_, err = c.TargetHttpsProxies().Get(ctx, &k)
}
if err != nil {
if err.(*googleapi.Error) == nil || err.(*googleapi.Error).Code != http.StatusNotFound {
return fmt.Errorf("TargetHTTPSProxy %s is not deleted/error to get: %s", k.Name, err)
}
} else {
resources = append(resources, k)
}
}
for k := range g.URLMap {
var err error
if k.Region != "" {
_, err = c.BetaRegionUrlMaps().Get(ctx, &k)
} else {
_, err = c.UrlMaps().Get(ctx, &k)
}
if err != nil {
if err.(*googleapi.Error) == nil || err.(*googleapi.Error).Code != http.StatusNotFound {
return fmt.Errorf("URLMap %s is not deleted/error to get: %s", k.Name, err)
}
} else {
resources = append(resources, k)
}
}
if options == nil || !options.SkipBackends {
for k := range g.BackendService {
var err error
var bs *compute.BackendService
if k.Region != "" {
bs, err = c.RegionBackendServices().Get(ctx, &k)
} else {
bs, err = c.BackendServices().Get(ctx, &k)
}
if err != nil {
if err.(*googleapi.Error) == nil || err.(*googleapi.Error).Code != http.StatusNotFound {
return fmt.Errorf("BackendService %s is not deleted/error to get: %s", k.Name, err)
}
} else {
if options != nil && options.SkipDefaultBackend {
desc := utils.DescriptionFromString(bs.Description)
if desc.ServiceName == fmt.Sprintf("%s/%s", kubeSystemNS, defaultHTTPBackend) {
continue
}
}
resources = append(resources, k)
}
}
for k := range g.NetworkEndpointGroup {
ns, err := c.BetaNetworkEndpointGroups().Get(ctx, &k)
if err != nil {
if err.(*googleapi.Error) == nil || err.(*googleapi.Error).Code != http.StatusNotFound {
return fmt.Errorf("NetworkEndpointGroup %s is not deleted/error to get: %s", k.Name, err)
}
} else {
// TODO(smatti): Add NEG description to make this less error prone.
// This is to ensure that ILB tests that use NEGs are not blocked on default NEG deletion.
// Also, the default NEG may not get recognized here if default http backend name is changed
// to cause truncation.
if options != nil && options.SkipDefaultBackend &&
strings.Contains(ns.Name, fmt.Sprintf("%s-%s", kubeSystemNS, defaultHTTPBackend)) {
continue
}
resources = append(resources, k)
}
}
}
if len(resources) != 0 {
var s []string
for _, r := range resources {
s = append(s, r.String())
}
return fmt.Errorf("resources still exist (%s)", strings.Join(s, ", "))
}
return nil
}
// CheckResourceDeletionByProtocol checks the existence of the resources for given protocol.
// Returns nil if all of the associated frontend resources no longer exist.
func (g *GCLB) CheckResourceDeletionByProtocol(ctx context.Context, c cloud.Cloud, options *GCLBDeleteOptions, protocol Protocol) error {
var resources []meta.Key
for k, gfr := range g.ForwardingRule {
// Check if forwarding rule matches given protocol.
if gfrProtocol, err := getForwardingRuleProtocol(gfr.GA); err != nil {
return err
} else if gfrProtocol != protocol {
continue
}
var err error
if k.Region != "" {
_, err = c.ForwardingRules().Get(ctx, &k)
} else {
_, err = c.GlobalForwardingRules().Get(ctx, &k)
}
if err != nil {
if err.(*googleapi.Error) == nil || err.(*googleapi.Error).Code != http.StatusNotFound {
return fmt.Errorf("ForwardingRule %s is not deleted/error to get: %s", k.Name, err)
}
} else {
resources = append(resources, k)
}
}
switch protocol {
case HttpProtocol:
for k := range g.TargetHTTPProxy {
_, err := c.TargetHttpProxies().Get(ctx, &k)
if err != nil {
if err.(*googleapi.Error) == nil || err.(*googleapi.Error).Code != http.StatusNotFound {
return fmt.Errorf("TargetHTTPProxy %s is not deleted/error to get: %s", k.Name, err)
}
} else {
resources = append(resources, k)
}
}
case HttpsProtocol:
for k := range g.TargetHTTPSProxy {
_, err := c.TargetHttpsProxies().Get(ctx, &k)
if err != nil {
if err.(*googleapi.Error) == nil || err.(*googleapi.Error).Code != http.StatusNotFound {
return fmt.Errorf("TargetHTTPSProxy %s is not deleted/error to get: %s", k.Name, err)
}
} else {
resources = append(resources, k)
}
}
default:
return fmt.Errorf("invalid protocol %q", protocol)
}
if len(resources) != 0 {
var s []string
for _, r := range resources {
s = append(s, r.String())
}
return fmt.Errorf("resources still exist (%s)", strings.Join(s, ", "))
}
return nil
}
// getForwardingRuleProtocol returns the protocol for given forwarding rule.
func getForwardingRuleProtocol(forwardingRule *compute.ForwardingRule) (Protocol, error) {
resID, err := cloud.ParseResourceURL(forwardingRule.Target)
if err != nil {
return "", fmt.Errorf("error parsing Target (%q): %v", forwardingRule.Target, err)
}
switch resID.Resource {
case targetHTTPProxyResource:
return HttpProtocol, nil
case targetHTTPSProxyResource:
return HttpsProtocol, nil
default:
return "", fmt.Errorf("unhandled resource %q", resID.Resource)
}
}
// CheckNEGDeletion checks that all NEGs associated with the GCLB have been deleted
func (g *GCLB) CheckNEGDeletion(ctx context.Context, c cloud.Cloud, options *GCLBDeleteOptions) error {
var resources []meta.Key
for k := range g.NetworkEndpointGroup {
_, err := c.BetaNetworkEndpointGroups().Get(ctx, &k)
if err != nil {
if err.(*googleapi.Error) == nil || err.(*googleapi.Error).Code != http.StatusNotFound {
return err
}
} else {
resources = append(resources, k)
}
}
if len(resources) != 0 {
var s []string
for _, r := range resources {
s = append(s, r.String())
}
return fmt.Errorf("NEGs still exist (%s)", strings.Join(s, ", "))
}
return nil
}
// CheckRedirectURLMapDeletion checks that the Redirect URL map associated with the GCLB is deleted
// This assumes that there is only one redirect url map
func (g *GCLB) CheckRedirectUrlMapDeletion(ctx context.Context, c cloud.Cloud) error {
for k := range g.URLMap {
if strings.Contains(k.Name, "-rm") {
_, err := c.UrlMaps().Get(ctx, &k)
if err != nil {
if err.(*googleapi.Error) == nil || err.(*googleapi.Error).Code != http.StatusNotFound {
return err
}
} else {
return fmt.Errorf("Redirect URL Map still exists: %v", k)
}
}
}
return nil
}
func hasAlphaResource(resourceType string, validators []FeatureValidator) bool {
for _, val := range validators {
if val.HasAlphaResource(resourceType) {
return true
}
}
return false
}
func hasBetaResource(resourceType string, validators []FeatureValidator) bool {
for _, val := range validators {
if val.HasBetaResource(resourceType) {
return true
}
}
return false
}
type GCLBForVIPParams struct {
VIP string
Region string
Network string
Validators []FeatureValidator
IsForRegionalXLB bool
}
// GCLBForVIP retrieves all of the resources associated with the GCLB for a given VIP.
func GCLBForVIP(ctx context.Context, c cloud.Cloud, params *GCLBForVIPParams) (*GCLB, error) {
gclb := NewGCLB(params.VIP)
if params.Region != "" {
err := RegionalGCLBForVIP(ctx, c, gclb, params)
return gclb, err
}
allGFRs, err := c.GlobalForwardingRules().List(ctx, filter.None)
if err != nil {
klog.Warningf("Error listing forwarding rules: %v", err)
return nil, err
}
var gfrs []*compute.ForwardingRule
for _, gfr := range allGFRs {
if gfr.IPAddress == params.VIP {
gfrs = append(gfrs, gfr)
}
}
// Return immediately if there are no forwarding rules exist.
if len(gfrs) == 0 {
klog.Warningf("No global forwarding rules found, can't get all GCLB resources")
return gclb, nil
}
var urlMapKey *meta.Key
for _, gfr := range gfrs {
frKey := meta.GlobalKey(gfr.Name)
gclb.ForwardingRule[*frKey] = &ForwardingRule{GA: gfr}
if hasAlphaResource("forwardingRule", params.Validators) {
fr, err := c.AlphaForwardingRules().Get(ctx, frKey)
if err != nil {
klog.Warningf("Error getting alpha forwarding rules: %v", err)
return nil, err
}
gclb.ForwardingRule[*frKey].Alpha = fr
}
if hasBetaResource("forwardingRule", params.Validators) {
return nil, errors.New("unsupported forwardingRule version")
}
// ForwardingRule => TargetProxy
resID, err := cloud.ParseResourceURL(gfr.Target)
if err != nil {
klog.Warningf("Error parsing Target (%q): %v", gfr.Target, err)
return nil, err
}
switch resID.Resource {
case targetHTTPProxyResource:
p, err := c.TargetHttpProxies().Get(ctx, resID.Key)
if err != nil {
klog.Warningf("Error getting TargetHttpProxy %s: %v", resID.Key, err)
return nil, err
}
gclb.TargetHTTPProxy[*resID.Key] = &TargetHTTPProxy{GA: p}
if hasAlphaResource("targetHttpProxy", params.Validators) || hasBetaResource("targetHttpProxy", params.Validators) {
return nil, errors.New("unsupported targetHttpProxy version")
}
urlMapResID, err := cloud.ParseResourceURL(p.UrlMap)
if err != nil {
klog.Warningf("Error parsing urlmap URL (%q): %v", p.UrlMap, err)
return nil, err
}
if urlMapKey == nil {
urlMapKey = urlMapResID.Key
}
if *urlMapKey != *urlMapResID.Key {
klog.Warningf("Error targetHttpProxy references are not the same (%s != %s)", *urlMapKey, *urlMapResID.Key)
return nil, fmt.Errorf("targetHttpProxy references are not the same: %+v != %+v", *urlMapKey, *urlMapResID.Key)
}
case targetHTTPSProxyResource:
p, err := c.TargetHttpsProxies().Get(ctx, resID.Key)
if err != nil {
klog.Warningf("Error getting targetHttpsProxy (%s): %v", resID.Key, err)
return nil, err
}
gclb.TargetHTTPSProxy[*resID.Key] = &TargetHTTPSProxy{GA: p}
if hasAlphaResource("targetHttpsProxy", params.Validators) || hasBetaResource("targetHttpsProxy", params.Validators) {
return nil, errors.New("unsupported targetHttpsProxy version")
}
urlMapResID, err := cloud.ParseResourceURL(p.UrlMap)
if err != nil {
klog.Warningf("Error parsing urlmap URL (%q): %v", p.UrlMap, err)
return nil, err
}
if urlMapKey == nil {
urlMapKey = urlMapResID.Key
}
// Ignore redirect urlmaps since they will not have backends, but add them to the gclb map
if strings.Contains(urlMapKey.Name, "-rm-") {
urlMap, err := c.UrlMaps().Get(ctx, urlMapKey)
if err != nil {
return nil, err
}
gclb.URLMap[*urlMapKey] = &URLMap{GA: urlMap}
urlMapKey = urlMapResID.Key
}
if *urlMapKey != *urlMapResID.Key {
klog.Warningf("Error targetHttpsProxy references are not the same (%s != %s)", *urlMapKey, *urlMapResID.Key)
return nil, fmt.Errorf("targetHttpsProxy references are not the same: %+v != %+v", *urlMapKey, *urlMapResID.Key)
}
default:
klog.Errorf("Unhandled resource: %q, grf = %+v", resID.Resource, gfr)
return nil, fmt.Errorf("unhandled resource %q", resID.Resource)
}
}
// TargetProxy => URLMap
urlMap, err := c.UrlMaps().Get(ctx, urlMapKey)
if err != nil {
return nil, err
}
gclb.URLMap[*urlMapKey] = &URLMap{GA: urlMap}
if hasAlphaResource("urlMap", params.Validators) || hasBetaResource("urlMap", params.Validators) {
return nil, errors.New("unsupported urlMap version")
}
// URLMap => BackendService(s)
var bsKeys []*meta.Key
resID, err := cloud.ParseResourceURL(urlMap.DefaultService)
if err != nil {
return nil, err
}
bsKeys = append(bsKeys, resID.Key)
for _, pm := range urlMap.PathMatchers {
resID, err := cloud.ParseResourceURL(pm.DefaultService)
if err != nil {
return nil, err
}
bsKeys = append(bsKeys, resID.Key)
for _, pr := range pm.PathRules {
resID, err := cloud.ParseResourceURL(pr.Service)
if err != nil {
return nil, err
}
bsKeys = append(bsKeys, resID.Key)
}
}
for _, bsKey := range bsKeys {
bs, err := c.BackendServices().Get(ctx, bsKey)
if err != nil {
return nil, err
}
gclb.BackendService[*bsKey] = &BackendService{GA: bs}
if hasAlphaResource("backendService", params.Validators) {
bs, err := c.AlphaBackendServices().Get(ctx, bsKey)
if err != nil {
return nil, err
}
gclb.BackendService[*bsKey].Alpha = bs
}
if hasBetaResource("backendService", params.Validators) {
bs, err := c.BetaBackendServices().Get(ctx, bsKey)
if err != nil {
return nil, err
}
gclb.BackendService[*bsKey].Beta = bs
}
for _, hcURL := range bs.HealthChecks {
rID, err := cloud.ParseResourceURL(hcURL)
if err != nil {
return nil, err
}
hc, err := c.HealthChecks().Get(ctx, rID.Key)
if err != nil {
return nil, err
}
gclb.HealthCheck[*rID.Key] = &HealthCheck{
GA: hc,
}
}
}
var negKeys []*meta.Key
var igKeys []*meta.Key
// Fetch NEG Backends
for _, bsKey := range bsKeys {
var beGroups []string
if hasAlphaResource("backendService", params.Validators) {
bs, err := c.AlphaBackendServices().Get(ctx, bsKey)
if err != nil {
return nil, err
}
for _, be := range bs.Backends {
beGroups = append(beGroups, be.Group)
}
} else {
bs, err := c.BetaBackendServices().Get(ctx, bsKey)
if err != nil {
return nil, err
}
for _, be := range bs.Backends {
beGroups = append(beGroups, be.Group)
}
}
for _, group := range beGroups {
if strings.Contains(group, NegResourceType) {
resourceId, err := cloud.ParseResourceURL(group)
if err != nil {
return nil, err
}
negKeys = append(negKeys, resourceId.Key)
}
if strings.Contains(group, IgResourceType) {
resourceId, err := cloud.ParseResourceURL(group)
if err != nil {
return nil, err
}
igKeys = append(igKeys, resourceId.Key)
}
}
}
for _, negKey := range negKeys {
neg, err := c.NetworkEndpointGroups().Get(ctx, negKey)
if err != nil {
return nil, err
}
gclb.NetworkEndpointGroup[*negKey] = &NetworkEndpointGroup{GA: neg}
if hasAlphaResource(NegResourceType, params.Validators) {
neg, err := c.AlphaNetworkEndpointGroups().Get(ctx, negKey)
if err != nil {
return nil, err
}
gclb.NetworkEndpointGroup[*negKey].Alpha = neg
}
if hasBetaResource(NegResourceType, params.Validators) {
neg, err := c.BetaNetworkEndpointGroups().Get(ctx, negKey)
if err != nil {
return nil, err
}
gclb.NetworkEndpointGroup[*negKey].Beta = neg
}
}
for _, igKey := range igKeys {
ig, err := c.InstanceGroups().Get(ctx, igKey)
if err != nil {
return nil, err
}
gclb.InstanceGroup[*igKey] = &InstanceGroup{GA: ig}
}
return gclb, err
}
// RegionalGCLBForVIP retrieves all of the resources associated with the GCLB for a given VIP.
func RegionalGCLBForVIP(ctx context.Context, c cloud.Cloud, gclb *GCLB, params *GCLBForVIPParams) error {
allRFRs, err := c.ForwardingRules().List(ctx, params.Region, filter.None)
if err != nil {
klog.Warningf("Error listing forwarding rules: %v", err)
return err
}
var rfrs []*compute.ForwardingRule
for _, rfr := range allRFRs {
if rfr.IPAddress == params.VIP {
if rfr.Network == "" {
continue
}
netResID, err := cloud.ParseResourceURL(rfr.Network)
if err != nil {
klog.Warningf("Error parsing Network (%q): %v", rfr.Network, err)
return err
}
if netResID.Key.Name == params.Network {
rfrs = append(rfrs, rfr)
}
}
}
if len(rfrs) == 0 {
klog.Warningf("No regional forwarding rules found for IP %s, network %s, can't get all GCLB resources", params.VIP, params.Network)
return nil
}
var urlMapKey *meta.Key
for _, rfr := range rfrs {
frKey := meta.RegionalKey(rfr.Name, params.Region)
gclb.ForwardingRule[*frKey] = &ForwardingRule{GA: rfr}
if hasAlphaResource("forwardingRule", params.Validators) {
fr, err := c.AlphaForwardingRules().Get(ctx, frKey)
if err != nil {
klog.Warningf("Error getting alpha forwarding rules: %v", err)
return err
}
gclb.ForwardingRule[*frKey].Alpha = fr
}
if hasBetaResource("forwardingRule", params.Validators) {
fr, err := c.BetaForwardingRules().Get(ctx, frKey)
if err != nil {
klog.Warningf("Error getting alpha forwarding rules: %v", err)
return err
}
gclb.ForwardingRule[*frKey].Beta = fr
}
// ForwardingRule => TargetProxy
resID, err := cloud.ParseResourceURL(rfr.Target)
if err != nil {
klog.Warningf("Error parsing Target (%q): %v", rfr.Target, err)
return err
}
switch resID.Resource {
case "targetHttpProxies":
// Use beta by default since not GA yet
p, err := c.BetaRegionTargetHttpProxies().Get(ctx, resID.Key)
if err != nil {
klog.Warningf("Error getting TargetHttpProxy %s: %v", resID.Key, err)
return err
}
gclb.TargetHTTPProxy[*resID.Key] = &TargetHTTPProxy{Beta: p}
if hasAlphaResource("targetHttpProxy", params.Validators) || hasBetaResource("targetHttpProxy", params.Validators) {
return errors.New("unsupported targetHttpProxy version")
}
urlMapResID, err := cloud.ParseResourceURL(p.UrlMap)
if err != nil {
klog.Warningf("Error parsing urlmap URL (%q): %v", p.UrlMap, err)
return err
}
if urlMapKey == nil {
urlMapKey = urlMapResID.Key
}
if *urlMapKey != *urlMapResID.Key {
klog.Warningf("Error targetHttpProxy references are not the same (%s != %s)", *urlMapKey, *urlMapResID.Key)
return fmt.Errorf("targetHttpProxy references are not the same: %+v != %+v", *urlMapKey, *urlMapResID.Key)
}
case "targetHttpsProxies":
// Use Beta by default since not GA yet
p, err := c.BetaRegionTargetHttpsProxies().Get(ctx, resID.Key)
if err != nil {
klog.Warningf("Error getting targetHttpsProxy (%s): %v", resID.Key, err)
return err
}
gclb.TargetHTTPSProxy[*resID.Key] = &TargetHTTPSProxy{Beta: p}
if hasAlphaResource("targetHttpsProxy", params.Validators) || hasBetaResource("targetHttpsProxy", params.Validators) {
return errors.New("unsupported targetHttpsProxy version")
}
urlMapResID, err := cloud.ParseResourceURL(p.UrlMap)
if err != nil {
klog.Warningf("Error parsing urlmap URL (%q): %v", p.UrlMap, err)
return err
}
if urlMapKey == nil {
urlMapKey = urlMapResID.Key
}
// Ignore redirect urlmaps since they will not have backends, but add them to the gclb map
if params.IsForRegionalXLB && strings.Contains(urlMapKey.Name, "-rm-") {
urlMap, err := c.RegionUrlMaps().Get(ctx, urlMapKey)
if err != nil {
return err
}
gclb.URLMap[*urlMapKey] = &URLMap{GA: urlMap}
urlMapKey = urlMapResID.Key
}
if *urlMapKey != *urlMapResID.Key {
klog.Warningf("Error targetHttpsProxy references are not the same (%s != %s)", *urlMapKey, *urlMapResID.Key)
return fmt.Errorf("targetHttpsProxy references are not the same: %+v != %+v", *urlMapKey, *urlMapResID.Key)
}
default:
klog.Errorf("Unhandled resource: %q, grf = %+v", resID.Resource, rfr)
return fmt.Errorf("unhandled resource %q", resID.Resource)
}
}
// TargetProxy => URLMap
// Use beta since params.Region is not GA yet
urlMap, err := c.BetaRegionUrlMaps().Get(ctx, urlMapKey)
if err != nil {
return err
}
gclb.URLMap[*urlMapKey] = &URLMap{Beta: urlMap}
if hasAlphaResource("urlMap", params.Validators) || hasBetaResource("urlMap", params.Validators) {
return errors.New("unsupported urlMap version")
}
// URLMap => BackendService(s)
var bsKeys []*meta.Key
resID, err := cloud.ParseResourceURL(urlMap.DefaultService)
if err != nil {
return err
}
bsKeys = append(bsKeys, resID.Key)
for _, pm := range urlMap.PathMatchers {
resID, err := cloud.ParseResourceURL(pm.DefaultService)
if err != nil {
return err
}
bsKeys = append(bsKeys, resID.Key)
for _, pr := range pm.PathRules {
resID, err := cloud.ParseResourceURL(pr.Service)
if err != nil {
return err
}
bsKeys = append(bsKeys, resID.Key)
}
}
for _, bsKey := range bsKeys {
bs, err := c.RegionBackendServices().Get(ctx, bsKey)
if err != nil {
return err
}
gclb.BackendService[*bsKey] = &BackendService{GA: bs}
if hasAlphaResource("backendService", params.Validators) {
bs, err := c.AlphaRegionBackendServices().Get(ctx, bsKey)
if err != nil {
return err
}
gclb.BackendService[*bsKey].Alpha = bs
}
if hasBetaResource("backendService", params.Validators) {
bs, err := c.BetaRegionBackendServices().Get(ctx, bsKey)
if err != nil {
return err
}
gclb.BackendService[*bsKey].Beta = bs
}
for _, hcURL := range bs.HealthChecks {
rID, err := cloud.ParseResourceURL(hcURL)
if err != nil {
return err
}
hc, err := c.RegionHealthChecks().Get(ctx, rID.Key)
if err != nil {
return err
}
gclb.HealthCheck[*rID.Key] = &HealthCheck{
GA: hc,
}
}
}
var negKeys []*meta.Key
var igKeys []*meta.Key
// Fetch NEG Backends
for _, bsKey := range bsKeys {
var beGroups []string
if hasAlphaResource("backendService", params.Validators) {
bs, err := c.AlphaRegionBackendServices().Get(ctx, bsKey)
if err != nil {
return err
}
for _, be := range bs.Backends {
beGroups = append(beGroups, be.Group)
}
} else {
bs, err := c.BetaRegionBackendServices().Get(ctx, bsKey)
if err != nil {
return err
}
for _, be := range bs.Backends {
beGroups = append(beGroups, be.Group)
}
}
for _, group := range beGroups {
if strings.Contains(group, NegResourceType) {
resourceId, err := cloud.ParseResourceURL(group)
if err != nil {
return err
}
negKeys = append(negKeys, resourceId.Key)
}
if strings.Contains(group, IgResourceType) {
resourceId, err := cloud.ParseResourceURL(group)
if err != nil {
return err
}
igKeys = append(igKeys, resourceId.Key)
}
}
}
for _, negKey := range negKeys {
neg, err := c.NetworkEndpointGroups().Get(ctx, negKey)
if err != nil {
return err
}
gclb.NetworkEndpointGroup[*negKey] = &NetworkEndpointGroup{GA: neg}
if hasAlphaResource(NegResourceType, params.Validators) {
neg, err := c.AlphaNetworkEndpointGroups().Get(ctx, negKey)
if err != nil {
return err
}
gclb.NetworkEndpointGroup[*negKey].Alpha = neg
}
if hasBetaResource(NegResourceType, params.Validators) {
neg, err := c.BetaNetworkEndpointGroups().Get(ctx, negKey)
if err != nil {
return err
}
gclb.NetworkEndpointGroup[*negKey].Beta = neg
}
}
for _, igKey := range igKeys {
ig, err := c.InstanceGroups().Get(ctx, igKey)
if err != nil {
return err
}
gclb.InstanceGroup[*igKey] = &InstanceGroup{GA: ig}
}
return err
}
// NetworkEndpointsInNegs retrieves the network Endpoints from NEGs with one name in multiple zones
func NetworkEndpointsInNegs(ctx context.Context, c cloud.Cloud, name string, zones []string) (map[meta.Key]*NetworkEndpoints, error) {
ret := map[meta.Key]*NetworkEndpoints{}
for _, zone := range zones {
key := meta.ZonalKey(name, zone)
neg, err := c.NetworkEndpointGroups().Get(ctx, key)
if err != nil {
return nil, err
}
networkEndpoints := &NetworkEndpoints{
NEG: neg,
}
nes, err := c.NetworkEndpointGroups().ListNetworkEndpoints(ctx, key, &compute.NetworkEndpointGroupsListEndpointsRequest{HealthStatus: "SHOW"}, nil)
if err != nil {
return nil, err
}
networkEndpoints.Endpoints = nes
ret[*key] = networkEndpoints
}
return ret, nil
}
// CheckStandaloneNEGDeletion checks that specified NEG has been deleted
func CheckStandaloneNEGDeletion(ctx context.Context, c cloud.Cloud, negName, port string, zones []string) (bool, error) {
var foundNegs []string
for _, zone := range zones {
key := meta.ZonalKey(negName, zone)
neg, err := c.NetworkEndpointGroups().Get(ctx, key)
if err != nil {
if e, ok := err.(*googleapi.Error); ok && e.Code == http.StatusNotFound {
continue
}
return false, err
}
if neg.Description != "" {
desc, err := utils.NegDescriptionFromString(neg.Description)
if err == nil && desc.Port != port {