From 40781fcff17eac4575f82176fbb002ae89efc378 Mon Sep 17 00:00:00 2001 From: Jonada Hoxha Date: Tue, 29 Oct 2024 08:14:30 +0100 Subject: [PATCH] Enhance service synchronization with new ServiceFactory and ServicePod tracking --- cmd/icinga-kubernetes/main.go | 3 ++- pkg/schema/v1/service.go | 25 ++++++++++++++++++++++--- schema/mysql/schema.sql | 6 ++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/cmd/icinga-kubernetes/main.go b/cmd/icinga-kubernetes/main.go index 91fc818..e1eb1f5 100644 --- a/cmd/icinga-kubernetes/main.go +++ b/cmd/icinga-kubernetes/main.go @@ -436,7 +436,8 @@ func main() { }) g.Go(func() error { - s := syncv1.NewSync(db, factory.Core().V1().Services().Informer(), log.WithName("services"), schemav1.NewService) + f := schemav1.NewServiceFactory(clientset) + s := syncv1.NewSync(db, factory.Core().V1().Services().Informer(), log.WithName("services"), f.NewService) return s.Run(ctx) }) diff --git a/pkg/schema/v1/service.go b/pkg/schema/v1/service.go index 6dd791c..15561e4 100644 --- a/pkg/schema/v1/service.go +++ b/pkg/schema/v1/service.go @@ -10,9 +10,14 @@ 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 @@ -40,6 +45,8 @@ type Service struct { Annotations []Annotation `db:"-"` ServiceAnnotations []ServiceAnnotation `db:"-"` ResourceAnnotations []ResourceAnnotation `db:"-"` + ServicePods []ServicePod `db:"-"` + factory *ServiceFactory } type ServiceSelector struct { @@ -77,8 +84,19 @@ type ServiceAnnotation struct { AnnotationUuid types.UUID } -func NewService() Resource { - return &Service{} +type ServicePod struct { + ServiceUuid types.UUID + PodUuid types.UUID +} + +func NewServiceFactory(clientset *kubernetes.Clientset) *ServiceFactory { + return &ServiceFactory{ + clientset: clientset, + } +} + +func (f *ServiceFactory) NewService() Resource { + return &Service{factory: f} } func (s *Service) Obtain(k8s kmetav1.Object, clusterUuid types.UUID) { @@ -216,7 +234,6 @@ func (s *Service) Obtain(k8s kmetav1.Object, clusterUuid types.UUID) { internalTrafficPolicy = string(*service.Spec.InternalTrafficPolicy) } s.InternalTrafficPolicy = internalTrafficPolicy - scheme := kruntime.NewScheme() _ = kcorev1.AddToScheme(scheme) codec := kserializer.NewCodecFactory(scheme).EncoderForVersion(kjson.NewYAMLSerializer(kjson.DefaultMetaFactory, scheme, scheme), kcorev1.SchemeGroupVersion) @@ -238,5 +255,7 @@ func (s *Service) Relations() []database.Relation { database.HasMany(s.ResourceAnnotations, database.WithForeignKey("resource_uuid")), database.HasMany(s.Annotations, database.WithoutCascadeDelete()), database.HasMany(s.ServiceAnnotations, fk), + database.HasMany(s.ResourceAnnotations, fk), + database.HasMany(s.ServicePods, fk), } } diff --git a/schema/mysql/schema.sql b/schema/mysql/schema.sql index ca6bdf6..31ca619 100644 --- a/schema/mysql/schema.sql +++ b/schema/mysql/schema.sql @@ -919,6 +919,12 @@ CREATE TABLE service_label ( PRIMARY KEY (service_uuid, label_uuid) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +CREATE TABLE service_pod ( + service_uuid binary(16) NOT NULL, + pod_uuid binary(16) NOT NULL, + PRIMARY KEY (service_uuid, pod_uuid) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; + CREATE TABLE service_port ( service_uuid binary(16) NOT NULL, name varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,