kubernetes: Admission configuration managers incorrectly implement `HasSynced()`
The admission configation managers have some logic errors leading to HasSynced() reporting true prematurely. validatingWebhookConfigurationManager and mutatingWebhookConfigurationManager both suffer this issue.
Description of the problem:
HasSynced() checks the Informer is synced, and initialConfigurationSynced==true
The intent here is that each handler will sync the full state of the world by calling StoreState(informer.List()); synced.Set(true). HasSynced() then checks informer.HasSynced() && synced
This logic relies on the incorrect assumption that the underlying store will be atomically updated; this is not the case. As a result, the first call to the handler will call synced.Set(true). Eventually, the store will be fully updated, so informer.HasSynced() = true. The race occurs in that the handler calling is async; there is a race between the store fully populating and the handler being called with the fully populated store. During this time, the configuration manager will incorrectly return HasSynced=true
Full flow of data that triggers it:
- Initial state: informer is unsynced
- Event handler is added
- Initial list comes in. Lets say this returns
[a,b] - processDelta will process each item one by one. This means it will store
a, then call event handler fora - Event handler triggers for
a. This callsupdateConfiguration, which calls List() (returns[a]!!!), syncsm.configurationbased on the result, and setsinitialConfigurationSynced=true. - processDelta stores
b. It marks the informer asHasSynced=true. In this case, we will consider that the event handler has not been called forb(it is async, so can be delayed) mutatingWebhookConfigurationManager.HasSynced()checked. We check 2 things:informer.HasSynced()=trueandinitialConfigurationSynced=true, so we return true At this point, we have declared the controller as synced. However, it is not.
This can be reproduced by making the test a bit more robust. Currently, its not triggered because we only store a single item (it needs >1 items to trigger the race) and the test writes objects in a way not resembling real world usage.
This test, however, triggers the issue ~2% of the time on my machine:
/*
Copyright 2017 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 configuration
import (
"fmt"
"testing"
"time"
"k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/cache"
)
func TestGetMutatingWebhookConfig(t *testing.T) {
webhooks := []runtime.Object{
&v1.MutatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{Name: "webhook1"},
Webhooks: []v1.MutatingWebhook{{Name: "webhook1.1"}},
},
&v1.MutatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{Name: "webhook2"},
Webhooks: []v1.MutatingWebhook{{Name: "webhook2.1"}},
},
}
// Build a test client that the admission plugin can use to look up the MutatingWebhookConfiguration
client := fake.NewSimpleClientset(webhooks...)
informerFactory := informers.NewSharedInformerFactory(client, 0)
configManager := NewMutatingWebhookConfigurationManager(informerFactory).(*mutatingWebhookConfigurationManager)
stop := make(chan struct{})
defer close(stop)
informerFactory.Start(stop)
WaitForInformerFactorySync(stop, informerFactory)
WaitForCacheSync(stop, configManager.HasSynced)
informerFactory.WaitForCacheSync(stop)
// configuration populated
configurations := configManager.Webhooks()
if len(configurations) == 0 {
t.Errorf("expected non empty webhooks")
}
if len(configurations) != len(webhooks) {
t.Fatalf("expected %d, got %d", len(webhooks), len(configurations))
}
}
// Custom sync function to check more aggressively, exposing race conditions easier
func WaitForInformerFactorySync(stop <-chan struct{}, informerFactory informers.SharedInformerFactory) {
returnImmediately := make(chan struct{})
close(returnImmediately)
_ = wait.PollImmediate(time.Microsecond*10, wait.ForeverTestTimeout, func() (bool, error) {
select {
case <-stop:
return false, fmt.Errorf("channel closed")
default:
}
for _, synced := range informerFactory.WaitForCacheSync(returnImmediately) {
if !synced {
return false, nil
}
}
return true, nil
})
}
// Like standard WaitForCacheSync but more aggressive, to more easily detect race conditions.
func WaitForCacheSync(stop <-chan struct{}, cacheSyncs ...cache.InformerSynced) bool {
max := time.Millisecond * 100
delay := time.Millisecond
f := func() bool {
for _, syncFunc := range cacheSyncs {
if !syncFunc() {
return false
}
}
return true
}
for {
select {
case <-stop:
return false
default:
}
res := f()
if res {
return true
}
delay *= 2
if delay > max {
delay = max
}
time.Sleep(delay)
}
}
Some more context on this was discussed in https://kubernetes.slack.com/archives/C0EG7JC6T/p1591115369066700
/sig api-machinery
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 22 (15 by maintainers)
wait… deltafifo HasSynced checks both that Replace() was called and Pop has run as many times as initial items passed to Replace()edit: nevermind, that ensures HasSynced() doesn’t return true until the store is up to date, but does not guarantee the handlers were invoked after that point