Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ingress: refactor service UUID #171

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
41 changes: 41 additions & 0 deletions pkg/schema/v1/service_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package v1

import (
"context"
"database/sql"
"fmt"
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icinga-kubernetes/pkg/database"
"k8s.io/client-go/kubernetes"
)

type ServiceFactory struct {
clientset *kubernetes.Clientset
db *database.Database
ctx context.Context
}

func NewServiceFactory(clientset *kubernetes.Clientset, db *database.Database, ctx context.Context) *ServiceFactory {
return &ServiceFactory{
clientset: clientset,
db: db,
ctx: ctx,
}
}

// GetServiceUUID fetches the UUID of a service by its namespace and name
func (f *ServiceFactory) GetServiceUUID(ctx context.Context, namespace, name string) (types.UUID, error) {
var serviceUuid types.UUID

query := `SELECT uuid FROM service WHERE namespace = ? AND name = ?`

err := f.db.QueryRowContext(ctx, query, namespace, name).Scan(&serviceUuid)
if err != nil {
if err == sql.ErrNoRows {
return types.UUID{}, fmt.Errorf("service '%s' not found in namespace '%s'", name, namespace)
}
return types.UUID{}, fmt.Errorf("error fetching service UUID: %w", err)
}

return serviceUuid, nil
}
Loading