Skip to content
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

Wait for Antrea client to be ready before starting watches #1042

Merged
merged 1 commit into from
Aug 8, 2020
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
15 changes: 9 additions & 6 deletions pkg/agent/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ func NewAntreaClientProvider(config config.ClientConnectionConfiguration, kubeCl
return antreaClientProvider
}

// RunOnce runs the task a single time synchronously, ensuring client is initialized.
// RunOnce runs the task a single time synchronously, ensuring client is initialized if kubeconfig is specified.
func (p *antreaClientProvider) RunOnce() error {
p.caContentProvider.RunOnce()
return p.updateAntreaClient()
}

Expand All @@ -93,18 +92,22 @@ func (p *antreaClientProvider) Enqueue() {
func (p *antreaClientProvider) GetAntreaClient() (versioned.Interface, error) {
p.mutex.RLock()
defer p.mutex.RUnlock()
if p.client == nil {
return nil, fmt.Errorf("Antrea client is not ready")
}
return p.client, nil
}

func (p *antreaClientProvider) updateAntreaClient() error {
caBundle := p.caContentProvider.CurrentCABundleContent()
if caBundle == nil {
klog.Warningf("Didn't get CA certificate from ConfigMap. May not be able to verify server cert")
}
var kubeConfig *rest.Config
var err error
if len(p.config.Kubeconfig) == 0 {
klog.Info("No antrea kubeconfig file was specified. Falling back to in-cluster config")
caBundle := p.caContentProvider.CurrentCABundleContent()
if caBundle == nil {
klog.Info("Didn't get CA certificate, skip updating Antrea Client")
return nil
}
kubeConfig, err = inClusterConfig(caBundle)
} else {
kubeConfig, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
Expand Down
19 changes: 17 additions & 2 deletions pkg/agent/controller/networkpolicy/networkpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,23 @@ func (c *Controller) GetControllerConnectionStatus() bool {
// Run begins watching and processing Antrea AddressGroups, AppliedToGroups
// and NetworkPolicies, and spawns workers that reconciles NetworkPolicy rules.
// Run will not return until stopCh is closed.
func (c *Controller) Run(stopCh <-chan struct{}) error {
func (c *Controller) Run(stopCh <-chan struct{}) {
attempts := 0
if err := wait.PollImmediateUntil(200*time.Millisecond, func() (bool, error) {
if attempts%10 == 0 {
klog.Info("Waiting for Antrea client to be ready")
}
if _, err := c.antreaClientProvider.GetAntreaClient(); err != nil {
attempts++
return false, nil
}
return true, nil
}, stopCh); err != nil {
klog.Info("Stopped waiting for Antrea client")
return
}
klog.Info("Antrea client is ready")

// Use NonSlidingUntil so that normal reconnection (disconnected after
// running a while) can reconnect immediately while abnormal reconnection
// won't be too aggressive.
Expand All @@ -318,7 +334,6 @@ func (c *Controller) Run(stopCh <-chan struct{}) error {
}

<-stopCh
return nil
}

func (c *Controller) enqueueRule(ruleID string) {
Expand Down