Skip to content

Commit

Permalink
update to kubernetes_resource
Browse files Browse the repository at this point in the history
Signed-off-by: Pranav Singh <[email protected]>
  • Loading branch information
theBeginner86 committed Oct 16, 2023
1 parent 7b731c9 commit 725ce74
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 55 deletions.
10 changes: 5 additions & 5 deletions meshsync/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ func (h *Handler) ListenToRequests() {
}
}

func (h *Handler) listStoreObjects() []model.KubernetesObject {
func (h *Handler) listStoreObjects() []model.KubernetesResource {

Check warning on line 121 in meshsync/handlers.go

View check run for this annotation

Codecov / codecov/patch

meshsync/handlers.go#L121

Added line #L121 was not covered by tests
objects := make([]interface{}, 0)
for _, v := range h.stores {
objects = append(objects, v.List()...)
}
parsedObjects := make([]model.KubernetesObject, 0)
parsedObjects := make([]model.KubernetesResource, 0)

Check warning on line 126 in meshsync/handlers.go

View check run for this annotation

Codecov / codecov/patch

meshsync/handlers.go#L126

Added line #L126 was not covered by tests
for _, obj := range objects {
parsedObjects = append(parsedObjects, model.ParseList(*obj.(*unstructured.Unstructured)))
}
Expand All @@ -133,9 +133,9 @@ func (h *Handler) listStoreObjects() []model.KubernetesObject {
// TODO: move this to meshkit
// given [1,2,3,4,5,6,7,5,4,4] and 3 as its arguments, it would
// return [[1,2,3], [4,5,6], [7,5,4], [4]]
func splitIntoMultipleSlices(s []model.KubernetesObject, maxItmsPerSlice int) []([]model.KubernetesObject) {
result := make([]([]model.KubernetesObject), 0)
temp := make([]model.KubernetesObject, 0)
func splitIntoMultipleSlices(s []model.KubernetesResource, maxItmsPerSlice int) []([]model.KubernetesResource) {
result := make([]([]model.KubernetesResource), 0)
temp := make([]model.KubernetesResource, 0)

for idx, val := range s {
temp = append(temp, val)
Expand Down
12 changes: 6 additions & 6 deletions meshsync/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ import (
func TestSplitIntoMultipleSlices(t *testing.T) {
testCases := []struct {
name string
input []model.KubernetesObject
input []model.KubernetesResource
maxItmsPerSlice int
expectedOutput [][]model.KubernetesObject
expectedOutput [][]model.KubernetesResource
}{
{
name: "test with 0 items",
input: []model.KubernetesObject{},
input: []model.KubernetesResource{},
maxItmsPerSlice: 10,
expectedOutput: [][]model.KubernetesObject{},
expectedOutput: [][]model.KubernetesResource{},
},

{
name: "test with 1 item",
input: []model.KubernetesObject{
input: []model.KubernetesResource{
{
Kind: "test",
},
},
maxItmsPerSlice: 10,
expectedOutput: [][]model.KubernetesObject{
expectedOutput: [][]model.KubernetesResource{
{
{
Kind: "test",
Expand Down
56 changes: 28 additions & 28 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ const (
KindAnnotation string = "annotation"
)

type KubernetesObject struct {
ID string `json:"id" gorm:"primarykey"`
APIVersion string `json:"apiVersion" gorm:"index"`
Kind string `json:"kind" gorm:"index"`
KubernetesObjectMeta *KubernetesResourceObjectMeta `json:"metadata" gorm:"foreignkey:ID;references:id;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
Spec *KubernetesResourceSpec `json:"spec,omitempty" gorm:"foreignkey:ID;references:id;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
Status *KubernetesResourceStatus `json:"status,omitempty" gorm:"foreignkey:ID;references:id;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
ClusterID string `json:"cluster_id"`
PatternResource *uuid.UUID `json:"pattern_resource"`
type KubernetesResource struct {
ID string `json:"id" gorm:"primarykey"`
APIVersion string `json:"apiVersion" gorm:"index"`
Kind string `json:"kind" gorm:"index"`
KubernetesResourceMeta *KubernetesResourceObjectMeta `json:"metadata" gorm:"foreignkey:ID;references:id;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
Spec *KubernetesResourceSpec `json:"spec,omitempty" gorm:"foreignkey:ID;references:id;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
Status *KubernetesResourceStatus `json:"status,omitempty" gorm:"foreignkey:ID;references:id;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
ClusterID string `json:"cluster_id"`
PatternResource *uuid.UUID `json:"pattern_resource"`

// Secondary fields for configsmaps and secrets
Immutable string `json:"immutable,omitempty"`
Expand All @@ -37,24 +37,24 @@ type KubernetesKeyValue struct {
}

type KubernetesResourceObjectMeta struct {
ID string `json:"id" gorm:"primarykey"`
Name string `json:"name,omitempty" gorm:"index"`
GenerateName string `json:"generateName,omitempty"`
Namespace string `json:"namespace,omitempty"`
SelfLink string `json:"selfLink,omitempty"`
UID string `json:"uid"`
ResourceVersion string `json:"resourceVersion,omitempty"`
Generation int64 `json:"generation,omitempty"`
CreationTimestamp string `json:"creationTimestamp,omitempty"`
DeletionTimestamp string `json:"deletionTimestamp,omitempty"`
DeletionGracePeriodSeconds *int64 `json:"deletionGracePeriodSeconds,omitempty"`
ID string `json:"id" gorm:"primarykey"`
Name string `json:"name,omitempty" gorm:"index"`
GenerateName string `json:"generateName,omitempty"`
Namespace string `json:"namespace,omitempty"`
SelfLink string `json:"selfLink,omitempty"`
UID string `json:"uid"`
ResourceVersion string `json:"resourceVersion,omitempty"`
Generation int64 `json:"generation,omitempty"`
CreationTimestamp string `json:"creationTimestamp,omitempty"`
DeletionTimestamp string `json:"deletionTimestamp,omitempty"`
DeletionGracePeriodSeconds *int64 `json:"deletionGracePeriodSeconds,omitempty"`
Labels []*KubernetesKeyValue `json:"labels,omitempty" gorm:"foreignkey:ID;references:id;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
Annotations []*KubernetesKeyValue `json:"annotations,omitempty" gorm:"foreignkey:ID;references:id;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
OwnerReferences string `json:"ownerReferences,omitempty" gorm:"-"`
Finalizers string `json:"finalizers,omitempty" gorm:"-"`
ClusterName string `json:"clusterName,omitempty"`
ManagedFields string `json:"managedFields,omitempty" gorm:"-"`
ClusterID string `json:"cluster_id"`
OwnerReferences string `json:"ownerReferences,omitempty" gorm:"-"`
Finalizers string `json:"finalizers,omitempty" gorm:"-"`
ClusterName string `json:"clusterName,omitempty"`
ManagedFields string `json:"managedFields,omitempty" gorm:"-"`
ClusterID string `json:"cluster_id"`
}

type KubernetesResourceSpec struct {
Expand All @@ -67,17 +67,17 @@ type KubernetesResourceStatus struct {
Attribute string `json:"attribute,omitempty"`
}

func (obj *KubernetesObject) BeforeCreate(tx *gorm.DB) (err error) {
func (obj *KubernetesResource) BeforeCreate(tx *gorm.DB) (err error) {
SetID(obj)
return nil
}

func (obj *KubernetesObject) BeforeSave(tx *gorm.DB) (err error) {
func (obj *KubernetesResource) BeforeSave(tx *gorm.DB) (err error) {
SetID(obj)
return nil
}

func (obj *KubernetesObject) BeforeDelete(tx *gorm.DB) (err error) {
func (obj *KubernetesResource) BeforeDelete(tx *gorm.DB) (err error) {
SetID(obj)
return nil
}
32 changes: 16 additions & 16 deletions pkg/model/model_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func ParseList(object unstructured.Unstructured) KubernetesObject {
func ParseList(object unstructured.Unstructured) KubernetesResource {
data, _ := object.MarshalJSON()
result := KubernetesObject{}
result := KubernetesResource{}

_ = utils.Unmarshal(string(data), &result)

Expand All @@ -34,7 +34,7 @@ func ParseList(object unstructured.Unstructured) KubernetesObject {

return nil
}, "metadata", "labels")
result.KubernetesObjectMeta.Labels = labels
result.KubernetesResourceMeta.Labels = labels

annotations := make([]*KubernetesKeyValue, 0)
_ = jsonparser.ObjectEach(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
Expand All @@ -45,18 +45,18 @@ func ParseList(object unstructured.Unstructured) KubernetesObject {
})
return nil
}, "metadata", "annotations")
result.KubernetesObjectMeta.Annotations = annotations
result.KubernetesResourceMeta.Annotations = annotations

if finalizers, _, _, err := jsonparser.Get(data, "metadata", "finalizers"); err == nil {
result.KubernetesObjectMeta.Finalizers = string(finalizers)
result.KubernetesResourceMeta.Finalizers = string(finalizers)
}

if managedFields, _, _, err := jsonparser.Get(data, "metadata", "managedFields"); err == nil {
result.KubernetesObjectMeta.ManagedFields = string(managedFields)
result.KubernetesResourceMeta.ManagedFields = string(managedFields)
}

if ownerReferences, _, _, err := jsonparser.Get(data, "metadata", "ownerReferences"); err == nil {
result.KubernetesObjectMeta.OwnerReferences = string(ownerReferences)
result.KubernetesResourceMeta.OwnerReferences = string(ownerReferences)
}

if spec, _, _, err := jsonparser.Get(data, "spec"); err == nil {
Expand Down Expand Up @@ -92,27 +92,27 @@ func ParseList(object unstructured.Unstructured) KubernetesObject {
return result
}

func IsObject(obj KubernetesObject) bool {
return obj.KubernetesObjectMeta != nil
func IsObject(obj KubernetesResource) bool {
return obj.KubernetesResourceMeta != nil
}

func SetID(obj *KubernetesObject) {
func SetID(obj *KubernetesResource) {
if obj != nil && IsObject(*obj) {
id := base64.StdEncoding.EncodeToString([]byte(
fmt.Sprintf("%s.%s.%s.%s.%s", obj.ClusterID, obj.Kind, obj.APIVersion, obj.KubernetesObjectMeta.Namespace, obj.KubernetesObjectMeta.Name),
fmt.Sprintf("%s.%s.%s.%s.%s", obj.ClusterID, obj.Kind, obj.APIVersion, obj.KubernetesResourceMeta.Namespace, obj.KubernetesResourceMeta.Name),
))
obj.ID = id
obj.KubernetesObjectMeta.ID = id
obj.KubernetesResourceMeta.ID = id

if len(obj.KubernetesObjectMeta.Labels) > 0 {
for _, label := range obj.KubernetesObjectMeta.Labels {
if len(obj.KubernetesResourceMeta.Labels) > 0 {
for _, label := range obj.KubernetesResourceMeta.Labels {
label.ID = id
label.UniqueID = uuid.New().String()
}
}

if len(obj.KubernetesObjectMeta.Annotations) > 0 {
for _, annotation := range obj.KubernetesObjectMeta.Annotations {
if len(obj.KubernetesResourceMeta.Annotations) > 0 {
for _, annotation := range obj.KubernetesResourceMeta.Annotations {
annotation.ID = id
annotation.UniqueID = uuid.New().String()
}
Expand Down

0 comments on commit 725ce74

Please sign in to comment.