Skip to content

Commit

Permalink
Use serviceFactory to fetch service UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoxhaa committed Jan 24, 2025
1 parent f0ebd31 commit f68a252
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
5 changes: 3 additions & 2 deletions cmd/icinga-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
})
Expand Down
20 changes: 17 additions & 3 deletions pkg/schema/v1/ingress.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1

import (
"context"
"database/sql"
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icinga-kubernetes/pkg/database"
Expand All @@ -25,6 +26,8 @@ type Ingress struct {
Annotations []Annotation `db:"-"`
IngressAnnotations []IngressAnnotation `db:"-"`
ResourceAnnotations []ResourceAnnotation `db:"-"`
serviceFactory *ServiceFactory
ctx context.Context
}

type IngressTls struct {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 6 additions & 12 deletions pkg/schema/v1/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1

import (
"context"
"database/sql"
"github.com/icinga/icinga-go-library/strcase"
"github.com/icinga/icinga-go-library/types"
Expand All @@ -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
Expand Down Expand Up @@ -47,6 +43,7 @@ type Service struct {
ResourceAnnotations []ResourceAnnotation `db:"-"`
ServicePods []ServicePod `db:"-"`
factory *ServiceFactory
ctx context.Context
}

type ServiceSelector struct {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit f68a252

Please sign in to comment.