Skip to content

Commit

Permalink
Sync pods that belong to a service
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoxhaa committed Dec 6, 2024
1 parent da3a566 commit 0f26186
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
3 changes: 2 additions & 1 deletion cmd/icinga-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,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
53 changes: 51 additions & 2 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,9 +11,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 +46,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 +85,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) {
Expand Down Expand Up @@ -217,13 +236,42 @@ func (s *Service) Obtain(k8s kmetav1.Object) {
}
s.InternalTrafficPolicy = internalTrafficPolicy

if len(service.Spec.Selector) > 0 {
selector := kmetav1.LabelSelector{
MatchLabels: service.Spec.Selector,
}
labelSelector, err := kmetav1.LabelSelectorAsSelector(&selector)
if err != nil {
return
}

pods, err := s.queryMatchingPods(labelSelector.String())
if err != nil {
return
}

for _, pod := range pods.Items {
podUuid := EnsureUUID(pod.ObjectMeta.UID)
s.ServicePods = append(s.ServicePods, ServicePod{
ServiceUuid: s.Uuid,
PodUuid: podUuid,
})
}
}

scheme := kruntime.NewScheme()
_ = kcorev1.AddToScheme(scheme)
codec := kserializer.NewCodecFactory(scheme).EncoderForVersion(kjson.NewYAMLSerializer(kjson.DefaultMetaFactory, scheme, scheme), kcorev1.SchemeGroupVersion)
output, _ := kruntime.Encode(codec, service)
s.Yaml = string(output)
}

func (s *Service) queryMatchingPods(labelSelector string) (*kcorev1.PodList, error) {
return s.factory.clientset.CoreV1().Pods("").List(context.TODO(), kmetav1.ListOptions{
LabelSelector: labelSelector,
})
}

func (s *Service) Relations() []database.Relation {
fk := database.WithForeignKey("service_uuid")

Expand All @@ -238,5 +286,6 @@ func (s *Service) Relations() []database.Relation {
database.HasMany(s.ServiceAnnotations, fk),
database.HasMany(s.Annotations, database.WithoutCascadeDelete()),
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 @@ -897,6 +897,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 0f26186

Please sign in to comment.