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

Apply netpols async with retry #5909

Merged
merged 1 commit into from
May 23, 2024
Merged
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
31 changes: 19 additions & 12 deletions pkg/rke2/np.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"sync"
"time"

"github.com/k3s-io/k3s/pkg/cli/cmds"
"github.com/k3s-io/k3s/pkg/util"
Expand All @@ -15,6 +16,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/retry"
)
Expand Down Expand Up @@ -221,7 +223,7 @@ func setPoliciesFromTemplates(ctx context.Context, cs kubernetes.Interface, temp
}
return nil
}); err != nil {
logrus.Fatalf("Failed to apply network policy %s to namespace %s: %v", template.name, ns.Name, err)
return fmt.Errorf("failed to apply network policy %s to namespace %s: %v", template.name, ns.Name, err)
}
}
}
Expand All @@ -238,27 +240,32 @@ func setNetworkPolicies(cisMode bool, namespaces []string) cmds.StartupHook {
return nil
}

logrus.Info("Applying network policies...")
go func() {
defer wg.Done()
<-args.APIServerReady
cs, err := util.GetClientSet(args.KubeConfigSupervisor)
if err != nil {
logrus.Fatalf("np: new k8s client: %v", err)
}
for _, namespace := range namespaces {
if err := setPoliciesFromTemplates(ctx, cs, defaultNamespacePolicies, namespace); err != nil {
logrus.Fatal(err)
}
if namespace == metav1.NamespaceSystem {
if err := setPoliciesFromTemplates(ctx, cs, defaultKubeSystemPolicies, namespace); err != nil {
logrus.Fatal(err)

go wait.PollImmediateInfiniteWithContext(ctx, 5*time.Second, func(ctx context.Context) (bool, error) {
logrus.Info("Applying network policies...")
for _, namespace := range namespaces {
if err := setPoliciesFromTemplates(ctx, cs, defaultNamespacePolicies, namespace); err != nil {
logrus.Errorf("Network policy apply failed, will retry: %v", err)
return false, nil
}
if namespace == metav1.NamespaceSystem {
if err := setPoliciesFromTemplates(ctx, cs, defaultKubeSystemPolicies, namespace); err != nil {
logrus.Errorf("Network policy apply failed, will retry: %v", err)
return false, nil
}
}
}
}
logrus.Info("Applying network policies complete")
logrus.Info("Applying network policies complete")
return true, nil
})
}()

return nil
}
}
Expand Down
Loading