Skip to content

Commit a052cf4

Browse files
committed
Add filter on namespaces
1 parent 80da387 commit a052cf4

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

cmd/icinga-kubernetes/main.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,26 @@ func main() {
6666
}
6767
}
6868

69-
informers := kinformers.NewSharedInformerFactory(k, 0)
69+
informers := kinformers.NewSharedInformerFactoryWithOptions(k, 0, kinformers.WithNamespace(flags.Namespace))
7070

7171
g, ctx := errgroup.WithContext(ctx)
7272

7373
g.Go(func() error {
7474
return sync.NewSync(
7575
db, schema.NewNode, informers.Core().V1().Nodes().Informer(), logs.GetChildLogger("Nodes"),
76-
).Run(ctx)
76+
).Run(ctx, flags.Namespace)
7777
})
7878

7979
g.Go(func() error {
8080
return sync.NewSync(
8181
db, schema.NewNamespace, informers.Core().V1().Namespaces().Informer(), logs.GetChildLogger("Namespaces"),
82-
).Run(ctx)
82+
).Run(ctx, flags.Namespace)
8383
})
8484

8585
g.Go(func() error {
8686
return sync.NewSync(
8787
db, schema.NewPod, informers.Core().V1().Pods().Informer(), logs.GetChildLogger("Pods"),
88-
).Run(ctx)
88+
).Run(ctx, flags.Namespace)
8989
})
9090

9191
if err := g.Wait(); err != nil {

internal/flags.go

+3
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ package internal
44
type Flags struct {
55
// Config is the path to the config file
66
Config string `short:"c" long:"config" description:"path to config file" required:"true" default:"./config.yml"`
7+
8+
// Namespace is the namespace to sync
9+
Namespace string `short:"n" long:"namespace" description:"namespace to sync" required:"true" default:""`
710
}

pkg/schema/pod.go

+6
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ func NewPod() contracts.Resource {
1616
func (p *Pod) Obtain(kobject kmetav1.Object) {
1717
p.kmetaWithNamespace.Obtain(kobject)
1818
}
19+
20+
func (p *Pod) Scope() any {
21+
return &struct {
22+
Namespace string
23+
}{}
24+
}

pkg/sync/sync.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
type Sync interface {
18-
Run(context.Context) error
18+
Run(context.Context, string) error
1919
}
2020

2121
type sync struct {
@@ -39,12 +39,12 @@ func NewSync(
3939
}
4040
}
4141

42-
func (s *sync) Run(ctx context.Context) error {
42+
func (s *sync) Run(ctx context.Context, namespace string) error {
4343
s.logger.Info("Starting sync")
4444

4545
s.logger.Debug("Warming up")
4646

47-
err := s.Warmup(ctx)
47+
err := s.Warmup(ctx, namespace)
4848
if err != nil {
4949
return errors.Wrap(err, "warmup failed")
5050
}
@@ -139,15 +139,17 @@ func (s *sync) Run(ctx context.Context) error {
139139
return g.Wait()
140140
}
141141

142-
func (s *sync) Warmup(ctx context.Context) error {
142+
func (s *sync) Warmup(ctx context.Context, namespace string) error {
143143
g, ctx := errgroup.WithContext(ctx)
144144

145145
resource := s.factory()
146146
entities, err := s.db.YieldAll(ctx, func() database.Entity {
147147
return s.factory()
148-
}, s.db.BuildSelectStmt(resource, resource.Fingerprint()), struct{}{})
148+
}, s.db.BuildSelectStmt(resource, resource.Fingerprint()), struct{ Namespace string }{Namespace: namespace})
149149
com.ErrgroupReceive(ctx, g, err)
150150

151+
fmt.Println(s.db.BuildSelectStmt(resource, resource.Fingerprint()))
152+
151153
g.Go(func() error {
152154
for {
153155
select {

0 commit comments

Comments
 (0)