Skip to content

Commit

Permalink
Enhance service synchronization with new ServiceFactory and ServicePo…
Browse files Browse the repository at this point in the history
…d tracking
  • Loading branch information
jhoxhaa committed Dec 17, 2024
1 parent 834fb17 commit 40781fc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cmd/icinga-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
25 changes: 22 additions & 3 deletions pkg/schema/v1/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,6 +45,8 @@ type Service struct {
Annotations []Annotation `db:"-"`
ServiceAnnotations []ServiceAnnotation `db:"-"`
ResourceAnnotations []ResourceAnnotation `db:"-"`
ServicePods []ServicePod `db:"-"`
factory *ServiceFactory
}

type ServiceSelector struct {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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),
}
}
6 changes: 6 additions & 0 deletions schema/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 40781fc

Please sign in to comment.