diff --git a/cmd/icinga-kubernetes/main.go b/cmd/icinga-kubernetes/main.go index 56a1356..671cc24 100644 --- a/cmd/icinga-kubernetes/main.go +++ b/cmd/icinga-kubernetes/main.go @@ -471,7 +471,7 @@ func main() { }) g.Go(func() error { - f := schemav1.NewServiceFactory(clientset) + f := schemav1.NewServiceFactory(clientset, kdb, ctx) s := syncv1.NewSync(kdb, factory.Core().V1().Services().Informer(), log.WithName("services"), f.NewService) return s.Run( @@ -528,7 +528,8 @@ func main() { }) g.Go(func() error { - s := syncv1.NewSync(kdb, factory.Networking().V1().Ingresses().Informer(), log.WithName("ingresses"), schemav1.NewIngress) + serviceFactory := schemav1.NewServiceFactory(clientset, kdb, ctx) + s := syncv1.NewSync(kdb, factory.Networking().V1().Ingresses().Informer(), log.WithName("ingresses"), schemav1.NewIngress(serviceFactory, ctx)) return s.Run(ctx) }) diff --git a/pkg/schema/v1/ingress.go b/pkg/schema/v1/ingress.go index 2c6673f..a5dbf50 100644 --- a/pkg/schema/v1/ingress.go +++ b/pkg/schema/v1/ingress.go @@ -1,6 +1,7 @@ package v1 import ( + "context" "database/sql" "github.com/icinga/icinga-go-library/types" "github.com/icinga/icinga-kubernetes/pkg/database" @@ -25,6 +26,8 @@ type Ingress struct { Annotations []Annotation `db:"-"` IngressAnnotations []IngressAnnotation `db:"-"` ResourceAnnotations []ResourceAnnotation `db:"-"` + serviceFactory *ServiceFactory + ctx context.Context } type IngressTls struct { @@ -70,8 +73,13 @@ type IngressAnnotation struct { AnnotationUuid types.UUID } -func NewIngress() Resource { - return &Ingress{} +func NewIngress(serviceFactory *ServiceFactory, ctx context.Context) func() Resource { + return func() Resource { + return &Ingress{ + serviceFactory: serviceFactory, + ctx: ctx, + } + } } func (i *Ingress) Obtain(k8s kmetav1.Object, clusterUuid types.UUID) { @@ -127,7 +135,13 @@ func (i *Ingress) Obtain(k8s kmetav1.Object, clusterUuid types.UUID) { pathType := string(*ruleValue.PathType) if ruleValue.Backend.Service != nil { ingressRuleUuid := NewUUID(i.Uuid, rules.Host+ruleValue.Path+ruleValue.Backend.Service.Name) - serviceUuid := NewUUID(ingressRuleUuid, ruleValue.Backend.Service.Name) + + // Use the serviceFactory to fetch the service UUID + serviceUuid, err := i.serviceFactory.GetServiceUUID(i.ctx, i.Namespace, ruleValue.Backend.Service.Name) + if err != nil { + continue + } + i.IngressBackendService = append(i.IngressBackendService, IngressBackendService{ ServiceUuid: serviceUuid, IngressUuid: i.Uuid, diff --git a/pkg/schema/v1/service.go b/pkg/schema/v1/service.go index 15561e4..313c0ad 100644 --- a/pkg/schema/v1/service.go +++ b/pkg/schema/v1/service.go @@ -1,6 +1,7 @@ package v1 import ( + "context" "database/sql" "github.com/icinga/icinga-go-library/strcase" "github.com/icinga/icinga-go-library/types" @@ -10,14 +11,9 @@ import ( kruntime "k8s.io/apimachinery/pkg/runtime" kserializer "k8s.io/apimachinery/pkg/runtime/serializer" kjson "k8s.io/apimachinery/pkg/runtime/serializer/json" - "k8s.io/client-go/kubernetes" "strings" ) -type ServiceFactory struct { - clientset *kubernetes.Clientset -} - type Service struct { Meta ClusterIP string @@ -47,6 +43,7 @@ type Service struct { ResourceAnnotations []ResourceAnnotation `db:"-"` ServicePods []ServicePod `db:"-"` factory *ServiceFactory + ctx context.Context } type ServiceSelector struct { @@ -89,14 +86,11 @@ type ServicePod struct { PodUuid types.UUID } -func NewServiceFactory(clientset *kubernetes.Clientset) *ServiceFactory { - return &ServiceFactory{ - clientset: clientset, - } -} - func (f *ServiceFactory) NewService() Resource { - return &Service{factory: f} + return &Service{ + factory: f, + ctx: f.ctx, + } } func (s *Service) Obtain(k8s kmetav1.Object, clusterUuid types.UUID) {