Skip to content

Commit

Permalink
Implement contracts.Entity interface for schemav1.Types
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Jul 7, 2023
1 parent e01e625 commit 406657f
Show file tree
Hide file tree
Showing 15 changed files with 589 additions and 379 deletions.
61 changes: 61 additions & 0 deletions pkg/schema/v1/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package v1

import (
"database/sql"
"github.com/icinga/icinga-kubernetes/pkg/contracts"
"github.com/icinga/icinga-kubernetes/pkg/database"
"github.com/icinga/icinga-kubernetes/pkg/types"
)

type Container struct {
PodMeta
Name string
Image string
CpuLimits int64
CpuRequests int64
MemoryLimits int64
MemoryRequests int64
State sql.NullString
StateDetails string
Ready types.Bool
Started types.Bool
RestartCount int32
Devices []*ContainerDevice `db:"-"`
Mounts []*ContainerMount `db:"-"`
}

func (c *Container) Relations() []database.Relation {
fk := database.WithForeignKey("container_id")

return []database.Relation{
database.HasMany(c.Devices, fk),
database.HasMany(c.Mounts, fk),
}
}

type ContainerMeta struct {
contracts.Meta
ContainerId types.Binary
}

func (cm *ContainerMeta) Fingerprint() contracts.FingerPrinter {
return cm
}

func (cm *ContainerMeta) ParentID() types.Binary {
return cm.ContainerId
}

type ContainerDevice struct {
ContainerMeta
Name string
Path string
}

type ContainerMount struct {
ContainerMeta
VolumeName string
Path string
SubPath string
ReadOnly types.Bool
}
90 changes: 52 additions & 38 deletions pkg/schema/v1/contracts.go
Original file line number Diff line number Diff line change
@@ -1,64 +1,78 @@
package v1

import (
"github.com/icinga/icinga-kubernetes/pkg/contracts"
"github.com/icinga/icinga-kubernetes/pkg/types"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ktypes "k8s.io/apimachinery/pkg/types"
)

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

