Skip to content

Enforcing read-timeout for informer-factory processor via property "kubernetes.informer.clientReadTimeout" (default 5min) #1588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformer;
import io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformers;
import io.kubernetes.client.spring.extended.controller.config.KubernetesInformerProperties;
import io.kubernetes.client.spring.extended.controller.factory.KubernetesControllerFactory;
import io.kubernetes.client.util.generic.GenericKubernetesApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
Expand All @@ -45,6 +50,10 @@ public class KubernetesInformerFactoryProcessor

public static final int ORDER = 0;

private static final Logger log = LoggerFactory.getLogger(KubernetesControllerFactory.class);

@Autowired private KubernetesInformerProperties informerProperties;

private ConfigurableListableBeanFactory beanFactory;

@Override
Expand Down Expand Up @@ -122,6 +131,20 @@ private <T extends KubernetesObject> Lister<T> lister(Class<T> type) {
private <T extends KubernetesObject> SharedInformer<T> informer(
Class<T> type, KubernetesInformer kubernetesInformer) {
ApiClient apiClient = this.beanFactory.getBean(ApiClient.class);

if (apiClient.getHttpClient().readTimeoutMillis() > 0) {
log.warn(
"Enforcing read-timeout of the ApiClient {} to {} so that the watch connection won't abort from client-side",
apiClient,
informerProperties.getClientReadTimeout());
apiClient.setHttpClient(
apiClient
.getHttpClient()
.newBuilder()
.readTimeout(informerProperties.getClientReadTimeout())
.build());
}

SharedInformerFactory sharedInformerFactory =
this.beanFactory.getBean(SharedInformerFactory.class);
final GenericKubernetesApi api =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
import io.kubernetes.client.util.ClientBuilder;
import java.io.IOException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
@ConditionalOnKubernetesInformerEnabled
@EnableConfigurationProperties({
KubernetesInformerProperties.class,
})
public class KubernetesInformerAutoConfiguration {

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2021 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 io.kubernetes.client.spring.extended.controller.config;

import io.kubernetes.client.informer.cache.ReflectorRunnable;
import java.time.Duration;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("kubernetes.informer")
public class KubernetesInformerProperties {

private Duration clientReadTimeout = ReflectorRunnable.REFLECTOR_WATCH_CLIENTSIDE_TIMEOUT;

public Duration getClientReadTimeout() {
return clientReadTimeout;
}

public KubernetesInformerProperties setClientReadTimeout(Duration clientReadTimeout) {
this.clientReadTimeout = clientReadTimeout;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class ReflectorRunnable<
ApiType extends KubernetesObject, ApiListType extends KubernetesListObject>
implements Runnable {

public static Duration REFLECTOR_WATCH_CLIENTSIDE_TIMEOUT = Duration.ofMinutes(5);

private static final Logger log = LoggerFactory.getLogger(ReflectorRunnable.class);

private String lastSyncResourceVersion;
Expand Down Expand Up @@ -113,7 +115,7 @@ public void run() {
new CallGeneratorParams(
Boolean.TRUE,
lastSyncResourceVersion,
Long.valueOf(Duration.ofMinutes(5).getSeconds()).intValue()));
Long.valueOf(REFLECTOR_WATCH_CLIENTSIDE_TIMEOUT.getSeconds()).intValue()));

synchronized (this) {
if (!isActive.get()) {
Expand Down