From dc7a4b1fb99ebe612b64cb31694ae89e083ef8d0 Mon Sep 17 00:00:00 2001 From: Tomoki Sugiura Date: Thu, 9 May 2024 13:17:07 +0900 Subject: [PATCH] Make setup egress before the manager starts Signed-off-by: Tomoki Sugiura --- v2/controllers/pod_watcher.go | 30 ++++++++++++++++++++++++++++++ v2/go.mod | 1 + v2/go.sum | 2 ++ 3 files changed, 33 insertions(+) diff --git a/v2/controllers/pod_watcher.go b/v2/controllers/pod_watcher.go index e4c86cbc..7fa35ff8 100644 --- a/v2/controllers/pod_watcher.go +++ b/v2/controllers/pod_watcher.go @@ -12,11 +12,13 @@ import ( "github.com/cybozu-go/coil/v2/pkg/founat" "github.com/go-logr/logr" "github.com/prometheus/client_golang/prometheus" + "golang.org/x/sync/errgroup" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/config" "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/metrics" ) @@ -55,6 +57,34 @@ func SetupPodWatcher(mgr ctrl.Manager, ns, name string, ft founat.FoUTunnel, enc peers: make(map[string]map[string]struct{}), } + client, err := client.New(config.GetConfigOrDie(), client.Options{}) + if err != nil { + return err + } + + var pods corev1.PodList + if err := client.List(context.Background(), &pods); err != nil { + return err + } + + g := new(errgroup.Group) + for _, pod := range pods.Items { + pod := pod + g.Go(func() error { + if isTerminated(&pod) { + return nil + } + if !r.shouldHandle(&pod) { + return nil + } + fmt.Printf("shanpu: %s\n", pod.Name) + return r.addPod(&pod, log.FromContext(context.Background())) + }) + } + if err := g.Wait(); err != nil { + return err + } + return ctrl.NewControllerManagedBy(mgr). For(&corev1.Pod{}). Complete(r) diff --git a/v2/go.mod b/v2/go.mod index e73ec80c..7e974650 100644 --- a/v2/go.mod +++ b/v2/go.mod @@ -22,6 +22,7 @@ require ( github.com/spf13/viper v1.18.2 github.com/vishvananda/netlink v1.2.1-beta.2.0.20230807190133-6afddb37c1f0 go.uber.org/zap v1.27.0 + golang.org/x/sync v0.6.0 golang.org/x/sys v0.19.0 google.golang.org/grpc v1.63.2 google.golang.org/protobuf v1.33.0 diff --git a/v2/go.sum b/v2/go.sum index c3148dcf..c9590689 100644 --- a/v2/go.sum +++ b/v2/go.sum @@ -220,6 +220,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= +golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=