@@ -3,10 +3,15 @@ package clusters
3
3
import (
4
4
"bytes"
5
5
"fmt"
6
+ "reflect"
7
+ "sort"
8
+ "strings"
9
+ "time"
10
+
6
11
argo "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
7
12
"github.com/gogo/protobuf/types"
8
13
"github.com/istio-ecosystem/admiral/admiral/pkg/apis/admiral/model"
9
- "github.com/istio-ecosystem/admiral/admiral/pkg/apis/admiral/v1"
14
+ v1 "github.com/istio-ecosystem/admiral/admiral/pkg/apis/admiral/v1"
10
15
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/admiral"
11
16
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/common"
12
17
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/util"
@@ -16,10 +21,6 @@ import (
16
21
k8sAppsV1 "k8s.io/api/apps/v1"
17
22
k8sV1 "k8s.io/api/core/v1"
18
23
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
19
- "reflect"
20
- "sort"
21
- "strings"
22
- "time"
23
24
)
24
25
25
26
const ROLLOUT_POD_HASH_LABEL string = "rollouts-pod-template-hash"
@@ -45,8 +46,8 @@ type SidecarHandler struct {
45
46
}
46
47
47
48
type WeightedService struct {
48
- Weight int32
49
- Service * k8sV1.Service
49
+ Weight int32
50
+ Service * k8sV1.Service
50
51
}
51
52
52
53
func updateIdentityDependencyCache (sourceIdentity string , identityDependencyCache * common.MapOfMaps , dr * v1.Dependency ) {
@@ -70,9 +71,9 @@ func getDestinationRule(host string, locality string, gtpTrafficPolicy *model.Tr
70
71
processGtp = false
71
72
}
72
73
outlierDetection := & v1alpha32.OutlierDetection {
73
- BaseEjectionTime : & types.Duration {Seconds : 300 },
74
+ BaseEjectionTime : & types.Duration {Seconds : 300 },
74
75
Consecutive_5XxErrors : & types.UInt32Value {Value : uint32 (10 )},
75
- Interval : & types.Duration {Seconds : 60 },
76
+ Interval : & types.Duration {Seconds : 60 },
76
77
}
77
78
if gtpTrafficPolicy != nil && processGtp {
78
79
var loadBalancerSettings = & v1alpha32.LoadBalancerSettings {
@@ -108,52 +109,52 @@ func getDestinationRule(host string, locality string, gtpTrafficPolicy *model.Tr
108
109
109
110
func (se * ServiceEntryHandler ) Added (obj * v1alpha3.ServiceEntry ) {
110
111
if IgnoreIstioResource (obj .Spec .ExportTo , obj .Annotations , obj .Namespace ) {
111
- log .Infof (LogFormat , "Add" , "ServiceEntry" , obj .Name , se .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
112
+ log .Infof (LogFormat , "Add" , "ServiceEntry" , obj .Name , se .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
112
113
return
113
114
}
114
115
}
115
116
116
117
func (se * ServiceEntryHandler ) Updated (obj * v1alpha3.ServiceEntry ) {
117
118
if IgnoreIstioResource (obj .Spec .ExportTo , obj .Annotations , obj .Namespace ) {
118
- log .Infof (LogFormat , "Update" , "ServiceEntry" , obj .Name , se .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
119
+ log .Infof (LogFormat , "Update" , "ServiceEntry" , obj .Name , se .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
119
120
return
120
121
}
121
122
}
122
123
123
124
func (se * ServiceEntryHandler ) Deleted (obj * v1alpha3.ServiceEntry ) {
124
125
if IgnoreIstioResource (obj .Spec .ExportTo , obj .Annotations , obj .Namespace ) {
125
- log .Infof (LogFormat , "Delete" , "ServiceEntry" , obj .Name , se .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
126
+ log .Infof (LogFormat , "Delete" , "ServiceEntry" , obj .Name , se .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
126
127
return
127
128
}
128
129
}
129
130
130
131
func (dh * DestinationRuleHandler ) Added (obj * v1alpha3.DestinationRule ) {
131
132
if IgnoreIstioResource (obj .Spec .ExportTo , obj .Annotations , obj .Namespace ) {
132
- log .Infof (LogFormat , "Add" , "DestinationRule" , obj .Name , dh .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
133
+ log .Infof (LogFormat , "Add" , "DestinationRule" , obj .Name , dh .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
133
134
return
134
135
}
135
136
handleDestinationRuleEvent (obj , dh , common .Add , common .DestinationRule )
136
137
}
137
138
138
139
func (dh * DestinationRuleHandler ) Updated (obj * v1alpha3.DestinationRule ) {
139
140
if IgnoreIstioResource (obj .Spec .ExportTo , obj .Annotations , obj .Namespace ) {
140
- log .Infof (LogFormat , "Update" , "DestinationRule" , obj .Name , dh .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
141
+ log .Infof (LogFormat , "Update" , "DestinationRule" , obj .Name , dh .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
141
142
return
142
143
}
143
144
handleDestinationRuleEvent (obj , dh , common .Update , common .DestinationRule )
144
145
}
145
146
146
147
func (dh * DestinationRuleHandler ) Deleted (obj * v1alpha3.DestinationRule ) {
147
148
if IgnoreIstioResource (obj .Spec .ExportTo , obj .Annotations , obj .Namespace ) {
148
- log .Infof (LogFormat , "Delete" , "DestinationRule" , obj .Name , dh .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
149
+ log .Infof (LogFormat , "Delete" , "DestinationRule" , obj .Name , dh .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
149
150
return
150
151
}
151
152
handleDestinationRuleEvent (obj , dh , common .Delete , common .DestinationRule )
152
153
}
153
154
154
155
func (vh * VirtualServiceHandler ) Added (obj * v1alpha3.VirtualService ) {
155
156
if IgnoreIstioResource (obj .Spec .ExportTo , obj .Annotations , obj .Namespace ) {
156
- log .Infof (LogFormat , "Add" , "VirtualService" , obj .Name , vh .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
157
+ log .Infof (LogFormat , "Add" , "VirtualService" , obj .Name , vh .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
157
158
return
158
159
}
159
160
err := handleVirtualServiceEvent (obj , vh , common .Add , common .VirtualService )
@@ -164,7 +165,7 @@ func (vh *VirtualServiceHandler) Added(obj *v1alpha3.VirtualService) {
164
165
165
166
func (vh * VirtualServiceHandler ) Updated (obj * v1alpha3.VirtualService ) {
166
167
if IgnoreIstioResource (obj .Spec .ExportTo , obj .Annotations , obj .Namespace ) {
167
- log .Infof (LogFormat , "Update" , "VirtualService" , obj .Name , vh .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
168
+ log .Infof (LogFormat , "Update" , "VirtualService" , obj .Name , vh .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
168
169
return
169
170
}
170
171
err := handleVirtualServiceEvent (obj , vh , common .Update , common .VirtualService )
@@ -175,7 +176,7 @@ func (vh *VirtualServiceHandler) Updated(obj *v1alpha3.VirtualService) {
175
176
176
177
func (vh * VirtualServiceHandler ) Deleted (obj * v1alpha3.VirtualService ) {
177
178
if IgnoreIstioResource (obj .Spec .ExportTo , obj .Annotations , obj .Namespace ) {
178
- log .Infof (LogFormat , "Delete" , "VirtualService" , obj .Name , vh .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
179
+ log .Infof (LogFormat , "Delete" , "VirtualService" , obj .Name , vh .ClusterID , "Skipping resource from namespace=" + obj .Namespace )
179
180
return
180
181
}
181
182
err := handleVirtualServiceEvent (obj , vh , common .Delete , common .VirtualService )
@@ -192,7 +193,7 @@ func (dh *SidecarHandler) Deleted(obj *v1alpha3.Sidecar) {}
192
193
193
194
func IgnoreIstioResource (exportTo []string , annotations map [string ]string , namespace string ) bool {
194
195
195
- if len (annotations ) > 0 && annotations [common .AdmiralIgnoreAnnotation ] == "true" {
196
+ if len (annotations ) > 0 && annotations [common .AdmiralIgnoreAnnotation ] == "true" {
196
197
return true
197
198
}
198
199
@@ -225,9 +226,9 @@ func handleDestinationRuleEvent(obj *v1alpha3.DestinationRule, dh *DestinationRu
225
226
226
227
r := dh .RemoteRegistry
227
228
228
- dependentClusters := r .AdmiralCache .CnameDependentClusterCache .Get (destinationRule .Host )
229
+ dependentClusters := r .AdmiralCache .CnameDependentClusterCache .Get (destinationRule .Host ). Copy ()
229
230
230
- if dependentClusters != nil && len (dependentClusters . Map () ) > 0 {
231
+ if len (dependentClusters ) > 0 {
231
232
232
233
log .Infof (LogFormat , "Event" , "DestinationRule" , obj .Name , clusterId , "Processing" )
233
234
@@ -240,7 +241,7 @@ func handleDestinationRuleEvent(obj *v1alpha3.DestinationRule, dh *DestinationRu
240
241
241
242
allDependentClusters := make (map [string ]string )
242
243
243
- util .MapCopy (allDependentClusters , dependentClusters . Map () )
244
+ util .MapCopy (allDependentClusters , dependentClusters )
244
245
245
246
allDependentClusters [clusterId ] = clusterId
246
247
@@ -416,7 +417,7 @@ func handleVirtualServiceEvent(obj *v1alpha3.VirtualService, vh *VirtualServiceH
416
417
}
417
418
418
419
//check if this virtual service is used by Argo rollouts for canary strategy, if so, update the corresponding SE with appropriate weights
419
- if common .GetAdmiralParams ().ArgoRolloutsEnabled {
420
+ if common .GetAdmiralParams ().ArgoRolloutsEnabled {
420
421
rollouts , err := vh .RemoteRegistry .RemoteControllers [clusterId ].RolloutController .RolloutClient .Rollouts (obj .Namespace ).List (v12.ListOptions {})
421
422
422
423
if err != nil {
@@ -432,11 +433,11 @@ func handleVirtualServiceEvent(obj *v1alpha3.VirtualService, vh *VirtualServiceH
432
433
}
433
434
}
434
435
435
- dependentClusters := r .AdmiralCache .CnameDependentClusterCache .Get (virtualService .Hosts [0 ])
436
+ dependentClusters := r .AdmiralCache .CnameDependentClusterCache .Get (virtualService .Hosts [0 ]). Copy ()
436
437
437
- if dependentClusters != nil && len (dependentClusters . Map () ) > 0 {
438
+ if len (dependentClusters ) > 0 {
438
439
439
- for _ , dependentCluster := range dependentClusters . Map () {
440
+ for _ , dependentCluster := range dependentClusters {
440
441
441
442
rc := r .RemoteControllers [dependentCluster ]
442
443
@@ -480,7 +481,7 @@ func handleVirtualServiceEvent(obj *v1alpha3.VirtualService, vh *VirtualServiceH
480
481
}
481
482
return nil
482
483
} else {
483
- log .Infof (LogFormat ,"Event" , "VirtualService" , obj .Name , clusterId , "No dependent clusters found" )
484
+ log .Infof (LogFormat , "Event" , "VirtualService" , obj .Name , clusterId , "No dependent clusters found" )
484
485
}
485
486
486
487
//copy the VirtualService `as is` if they are not generated by Admiral (not in CnameDependentClusterCache)
@@ -510,7 +511,7 @@ func addUpdateVirtualService(obj *v1alpha3.VirtualService, exist *v1alpha3.Virtu
510
511
if obj .Annotations == nil {
511
512
obj .Annotations = map [string ]string {}
512
513
}
513
- obj .Annotations ["app.kubernetes.io/created-by" ] = "admiral"
514
+ obj .Annotations ["app.kubernetes.io/created-by" ] = "admiral"
514
515
if exist == nil || len (exist .Spec .Hosts ) == 0 {
515
516
obj .Namespace = namespace
516
517
obj .ResourceVersion = ""
@@ -537,20 +538,20 @@ func addUpdateServiceEntry(obj *v1alpha3.ServiceEntry, exist *v1alpha3.ServiceEn
537
538
if obj .Annotations == nil {
538
539
obj .Annotations = map [string ]string {}
539
540
}
540
- obj .Annotations ["app.kubernetes.io/created-by" ] = "admiral"
541
+ obj .Annotations ["app.kubernetes.io/created-by" ] = "admiral"
541
542
if exist == nil || exist .Spec .Hosts == nil {
542
543
obj .Namespace = namespace
543
544
obj .ResourceVersion = ""
544
545
_ , err = rc .ServiceEntryController .IstioClient .NetworkingV1alpha3 ().ServiceEntries (namespace ).Create (obj )
545
546
op = "Add"
546
- log .Infof (LogFormat + " SE=%s" , op , "ServiceEntry" , obj .Name , rc .ClusterID , "New SE" , obj .Spec .String ())
547
+ log .Infof (LogFormat + " SE=%s" , op , "ServiceEntry" , obj .Name , rc .ClusterID , "New SE" , obj .Spec .String ())
547
548
} else {
548
549
exist .Labels = obj .Labels
549
550
exist .Annotations = obj .Annotations
550
551
op = "Update"
551
552
skipUpdate , diff := skipDestructiveUpdate (rc , obj , exist )
552
553
if diff != "" {
553
- log .Infof (LogFormat + " diff=%s" , op , "ServiceEntry" , obj .Name , rc .ClusterID , "Diff in update" , diff )
554
+ log .Infof (LogFormat + " diff=%s" , op , "ServiceEntry" , obj .Name , rc .ClusterID , "Diff in update" , diff )
554
555
}
555
556
if skipUpdate {
556
557
log .Infof (LogFormat , op , "ServiceEntry" , obj .Name , rc .ClusterID , "Update skipped as it was destructive during Admiral's bootup phase" )
@@ -573,7 +574,7 @@ func skipDestructiveUpdate(rc *RemoteController, new *v1alpha3.ServiceEntry, old
573
574
skipDestructive = false
574
575
destructive , diff := getServiceEntryDiff (new , old )
575
576
//do not update SEs during bootup phase if they are destructive
576
- if time .Since (rc .StartTime ) < (2 * common .GetAdmiralParams ().CacheRefreshDuration ) && destructive {
577
+ if time .Since (rc .StartTime ) < (2 * common .GetAdmiralParams ().CacheRefreshDuration ) && destructive {
577
578
skipDestructive = true
578
579
}
579
580
@@ -603,17 +604,17 @@ func getServiceEntryDiff(new *v1alpha3.ServiceEntry, old *v1alpha3.ServiceEntry)
603
604
found [nEndpoint .Address ] = "1"
604
605
if ! reflect .DeepEqual (val , nEndpoint ) {
605
606
destructive = true
606
- buffer .WriteString (fmt .Sprintf (format , "endpoint" , "Update" , val .String (), nEndpoint .String ()))
607
+ buffer .WriteString (fmt .Sprintf (format , "endpoint" , "Update" , val .String (), nEndpoint .String ()))
607
608
}
608
609
} else {
609
- buffer .WriteString (fmt .Sprintf (format , "endpoint" , "Add" , "" , nEndpoint .String ()))
610
+ buffer .WriteString (fmt .Sprintf (format , "endpoint" , "Add" , "" , nEndpoint .String ()))
610
611
}
611
612
}
612
613
613
614
for key := range oldEndpointMap {
614
615
if _ , ok := found [key ]; ! ok {
615
616
destructive = true
616
- buffer .WriteString (fmt .Sprintf (format , "endpoint" , "Delete" , oldEndpointMap [key ].String (), "" ))
617
+ buffer .WriteString (fmt .Sprintf (format , "endpoint" , "Delete" , oldEndpointMap [key ].String (), "" ))
617
618
}
618
619
}
619
620
@@ -638,7 +639,7 @@ func addUpdateDestinationRule(obj *v1alpha3.DestinationRule, exist *v1alpha3.Des
638
639
if obj .Annotations == nil {
639
640
obj .Annotations = map [string ]string {}
640
641
}
641
- obj .Annotations ["app.kubernetes.io/created-by" ] = "admiral"
642
+ obj .Annotations ["app.kubernetes.io/created-by" ] = "admiral"
642
643
if exist == nil || exist .Name == "" || exist .Spec .Host == "" {
643
644
obj .Namespace = namespace
644
645
obj .ResourceVersion = ""
@@ -714,22 +715,24 @@ func getServiceForDeployment(rc *RemoteController, deployment *k8sAppsV1.Deploym
714
715
return matchedService
715
716
}
716
717
717
- func getDependentClusters (dependents * common. Map , identityClusterCache * common.MapOfMaps , sourceServices map [string ]* k8sV1.Service ) map [string ]string {
718
+ func getDependentClusters (dependents map [ string ] string , identityClusterCache * common.MapOfMaps , sourceServices map [string ]* k8sV1.Service ) map [string ]string {
718
719
var dependentClusters = make (map [string ]string )
719
- //TODO optimize this map construction
720
- if dependents != nil {
721
- for identity , clusters := range identityClusterCache .Map () {
722
- for depIdentity := range dependents .Map () {
723
- if identity == depIdentity {
724
- for _ , clusterId := range clusters .Map () {
725
- _ , ok := sourceServices [clusterId ]
726
- if ! ok {
727
- dependentClusters [clusterId ] = clusterId
728
- }
729
- }
730
- }
731
- }
720
+
721
+ if dependents == nil {
722
+ return dependentClusters
723
+ }
724
+
725
+ for depIdentity := range dependents {
726
+ clusters := identityClusterCache .Get (depIdentity )
727
+ if clusters == nil {
728
+ continue
732
729
}
730
+ clusters .Range (func (k string , clusterID string ) {
731
+ _ , ok := sourceServices [clusterID ]
732
+ if ! ok {
733
+ dependentClusters [clusterID ] = clusterID
734
+ }
735
+ })
733
736
}
734
737
return dependentClusters
735
738
}
@@ -761,7 +764,7 @@ func getServiceForRollout(rc *RemoteController, rollout *argo.Rollout) map[strin
761
764
return nil
762
765
}
763
766
764
- var canaryService , stableService , virtualServiceRouteName string
767
+ var canaryService , stableService , virtualServiceRouteName string
765
768
766
769
var istioCanaryWeights = make (map [string ]int32 )
767
770
@@ -827,7 +830,6 @@ func getServiceForRollout(rc *RemoteController, rollout *argo.Rollout) map[strin
827
830
828
831
var matchedServices = make (map [string ]* WeightedService )
829
832
830
-
831
833
//if we have more than one matching service we will pick the first one, for this to be deterministic we sort services
832
834
var servicesInNamespace = cachedService .Service [rollout .Namespace ]
833
835
0 commit comments