Skip to content

Commit

Permalink
Add YAML serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoxhaa committed Apr 17, 2024
1 parent 230491b commit 998330d
Show file tree
Hide file tree
Showing 17 changed files with 230 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/schema/v1/config_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import (
"github.com/icinga/icinga-kubernetes/pkg/types"
kcorev1 "k8s.io/api/core/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
kserializer "k8s.io/apimachinery/pkg/runtime/serializer"
kjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
"strings"
)

type ConfigMap struct {
Meta
Id types.Binary
Immutable types.Bool
Yaml string
Data []Data `db:"-"`
ConfigMapsData []ConfigMapData `db:"-"`
Labels []Label `db:"-"`
Expand Down Expand Up @@ -73,6 +77,15 @@ func (c *ConfigMap) Obtain(k8s kmetav1.Object) {
LabelId: labelId,
})
}

scheme := kruntime.NewScheme()
err := kcorev1.AddToScheme(scheme)
if err != nil {
panic(err)
}
codec := kserializer.NewCodecFactory(scheme).EncoderForVersion(kjson.NewYAMLSerializer(kjson.DefaultMetaFactory, scheme, scheme), kcorev1.SchemeGroupVersion)
output, _ := kruntime.Encode(codec, configMap)
c.Yaml = string(output)
}

func (c *ConfigMap) Relations() []database.Relation {
Expand Down
13 changes: 13 additions & 0 deletions pkg/schema/v1/cron_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"github.com/icinga/icinga-kubernetes/pkg/types"
kbatchv1 "k8s.io/api/batch/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
kserializer "k8s.io/apimachinery/pkg/runtime/serializer"
kjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
"strings"
)

Expand All @@ -21,6 +24,7 @@ type CronJob struct {
Active int32
LastScheduleTime types.UnixMilli
LastSuccessfulTime types.UnixMilli
Yaml string
Labels []Label `db:"-"`
CronJobLabels []CronJobLabel `db:"-"`
}
Expand Down Expand Up @@ -89,6 +93,15 @@ func (c *CronJob) Obtain(k8s kmetav1.Object) {
LabelId: labelId,
})
}

scheme := kruntime.NewScheme()
err := kbatchv1.AddToScheme(scheme)
if err != nil {
panic(err)
}
codec := kserializer.NewCodecFactory(scheme).EncoderForVersion(kjson.NewYAMLSerializer(kjson.DefaultMetaFactory, scheme, scheme), kbatchv1.SchemeGroupVersion)
output, _ := kruntime.Encode(codec, cronJob)
c.Yaml = string(output)
}

func (c *CronJob) Relations() []database.Relation {
Expand Down
12 changes: 12 additions & 0 deletions pkg/schema/v1/daemon_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"github.com/icinga/icinga-kubernetes/pkg/types"
kappsv1 "k8s.io/api/apps/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
kserializer "k8s.io/apimachinery/pkg/runtime/serializer"
kjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
"strings"
)

Expand All @@ -21,6 +24,7 @@ type DaemonSet struct {
UpdateNumberScheduled int32
NumberAvailable int32
NumberUnavailable int32
Yaml string
Conditions []DaemonSetCondition `db:"-"`
Labels []Label `db:"-"`
DaemonSetLabels []DaemonSetLabel `db:"-"`
Expand Down Expand Up @@ -83,6 +87,14 @@ func (d *DaemonSet) Obtain(k8s kmetav1.Object) {
LabelId: labelId,
})
}
scheme := kruntime.NewScheme()
err := kappsv1.AddToScheme(scheme)
if err != nil {
panic(err)
}
codec := kserializer.NewCodecFactory(scheme).EncoderForVersion(kjson.NewYAMLSerializer(kjson.DefaultMetaFactory, scheme, scheme), kappsv1.SchemeGroupVersion)
output, _ := kruntime.Encode(codec, daemonSet)
d.Yaml = string(output)
}

func (d *DaemonSet) Relations() []database.Relation {
Expand Down
13 changes: 13 additions & 0 deletions pkg/schema/v1/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"github.com/icinga/icinga-kubernetes/pkg/types"
kappsv1 "k8s.io/api/apps/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
kserializer "k8s.io/apimachinery/pkg/runtime/serializer"
kjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
"strings"
)

Expand All @@ -22,6 +25,7 @@ type Deployment struct {
ReadyReplicas int32
AvailableReplicas int32
UnavailableReplicas int32
Yaml string
Conditions []DeploymentCondition `db:"-"`
Labels []Label `db:"-"`
DeploymentLabels []DeploymentLabel `db:"-"`
Expand Down Expand Up @@ -97,6 +101,15 @@ func (d *Deployment) Obtain(k8s kmetav1.Object) {
LabelId: labelId,
})
}

