Skip to content

Commit

Permalink
Wait for taint to be gone in the node before starting the netpol cont…
Browse files Browse the repository at this point in the history
…roller

Signed-off-by: Manuel Buil <[email protected]>
  • Loading branch information
manuelbuil committed Dec 20, 2023
1 parent 9411196 commit 3de122b
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion pkg/agent/netpol/netpol.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"runtime"
"strings"
"sync"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cloudproviderapi "k8s.io/cloud-provider/api"

"github.com/cloudnativelabs/kube-router/v2/pkg/version"

Expand Down Expand Up @@ -55,6 +59,11 @@ func Run(ctx context.Context, nodeConfig *config.Node) error {
return err
}

// Loop until the node has addresses ready (max 1 minute)
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
checkNodeTaint(ctx, client, nodeConfig.AgentConfig.NodeName)

krConfig := options.NewKubeRouterConfig()
var serviceIPs []string
for _, elem := range nodeConfig.AgentConfig.ServiceCIDRs {
Expand Down Expand Up @@ -137,8 +146,38 @@ func Run(ctx context.Context, nodeConfig *config.Node) error {
npInformer.AddEventHandler(npc.NetworkPolicyEventHandler)

wg.Add(1)
logrus.Infof("Starting the netpol controller version %s, built on %s, %s", version.Version, version.BuildDate, runtime.Version())
logrus.Infof("Starting the netpol , nilcontroller version %s, built on %s, %s", version.Version, version.BuildDate, runtime.Version())
go npc.Run(healthCh, stopCh, &wg)

return nil
}

// checkNodeTaint checks if the node has the uninitialized tainted removed. If not loops until it is gone
func checkNodeTaint(ctx context.Context, client kubernetes.Interface, nodeName string) {
for {
taintFound := false
select {
case <-ctx.Done():
logrus.Warn("Timed out waiting for the node to be ready to start netpol controller")
return
default:
// Get the node object
node, err := client.CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{})
if err != nil {
logrus.Errorf("Error getting the node object: %v", err)
}

// Check for the uninitialized taint
for _, taint := range node.Spec.Taints {
if taint.Key == cloudproviderapi.TaintExternalCloudProvider {
taintFound = true
}
}
if !taintFound {
return
}
//2 seconds and try again
time.Sleep(2*time.Second)
}
}
}

0 comments on commit 3de122b

Please sign in to comment.