Skip to content

Commit

Permalink
Make setup egress before the manager starts
Browse files Browse the repository at this point in the history
Signed-off-by: Tomoki Sugiura <[email protected]>
  • Loading branch information
chez-shanpu committed May 27, 2024
1 parent 5faaa12 commit 500111e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion v2/cmd/coil-egress/sub/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func subMain() error {
return err
}

if err := controllers.SetupPodWatcher(mgr, myNS, myName, ft, config.enableSportAuto, eg); err != nil {
if err := controllers.SetupPodWatcher(mgr, myNS, myName, ft, config.enableSportAuto, eg, nil); err != nil {
return err
}

Expand Down
37 changes: 36 additions & 1 deletion v2/controllers/pod_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ 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"
"k8s.io/client-go/rest"
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"
)
Expand All @@ -40,7 +43,7 @@ func init() {
// +kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch

// SetupPodWatcher registers pod watching reconciler to mgr.
func SetupPodWatcher(mgr ctrl.Manager, ns, name string, ft founat.FoUTunnel, encapSportAuto bool, eg founat.Egress) error {
func SetupPodWatcher(mgr ctrl.Manager, ns, name string, ft founat.FoUTunnel, encapSportAuto bool, eg founat.Egress, cfg *rest.Config) error {
clientPods.Reset()

r := &podWatcher{
Expand All @@ -55,6 +58,38 @@ func SetupPodWatcher(mgr ctrl.Manager, ns, name string, ft founat.FoUTunnel, enc
peers: make(map[string]map[string]struct{}),
}

if cfg == nil {
cfg = config.GetConfigOrDie()
}

client, err := client.New(cfg, client.Options{})
if err != nil {
return err
}

var pods corev1.PodList
ctx := context.Background()
if err := client.List(ctx, &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
}
return r.addPod(&pod, log.FromContext(ctx))
})
}
if err := g.Wait(); err != nil {
return err
}

return ctrl.NewControllerManagedBy(mgr).
For(&corev1.Pod{}).
Complete(r)
Expand Down
2 changes: 1 addition & 1 deletion v2/controllers/pod_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ var _ = Describe("Pod watcher", func() {
})
Expect(err).ToNot(HaveOccurred())

err = SetupPodWatcher(mgr, "internet", "egress2", ft, true, eg)
err = SetupPodWatcher(mgr, "internet", "egress2", ft, true, eg, cfg)
Expect(err).ToNot(HaveOccurred())

go func() {
Expand Down
1 change: 1 addition & 0 deletions v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 500111e

Please sign in to comment.