scheme := kruntime.NewScheme()
err := kappsv1.AddToScheme(scheme)
if err != nil {
panic(err)
}
codec := kserializer.NewCodecFactory(scheme).EncoderForVersion(kjson.NewYAMLSerializer(kjson.DefaultMetaFactory, scheme, scheme), kappsv1.SchemeGroupVersion)
output, _ := kruntime.Encode(codec, deployment)
d.Yaml = string(output)
}

func (d *Deployment) Relations() []database.Relation {
Expand Down
13 changes: 13 additions & 0 deletions pkg/schema/v1/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"github.com/icinga/icinga-kubernetes/pkg/types"
keventsv1 "k8s.io/api/events/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
kserializer "k8s.io/apimachinery/pkg/runtime/serializer"
kjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
)

type Event struct {
Expand All @@ -21,6 +24,7 @@ type Event struct {
FirstSeen types.UnixMilli
LastSeen types.UnixMilli
Count int32
Yaml string
}

func NewEvent() Resource {
Expand Down Expand Up @@ -53,6 +57,15 @@ func (e *Event) Obtain(k8s kmetav1.Object) {
e.LastSeen = types.UnixMilli(event.DeprecatedLastTimestamp.Time)
}
e.Count = event.DeprecatedCount

scheme := kruntime.NewScheme()
err := keventsv1.AddToScheme(scheme)
if err != nil {
panic(err)
}
codec := kserializer.NewCodecFactory(scheme).EncoderForVersion(kjson.NewYAMLSerializer(kjson.DefaultMetaFactory, scheme, scheme), keventsv1.SchemeGroupVersion)
output, _ := kruntime.Encode(codec, event)
e.Yaml = string(output)
// e.FirstSeen = types.UnixMilli(event.EventTime.Time)
// if event.Series != nil {
// e.LastSeen = types.UnixMilli(event.Series.LastObservedTime.Time)
Expand Down
13 changes: 13 additions & 0 deletions pkg/schema/v1/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import (
"github.com/icinga/icinga-kubernetes/pkg/types"
networkingv1 "k8s.io/api/networking/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
kserializer "k8s.io/apimachinery/pkg/runtime/serializer"
kjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
)

type Ingress struct {
Meta
Id types.Binary
Yaml string
IngressTls []IngressTls `db:"-"`
IngressBackendService []IngressBackendService `db:"-"`
IngressBackendResource []IngressBackendResource `db:"-"`
Expand Down Expand Up @@ -156,6 +160,15 @@ func (i *Ingress) Obtain(k8s kmetav1.Object) {
}

}

scheme := kruntime.NewScheme()
err := networkingv1.AddToScheme(scheme)
if err != nil {
panic(err)
}
codec := kserializer.NewCodecFactory(scheme).EncoderForVersion(kjson.NewYAMLSerializer(kjson.DefaultMetaFactory, scheme, scheme), networkingv1.SchemeGroupVersion)
output, _ := kruntime.Encode(codec, ingress)
i.Yaml = string(output)
}

func (i *Ingress) Relations() []database.Relation {
Expand Down
13 changes: 13 additions & 0 deletions pkg/schema/v1/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"github.com/icinga/icinga-kubernetes/pkg/types"
kbatchv1 "k8s.io/api/batch/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
kserializer "k8s.io/apimachinery/pkg/runtime/serializer"
kjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
"strings"
)

Expand All @@ -25,6 +28,7 @@ type Job struct {
Active int32
Succeeded int32
Failed int32
Yaml string
Conditions []JobCondition `db:"-"`
Labels []Label `db:"-"`
JobLabels []JobLabel `db:"-"`
Expand Down Expand Up @@ -136,6 +140,15 @@ func (j *Job) Obtain(k8s kmetav1.Object) {
LabelId: labelId,
})
}

scheme := kruntime.NewScheme()
err := kbatchv1.AddToScheme(scheme)
if err != nil {
panic(err)
}
codec := kserializer.NewCodecFactory(scheme).EncoderForVersion(kjson.NewYAMLSerializer(kjson.DefaultMetaFactory, scheme, scheme), kbatchv1.SchemeGroupVersion)
output, _ := kruntime.Encode(codec, job)
j.Yaml = string(output)
}

func (j *Job) Relations() []database.Relation {
Expand Down
13 changes: 13 additions & 0 deletions pkg/schema/v1/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import (
"github.com/icinga/icinga-kubernetes/pkg/types"
kcorev1 "k8s.io/api/core/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
kserializer "k8s.io/apimachinery/pkg/runtime/serializer"
kjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
"strings"
)

type Namespace struct {
Meta
Id types.Binary
Phase string
Yaml string
Conditions []NamespaceCondition `db:"-"`
Labels []Label `db:"-"`
NamespaceLabels []NamespaceLabel `db:"-"`
Expand Down Expand Up @@ -66,6 +70,15 @@ func (n *Namespace) Obtain(k8s kmetav1.Object) {
LabelId: labelId,
})
}