type Meta struct {
type ResourceMeta struct {
contracts.Meta
Uid ktypes.UID
Namespace string
Name string
ResourceVersion string
Created types.UnixMilli
}

func (m *Meta) ObtainMeta(k8s kmetav1.Object) {
func (m *ResourceMeta) Fingerprint() contracts.FingerPrinter {
return m
}

func (m *ResourceMeta) ObtainMeta(k8s kmetav1.Object) {
m.Uid = k8s.GetUID()
m.Namespace = k8s.GetNamespace()
m.Name = k8s.GetName()
m.ResourceVersion = k8s.GetResourceVersion()
m.Created = types.UnixMilli(k8s.GetCreationTimestamp().Time)
}

func (m *Meta) GetNamespace() string { return m.Namespace }
func (m *Meta) SetNamespace(string) { panic("Not expected to be called") }
func (m *Meta) GetName() string { return m.Name }
func (m *Meta) SetName(string) { panic("Not expected to be called") }
func (m *Meta) GetGenerateName() string { panic("Not expected to be called") }
func (m *Meta) SetGenerateName(string) { panic("Not expected to be called") }
func (m *Meta) GetUID() ktypes.UID { return m.Uid }
func (m *Meta) SetUID(ktypes.UID) { panic("Not expected to be called") }
func (m *Meta) GetResourceVersion() string { return m.ResourceVersion }
func (m *Meta) SetResourceVersion(string) { panic("Not expected to be called") }
func (m *Meta) GetGeneration() int64 { panic("Not expected to be called") }
func (m *Meta) SetGeneration(int64) { panic("Not expected to be called") }
func (m *Meta) GetSelfLink() string { panic("Not expected to be called") }
func (m *Meta) SetSelfLink(string) { panic("Not expected to be called") }
func (m *Meta) GetCreationTimestamp() kmetav1.Time { return kmetav1.NewTime(m.Created.Time()) }
func (m *Meta) SetCreationTimestamp(kmetav1.Time) { panic("Not expected to be called") }
func (m *Meta) GetDeletionTimestamp() *kmetav1.Time { panic("Not expected to be called") }
func (m *Meta) SetDeletionTimestamp(*kmetav1.Time) { panic("Not expected to be called") }
func (m *Meta) GetDeletionGracePeriodSeconds() *int64 { panic("Not expected to be called") }
func (m *Meta) SetDeletionGracePeriodSeconds(*int64) { panic("Not expected to be called") }
func (m *Meta) GetLabels() map[string]string { panic("Not expected to be called") }
func (m *Meta) SetLabels(map[string]string) { panic("Not expected to be called") }
func (m *Meta) GetAnnotations() map[string]string { panic("Not expected to be called") }
func (m *Meta) SetAnnotations(_ map[string]string) { panic("Not expected to be called") }
func (m *Meta) GetFinalizers() []string { panic("Not expected to be called") }
func (m *Meta) SetFinalizers([]string) { panic("Not expected to be called") }
func (m *Meta) GetOwnerReferences() []kmetav1.OwnerReference { panic("Not expected to be called") }
func (m *Meta) SetOwnerReferences([]kmetav1.OwnerReference) { panic("Not expected to be called") }
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 (m *ResourceMeta) GetNamespace() string { return m.Namespace }
func (m *ResourceMeta) SetNamespace(string) { panic("Not expected to be called") }
func (m *ResourceMeta) GetName() string { return m.Name }
func (m *ResourceMeta) SetName(string) { panic("Not expected to be called") }
func (m *ResourceMeta) GetGenerateName() string { panic("Not expected to be called") }
func (m *ResourceMeta) SetGenerateName(string) { panic("Not expected to be called") }
func (m *ResourceMeta) GetUID() ktypes.UID { return m.Uid }
func (m *ResourceMeta) SetUID(ktypes.UID) { panic("Not expected to be called") }
func (m *ResourceMeta) GetResourceVersion() string { return m.ResourceVersion }
func (m *ResourceMeta) SetResourceVersion(string) { panic("Not expected to be called") }
func (m *ResourceMeta) GetGeneration() int64 { panic("Not expected to be called") }
func (m *ResourceMeta) SetGeneration(int64) { panic("Not expected to be called") }
func (m *ResourceMeta) GetSelfLink() string { panic("Not expected to be called") }
func (m *ResourceMeta) SetSelfLink(string) { panic("Not expected to be called") }
func (m *ResourceMeta) GetCreationTimestamp() kmetav1.Time { return kmetav1.NewTime(m.Created.Time()) }
func (m *ResourceMeta) SetCreationTimestamp(kmetav1.Time) { panic("Not expected to be called") }
func (m *ResourceMeta) GetDeletionTimestamp() *kmetav1.Time { panic("Not expected to be called") }
func (m *ResourceMeta) SetDeletionTimestamp(*kmetav1.Time) { panic("Not expected to be called") }
func (m *ResourceMeta) GetDeletionGracePeriodSeconds() *int64 { panic("Not expected to be called") }
func (m *ResourceMeta) SetDeletionGracePeriodSeconds(*int64) { panic("Not expected to be called") }
func (m *ResourceMeta) GetLabels() map[string]string { panic("Not expected to be called") }
func (m *ResourceMeta) SetLabels(map[string]string) { panic("Not expected to be called") }
func (m *ResourceMeta) GetAnnotations() map[string]string { panic("Not expected to be called") }
func (m *ResourceMeta) SetAnnotations(_ map[string]string) { panic("Not expected to be called") }
func (m *ResourceMeta) GetFinalizers() []string { panic("Not expected to be called") }
func (m *ResourceMeta) SetFinalizers([]string) { panic("Not expected to be called") }
func (m *ResourceMeta) GetOwnerReferences() []kmetav1.OwnerReference {
panic("Not expected to be called")
}
func (m *ResourceMeta) SetOwnerReferences([]kmetav1.OwnerReference) {
panic("Not expected to be called")
}
func (m *ResourceMeta) GetManagedFields() []kmetav1.ManagedFieldsEntry {
panic("Not expected to be called")
}
func (m *ResourceMeta) SetManagedFields([]kmetav1.ManagedFieldsEntry) {
panic("Not expected to be called")
}

// Assert interface compliance.
var (
_ kmetav1.Object = (*Meta)(nil)
_ kmetav1.Object = (*ResourceMeta)(nil)
_ contracts.FingerPrinter = (*ResourceMeta)(nil)
_ contracts.Checksumer = (*ResourceMeta)(nil)
_ contracts.IDer = (*ResourceMeta)(nil)
_ contracts.ParentIDer = (*ResourceMeta)(nil)
_ contracts.Entity = (*ResourceMeta)(nil)
)
64 changes: 36 additions & 28 deletions pkg/schema/v1/daemon_set.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package v1

import (
"github.com/icinga/icinga-kubernetes/pkg/contracts"
"github.com/icinga/icinga-kubernetes/pkg/database"
"github.com/icinga/icinga-kubernetes/pkg/strcase"
"github.com/icinga/icinga-kubernetes/pkg/types"
kappsv1 "k8s.io/api/apps/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
)

type DaemonSet struct {
Meta
Id types.Binary
ResourceMeta
UpdateStrategy string
MinReadySeconds int32
DesiredNumberScheduled int32
Expand All @@ -21,26 +20,33 @@ type DaemonSet struct {
UpdateNumberScheduled int32
NumberAvailable int32
NumberUnavailable int32
Conditions []DaemonSetCondition `db:"-"`
Labels []Label `db:"-"`
DaemonSetLabels []DaemonSetLabel `db:"-"`
Conditions []*DaemonSetCondition `json:"-" db:"-"`
Labels []*Label `json:"-" db:"-"`
}

type DaemonSetMeta struct {
contracts.Meta
DaemonSetId types.Binary
}

func (dm *DaemonSetMeta) Fingerprint() contracts.FingerPrinter {
return dm
}

func (dm *DaemonSetMeta) ParentID() types.Binary {
return dm.DaemonSetId
}

type DaemonSetCondition struct {
DaemonSetId types.Binary
DaemonSetMeta
Type string
Status string
LastTransition types.UnixMilli
Reason string
Message string
}

type DaemonSetLabel struct {
DaemonSetId types.Binary
LabelId types.Binary
}

func NewDaemonSet() Resource {
func NewDaemonSet() contracts.Entity {
return &DaemonSet{}
}

Expand All @@ -60,28 +66,31 @@ func (d *DaemonSet) Obtain(k8s kmetav1.Object) {
d.NumberAvailable = daemonSet.Status.NumberAvailable
d.NumberUnavailable = daemonSet.Status.NumberUnavailable

d.PropertiesChecksum = types.Checksum(MustMarshalJSON(d))

for _, condition := range daemonSet.Status.Conditions {
d.Conditions = append(d.Conditions, DaemonSetCondition{
DaemonSetId: d.Id,
daemonCond := &DaemonSetCondition{
DaemonSetMeta: DaemonSetMeta{
DaemonSetId: d.Id,
Meta: contracts.Meta{Id: types.Checksum(types.MustPackSlice(d.Id, condition.Type))},
},
Type: string(condition.Type),
Status: string(condition.Status),
LastTransition: types.UnixMilli(condition.LastTransitionTime.Time),
Reason: condition.Reason,
Message: condition.Message,
})
}
daemonCond.PropertiesChecksum = types.Checksum(MustMarshalJSON(daemonCond))

d.Conditions = append(d.Conditions, daemonCond)
}

for labelName, labelValue := range daemonSet.Labels {
labelId := types.Checksum(strings.ToLower(labelName + ":" + labelValue))
d.Labels = append(d.Labels, Label{
Id: labelId,
Name: labelName,
Value: labelValue,
})
d.DaemonSetLabels = append(d.DaemonSetLabels, DaemonSetLabel{
DaemonSetId: d.Id,
LabelId: labelId,
})
label := NewLabel(labelName, labelValue)
label.DaemonSetId = d.Id
label.PropertiesChecksum = types.Checksum(MustMarshalJSON(label))

d.Labels = append(d.Labels, label)
}
}

Expand All @@ -90,7 +99,6 @@ func (d *DaemonSet) Relations() []database.Relation {

return []database.Relation{
database.HasMany(d.Conditions, fk),
database.HasMany(d.DaemonSetLabels, fk),
database.HasMany(d.Labels, database.WithoutCascadeDelete()),
database.HasMany(d.Labels, fk),
}
}
Loading

0 comments on commit 406657f

Please sign in to comment.