Skip to content

Commit 2571be7

Browse files
committed
PR Feedback: Nick
- improve the Validate semantics - avoid config.Get()
1 parent 3fd1a28 commit 2571be7

5 files changed

+33
-22
lines changed

business/istio_validations.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,11 @@ func (in *validationInfo) forceCheckers() bool {
257257
return in.hasBaseChange
258258
}
259259

260-
// Validate runs a full validation on all objects. It returns an IstioValidations object with all the checks found when running all
261-
// the enabled checkers.
262-
func (in *IstioValidationsService) Validate(ctx context.Context, cluster string, vInfo *validationInfo) (models.IstioValidations, error) {
260+
// Validate runs a full validation on all objects. The first return variable is the "validationPerformed" bool, indicating whether or not
261+
// the validation checkers were run. It will return false if a changeMap is provided in vInfo and no config changes were detected for the
262+
// cluster. Otherwise, it will return true. When true the new "validations" are returned in the second return variable. When false
263+
// the second argument is nil.
264+
func (in *IstioValidationsService) Validate(ctx context.Context, cluster string, vInfo *validationInfo) (bool, models.IstioValidations, error) {
263265
var end observability.EndFunc
264266
ctx, end = observability.StartSpan(ctx, "getValidations",
265267
observability.Attribute("package", "business"),
@@ -299,15 +301,15 @@ func (in *IstioValidationsService) Validate(ctx context.Context, cluster string,
299301
}
300302
istioConfigList, err := in.istioConfig.GetIstioConfigListForCluster(ctx, cluster, meta_v1.NamespaceAll, criteria)
301303
if err != nil {
302-
return nil, err
304+
return false, nil, err
303305
}
304306
vInfo.clusterInfo.istioConfig = istioConfigList
305307

306308
// if change detection is enabled then decide if we need to run the checkers
307309
if vInfo.changeDetectionEnabled() {
308310
changeDetected := detectClusterConfigChange(vInfo)
309311
if !changeDetected && !vInfo.forceCheckers() {
310-
return nil, nil
312+
return false, nil, nil
311313
}
312314
}
313315

@@ -318,19 +320,19 @@ func (in *IstioValidationsService) Validate(ctx context.Context, cluster string,
318320

319321
err := in.setNamespaceIstioConfig(vInfo)
320322
if err != nil {
321-
return nil, err
323+
return false, nil, err
322324
}
323325

324326
if err := in.setNonLocalMTLSConfig(vInfo); err != nil {
325-
return nil, err
327+
return false, nil, err
326328
}
327329

328330
objectCheckers := in.getAllObjectCheckers(vInfo)
329331

330332
validations.MergeValidations(runObjectCheckers(objectCheckers))
331333
}
332334

333-
return validations, nil
335+
return true, validations, nil
334336
}
335337

336338
// toWorkloadMap takes a list of workloads from different namespaces, and returns a map: namespace => models.Workloads

business/istio_validations_perf_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ func TestGetValidationsPerf(t *testing.T) {
7070
now := time.Now()
7171
vInfo, err := vs.NewValidationInfo(context.TODO(), []string{conf.KubernetesConfig.ClusterName}, nil)
7272
require.NoError(err)
73-
validations, err := vs.Validate(context.TODO(), conf.KubernetesConfig.ClusterName, vInfo)
73+
validationPerformed, validations, err := vs.Validate(context.TODO(), conf.KubernetesConfig.ClusterName, vInfo)
7474
require.NoError(err)
7575
log.Debugf("Validation Performance test took %f seconds for %d namespaces", time.Since(now).Seconds(), numNs)
76+
assert.True(validationPerformed)
7677
assert.NotEmpty(validations)
7778
}
7879

@@ -182,7 +183,7 @@ func BenchmarkValidate(b *testing.B) {
182183

183184
b.ResetTimer()
184185
for n := 0; n < b.N; n++ {
185-
_, err = vs.Validate(context.TODO(), conf.KubernetesConfig.ClusterName, vInfo)
186+
_, _, err = vs.Validate(context.TODO(), conf.KubernetesConfig.ClusterName, vInfo)
186187
if err != nil {
187188
b.Fatal(err)
188189
}

business/istio_validations_test.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ func TestGetNamespaceValidations(t *testing.T) {
6565
var changeMap = map[string]string{}
6666
vInfo, err := vs.NewValidationInfo(context.Background(), []string{conf.KubernetesConfig.ClusterName}, changeMap)
6767
require.NoError(err)
68-
validations, err := vs.Validate(context.Background(), conf.KubernetesConfig.ClusterName, vInfo)
68+
validationPerformed, validations, err := vs.Validate(context.Background(), conf.KubernetesConfig.ClusterName, vInfo)
6969
require.NoError(err)
70+
assert.True(validationPerformed)
7071
vs.kialiCache.Validations().Replace(validations)
7172

7273
validations, err = vs.GetValidations(context.TODO(), conf.KubernetesConfig.ClusterName)
@@ -77,17 +78,19 @@ func TestGetNamespaceValidations(t *testing.T) {
7778
// simulate a reconcile w/o a config change should skip running the checkers (new vInfo but re-use the changemap)
7879
vInfo, err = vs.NewValidationInfo(context.Background(), []string{conf.KubernetesConfig.ClusterName}, changeMap)
7980
require.NoError(err)
80-
validations, err = vs.Validate(context.Background(), conf.KubernetesConfig.ClusterName, vInfo)
81+
validationPerformed, validations, err = vs.Validate(context.Background(), conf.KubernetesConfig.ClusterName, vInfo)
8182
require.NoError(err)
83+
assert.False(validationPerformed)
8284
assert.Nil(validations)
8385

8486
// refresh the config but keep the changeMap, and we should see new validations. (note PeerAuthentication config updates its ResourceVersion)
8587
vs = mockCombinedValidationService(t, conf, fakeIstioConfigList(),
8688
[]string{"details.test.svc.cluster.local", "product.test.svc.cluster.local", "product2.test.svc.cluster.local", "customer.test.svc.cluster.local"})
8789
vInfo, err = vs.NewValidationInfo(context.Background(), []string{conf.KubernetesConfig.ClusterName}, changeMap)
8890
require.NoError(err)
89-
validations, err = vs.Validate(context.Background(), conf.KubernetesConfig.ClusterName, vInfo)
91+
validationPerformed, validations, err = vs.Validate(context.Background(), conf.KubernetesConfig.ClusterName, vInfo)
9092
require.NoError(err)
93+
assert.True(validationPerformed)
9194
assert.NotNil(validations)
9295
}
9396

@@ -723,8 +726,9 @@ func TestValidatingSingleObjectUpdatesList(t *testing.T) {
723726

724727
vInfo, err := vs.NewValidationInfo(context.Background(), []string{conf.KubernetesConfig.ClusterName}, nil)
725728
require.NoError(err)
726-
validations, err := vs.Validate(context.Background(), conf.KubernetesConfig.ClusterName, vInfo)
729+
validationPerformed, validations, err := vs.Validate(context.Background(), conf.KubernetesConfig.ClusterName, vInfo)
727730
require.NoError(err)
731+
assert.True(validationPerformed)
728732
vs.kialiCache.Validations().Replace(validations)
729733

730734
currentValidations, err := vs.GetValidations(context.Background(), conf.KubernetesConfig.ClusterName)
@@ -740,8 +744,9 @@ func TestValidatingSingleObjectUpdatesList(t *testing.T) {
740744
// make sure validations are updated in a cache before retrieving them
741745
vInfo, err = vs.NewValidationInfo(context.Background(), []string{conf.KubernetesConfig.ClusterName}, nil)
742746
require.NoError(err)
743-
validations, err = vs.Validate(context.Background(), conf.KubernetesConfig.ClusterName, vInfo)
747+
validationPerformed, validations, err = vs.Validate(context.Background(), conf.KubernetesConfig.ClusterName, vInfo)
744748
require.NoError(err)
749+
assert.True(validationPerformed)
745750
vs.kialiCache.Validations().Replace(validations)
746751

747752
updatedValidations, _, err := vs.ValidateIstioObject(context.Background(), conf.KubernetesConfig.ClusterName, "test", kubernetes.VirtualServices, "product-vs")

business/workloads.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,9 @@ func (in *WorkloadService) GetWorkloadList(ctx context.Context, criteria Workloa
282282
return *workloadList, err
283283
}
284284

285-
config := config.Get()
286285
for _, w := range ws {
287286
wItem := &models.WorkloadListItem{Health: *models.EmptyWorkloadHealth()}
288-
wItem.ParseWorkload(w, config)
287+
wItem.ParseWorkload(w, in.config)
289288
if istioConfigList, ok := istioConfigMap[cluster]; ok && criteria.IncludeIstioResources {
290289
wItem.IstioReferences = FilterUniqueIstioReferences(FilterWorkloadReferences(in.config, wItem.Labels, istioConfigList, cluster))
291290
}
@@ -1432,7 +1431,7 @@ func (in *WorkloadService) fetchWorkloadsFromCluster(ctx context.Context, cluste
14321431
// Add the Proxy Status to the workload
14331432
for _, pod := range w.Pods {
14341433
isWaypoint := w.IsWaypoint()
1435-
if config.Get().ExternalServices.Istio.IstioAPIEnabled && (pod.HasIstioSidecar() || isWaypoint) {
1434+
if in.config.ExternalServices.Istio.IstioAPIEnabled && (pod.HasIstioSidecar() || isWaypoint) {
14361435
pod.ProxyStatus = in.businessLayer.ProxyStatus.GetPodProxyStatus(cluster, namespace, pod.Name, !isWaypoint)
14371436
}
14381437
// Add the Proxy Status to the workload
@@ -2122,7 +2121,7 @@ func (in *WorkloadService) fetchWorkload(ctx context.Context, criteria WorkloadC
21222121

21232122
// Add the Proxy Status to the workload
21242123
for _, pod := range w.Pods {
2125-
if config.Get().ExternalServices.Istio.IstioAPIEnabled && (pod.HasIstioSidecar() || isWaypoint) {
2124+
if in.config.ExternalServices.Istio.IstioAPIEnabled && (pod.HasIstioSidecar() || isWaypoint) {
21262125
pod.ProxyStatus = in.businessLayer.ProxyStatus.GetPodProxyStatus(criteria.Cluster, criteria.Namespace, pod.Name, !isWaypoint)
21272126
}
21282127
// If Ambient is enabled for pod, check if has any Waypoint proxy

controller/validations.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ func (r *ValidationsReconciler) Reconcile(ctx context.Context, req ctrl.Request)
117117
version := r.kialiCache.Validations().Version()
118118
newValidations := make(models.IstioValidations)
119119
prevValidations := r.kialiCache.Validations().Items()
120+
121+
// If enabled, the same changeMap is used on each Reconcile. It holds the "resource version" of
122+
// each object used in the validation, and is then used to look for version changes (or a change in
123+
// the number of objects). We keep it in the cache for sane access.
120124
var changeMap business.ValidationChangeMap
121125
if config.Get().ExternalServices.Istio.ValidationChangeDetectionEnabled {
122126
changeMap = r.kialiCache.ValidationConfig().Items()
@@ -130,14 +134,14 @@ func (r *ValidationsReconciler) Reconcile(ctx context.Context, req ctrl.Request)
130134
}
131135

132136
for _, cluster := range r.clusters {
133-
clusterValidations, err := r.validationsService.Validate(ctx, cluster, vInfo)
137+
validationPerformed, clusterValidations, err := r.validationsService.Validate(ctx, cluster, vInfo)
134138
if err != nil {
135-
log.Errorf("[ValidationsReconciler] Error creating validations for cluster %s: %s", cluster, err)
139+
log.Errorf("[ValidationsReconciler] Error performing validation for cluster %s: %s", cluster, err)
136140
return ctrl.Result{}, err
137141
}
138142

139143
// if there have been no config changes for the cluster, just re-use the prior validations
140-
if clusterValidations == nil {
144+
if !validationPerformed {
141145
log.Tracef("validations: no changes for cluster [%s], re-using", cluster)
142146
clusterValidations = models.IstioValidations(prevValidations).FilterByCluster(cluster)
143147
} else {

0 commit comments

Comments
 (0)