scheme := kruntime.NewScheme()
err := kcorev1.AddToScheme(scheme)
if err != nil {
panic(err)
}
codec := kserializer.NewCodecFactory(scheme).EncoderForVersion(kjson.NewYAMLSerializer(kjson.DefaultMetaFactory, scheme, scheme), kcorev1.SchemeGroupVersion)
output, _ := kruntime.Encode(codec, namespace)
n.Yaml = string(output)
}

func (n *Namespace) Relations() []database.Relation {
Expand Down
13 changes: 13 additions & 0 deletions pkg/schema/v1/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"github.com/pkg/errors"
kcorev1 "k8s.io/api/core/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
kserializer "k8s.io/apimachinery/pkg/runtime/serializer"
kjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
knet "k8s.io/utils/net"
"net"
"strings"
Expand All @@ -23,6 +26,7 @@ type Node struct {
MemoryCapacity int64
MemoryAllocatable int64
PodCapacity int64
Yaml string
Conditions []NodeCondition `db:"-"`
Volumes []NodeVolume `db:"-"`
Labels []Label `db:"-"`
Expand Down Expand Up @@ -125,6 +129,15 @@ func (n *Node) Obtain(k8s kmetav1.Object) {
LabelId: labelId,
})
}

scheme := kruntime.NewScheme()
err := kcorev1.AddToScheme(scheme)
if err != nil {
panic(err)
}
codec := kserializer.NewCodecFactory(scheme).EncoderForVersion(kjson.NewYAMLSerializer(kjson.DefaultMetaFactory, scheme, scheme), kcorev1.SchemeGroupVersion)
output, _ := kruntime.Encode(codec, node)
n.Yaml = string(output)
}

func (n *Node) Relations() []database.Relation {
Expand Down
13 changes: 13 additions & 0 deletions pkg/schema/v1/persistent_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"github.com/icinga/icinga-kubernetes/pkg/types"
kcorev1 "k8s.io/api/core/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
kserializer "k8s.io/apimachinery/pkg/runtime/serializer"
kjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
ktypes "k8s.io/apimachinery/pkg/types"
)

Expand All @@ -23,6 +26,7 @@ type PersistentVolume struct {
Phase string
Reason string
Message string
Yaml string
Claim *PersistentVolumeClaimRef `db:"-"`
}

Expand Down Expand Up @@ -70,6 +74,15 @@ func (p *PersistentVolume) Obtain(k8s kmetav1.Object) {
Uid: persistentVolume.Spec.ClaimRef.UID,
}
}

scheme := kruntime.NewScheme()
err = kcorev1.AddToScheme(scheme)
if err != nil {
panic(err)
}
codec := kserializer.NewCodecFactory(scheme).EncoderForVersion(kjson.NewYAMLSerializer(kjson.DefaultMetaFactory, scheme, scheme), kcorev1.SchemeGroupVersion)
output, _ := kruntime.Encode(codec, persistentVolume)
p.Yaml = string(output)
}

func (p *PersistentVolume) Relations() []database.Relation {
Expand Down
20 changes: 20 additions & 0 deletions pkg/schema/v1/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"github.com/icinga/icinga-kubernetes/pkg/types"
kcorev1 "k8s.io/api/core/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
kserializer "k8s.io/apimachinery/pkg/runtime/serializer"
kjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
ktypes "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"strings"
Expand All @@ -31,6 +34,7 @@ type Pod struct {
Message string
Qos string
RestartPolicy string
Yaml string
Conditions []PodCondition `db:"-"`
Containers []Container `db:"-"`
Owners []PodOwner `db:"-"`
Expand All @@ -41,6 +45,13 @@ type Pod struct {
factory *PodFactory
}

type PodYaml struct {
PodId types.Binary
Kind string
ApiVersion string
YamlData string
}

type PodCondition struct {
PodId types.Binary
Type string
Expand Down Expand Up @@ -267,6 +278,15 @@ func (p *Pod) Obtain(k8s kmetav1.Object) {
})
}
}

scheme := kruntime.NewScheme()
err := kcorev1.AddToScheme(scheme)
if err != nil {
panic(err)
}
codec := kserializer.NewCodecFactory(scheme).EncoderForVersion(kjson.NewYAMLSerializer(kjson.DefaultMetaFactory, scheme, scheme), kcorev1.SchemeGroupVersion)
output, _ := kruntime.Encode(codec, pod)
p.Yaml = string(output)
}

func (p *Pod) Relations() []database.Relation {
Expand Down
Loading

0 comments on commit 998330d

Please sign in to comment.