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

Uuids #93

Merged
merged 3 commits into from
Jun 3, 2024
Merged
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
2 changes: 1 addition & 1 deletion cmd/icinga-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func main() {
_, err := db.CleanupOlderThan(
ctx, database.CleanupStmt{
Table: "event",
PK: "id",
PK: "uuid",
Column: "created",
}, 5000, olderThan,
)
Expand Down
30 changes: 13 additions & 17 deletions pkg/schema/v1/config_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package v1

import (
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icinga-go-library/utils"
"github.com/icinga/icinga-kubernetes/pkg/database"
kcorev1 "k8s.io/api/core/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -11,7 +10,6 @@ import (

type ConfigMap struct {
Meta
Id types.Binary
Immutable types.Bool
Data []Data `db:"-"`
ConfigMapsData []ConfigMapData `db:"-"`
Expand All @@ -20,13 +18,13 @@ type ConfigMap struct {
}

type ConfigMapData struct {
ConfigMapId types.Binary
DataId types.Binary
ConfigMapUuid types.UUID
DataUuid types.UUID
}

type ConfigMapLabel struct {
ConfigMapId types.Binary
LabelId types.Binary
ConfigMapUuid types.UUID
LabelUuid types.UUID
}

func NewConfigMap() Resource {
Expand All @@ -38,8 +36,6 @@ func (c *ConfigMap) Obtain(k8s kmetav1.Object) {

configMap := k8s.(*kcorev1.ConfigMap)

c.Id = utils.Checksum(configMap.Namespace + "/" + configMap.Name)

var immutable bool
if configMap.Immutable != nil {
immutable = *configMap.Immutable
Expand All @@ -50,34 +46,34 @@ func (c *ConfigMap) Obtain(k8s kmetav1.Object) {
}

for dataName, dataValue := range configMap.Data {
dataId := utils.Checksum(dataName + ":" + dataValue)
dataUuid := NewUUID(c.Uuid, strings.ToLower(dataName+":"+dataValue))
c.Data = append(c.Data, Data{
Id: dataId,
Uuid: dataUuid,
Name: dataName,
Value: dataValue,
})
c.ConfigMapsData = append(c.ConfigMapsData, ConfigMapData{
ConfigMapId: c.Id,
DataId: dataId,
ConfigMapUuid: c.Uuid,
DataUuid: dataUuid,
})
}

for labelName, labelValue := range configMap.Labels {
labelId := utils.Checksum(strings.ToLower(labelName + ":" + labelValue))
labelUuid := NewUUID(c.Uuid, strings.ToLower(labelName+":"+labelValue))
c.Labels = append(c.Labels, Label{
Id: labelId,
Uuid: labelUuid,
Name: labelName,
Value: labelValue,
})
c.ConfigMapLabels = append(c.ConfigMapLabels, ConfigMapLabel{
ConfigMapId: c.Id,
LabelId: labelId,
ConfigMapUuid: c.Uuid,
LabelUuid: labelUuid,
})
}
}

func (c *ConfigMap) Relations() []database.Relation {
fk := database.WithForeignKey("config_map_id")
fk := database.WithForeignKey("config_map_uuid")

return []database.Relation{
database.HasMany(c.Labels, database.WithoutCascadeDelete()),
Expand Down
62 changes: 31 additions & 31 deletions pkg/schema/v1/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const (
)

type ContainerMeta struct {
Id types.Binary `db:"id"`
PodId types.Binary `db:"pod_id"`
Uuid types.UUID `db:"uuid"`
PodUuid types.UUID `db:"pod_uuid"`
}

type Container struct {
Expand Down Expand Up @@ -68,19 +68,19 @@ func (c *Container) Relations() []database.Relation {
}

type ContainerDevice struct {
ContainerId types.Binary
PodId types.Binary
Name string
Path string
ContainerUuid types.UUID
PodUuid types.UUID
Name string
Path string
}

type ContainerMount struct {
ContainerId types.Binary
PodId types.Binary
VolumeName string
Path string
SubPath sql.NullString
ReadOnly types.Bool
ContainerUuid types.UUID
PodUuid types.UUID
VolumeName string
Path string
SubPath sql.NullString
ReadOnly types.Bool
}

type ContainerLogMeta struct {
Expand All @@ -89,8 +89,8 @@ type ContainerLogMeta struct {
}

type ContainerLog struct {
PodId types.Binary `db:"pod_id"`
ContainerId types.Binary `db:"container_id"`
PodUuid types.UUID `db:"pod_uuid"`
ContainerUuid types.UUID `db:"container_uuid"`
ContainerLogMeta

Namespace string `db:"-"`
Expand Down Expand Up @@ -163,24 +163,24 @@ func SyncContainers(ctx context.Context, db *database.Database, g *errgroup.Grou
scheduler.StartAsync()
defer scheduler.Stop()

query := db.BuildSelectStmt(&Container{}, ContainerMeta{}) + ` WHERE pod_id=:pod_id`
query := db.BuildSelectStmt(&Container{}, ContainerMeta{}) + ` WHERE pod_uuid=:pod_uuid`

for {
select {
case <-ctx.Done():
return ctx.Err()
case podId, ok := <-deletePods:
case podUuid, ok := <-deletePods:
if !ok {
return nil
}

meta := &ContainerMeta{PodId: podId.([]byte)}
if _, ok := deletedPodIds[meta.PodId.String()]; ok {
meta := &ContainerMeta{PodUuid: podUuid.(types.UUID)}
if _, ok := deletedPodIds[meta.PodUuid.String()]; ok {
// Due to the recursive relation resolution in the `DB#DeleteStreamed()` method, we may get the
// same pod ID multiple times since they all share the same `on success` handler.
break
}
deletedPodIds[meta.PodId.String()] = true
deletedPodIds[meta.PodUuid.String()] = true

entities, errs := db.YieldAll(ctx, func() (interface{}, error) {
return &Container{}, nil
Expand All @@ -201,18 +201,18 @@ func SyncContainers(ctx context.Context, db *database.Database, g *errgroup.Grou

container := e.(*Container)
select {
case containerIds <- container.Id:
case containerIds <- container.Uuid:
case <-ctx.Done():
return ctx.Err()
}

err := scheduler.RemoveByTag(container.Id.String())
err := scheduler.RemoveByTag(container.Uuid.String())
if err != nil && !errors.Is(err, gocron.ErrJobNotFoundWithTag) {
return err
}

containerLogsMu.Lock()
delete(containerLogs, container.Id.String())
delete(containerLogs, container.Uuid.String())
containerLogsMu.Unlock()
}
}
Expand All @@ -224,42 +224,42 @@ func SyncContainers(ctx context.Context, db *database.Database, g *errgroup.Grou

pod := e.(*Pod)

delete(deletedPodIds, pod.Id.String())
delete(deletedPodIds, pod.Uuid.String())

for _, container := range pod.Containers {
_, err := scheduler.FindJobsByTag(container.Id.String())
_, err := scheduler.FindJobsByTag(container.Uuid.String())
if err != nil && !errors.Is(err, gocron.ErrJobNotFoundWithTag) {
return err
}

if container.Started.Bool && err != nil {
containerLog := &ContainerLog{
ContainerId: container.Id,
PodId: container.PodId,
ContainerUuid: container.Uuid,
PodUuid: container.PodUuid,
ContainerName: container.Name,
Namespace: pod.Namespace,
PodName: pod.Name,
}

containerLogsMu.Lock()
if cl, ok := containerLogs[container.Id.String()]; ok {
if cl, ok := containerLogs[container.Uuid.String()]; ok {
containerLog.Logs = cl.Logs
}
containerLogsMu.Unlock()

scheduler.Every(ScheduleInterval.String()).Tag(container.Id.String())
scheduler.Every(ScheduleInterval.String()).Tag(container.Uuid.String())
_, err = scheduler.Do(containerLog.syncContainerLogs, ctx, pod.factory.clientset, db)
if err != nil {
return err
}
} else if err == nil {
err := scheduler.RemoveByTag(container.Id.String())
err := scheduler.RemoveByTag(container.Uuid.String())
if err != nil {
return err
}

containerLogsMu.Lock()
delete(containerLogs, container.Id.String())
delete(containerLogs, container.Uuid.String())
containerLogsMu.Unlock()
}
}
Expand Down Expand Up @@ -293,7 +293,7 @@ func warmup(ctx context.Context, db *database.Database) error {
}

containerLog := e.(*ContainerLog)
containerLogs[containerLog.ContainerId.String()] = *containerLog
containerLogs[containerLog.ContainerUuid.String()] = *containerLog
}
}
})
Expand Down
17 changes: 17 additions & 0 deletions pkg/schema/v1/contracts.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package v1

import (
"github.com/google/uuid"
"github.com/icinga/icinga-go-library/types"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ktypes "k8s.io/apimachinery/pkg/types"
)

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

type Resource interface {
kmetav1.Object
Obtain(k8s kmetav1.Object)
}

type Meta struct {
Uuid types.UUID
Uid ktypes.UID
Namespace string
Name string
Expand All @@ -20,6 +24,7 @@ type Meta struct {
}

func (m *Meta) ObtainMeta(k8s kmetav1.Object) {
m.Uuid = EnsureUUID(k8s.GetUID())
m.Uid = k8s.GetUID()
m.Namespace = k8s.GetNamespace()
m.Name = k8s.GetName()
Expand Down Expand Up @@ -58,6 +63,18 @@ func (m *Meta) SetOwnerReferences([]kmetav1.OwnerReference) { panic("Not expe
func (m *Meta) GetManagedFields() []kmetav1.ManagedFieldsEntry { panic("Not expected to be called") }
func (m *Meta) SetManagedFields([]kmetav1.ManagedFieldsEntry) { panic("Not expected to be called") }

func EnsureUUID(uid ktypes.UID) types.UUID {
if id, err := uuid.Parse(string(uid)); err == nil {
return types.UUID{UUID: id}
}

return types.UUID{UUID: uuid.NewSHA1(NameSpaceKubernetes, []byte(uid))}
}

func NewUUID(space types.UUID, data string) types.UUID {
return types.UUID{UUID: uuid.NewSHA1(space.UUID, []byte(data))}
}

// Assert interface compliance.
var (
_ kmetav1.Object = (*Meta)(nil)
Expand Down
17 changes: 7 additions & 10 deletions pkg/schema/v1/cron_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package v1

import (
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icinga-go-library/utils"
"github.com/icinga/icinga-kubernetes/pkg/database"
kbatchv1 "k8s.io/api/batch/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -11,7 +10,6 @@ import (

type CronJob struct {
Meta
Id types.Binary
Schedule string
Timezone string
StartingDeadlineSeconds int64
Expand All @@ -27,8 +25,8 @@ type CronJob struct {
}

type CronJobLabel struct {
CronJobId types.Binary
LabelId types.Binary
CronJobUuid types.UUID
LabelUuid types.UUID
}

func NewCronJob() Resource {
Expand Down Expand Up @@ -68,7 +66,6 @@ func (c *CronJob) Obtain(k8s kmetav1.Object) {
c.LastSuccessfulTime = types.UnixMilli(cronJob.Status.LastSuccessfulTime.Time)
}

c.Id = utils.Checksum(c.Namespace + "/" + c.Name)
c.Schedule = cronJob.Spec.Schedule
c.Timezone = timeZone
c.StartingDeadlineSeconds = startingDeadlineSeconds
Expand All @@ -79,21 +76,21 @@ func (c *CronJob) Obtain(k8s kmetav1.Object) {
c.Active = int32(len(cronJob.Status.Active))

for labelName, labelValue := range cronJob.Labels {
labelId := utils.Checksum(strings.ToLower(labelName + ":" + labelValue))
labelUuid := NewUUID(c.Uuid, strings.ToLower(labelName+":"+labelValue))
c.Labels = append(c.Labels, Label{
Id: labelId,
Uuid: labelUuid,
Name: labelName,
Value: labelValue,
})
c.CronJobLabels = append(c.CronJobLabels, CronJobLabel{
CronJobId: c.Id,
LabelId: labelId,
CronJobUuid: c.Uuid,
LabelUuid: labelUuid,
})
}
}

func (c *CronJob) Relations() []database.Relation {
fk := database.WithForeignKey("cron_job_id")
fk := database.WithForeignKey("cron_job_uuid")

return []database.Relation{
database.HasMany(c.Labels, database.WithoutCascadeDelete()),
Expand Down
Loading
Loading