Skip to content

Commit

Permalink
fix: kubeclient timeout
Browse files Browse the repository at this point in the history
As the previous timeout is 0, the patch annotation operations might accidentally hang forever under high pressure. Use a separate kubeclient with timeout to fail fast when this happens
  • Loading branch information
oilbeater committed Sep 15, 2021
1 parent 4c3e428 commit 39c8a19
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
62 changes: 54 additions & 8 deletions pkg/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"time"

attacnetclientset "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned"
"github.com/spf13/pflag"
Expand All @@ -18,16 +19,21 @@ import (

// Configuration is the controller conf
type Configuration struct {
BindAddress string
OvnNbAddr string
OvnSbAddr string
OvnTimeout int
KubeConfigFile string
KubeRestConfig *rest.Config
BindAddress string
OvnNbAddr string
OvnSbAddr string
OvnTimeout int
KubeConfigFile string
KubeRestConfig *rest.Config

KubeClient kubernetes.Interface
KubeOvnClient clientset.Interface
AttachNetClient attacnetclientset.Interface

// with no timeout
KubeFactoryClient kubernetes.Interface
KubeOvnFactoryClient clientset.Interface

DefaultLogicalSwitch string
DefaultCIDR string
DefaultGateway string
Expand Down Expand Up @@ -177,6 +183,10 @@ func ParseFlags() (*Configuration, error) {
return nil, err
}

if err := config.initKubeFactoryClient(); err != nil {
return nil, err
}

klog.Infof("config is %+v", config)
return config, nil
}
Expand All @@ -196,8 +206,8 @@ func (config *Configuration) initKubeClient() error {
}
cfg.QPS = 1000
cfg.Burst = 2000

config.KubeRestConfig = cfg
// use cmd arg to modify timeout later
cfg.Timeout = 30 * time.Second

AttachNetClient, err := attacnetclientset.NewForConfig(cfg)
if err != nil {
Expand All @@ -223,3 +233,39 @@ func (config *Configuration) initKubeClient() error {
config.KubeClient = kubeClient
return nil
}

func (config *Configuration) initKubeFactoryClient() error {
var cfg *rest.Config
var err error
if config.KubeConfigFile == "" {
klog.Infof("no --kubeconfig, use in-cluster kubernetes config")
cfg, err = rest.InClusterConfig()
} else {
cfg, err = clientcmd.BuildConfigFromFlags("", config.KubeConfigFile)
}
if err != nil {
klog.Errorf("failed to build kubeconfig %v", err)
return err
}
cfg.QPS = 1000
cfg.Burst = 2000

config.KubeRestConfig = cfg

kubeOvnClient, err := clientset.NewForConfig(cfg)
if err != nil {
klog.Errorf("init kubeovn client failed %v", err)
return err
}
config.KubeOvnFactoryClient = kubeOvnClient

cfg.ContentType = "application/vnd.kubernetes.protobuf"
cfg.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json"
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
klog.Errorf("init kubernetes client failed %v", err)
return err
}
config.KubeFactoryClient = kubeClient
return nil
}
10 changes: 5 additions & 5 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/kubeovn/kube-ovn/pkg/util"
)

const controllerAgentName = "ovn-controller"
const controllerAgentName = "kube-ovn-controller"

// Controller is kube-ovn main controller that watch ns/pod/node/svc/ep and operate ovn
type Controller struct {
Expand Down Expand Up @@ -134,18 +134,18 @@ func NewController(config *Configuration) *Controller {
klog.V(4).Info("Creating event broadcaster")
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(klog.Infof)
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: config.KubeClient.CoreV1().Events("")})
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: config.KubeFactoryClient.CoreV1().Events("")})
recorder := eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: controllerAgentName})

informerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(config.KubeClient, 0,
informerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(config.KubeFactoryClient, 0,
kubeinformers.WithTweakListOptions(func(listOption *metav1.ListOptions) {
listOption.AllowWatchBookmarks = true
}))
cmInformerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(config.KubeClient, 0,
cmInformerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(config.KubeFactoryClient, 0,
kubeinformers.WithTweakListOptions(func(listOption *metav1.ListOptions) {
listOption.AllowWatchBookmarks = true
}), kubeinformers.WithNamespace(config.PodNamespace))
kubeovnInformerFactory := kubeovninformer.NewSharedInformerFactoryWithOptions(config.KubeOvnClient, 0,
kubeovnInformerFactory := kubeovninformer.NewSharedInformerFactoryWithOptions(config.KubeOvnFactoryClient, 0,
kubeovninformer.WithTweakListOptions(func(listOption *metav1.ListOptions) {
listOption.AllowWatchBookmarks = true
}))
Expand Down

0 comments on commit 39c8a19

Please sign in to comment.