Skip to content

Commit 6764afb

Browse files
committed
Fix VAP flakes
Signed-off-by: Mangirdas Judeikis <mangirdas@judeikis.lt> On-behalf-of: @SAP mangirdas.judeikis@sap.com
1 parent fa243f8 commit 6764afb

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

pkg/admission/validatingadmissionpolicy/validating_admission_policy.go

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,15 @@ func Register(plugins *admission.Plugins) {
6464
func NewKubeValidatingAdmissionPolicy() *KubeValidatingAdmissionPolicy {
6565
return &KubeValidatingAdmissionPolicy{
6666
Handler: admission.NewHandler(admission.Connect, admission.Create, admission.Delete, admission.Update),
67-
delegates: make(map[logicalcluster.Name]*stoppableValidatingAdmissionPolicy),
67+
delegates: make(map[delegateKey]*stoppableValidatingAdmissionPolicy),
6868
}
6969
}
7070

71+
type delegateKey struct {
72+
policyCluster logicalcluster.Name
73+
targetCluster logicalcluster.Name
74+
}
75+
7176
type KubeValidatingAdmissionPolicy struct {
7277
*admission.Handler
7378

@@ -83,7 +88,7 @@ type KubeValidatingAdmissionPolicy struct {
8388
getAPIBindings func(clusterName logicalcluster.Name) ([]*apisv1alpha2.APIBinding, error)
8489

8590
delegatesLock sync.RWMutex
86-
delegates map[logicalcluster.Name]*stoppableValidatingAdmissionPolicy
91+
delegates map[delegateKey]*stoppableValidatingAdmissionPolicy
8792

8893
logicalClusterDeletionMonitorStarter sync.Once
8994
}
@@ -120,8 +125,9 @@ func (k *KubeValidatingAdmissionPolicy) SetKcpInformers(local, global kcpinforme
120125
k.delegatesLock.Lock()
121126
defer k.delegatesLock.Unlock()
122127

128+
// Remove all delegates that involve this cluster (either as policy or target)
123129
for key, delegate := range k.delegates {
124-
if key == clName {
130+
if key.policyCluster == clName || key.targetCluster == clName {
125131
delete(k.delegates, key)
126132
delegate.stop()
127133
}
@@ -168,7 +174,7 @@ func (k *KubeValidatingAdmissionPolicy) Validate(ctx context.Context, a admissio
168174
return err
169175
}
170176

171-
delegate, err := k.getOrCreateDelegate(sourceCluster)
177+
delegate, err := k.getOrCreateDelegate(sourceCluster, cluster.Name)
172178
if err != nil {
173179
return err
174180
}
@@ -193,10 +199,16 @@ func (k *KubeValidatingAdmissionPolicy) getSourceClusterForGroupResource(cluster
193199
return clusterName, nil
194200
}
195201

196-
// getOrCreateDelegate creates an actual plugin for clusterName.
197-
func (k *KubeValidatingAdmissionPolicy) getOrCreateDelegate(clusterName logicalcluster.Name) (*stoppableValidatingAdmissionPolicy, error) {
202+
// getOrCreateDelegate creates an actual plugin for policyClusterName (where policies are defined).
203+
// targetClusterName is the cluster where the object being validated resides.
204+
func (k *KubeValidatingAdmissionPolicy) getOrCreateDelegate(policyClusterName, targetClusterName logicalcluster.Name) (*stoppableValidatingAdmissionPolicy, error) {
205+
key := delegateKey{
206+
policyCluster: policyClusterName,
207+
targetCluster: targetClusterName,
208+
}
209+
198210
k.delegatesLock.RLock()
199-
delegate := k.delegates[clusterName]
211+
delegate := k.delegates[key]
200212
k.delegatesLock.RUnlock()
201213

202214
if delegate != nil {
@@ -206,7 +218,7 @@ func (k *KubeValidatingAdmissionPolicy) getOrCreateDelegate(clusterName logicalc
206218
k.delegatesLock.Lock()
207219
defer k.delegatesLock.Unlock()
208220

209-
delegate = k.delegates[clusterName]
221+
delegate = k.delegates[key]
210222
if delegate != nil {
211223
return delegate, nil
212224
}
@@ -229,29 +241,30 @@ func (k *KubeValidatingAdmissionPolicy) getOrCreateDelegate(clusterName logicalc
229241
stop: cancel,
230242
}
231243

232-
plugin.SetNamespaceInformer(k.localKubeSharedInformerFactory.Core().V1().Namespaces().Cluster(clusterName))
233-
plugin.SetExternalKubeClientSet(k.kubeClusterClient.Cluster(clusterName.Path()))
244+
// Use the target cluster's namespace informer, as that's where the objects being validated reside
245+
plugin.SetNamespaceInformer(k.localKubeSharedInformerFactory.Core().V1().Namespaces().Cluster(targetClusterName))
246+
plugin.SetExternalKubeClientSet(k.kubeClusterClient.Cluster(policyClusterName.Path()))
234247

235248
// TODO(ncdc): this is super inefficient to do per workspace
236-
discoveryClient := memory.NewMemCacheClient(k.kubeClusterClient.Cluster(clusterName.Path()).Discovery())
249+
discoveryClient := memory.NewMemCacheClient(k.kubeClusterClient.Cluster(policyClusterName.Path()).Discovery())
237250
restMapper := restmapper.NewDeferredDiscoveryRESTMapper(discoveryClient)
238251
plugin.SetRESTMapper(restMapper)
239252

240-
plugin.SetDynamicClient(k.dynamicClusterClient.Cluster(clusterName.Path()))
253+
plugin.SetDynamicClient(k.dynamicClusterClient.Cluster(policyClusterName.Path()))
241254
plugin.SetDrainedNotification(ctx.Done())
242255
plugin.SetAuthorizer(k.authorizer)
243-
plugin.SetClusterName(clusterName)
244-
plugin.SetSourceFactory(func(_ informers.SharedInformerFactory, client kubernetes.Interface, dynamicClient dynamic.Interface, restMapper meta.RESTMapper, clusterName logicalcluster.Name) generic.Source[validating.PolicyHook] {
256+
plugin.SetClusterName(policyClusterName)
257+
plugin.SetSourceFactory(func(_ informers.SharedInformerFactory, client kubernetes.Interface, dynamicClient dynamic.Interface, restMapper meta.RESTMapper, cn logicalcluster.Name) generic.Source[validating.PolicyHook] {
245258
return generic.NewPolicySource(
246-
k.globalKubeSharedInformerFactory.Admissionregistration().V1().ValidatingAdmissionPolicies().Informer().Cluster(clusterName),
247-
k.globalKubeSharedInformerFactory.Admissionregistration().V1().ValidatingAdmissionPolicyBindings().Informer().Cluster(clusterName),
259+
k.globalKubeSharedInformerFactory.Admissionregistration().V1().ValidatingAdmissionPolicies().Informer().Cluster(cn),
260+
k.globalKubeSharedInformerFactory.Admissionregistration().V1().ValidatingAdmissionPolicyBindings().Informer().Cluster(cn),
248261
validating.NewValidatingAdmissionPolicyAccessor,
249262
validating.NewValidatingAdmissionPolicyBindingAccessor,
250263
validating.CompilePolicy,
251264
nil,
252265
dynamicClient,
253266
restMapper,
254-
clusterName,
267+
cn,
255268
)
256269
})
257270

@@ -260,7 +273,7 @@ func (k *KubeValidatingAdmissionPolicy) getOrCreateDelegate(clusterName logicalc
260273
return nil, err
261274
}
262275

263-
k.delegates[clusterName] = delegate
276+
k.delegates[key] = delegate
264277

265278
return delegate, nil
266279
}
@@ -269,19 +282,22 @@ func (k *KubeValidatingAdmissionPolicy) logicalClusterDeleted(clusterName logica
269282
k.delegatesLock.Lock()
270283
defer k.delegatesLock.Unlock()
271284

272-
delegate := k.delegates[clusterName]
273-
274285
logger := klog.Background().WithValues("clusterName", clusterName)
275286

276-
if delegate == nil {
277-
logger.V(3).Info("received event to stop validating admission policy for logical cluster, but it wasn't in the map")
278-
return
287+
// Remove all delegates that involve this cluster (either as policy or target)
288+
found := false
289+
for key, delegate := range k.delegates {
290+
if key.policyCluster == clusterName || key.targetCluster == clusterName {
291+
logger.V(2).Info("stopping validating admission policy for logical cluster")
292+
delete(k.delegates, key)
293+
delegate.stop()
294+
found = true
295+
}
279296
}
280297

281-
logger.V(2).Info("stopping validating admission policy for logical cluster")
282-
283-
delete(k.delegates, clusterName)
284-
delegate.stop()
298+
if !found {
299+
logger.V(3).Info("received event to stop validating admission policy for logical cluster, but it wasn't in the map")
300+
}
285301
}
286302

287303
type stoppableValidatingAdmissionPolicy struct {

0 commit comments

Comments
 (0)