generated from SAP/repository-template
/
reconcile-domains.go
1288 lines (1156 loc) · 48.1 KB
/
reconcile-domains.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
/*
SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and cap-operator contributors
SPDX-License-Identifier: Apache-2.0
*/
package controller
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
certManagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
certManagermetav1 "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
certv1alpha1 "github.com/gardener/cert-management/pkg/apis/cert/v1alpha1"
dnsv1alpha1 "github.com/gardener/external-dns-management/pkg/apis/dns/v1alpha1"
"github.com/sap/cap-operator/pkg/apis/sme.sap.com/v1alpha1"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/types/known/durationpb"
networkingv1beta1 "istio.io/api/networking/v1beta1"
istionwv1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog/v2"
)
// TODO: ignore duplicates reconciliation calls for same dnsTarget, Finalizers... and a whole lot more!
const PrimaryDnsSuffix = "primary-dns"
const (
CAPOperator = "CAPOperator"
OperatorDomainLabel = CAPOperator + "." + OperatorDomains
OperatorDomainNamePrefix = "cap-operator-domains-"
)
var (
cNameLookup = int64(30)
ttl = int64(600)
)
const (
formatResourceState = "%s in state %s for %s %s.%s"
formatResourceStateErr = formatResourceState + ": %s"
)
func (c *Controller) handleDomains(ctx context.Context, ca *v1alpha1.CAPApplication) (*ReconcileResult, error) {
domains, err := json.Marshal(ca.Spec.Domains)
if err != nil {
return nil, fmt.Errorf("error occurred while encoding domains to json: %w", err)
}
domainsHash := sha256Sum(string(domains))
requeue := NewReconcileResult()
commonName := strings.Join([]string{"*", ca.Spec.Domains.Primary}, ".")
secretName := strings.Join([]string{strings.Join([]string{ca.Namespace, ca.Name}, "--"), SecretSuffix}, "-")
c.handlePrimaryDomainGateway(ctx, ca, secretName, ca.Namespace)
istioIngressGatewayInfo, err := c.getIngressGatewayInfo(ctx, ca)
if err != nil {
return nil, err
}
c.handlePrimaryDomainCertificate(ctx, ca, commonName, secretName, istioIngressGatewayInfo.Namespace)
c.handlePrimaryDomainDNSEntry(ctx, ca, commonName, ca.Namespace, sanitizeDNSTarget(istioIngressGatewayInfo.DNSTarget))
if domainsHash != ca.Status.DomainSpecHash {
// Reconcile Secondary domains via a dummy resource (separate reconciliation)
requeue.AddResource(ResourceOperatorDomains, "", metav1.NamespaceAll, 0)
requeue.AddResource(ResourceCAPApplication, ca.Name, ca.Namespace, 3*time.Second) // requeue CAPApplication for further processing
// notify tenants of domain specification change (dns entries, virtual services)
cats, err := c.getRelevantTenantsForCA(ca)
if err != nil {
return nil, err
}
for _, cat := range cats {
requeue.AddResource(ResourceCAPTenant, cat.Name, cat.Namespace, 2*time.Second)
}
ca.SetStatusWithReadyCondition(v1alpha1.CAPApplicationStateProcessing, metav1.ConditionFalse, EventActionProcessingDomainResources, "")
ca.SetStatusDomainSpecHash(domainsHash)
return requeue, nil
}
return nil, nil
}
func (c *Controller) handlePrimaryDomainGateway(ctx context.Context, ca *v1alpha1.CAPApplication, secretName string, namespace string) error {
gwName := getResourceName(ca.Spec.BTPAppName, GatewaySuffix)
ingressGWLabels := getIngressGatewayLabels(ca)
gwSpec := networkingv1beta1.Gateway{
Selector: ingressGWLabels,
Servers: []*networkingv1beta1.Server{
getGatewayServerSpec(ca.Spec.Domains.Primary, secretName),
},
}
// Calculate sha256 sum for GW spec
hash := sha256Sum(ca.Spec.Domains.Primary, secretName, fmt.Sprintf("%v", ingressGWLabels))
// check for existing gateway
gw, err := c.istioInformerFactory.Networking().V1beta1().Gateways().Lister().Gateways(namespace).Get(gwName)
// create gateway
if errors.IsNotFound(err) {
_, err = c.istioClient.NetworkingV1beta1().Gateways(namespace).Create(
ctx, &istionwv1beta1.Gateway{
ObjectMeta: metav1.ObjectMeta{
Name: gwName,
Namespace: namespace,
Annotations: map[string]string{
AnnotationResourceHash: hash,
AnnotationBTPApplicationIdentifier: ca.Spec.GlobalAccountId + "." + ca.Spec.BTPAppName,
},
Labels: map[string]string{
LabelBTPApplicationIdentifierHash: sha1Sum(ca.Spec.GlobalAccountId, ca.Spec.BTPAppName),
},
OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(ca, v1alpha1.SchemeGroupVersion.WithKind(v1alpha1.CAPApplicationKind))},
},
Spec: gwSpec,
}, metav1.CreateOptions{},
)
} else if err == nil && gw != nil && gw.Annotations[AnnotationResourceHash] != hash {
// Update the relevant gw parts, if there are changes (detected via sha256 sum)
gw.Spec = gwSpec
// Update hash value on annotation
updateResourceAnnotation(&gw.ObjectMeta, hash)
// Trigger the actual update on the resource
_, err = c.istioClient.NetworkingV1beta1().Gateways(namespace).Update(ctx, gw, metav1.UpdateOptions{})
}
if err == nil {
c.Event(ca, nil, corev1.EventTypeNormal, CAPApplicationEventPrimaryGatewayModified, EventActionProcessingDomainResources, fmt.Sprintf("primary gateway %s has been modified", gwName))
}
return err
}
func (c *Controller) handlePrimaryDomainCertificate(ctx context.Context, ca *v1alpha1.CAPApplication, commonName string, secretName string, istioNamespace string) error {
var err error
certName := getResourceName(ca.Spec.BTPAppName, CertificateSuffix)
// Calculate sha256 sum for Cert spec
hash := sha256Sum(commonName, secretName)
switch certificateManager() {
case certManagerGardener:
// check for existing certificate
gardenerCert, err := c.gardenerCertificateClient.CertV1alpha1().Certificates(istioNamespace).Get(context.TODO(), certName, metav1.GetOptions{})
gardenerCertSpec := getGardenerCertificateSpec(commonName, secretName)
if errors.IsNotFound(err) {
// create certificate
_, err = c.gardenerCertificateClient.CertV1alpha1().Certificates(istioNamespace).Create(
ctx, &certv1alpha1.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: certName,
Namespace: istioNamespace,
Annotations: map[string]string{
AnnotationResourceHash: hash,
AnnotationOwnerIdentifier: KindMap[ResourceCAPApplication] + "." + ca.Namespace + "." + ca.Name,
},
Labels: map[string]string{
LabelOwnerIdentifierHash: sha1Sum(KindMap[ResourceCAPApplication], ca.Namespace, ca.Name),
LabelOwnerGeneration: strconv.FormatInt(ca.Generation, 10),
},
Finalizers: []string{FinalizerCAPApplication},
},
Spec: gardenerCertSpec,
}, metav1.CreateOptions{},
)
} else if err == nil && gardenerCert != nil && gardenerCert.Annotations[AnnotationResourceHash] != hash {
// Update the certificate spec
gardenerCert.Spec = gardenerCertSpec
// Update hash value on annotation
updateResourceAnnotation(&gardenerCert.ObjectMeta, hash)
// Trigger the actual update on the resource
_, err = c.gardenerCertificateClient.CertV1alpha1().Certificates(istioNamespace).Update(ctx, gardenerCert, metav1.UpdateOptions{})
}
case certManagerCertManagerIO:
// check for existing certificate
certManagerCert, err := c.certManagerCertificateClient.CertmanagerV1().Certificates(istioNamespace).Get(context.TODO(), certName, metav1.GetOptions{})
certManagerCertSpec := getCertManagerCertificateSpec(commonName, secretName)
if errors.IsNotFound(err) {
// create certificate
_, err = c.certManagerCertificateClient.CertmanagerV1().Certificates(istioNamespace).Create(
ctx, &certManagerv1.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: certName,
Namespace: istioNamespace,
Annotations: map[string]string{
AnnotationResourceHash: hash,
AnnotationOwnerIdentifier: KindMap[ResourceCAPApplication] + "." + ca.Namespace + "." + ca.Name,
},
Labels: map[string]string{
LabelOwnerIdentifierHash: sha1Sum(KindMap[ResourceCAPApplication], ca.Namespace, ca.Name),
LabelOwnerGeneration: strconv.FormatInt(ca.Generation, 10),
},
Finalizers: []string{FinalizerCAPApplication},
},
Spec: certManagerCertSpec,
}, metav1.CreateOptions{},
)
} else if err == nil && certManagerCert != nil && certManagerCert.Annotations[AnnotationResourceHash] != hash {
// Update the certificate spec
certManagerCert.Spec = certManagerCertSpec
// Update hash value on annotation
updateResourceAnnotation(&certManagerCert.ObjectMeta, hash)
// Trigger the actual update on the resource
_, err = c.certManagerCertificateClient.CertmanagerV1().Certificates(istioNamespace).Update(ctx, certManagerCert, metav1.UpdateOptions{})
}
}
return err
}
func (c *Controller) handlePrimaryDomainDNSEntry(ctx context.Context, ca *v1alpha1.CAPApplication, commonName string, namespace string, dnsTarget string) error {
// nothing to do here for non-gardener scenario because external-dns handles istio gateways automatically
if dnsManager() == dnsManagerGardener {
dnsEntryName := getResourceName(ca.Spec.BTPAppName, PrimaryDnsSuffix)
dnsEntrySpec := dnsv1alpha1.DNSEntrySpec{
CNameLookupInterval: &cNameLookup,
DNSName: commonName,
Targets: []string{
dnsTarget,
},
TTL: &ttl,
}
// Calculate sha256 sum for DNSEntry spec
hash := sha256Sum(commonName, dnsTarget)
// check for existing DNSEntry
dnsEntry, err := c.gardenerDNSClient.DnsV1alpha1().DNSEntries(namespace).Get(context.TODO(), dnsEntryName, metav1.GetOptions{})
if errors.IsNotFound(err) {
// create DNSEntry
_, err = c.gardenerDNSClient.DnsV1alpha1().DNSEntries(namespace).Create(
ctx, &dnsv1alpha1.DNSEntry{
ObjectMeta: metav1.ObjectMeta{
Name: dnsEntryName,
Namespace: namespace,
Annotations: map[string]string{
AnnotationResourceHash: hash,
GardenerDNSClassIdentifier: GardenerDNSClassValue,
AnnotationOwnerIdentifier: KindMap[ResourceCAPApplication] + "." + ca.Namespace + "." + ca.Name,
},
Labels: map[string]string{
LabelOwnerIdentifierHash: sha1Sum(KindMap[ResourceCAPApplication], ca.Namespace, ca.Name),
},
OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(ca, v1alpha1.SchemeGroupVersion.WithKind(v1alpha1.CAPApplicationKind))},
},
Spec: dnsEntrySpec,
}, metav1.CreateOptions{},
)
} else if err == nil && dnsEntry != nil && dnsEntry.Annotations[AnnotationResourceHash] != hash {
// Update the DnsEntry spec
dnsEntry.Spec = dnsEntrySpec
// Update hash value on annotation
updateResourceAnnotation(&dnsEntry.ObjectMeta, hash)
// Trigger the actual update on the resource
_, err = c.gardenerDNSClient.DnsV1alpha1().DNSEntries(namespace).Update(ctx, dnsEntry, metav1.UpdateOptions{})
}
return err
}
return nil
}
func (c *Controller) checkPrimaryDomainResources(ctx context.Context, ca *v1alpha1.CAPApplication) (processing bool, err error) {
defer func() {
if err != nil {
// set CAPApplication status - with error
ca.SetStatusWithReadyCondition(v1alpha1.CAPApplicationStateError, metav1.ConditionFalse, "DomainResourcesError", err.Error())
}
}()
// check for existing gateway
_, err = c.istioClient.NetworkingV1beta1().Gateways(ca.Namespace).Get(ctx, getResourceName(ca.Spec.BTPAppName, GatewaySuffix), metav1.GetOptions{})
if err != nil {
return false, err
}
var istioIngressGatewayInfo *ingressGatewayInfo
istioIngressGatewayInfo, err = c.getIngressGatewayInfo(ctx, ca)
if err != nil {
return false, err
}
certName := getResourceName(ca.Spec.BTPAppName, CertificateSuffix)
// check for certificate status
if processing, err := c.checkCertificateStatus(ctx, ca, istioIngressGatewayInfo.Namespace, certName); err != nil || processing {
return processing, err
}
dnsEntryName := strings.Join([]string{ca.Spec.BTPAppName, PrimaryDnsSuffix}, "-")
if dnsManager() == dnsManagerGardener {
// check for existing DNSEntry
dnsEntry, err := c.gardenerDNSClient.DnsV1alpha1().DNSEntries(ca.Namespace).Get(context.TODO(), dnsEntryName, metav1.GetOptions{})
if err != nil {
return false, err
}
// check for ready state
if dnsEntry.Status.State == dnsv1alpha1.STATE_ERROR {
return false, fmt.Errorf(formatResourceStateErr, dnsv1alpha1.DNSEntryKind, dnsv1alpha1.STATE_ERROR, v1alpha1.CAPApplicationKind, ca.Namespace, ca.Name, *dnsEntry.Status.Message)
} else if dnsEntry.Status.State != dnsv1alpha1.STATE_READY {
klog.Infof(formatResourceState, dnsv1alpha1.DNSEntryKind, dnsEntry.Status.State, v1alpha1.CAPApplicationKind, ca.Namespace, ca.Name)
ca.SetStatusWithReadyCondition(v1alpha1.CAPApplicationStateProcessing, metav1.ConditionFalse, "DomainResourcesProcessing", "")
return true, nil
}
}
return
}
func (c *Controller) deletePrimaryDomainCertificate(ctx context.Context, ca *v1alpha1.CAPApplication) error {
var err error
ingressGatewayInfo, err := c.getIngressGatewayInfo(ctx, ca)
if err != nil {
return err
}
certName := getResourceName(ca.Spec.BTPAppName, CertificateSuffix)
// delete Certificate
switch certificateManager() {
case certManagerGardener:
err = c.deleteGardenerCertificate(ingressGatewayInfo, certName, ctx)
case certManagerCertManagerIO:
err = c.deleteCertManagerCertificate(ingressGatewayInfo, certName, ctx)
}
return err
}
func (c *Controller) deleteGardenerCertificate(ingressGatewayInfo *ingressGatewayInfo, certName string, ctx context.Context) error {
certificate, err := c.gardenerCertInformerFactory.Cert().V1alpha1().Certificates().Lister().Certificates(ingressGatewayInfo.Namespace).Get(certName)
if err != nil {
return err
}
// remove Finalizer from Certificate
if removeFinalizer(&certificate.Finalizers, FinalizerCAPApplication) {
if _, err = c.gardenerCertificateClient.CertV1alpha1().Certificates(ingressGatewayInfo.Namespace).Update(ctx, certificate, metav1.UpdateOptions{}); err != nil {
return err
}
}
// delete Certificate
if err = c.gardenerCertificateClient.CertV1alpha1().Certificates(ingressGatewayInfo.Namespace).Delete(ctx, certName, metav1.DeleteOptions{}); err != nil {
return err
}
return nil
}
func (c *Controller) deleteCertManagerCertificate(ingressGatewayInfo *ingressGatewayInfo, certName string, ctx context.Context) error {
certificate, err := c.certManagerInformerFactory.Certmanager().V1().Certificates().Lister().Certificates(ingressGatewayInfo.Namespace).Get(certName)
if err != nil {
return err
}
// remove Finalizer from Certificate
if removeFinalizer(&certificate.Finalizers, FinalizerCAPApplication) {
if _, err = c.certManagerCertificateClient.CertmanagerV1().Certificates(ingressGatewayInfo.Namespace).Update(ctx, certificate, metav1.UpdateOptions{}); err != nil {
return err
}
}
// delete Certificate
if err = c.certManagerCertificateClient.CertmanagerV1().Certificates(ingressGatewayInfo.Namespace).Delete(ctx, certName, metav1.DeleteOptions{}); err != nil {
return err
}
return nil
}
func getResourceName(btpAppName string, resourceSuffix string) string {
return strings.Join([]string{btpAppName, resourceSuffix}, "-")
}
func (c *Controller) checkCertificateStatus(ctx context.Context, ca *v1alpha1.CAPApplication, certNamespace string, certName string) (bool, error) {
switch certificateManager() {
case certManagerGardener:
// check for existing certificate
certificate, err := c.gardenerCertificateClient.CertV1alpha1().Certificates(certNamespace).Get(context.TODO(), certName, metav1.GetOptions{})
if err != nil {
return false, err
}
// check for ready state
if certificate.Status.State == certv1alpha1.StateError {
return false, fmt.Errorf(formatResourceStateErr, certv1alpha1.CertificateKind, certv1alpha1.StateError, v1alpha1.CAPApplicationKind, ca.Namespace, ca.Name, *certificate.Status.Message)
} else if certificate.Status.State != certv1alpha1.StateReady {
klog.Infof(formatResourceState, certv1alpha1.CertificateKind, certificate.Status.State, v1alpha1.CAPApplicationKind, ca.Namespace, ca.Name)
return true, nil
}
case certManagerCertManagerIO:
// check for existing certificate
certificate, err := c.certManagerCertificateClient.CertmanagerV1().Certificates(certNamespace).Get(context.TODO(), certName, metav1.GetOptions{})
if err != nil {
return false, err
}
// get ready condition
readyCond := getCertManagerReadyCondition(certificate)
// check for ready state
if readyCond == nil || readyCond.Status == certManagermetav1.ConditionUnknown {
klog.Infof(formatResourceState, certManagerv1.CertificateKind, "unknown", v1alpha1.CAPApplicationKind, ca.Namespace, ca.Name)
return true, nil
} else if readyCond.Status == certManagermetav1.ConditionFalse {
return false, fmt.Errorf(formatResourceStateErr, certManagerv1.CertificateKind, "not ready", v1alpha1.CAPApplicationKind, ca.Namespace, ca.Name, readyCond.Message)
}
}
// Cert is Ready
return false, nil
}
func getCertManagerReadyCondition(certificate *certManagerv1.Certificate) *certManagerv1.CertificateCondition {
var readyCond *certManagerv1.CertificateCondition
for _, cond := range certificate.Status.Conditions {
if cond.Type == certManagerv1.CertificateConditionReady {
readyCond = &cond
break
}
}
return readyCond
}
func getGatewayServerSpec(domain string, credentialName string) *networkingv1beta1.Server {
return &networkingv1beta1.Server{
Hosts: []string{"*." + domain},
Port: &networkingv1beta1.Port{
Number: 443,
Protocol: "HTTPS",
Name: domain,
},
Tls: &networkingv1beta1.ServerTLSSettings{
CredentialName: credentialName,
Mode: networkingv1beta1.ServerTLSSettings_SIMPLE,
},
}
}
func getGardenerCertificateSpec(commonName string, secretName string) certv1alpha1.CertificateSpec {
return certv1alpha1.CertificateSpec{
CommonName: &commonName,
SecretName: &secretName,
}
}
func getCertManagerCertificateSpec(commonName string, secretName string) certManagerv1.CertificateSpec {
return certManagerv1.CertificateSpec{
CommonName: commonName,
DNSNames: []string{commonName},
SecretName: secretName,
IssuerRef: certManagermetav1.ObjectReference{
// TODO: make this configurable
Kind: certManagerv1.ClusterIssuerKind,
Name: "cluster-ca",
},
}
}
func (c *Controller) detectTenantDNSEntryChanges(ctx context.Context, cat *v1alpha1.CAPTenant, ca *v1alpha1.CAPApplication, hash string) (bool, error) {
labelOwner := map[string]string{
LabelOwnerIdentifierHash: sha1Sum(v1alpha1.CAPTenantKind, cat.Namespace, cat.Name),
}
dnsEntries, err := c.gardenerDNSClient.DnsV1alpha1().DNSEntries(ca.Namespace).List(ctx, metav1.ListOptions{LabelSelector: labels.SelectorFromSet(labelOwner).String()})
if err != nil {
return false, err
}
// When no DNSEntry exists --> assume we might have to create some
if len(dnsEntries.Items) == 0 {
return true, nil
}
// Detect changes on DNSEntry based on known mismatches (hash / length)
changeDetected := false
// length check
if len(dnsEntries.Items) != len(ca.Spec.Domains.Secondary) {
changeDetected = true
}
// hash check
if !changeDetected {
for _, dnsEntry := range dnsEntries.Items {
if dnsEntry.Annotations[AnnotationResourceHash] != hash {
changeDetected = true
break
}
}
}
// Delete all existing DNSEntries
if changeDetected {
// Delete all existing DNSEntries
err = c.gardenerDNSClient.DnsV1alpha1().DNSEntries(ca.Namespace).DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: labels.SelectorFromSet(labelOwner).String()})
if err != nil {
return false, err
}
}
return changeDetected, nil
}
func (c *Controller) reconcileTenantDNSEntries(ctx context.Context, cat *v1alpha1.CAPTenant) error {
if dnsManager() != dnsManagerGardener {
// Not a gardener managed cluster -> return
return nil
}
// get owning CAPApplication
ca, _ := c.getCachedCAPApplication(cat.Namespace, cat.Spec.CAPApplicationInstance)
ingressGatewayInfo, err := c.getIngressGatewayInfo(ctx, ca)
if err != nil {
return err
}
dnsTarget := sanitizeDNSTarget(ingressGatewayInfo.DNSTarget)
hash := sha256Sum(dnsTarget, cat.Spec.SubDomain, strings.Join(ca.Spec.Domains.Secondary, ""))
changeDetected, err := c.detectTenantDNSEntryChanges(ctx, cat, ca, hash)
if err != nil || !changeDetected {
return err
}
// Create DNS Entries
for index, domain := range ca.Spec.Domains.Secondary {
dnsEntryName := cat.Name + strconv.Itoa(index)
_, err = c.gardenerDNSClient.DnsV1alpha1().DNSEntries(ca.Namespace).Create(
ctx, &dnsv1alpha1.DNSEntry{
ObjectMeta: metav1.ObjectMeta{
Name: dnsEntryName,
Annotations: map[string]string{
GardenerDNSClassIdentifier: GardenerDNSClassValue,
AnnotationResourceHash: hash,
AnnotationOwnerIdentifier: v1alpha1.CAPTenantKind + "." + cat.Namespace + "." + cat.Name,
},
Labels: map[string]string{
LabelOwnerIdentifierHash: sha1Sum(v1alpha1.CAPTenantKind, cat.Namespace, cat.Name),
},
OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(cat, v1alpha1.SchemeGroupVersion.WithKind(v1alpha1.CAPTenantKind))},
},
Spec: dnsv1alpha1.DNSEntrySpec{
DNSName: cat.Spec.SubDomain + "." + domain,
Targets: []string{
dnsTarget,
},
},
}, metav1.CreateOptions{},
)
// Unknown error --> break loop
if err != nil {
break
}
}
return err
}
func (c *Controller) checkTenantDNSEntries(ctx context.Context, cat *v1alpha1.CAPTenant) (bool, error) {
// TODO: ensure that the CAPTenant is set to Ready only once all these DNSEntries are actually ready
if dnsManager() == dnsManagerGardener {
// get relevant DNSEntries
dnsEntries, err := c.gardenerDNSClient.DnsV1alpha1().DNSEntries(cat.Namespace).List(ctx, metav1.ListOptions{LabelSelector: labels.SelectorFromValidatedSet(map[string]string{LabelOwnerIdentifierHash: sha1Sum(KindMap[ResourceCAPTenant], cat.Namespace, cat.Name)}).String()})
if err != nil {
return false, err
}
if len(dnsEntries.Items) == 0 {
return false, fmt.Errorf("could not find dnsentries for %s %s.%s", v1alpha1.CAPTenantKind, cat.Namespace, cat.Name)
}
for _, dnsEntry := range dnsEntries.Items {
// check for ready state
if dnsEntry.Status.State == dnsv1alpha1.STATE_ERROR {
return false, fmt.Errorf(formatResourceStateErr, dnsv1alpha1.DNSEntryKind, dnsv1alpha1.STATE_ERROR, v1alpha1.CAPTenantKind, cat.Namespace, cat.Name, *dnsEntry.Status.Message)
} else if dnsEntry.Status.State != dnsv1alpha1.STATE_READY {
klog.Infof(formatResourceState, dnsv1alpha1.DNSEntryKind, dnsEntry.Status.State, v1alpha1.CAPTenantKind, cat.Namespace, cat.Name)
return true, nil
}
}
}
// Not a gardener managed cluster -or- DNSEntries Ready -> return
return false, nil
}
func (c *Controller) reconcileTenantNetworking(ctx context.Context, cat *v1alpha1.CAPTenant, cavName string, ca *v1alpha1.CAPApplication) (requeue *ReconcileResult, err error) {
var (
reason, message string
drModified, vsModified bool
eventType string = corev1.EventTypeNormal
)
defer func() {
if err != nil {
eventType = corev1.EventTypeWarning
message = err.Error()
if _, ok := err.(*OperatorGatewayMissingError); ok {
err = nil
requeue = NewReconcileResultWithResource(ResourceCAPTenant, cat.Name, cat.Namespace, 10*time.Second)
}
}
if reason != "" { // raise event only when there is a modification or problem
c.Event(cat, nil, eventType, reason, EventActionReconcileTenantNetworking, message)
}
}()
if drModified, err = c.reconcileTenantDestinationRule(ctx, cat, cavName, ca); err != nil {
reason = CAPTenantEventDestinationRuleModificationFailed
return
}
if vsModified, err = c.reconcileTenantVirtualService(ctx, cat, cavName, ca); err != nil {
reason = CAPTenantEventVirtualServiceModificationFailed
return
}
// update tenant status
if drModified || vsModified {
message = fmt.Sprintf("VirtualService (and DestinationRule) %s.%s was reconciled", cat.Namespace, cat.Name)
reason = CAPTenantEventTenantNetworkingModified
conditionStatus := metav1.ConditionFalse
if isCROConditionReady(cat.Status.GenericStatus) {
conditionStatus = metav1.ConditionTrue
}
cat.SetStatusWithReadyCondition(cat.Status.State, conditionStatus, CAPTenantEventTenantNetworkingModified, message)
}
return
}
func (c *Controller) reconcileTenantDestinationRule(ctx context.Context, cat *v1alpha1.CAPTenant, cavName string, ca *v1alpha1.CAPApplication) (modified bool, err error) {
var (
create, update bool
dr *istionwv1beta1.DestinationRule
)
dr, err = c.istioClient.NetworkingV1beta1().DestinationRules(cat.Namespace).Get(ctx, cat.Name, metav1.GetOptions{})
if errors.IsNotFound(err) {
dr = &istionwv1beta1.DestinationRule{
ObjectMeta: metav1.ObjectMeta{
Name: cat.Name, // keep the same name as CAPTenant to avoid duplicates
Namespace: cat.Namespace,
Labels: map[string]string{},
OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(cat, v1alpha1.SchemeGroupVersion.WithKind(v1alpha1.CAPTenantKind))},
},
}
create = true
} else if err != nil {
return
}
if update, err = c.getUpdatedTenantDestinationRuleObject(ctx, cat, dr, cavName); err != nil {
return
}
if create {
_, err = c.istioClient.NetworkingV1beta1().DestinationRules(cat.Namespace).Create(ctx, dr, metav1.CreateOptions{})
} else if update {
_, err = c.istioClient.NetworkingV1beta1().DestinationRules(cat.Namespace).Update(ctx, dr, metav1.UpdateOptions{})
}
return create || update, err
}
func (c *Controller) getUpdatedTenantDestinationRuleObject(ctx context.Context, cat *v1alpha1.CAPTenant, dr *istionwv1beta1.DestinationRule, cavName string) (modified bool, err error) {
// verify owner reference
modified, err = c.enforceTenantResourceOwnership(&dr.ObjectMeta, &dr.TypeMeta, cat)
if err != nil {
return modified, err
}
routerPortInfo, err := c.getRouterServicePortInfo(cavName, cat.Namespace)
if err != nil {
return modified, err
}
spec := &networkingv1beta1.DestinationRule{
Host: routerPortInfo.WorkloadName + ServiceSuffix + "." + cat.Namespace + ".svc.cluster.local",
TrafficPolicy: &networkingv1beta1.TrafficPolicy{
LoadBalancer: &networkingv1beta1.LoadBalancerSettings{
LbPolicy: &networkingv1beta1.LoadBalancerSettings_ConsistentHash{
ConsistentHash: &networkingv1beta1.LoadBalancerSettings_ConsistentHashLB{
HashKey: &networkingv1beta1.LoadBalancerSettings_ConsistentHashLB_HttpCookie{
HttpCookie: &networkingv1beta1.LoadBalancerSettings_ConsistentHashLB_HTTPCookie{
Name: RouterHttpCookieName,
Ttl: durationpb.New(0 * time.Second),
Path: "/",
},
},
},
},
},
},
}
// check whether changes have to be applied using hash comparison
serializedSpec, err := json.Marshal(spec)
if err != nil {
return modified, fmt.Errorf("error serializing destination rule spec: %s", err.Error())
}
hash := sha256Sum(string(serializedSpec))
if dr.Annotations[AnnotationResourceHash] != hash {
dr.Spec = *spec
updateResourceAnnotation(&dr.ObjectMeta, hash)
modified = true
}
return modified, err
}
func (c *Controller) reconcileTenantVirtualService(ctx context.Context, cat *v1alpha1.CAPTenant, cavName string, ca *v1alpha1.CAPApplication) (modified bool, err error) {
var (
create, update bool
vs *istionwv1beta1.VirtualService
)
vs, err = c.istioClient.NetworkingV1beta1().VirtualServices(cat.Namespace).Get(ctx, cat.Name, metav1.GetOptions{})
if errors.IsNotFound(err) {
vs = &istionwv1beta1.VirtualService{
ObjectMeta: metav1.ObjectMeta{
Name: cat.Name, // keep the same name as CAPTenant to avoid duplicates
Namespace: cat.Namespace,
Labels: map[string]string{},
OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(cat, v1alpha1.SchemeGroupVersion.WithKind(v1alpha1.CAPTenantKind))},
},
}
create = true
} else if err != nil {
return
}
if update, err = c.getUpdatedTenantVirtualServiceObject(ctx, cat, vs, cavName, ca); err != nil {
return
}
if create {
_, err = c.istioClient.NetworkingV1beta1().VirtualServices(cat.Namespace).Create(ctx, vs, metav1.CreateOptions{})
} else if update {
_, err = c.istioClient.NetworkingV1beta1().VirtualServices(cat.Namespace).Update(ctx, vs, metav1.UpdateOptions{})
}
return create || update, err
}
func (c *Controller) getUpdatedTenantVirtualServiceObject(ctx context.Context, cat *v1alpha1.CAPTenant, vs *istionwv1beta1.VirtualService, cavName string, ca *v1alpha1.CAPApplication) (modified bool, err error) {
if ca == nil {
ca, err = c.getCachedCAPApplication(cat.Namespace, cat.Spec.CAPApplicationInstance)
if err != nil {
return modified, err
}
}
// verify owner reference
modified, err = c.enforceTenantResourceOwnership(&vs.ObjectMeta, &vs.TypeMeta, cat)
if err != nil {
return modified, err
}
routerPortInfo, err := c.getRouterServicePortInfo(cavName, ca.Namespace)
if err != nil {
return modified, err
}
spec := &networkingv1beta1.VirtualService{
Gateways: []string{ca.Spec.BTPAppName + "-gw"},
Hosts: []string{cat.Spec.SubDomain + "." + ca.Spec.Domains.Primary},
Http: []*networkingv1beta1.HTTPRoute{{
Match: []*networkingv1beta1.HTTPMatchRequest{
{Uri: &networkingv1beta1.StringMatch{MatchType: &networkingv1beta1.StringMatch_Prefix{Prefix: "/"}}},
},
Route: []*networkingv1beta1.HTTPRouteDestination{{
Destination: &networkingv1beta1.Destination{
Host: routerPortInfo.WorkloadName + ServiceSuffix + "." + cat.Namespace + ".svc.cluster.local",
Port: &networkingv1beta1.PortSelector{Number: uint32(routerPortInfo.Ports[0].Port)},
},
Weight: 100,
}},
}},
}
err = c.updateTenantVirtualServiceSpecWithSecondaryDomains(ctx, spec, cat, ca)
if err != nil {
return modified, err
}
// check whether changes have to be applied using hash comparison
serializedSpec, err := json.Marshal(spec)
if err != nil {
return modified, fmt.Errorf("error serializing virtual service spec: %s", err.Error())
}
hash := sha256Sum(string(serializedSpec))
if vs.Annotations[AnnotationResourceHash] != hash {
vs.Spec = *spec
updateResourceAnnotation(&vs.ObjectMeta, hash)
modified = true
}
return modified, err
}
type OperatorGatewayMissingError struct{}
func (err *OperatorGatewayMissingError) Error() string {
return "operator gateway for secondary domains missing"
}
func (c *Controller) updateTenantVirtualServiceSpecWithSecondaryDomains(ctx context.Context, spec *networkingv1beta1.VirtualService, cat *v1alpha1.CAPTenant, ca *v1alpha1.CAPApplication) error {
secondaryDomainsExist := ca.Spec.Domains.Secondary != nil && len(ca.Spec.Domains.Secondary) > 0
if !secondaryDomainsExist {
return nil
}
// add customer specific domains
for _, domain := range ca.Spec.Domains.Secondary {
spec.Hosts = append(spec.Hosts, cat.Spec.SubDomain+"."+domain)
}
// Determine Ingress GW service for this app
gwInfo, err := c.getIngressGatewayInfo(ctx, ca)
if err != nil {
return err
}
// Get the relevant central operator GW for this ingress GW
operatorGW, _ := c.getOperatorGateway(ctx, gwInfo.Namespace, sha1Sum(gwInfo.DNSTarget))
if operatorGW == nil {
// requeue for later reconciliation
return &OperatorGatewayMissingError{}
}
spec.Gateways = append(spec.Gateways, operatorGW.Namespace+"/"+operatorGW.Name)
return nil
}
func getIngressGatewayLabels(ca *v1alpha1.CAPApplication) map[string]string {
ingressLabels := map[string]string{}
for _, label := range ca.Spec.Domains.IstioIngressGatewayLabels {
ingressLabels[label.Name] = label.Value
}
return ingressLabels
}
func (c *Controller) getIngressGatewayInfo(ctx context.Context, ca *v1alpha1.CAPApplication) (ingGwInfo *ingressGatewayInfo, err error) {
// create ingress gateway selector from labels
ingressLabelSelector, err := labels.ValidatedSelectorFromSet(getIngressGatewayLabels(ca))
if err != nil {
return nil, err
}
defer func() {
if err != nil {
c.Event(ca, nil, corev1.EventTypeWarning, CAPApplicationEventMissingIngressGatewayInfo, EventActionProcessingDomainResources, err.Error())
}
}()
// Get relevant Ingress Gateway pods
ingressPods, err := c.kubeClient.CoreV1().Pods(metav1.NamespaceAll).List(ctx, metav1.ListOptions{LabelSelector: ingressLabelSelector.String()})
if err != nil {
return nil, err
}
// Determine relevant istio-ingressgateway namespace
namespace := ""
name := ""
// Create a dummy lookup map for determining relevant pods
relevantPodsNames := map[string]struct{}{}
for _, pod := range ingressPods.Items {
// We only support 1 ingress gateway pod namespace as of now! (Multiple pods e.g. replicas can exist in the same namespace)
if namespace == "" {
namespace = pod.Namespace
name = pod.Name
} else if namespace != pod.Namespace {
return nil, fmt.Errorf("more than one matching ingress gateway pod namespaces found for %s %s.%s", v1alpha1.CAPApplicationKind, ca.Namespace, ca.Name)
}
relevantPodsNames[pod.Name] = struct{}{}
}
if namespace == "" {
return nil, fmt.Errorf("no matching ingress gateway pod found for %s %s.%s", v1alpha1.CAPApplicationKind, ca.Namespace, ca.Name)
}
// Get dnsTarget
// First try to use dnsTarget --> if it is set
dnsTarget := ca.Spec.Domains.DnsTarget
// Attempt to get dnsTarget from Env
if dnsTarget == "" {
dnsTarget = envDNSTarget()
}
// Finally attempt to get dnsTarget from Service via annotation(s)
if dnsTarget == "" {
ingressGWSvc, err := c.getIngressGatewayService(ctx, namespace, relevantPodsNames, ca)
if err != nil {
return nil, err
}
if ingressGWSvc != nil {
dnsTarget = getDNSTarget(ingressGWSvc)
}
}
// No DNS Target --> Error
if dnsTarget == "" {
return nil, fmt.Errorf("ingress gateway service not annotated with dns target name for %s %s.%s", v1alpha1.CAPApplicationKind, ca.Namespace, ca.Name)
}
// Return ingress Gateway info (Namespace and DNS target)
return &ingressGatewayInfo{Namespace: namespace, Name: name, DNSTarget: dnsTarget}, nil
}
func getDNSTarget(ingressGWSvc *corev1.Service) string {
var dnsTarget string
switch dnsManager() {
case dnsManagerGardener:
dnsTarget = ingressGWSvc.Annotations[AnnotationGardenerDNSTarget]
case dnsManagerKubernetes:
dnsTarget = ingressGWSvc.Annotations[AnnotationKubernetesDNSTarget]
}
// Use the 1st value from Comma separated values (if any)
return strings.Split(dnsTarget, ",")[0]
}
func (c *Controller) getLoadBalancerSvcs(ctx context.Context, istioIngressGWNamespace string) ([]corev1.Service, error) {
// List all services in the same namespace as the istio-ingressgateway pod namespace
allServices, err := c.kubeClient.CoreV1().Services(istioIngressGWNamespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
// Filter out LoadBalancer services
loadBalancerSvcs := []corev1.Service{}
for _, svc := range allServices.Items {
if svc.Spec.Type == corev1.ServiceTypeLoadBalancer {
loadBalancerSvcs = append(loadBalancerSvcs, svc)
}
}
return loadBalancerSvcs, nil
}
func (c *Controller) getIngressGatewayService(ctx context.Context, istioIngressGWNamespace string, relevantPodNames map[string]struct{}, ca *v1alpha1.CAPApplication) (*corev1.Service, error) {
loadBalancerSvcs, err := c.getLoadBalancerSvcs(ctx, istioIngressGWNamespace)
if err != nil {
return nil, err
}
// Get Relevant services that match the ingress gw pod via selectors
var ingressGwSvc corev1.Service
for _, svc := range loadBalancerSvcs {
// Get all matching ingress GW pods in the ingress gw namespace via ingress gw service selectors
matchedPods, err := c.kubeClient.CoreV1().Pods(istioIngressGWNamespace).List(ctx, metav1.ListOptions{LabelSelector: labels.SelectorFromValidatedSet(svc.Spec.Selector).String()})
if err != nil {
return nil, err
}
for _, pod := range matchedPods.Items {
if _, ok := relevantPodNames[pod.Name]; ok {
if ingressGwSvc.Name == "" {
// we only expect 1 ingress gateway service in the cluster
ingressGwSvc = svc
break
} else if ingressGwSvc.Name != svc.Name {
return nil, fmt.Errorf("more than one matching ingress gateway service found for %s %s.%s", v1alpha1.CAPApplicationKind, ca.Namespace, ca.Name)
}
}
}
}
if ingressGwSvc.Name == "" {
return nil, fmt.Errorf("unable to find a matching ingress gateway service for %s %s.%s", v1alpha1.CAPApplicationKind, ca.Namespace, ca.Name)
}
return &ingressGwSvc, nil
}
type operatorDomainInfo struct {
Namespace string
Name string
ingressGWSelector map[string]string
dnsTarget string
Domains []string
}
// Operator Domains is a dummy resource that is referenced by a DNSTarget (in QueuedItem) to handle "secondary" domains across all relevant CAPApplications
// TODO: ignore duplicate reconciliation calls for same dnsTarget, Finalizers... and a whole lot more!
func (c *Controller) reconcileOperatorDomains(ctx context.Context, item QueueItem, attempts int) error {
// Get Relevant Domain Infos
relevantDomainInfos, err := c.getRelevantOperatorDomainInfo(ctx)
if err != nil {
return err
}
for dnsTargetSum, relevantDomainInfo := range relevantDomainInfos {
// When no secondary domains exists --> Cleanup and return
if len(relevantDomainInfo.Domains) == 0 {
return c.cleanUpOperatorDomains(ctx, relevantDomainInfo, dnsTargetSum)
}
// Handle Operator Gateway
gw, err := c.handleOperatorGateway(ctx, relevantDomainInfo, dnsTargetSum)
if err != nil {
return err
}
// Handle Operator Certificate
return c.handleOperatorCertificate(ctx, gw.Name, relevantDomainInfo, dnsTargetSum)
}
return nil
}
func (c *Controller) getRelevantOperatorDomainInfo(ctx context.Context) (map[string]*operatorDomainInfo, error) {