Skip to content

Commit f68a252

Browse files
committed
Use serviceFactory to fetch service UUID
1 parent f0ebd31 commit f68a252

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

cmd/icinga-kubernetes/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ func main() {
471471
})
472472

473473
g.Go(func() error {
474-
f := schemav1.NewServiceFactory(clientset)
474+
f := schemav1.NewServiceFactory(clientset, kdb, ctx)
475475
s := syncv1.NewSync(kdb, factory.Core().V1().Services().Informer(), log.WithName("services"), f.NewService)
476476

477477
return s.Run(
@@ -528,7 +528,8 @@ func main() {
528528
})
529529

530530
g.Go(func() error {
531-
s := syncv1.NewSync(kdb, factory.Networking().V1().Ingresses().Informer(), log.WithName("ingresses"), schemav1.NewIngress)
531+
serviceFactory := schemav1.NewServiceFactory(clientset, kdb, ctx)
532+
s := syncv1.NewSync(kdb, factory.Networking().V1().Ingresses().Informer(), log.WithName("ingresses"), schemav1.NewIngress(serviceFactory, ctx))
532533

533534
return s.Run(ctx)
534535
})

pkg/schema/v1/ingress.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package v1
22

33
import (
4+
"context"
45
"database/sql"
56
"github.com/icinga/icinga-go-library/types"
67
"github.com/icinga/icinga-kubernetes/pkg/database"
@@ -25,6 +26,8 @@ type Ingress struct {
2526
Annotations []Annotation `db:"-"`
2627
IngressAnnotations []IngressAnnotation `db:"-"`
2728
ResourceAnnotations []ResourceAnnotation `db:"-"`
29+
serviceFactory *ServiceFactory
30+
ctx context.Context
2831
}
2932

3033
type IngressTls struct {
@@ -70,8 +73,13 @@ type IngressAnnotation struct {
7073
AnnotationUuid types.UUID
7174
}
7275

73-
func NewIngress() Resource {
74-
return &Ingress{}
76+
func NewIngress(serviceFactory *ServiceFactory, ctx context.Context) func() Resource {
77+
return func() Resource {
78+
return &Ingress{
79+
serviceFactory: serviceFactory,
80+
ctx: ctx,
81+
}
82+
}
7583
}
7684

7785
func (i *Ingress) Obtain(k8s kmetav1.Object, clusterUuid types.UUID) {
@@ -127,7 +135,13 @@ func (i *Ingress) Obtain(k8s kmetav1.Object, clusterUuid types.UUID) {
127135
pathType := string(*ruleValue.PathType)
128136
if ruleValue.Backend.Service != nil {
129137
ingressRuleUuid := NewUUID(i.Uuid, rules.Host+ruleValue.Path+ruleValue.Backend.Service.Name)
130-
serviceUuid := NewUUID(ingressRuleUuid, ruleValue.Backend.Service.Name)
138+
139+
// Use the serviceFactory to fetch the service UUID
140+
serviceUuid, err := i.serviceFactory.GetServiceUUID(i.ctx, i.Namespace, ruleValue.Backend.Service.Name)
141+
if err != nil {
142+
continue
143+
}
144+
131145
i.IngressBackendService = append(i.IngressBackendService, IngressBackendService{
132146
ServiceUuid: serviceUuid,
133147
IngressUuid: i.Uuid,

pkg/schema/v1/service.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package v1
22

33
import (
4+
"context"
45
"database/sql"
56
"github.com/icinga/icinga-go-library/strcase"
67
"github.com/icinga/icinga-go-library/types"
@@ -10,14 +11,9 @@ import (
1011
kruntime "k8s.io/apimachinery/pkg/runtime"
1112
kserializer "k8s.io/apimachinery/pkg/runtime/serializer"
1213
kjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
13-
"k8s.io/client-go/kubernetes"
1414
"strings"
1515
)
1616

17-
type ServiceFactory struct {
18-
clientset *kubernetes.Clientset
19-
}
20-
2117
type Service struct {
2218
Meta
2319
ClusterIP string
@@ -47,6 +43,7 @@ type Service struct {
4743
ResourceAnnotations []ResourceAnnotation `db:"-"`
4844
ServicePods []ServicePod `db:"-"`
4945
factory *ServiceFactory
46+
ctx context.Context
5047
}
5148

5249
type ServiceSelector struct {
@@ -89,14 +86,11 @@ type ServicePod struct {
8986
PodUuid types.UUID
9087
}
9188

92-
func NewServiceFactory(clientset *kubernetes.Clientset) *ServiceFactory {
93-
return &ServiceFactory{
94-
clientset: clientset,
95-
}
96-
}
97-
9889
func (f *ServiceFactory) NewService() Resource {
99-
return &Service{factory: f}
90+
return &Service{
91+
factory: f,
92+
ctx: f.ctx,
93+
}
10094
}
10195

10296
func (s *Service) Obtain(k8s kmetav1.Object, clusterUuid types.UUID) {

0 commit comments

Comments
 (0)