SAControllerClientBuilder: Replace ListWatchUntil with UntilWithSync to

avoid watch timeouts
pull/564/head
Tomas Nozicka 2019-01-07 15:58:12 +01:00
parent fdf381098b
commit 9747c9a221
1 changed files with 10 additions and 6 deletions

View File

@ -17,6 +17,7 @@ limitations under the License.
package controller
import (
"context"
"fmt"
"time"
@ -34,11 +35,10 @@ import (
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
watchtools "k8s.io/client-go/tools/watch"
"k8s.io/klog"
"k8s.io/kubernetes/pkg/api/legacyscheme"
api "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/serviceaccount"
"k8s.io/klog"
)
// ControllerClientBuilder allows you to get clients and configs for controllers
@ -114,18 +114,22 @@ func (b SAControllerClientBuilder) Config(name string) (*restclient.Config, erro
}
var clientConfig *restclient.Config
fieldSelector := fields.SelectorFromSet(map[string]string{
api.SecretTypeField: string(v1.SecretTypeServiceAccountToken),
}).String()
lw := &cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
options.FieldSelector = fields.SelectorFromSet(map[string]string{api.SecretTypeField: string(v1.SecretTypeServiceAccountToken)}).String()
options.FieldSelector = fieldSelector
return b.CoreClient.Secrets(b.Namespace).List(options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
options.FieldSelector = fields.SelectorFromSet(map[string]string{api.SecretTypeField: string(v1.SecretTypeServiceAccountToken)}).String()
options.FieldSelector = fieldSelector
return b.CoreClient.Secrets(b.Namespace).Watch(options)
},
}
_, err = watchtools.ListWatchUntil(30*time.Second, lw,
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_, err = watchtools.UntilWithSync(ctx, lw, &v1.Secret{}, nil,
func(event watch.Event) (bool, error) {
switch event.Type {
case watch.Deleted: