Skip to content

Commit

Permalink
Sync clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoxhaa committed Nov 29, 2024
1 parent c9d3599 commit bf9a72c
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 45 deletions.
73 changes: 42 additions & 31 deletions cmd/icinga-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ import (
"time"
)

type clusterContextKeyType string

const clusterContextKey clusterContextKeyType = "clusterContextKey"

const expectedSchemaVersion = "0.2.0"

func main() {
Expand Down Expand Up @@ -123,16 +119,6 @@ func main() {

g, ctx := errgroup.WithContext(context.Background())

namespaceName := "kube-system"
ns, err := clientset.CoreV1().Namespaces().Get(ctx, namespaceName, v1.GetOptions{})
if err != nil {
klog.Fatalf("Failed to retrieve namespace '%s': %v. Ensure the cluster is accessible and the namespace exists.", namespaceName, err)
}

clusterUuid := schemav1.EnsureUUID(ns.UID)

ctx = context.WithValue(ctx, clusterContextKey, clusterUuid)

if hasSchema {
var version string

Expand Down Expand Up @@ -214,6 +200,31 @@ func main() {
klog.Fatal("IGL_DATABASE: ", err)
}

kubeconfigPath := kclientcmd.NewDefaultClientConfigLoadingRules().GetDefaultFilename()
kubeconfig, err := kclientcmd.LoadFromFile(kubeconfigPath)
if err != nil {
klog.Fatalf("Failed to load kubeconfig: %v", err)
}

name := kubeconfig.CurrentContext
namespaceName := "kube-system"
ns, err := clientset.CoreV1().Namespaces().Get(context.TODO(), namespaceName, v1.GetOptions{})
if err != nil {
klog.Fatalf("Failed to retrieve namespace '%s' for cluster '%s': %v", namespaceName, name, err)
}

cluster := &schemav1.Cluster{
Uuid: schemav1.EnsureUUID(ns.UID),
Name: name,
}

ctx = schemav1.NewClusterUuidContext(ctx, cluster.Uuid)

stmt, _ := db.BuildUpsertStmt(cluster)
if _, err := db.NamedExecContext(ctx, stmt, cluster); err != nil {
klog.Error(errors.Wrap(err, "can't update cluster"))
}

if _, err := db.ExecContext(ctx, "DELETE FROM kubernetes_instance"); err != nil {
klog.Fatal(errors.Wrap(err, "can't delete instance"))
}
Expand Down Expand Up @@ -304,7 +315,7 @@ func main() {
}

g.Go(func() error {
s := syncv1.NewSync(db, factory.Core().V1().Namespaces().Informer(), log.WithName("namespaces"), schemav1.NewNamespace, clusterUuid)
s := syncv1.NewSync(db, factory.Core().V1().Namespaces().Informer(), log.WithName("namespaces"), schemav1.NewNamespace)

return s.Run(ctx)
})
Expand All @@ -313,7 +324,7 @@ func main() {

wg.Add(1)
g.Go(func() error {
s := syncv1.NewSync(db, factory.Core().V1().Nodes().Informer(), log.WithName("nodes"), schemav1.NewNode, clusterUuid)
s := syncv1.NewSync(db, factory.Core().V1().Nodes().Informer(), log.WithName("nodes"), schemav1.NewNode)

var forwardForNotifications []syncv1.Feature
if cfg.Notifications.Url != "" {
Expand All @@ -340,7 +351,7 @@ func main() {
)

f := schemav1.NewPodFactory(clientset)
s := syncv1.NewSync(db, factory.Core().V1().Pods().Informer(), log.WithName("pods"), f.New, clusterUuid)
s := syncv1.NewSync(db, factory.Core().V1().Pods().Informer(), log.WithName("pods"), f.New)

wg.Done()

Expand All @@ -354,7 +365,7 @@ func main() {
wg.Add(1)
g.Go(func() error {
s := syncv1.NewSync(
db, factory.Apps().V1().Deployments().Informer(), log.WithName("deployments"), schemav1.NewDeployment, clusterUuid)
db, factory.Apps().V1().Deployments().Informer(), log.WithName("deployments"), schemav1.NewDeployment)

var forwardForNotifications []syncv1.Feature
if cfg.Notifications.Url != "" {
Expand All @@ -373,7 +384,7 @@ func main() {
wg.Add(1)
g.Go(func() error {
s := syncv1.NewSync(
db, factory.Apps().V1().DaemonSets().Informer(), log.WithName("daemon-sets"), schemav1.NewDaemonSet, clusterUuid)
db, factory.Apps().V1().DaemonSets().Informer(), log.WithName("daemon-sets"), schemav1.NewDaemonSet)

var forwardForNotifications []syncv1.Feature
if cfg.Notifications.Url != "" {
Expand All @@ -392,7 +403,7 @@ func main() {
wg.Add(1)
g.Go(func() error {
s := syncv1.NewSync(
db, factory.Apps().V1().ReplicaSets().Informer(), log.WithName("replica-sets"), schemav1.NewReplicaSet, clusterUuid)
db, factory.Apps().V1().ReplicaSets().Informer(), log.WithName("replica-sets"), schemav1.NewReplicaSet)

var forwardForNotifications []syncv1.Feature
if cfg.Notifications.Url != "" {
Expand All @@ -411,7 +422,7 @@ func main() {
wg.Add(1)
g.Go(func() error {
s := syncv1.NewSync(
db, factory.Apps().V1().StatefulSets().Informer(), log.WithName("stateful-sets"), schemav1.NewStatefulSet, clusterUuid)
db, factory.Apps().V1().StatefulSets().Informer(), log.WithName("stateful-sets"), schemav1.NewStatefulSet)

var forwardForNotifications []syncv1.Feature
if cfg.Notifications.Url != "" {
Expand All @@ -428,60 +439,60 @@ func main() {
})

g.Go(func() error {
s := syncv1.NewSync(db, factory.Core().V1().Services().Informer(), log.WithName("services"), schemav1.NewService, clusterUuid)
s := syncv1.NewSync(db, factory.Core().V1().Services().Informer(), log.WithName("services"), schemav1.NewService)

return s.Run(ctx)
})

g.Go(func() error {
s := syncv1.NewSync(db, factory.Discovery().V1().EndpointSlices().Informer(), log.WithName("endpoints"), schemav1.NewEndpointSlice, clusterUuid)
s := syncv1.NewSync(db, factory.Discovery().V1().EndpointSlices().Informer(), log.WithName("endpoints"), schemav1.NewEndpointSlice)

return s.Run(ctx)
})

g.Go(func() error {
s := syncv1.NewSync(db, factory.Core().V1().Secrets().Informer(), log.WithName("secrets"), schemav1.NewSecret, clusterUuid)
s := syncv1.NewSync(db, factory.Core().V1().Secrets().Informer(), log.WithName("secrets"), schemav1.NewSecret)
return s.Run(ctx)
})

g.Go(func() error {
s := syncv1.NewSync(db, factory.Core().V1().ConfigMaps().Informer(), log.WithName("config-maps"), schemav1.NewConfigMap, clusterUuid)
s := syncv1.NewSync(db, factory.Core().V1().ConfigMaps().Informer(), log.WithName("config-maps"), schemav1.NewConfigMap)

return s.Run(ctx)
})

g.Go(func() error {
s := syncv1.NewSync(db, factory.Events().V1().Events().Informer(), log.WithName("events"), schemav1.NewEvent, clusterUuid)
s := syncv1.NewSync(db, factory.Events().V1().Events().Informer(), log.WithName("events"), schemav1.NewEvent)

return s.Run(ctx, syncv1.WithNoDelete(), syncv1.WithNoWarumup())
})

g.Go(func() error {
s := syncv1.NewSync(db, factory.Core().V1().PersistentVolumeClaims().Informer(), log.WithName("pvcs"), schemav1.NewPvc, clusterUuid)
s := syncv1.NewSync(db, factory.Core().V1().PersistentVolumeClaims().Informer(), log.WithName("pvcs"), schemav1.NewPvc)

return s.Run(ctx)
})

g.Go(func() error {
s := syncv1.NewSync(db, factory.Core().V1().PersistentVolumes().Informer(), log.WithName("persistent-volumes"), schemav1.NewPersistentVolume, clusterUuid)
s := syncv1.NewSync(db, factory.Core().V1().PersistentVolumes().Informer(), log.WithName("persistent-volumes"), schemav1.NewPersistentVolume)

return s.Run(ctx)
})

g.Go(func() error {
s := syncv1.NewSync(db, factory.Batch().V1().Jobs().Informer(), log.WithName("jobs"), schemav1.NewJob, clusterUuid)
s := syncv1.NewSync(db, factory.Batch().V1().Jobs().Informer(), log.WithName("jobs"), schemav1.NewJob)

return s.Run(ctx)
})

g.Go(func() error {
s := syncv1.NewSync(db, factory.Batch().V1().CronJobs().Informer(), log.WithName("cron-jobs"), schemav1.NewCronJob, clusterUuid)
s := syncv1.NewSync(db, factory.Batch().V1().CronJobs().Informer(), log.WithName("cron-jobs"), schemav1.NewCronJob)

return s.Run(ctx)
})

g.Go(func() error {
s := syncv1.NewSync(db, factory.Networking().V1().Ingresses().Informer(), log.WithName("ingresses"), schemav1.NewIngress, clusterUuid)
s := syncv1.NewSync(db, factory.Networking().V1().Ingresses().Informer(), log.WithName("ingresses"), schemav1.NewIngress)

return s.Run(ctx)
})
Expand Down
10 changes: 10 additions & 0 deletions pkg/schema/v1/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package v1

import (
"github.com/icinga/icinga-go-library/types"
)

type Cluster struct {
Uuid types.UUID
Name string
}
27 changes: 27 additions & 0 deletions pkg/schema/v1/contracts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1

import (
"context"
"database/sql"
"fmt"
"github.com/google/uuid"
Expand All @@ -10,6 +11,12 @@ import (
"reflect"
)

// Private type to prevent collisions with other context keys
type contextKey string

// clusterContextKey is the key for Cluster values in contexts.
var clusterContextKey = contextKey("cluster")

var NameSpaceKubernetes = uuid.MustParse("3f249403-2bb0-428f-8e91-504d1fd7ddb6")

type Resource interface {
Expand Down Expand Up @@ -118,6 +125,26 @@ func NewNullableString(s any) sql.NullString {
panic(fmt.Sprintf("invalid type %T", s))
}

// NewClusterUuidContext returns a new Context that carries this Cluster as value.
func NewClusterUuidContext(parent context.Context, clusterUuid types.UUID) context.Context {
return context.WithValue(parent, clusterContextKey, clusterUuid)
}

// ClusterUuidFromContext returns the uuid value of the cluster stored in ctx, if any:
//
// e, ok := ClusterUuidFromContext(ctx)
// if !ok {
// // Error handling.
// }
func ClusterUuidFromContext(ctx context.Context) types.UUID {
clusterUuid, ok := ctx.Value(clusterContextKey).(types.UUID)

if !ok {
panic("cluster not found in context")
}
return clusterUuid
}

// Assert interface compliance.
var (
_ kmetav1.Object = (*Meta)(nil)
Expand Down
24 changes: 10 additions & 14 deletions pkg/sync/v1/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package v1
import (
"context"
"github.com/go-logr/logr"
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icinga-kubernetes/pkg/com"
"github.com/icinga/icinga-kubernetes/pkg/database"
schemav1 "github.com/icinga/icinga-kubernetes/pkg/schema/v1"
Expand All @@ -13,26 +12,23 @@ import (
)

type Sync struct {
db *database.Database
informer cache.SharedIndexInformer
log logr.Logger
factory func() schemav1.Resource
clusterUuid types.UUID
db *database.Database
informer cache.SharedIndexInformer
log logr.Logger
factory func() schemav1.Resource
}

func NewSync(
db *database.Database,
informer cache.SharedIndexInformer,
log logr.Logger,
factory func() schemav1.Resource,
clusterUuid types.UUID,
) *Sync {
return &Sync{
db: db,
informer: informer,
log: log,
factory: factory,
clusterUuid: clusterUuid,
db: db,
informer: informer,
log: log,
factory: factory,
}
}

Expand All @@ -53,7 +49,7 @@ func (s *Sync) Run(ctx context.Context, features ...Feature) error {
func (s *Sync) warmup(ctx context.Context, c *Controller) error {
g, ctx := errgroup.WithContext(ctx)

meta := &schemav1.Meta{ClusterUuid: s.clusterUuid}
meta := &schemav1.Meta{ClusterUuid: schemav1.ClusterUuidFromContext(ctx)}
query := s.db.BuildSelectStmt(s.factory(), meta) + ` WHERE cluster_uuid=:cluster_uuid`

entities, errs := s.db.YieldAll(ctx, func() (interface{}, error) {
Expand Down Expand Up @@ -88,7 +84,7 @@ func (s *Sync) warmup(ctx context.Context, c *Controller) error {
func (s *Sync) sync(ctx context.Context, c *Controller, features ...Feature) error {
sink := NewSink(func(i *Item) interface{} {
entity := s.factory()
entity.Obtain(*i.Item, s.clusterUuid)
entity.Obtain(*i.Item, schemav1.ClusterUuidFromContext(ctx))

return entity
}, func(k interface{}) interface{} {
Expand Down

0 comments on commit bf9a72c

Please sign in to comment.