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

[chore] Rename MeshSync Tables. #261

Merged
merged 4 commits into from
Oct 17, 2023
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
**errorutil_errors_export.json

.vscode/
cover.html
cover.html
.DS_Store
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.Object {
func (h *Handler) listStoreObjects() []model.KubernetesResource {
objects := make([]interface{}, 0)
for _, v := range h.stores {
objects = append(objects, v.List()...)
}
parsedObjects := make([]model.Object, 0)
parsedObjects := make([]model.KubernetesResource, 0)
for _, obj := range objects {
parsedObjects = append(parsedObjects, model.ParseList(*obj.(*unstructured.Unstructured)))
}
Expand All @@ -133,9 +133,9 @@ func (h *Handler) listStoreObjects() []model.Object {
// 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.Object, maxItmsPerSlice int) []([]model.Object) {
result := make([]([]model.Object), 0)
temp := make([]model.Object, 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.Object
input []model.KubernetesResource
maxItmsPerSlice int
expectedOutput [][]model.Object
expectedOutput [][]model.KubernetesResource
}{
{
name: "test with 0 items",
input: []model.Object{},
input: []model.KubernetesResource{},
maxItmsPerSlice: 10,
expectedOutput: [][]model.Object{},
expectedOutput: [][]model.KubernetesResource{},
},

{
name: "test with 1 item",
input: []model.Object{
input: []model.KubernetesResource{
{
Kind: "test",
},
},
maxItmsPerSlice: 10,
expectedOutput: [][]model.Object{
expectedOutput: [][]model.KubernetesResource{
{
{
Kind: "test",
Expand Down
68 changes: 34 additions & 34 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 Object struct {
ID string `json:"id" gorm:"primarykey"`
APIVersion string `json:"apiVersion" gorm:"index"`
Kind string `json:"kind" gorm:"index"`
ObjectMeta *ResourceObjectMeta `json:"metadata" gorm:"foreignkey:ID;references:id;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
Spec *ResourceSpec `json:"spec,omitempty" gorm:"foreignkey:ID;references:id;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
Status *ResourceStatus `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 @@ -28,56 +28,56 @@ type Object struct {
Type string `json:"type,omitempty"`
}

type KeyValue struct {
type KubernetesKeyValue struct {
ID string `json:"id" gorm:"primarykey"`
UniqueID string `json:"unique_id" gorm:"index"`
Kind string `json:"kind" gorm:"primarykey"`
Key string `json:"key,omitempty" gorm:"primarykey"`
Value string `json:"value,omitempty" gorm:"primarykey"`
}

type ResourceObjectMeta 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"`
Labels []*KeyValue `json:"labels,omitempty" gorm:"foreignkey:ID;references:id;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
Annotations []*KeyValue `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"`
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"`
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"`
}

type ResourceSpec struct {
type KubernetesResourceSpec struct {
ID string `json:"id" gorm:"primarykey"`
Attribute string `json:"attribute,omitempty"`
}

type ResourceStatus struct {
type KubernetesResourceStatus struct {
ID string `json:"id" gorm:"primarykey"`
Attribute string `json:"attribute,omitempty"`
}

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

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

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

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

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

// ObjectMeta internal models
labels := make([]*KeyValue, 0)
labels := make([]*KubernetesKeyValue, 0)
_ = jsonparser.ObjectEach(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
labels = append(labels, &KeyValue{
labels = append(labels, &KubernetesKeyValue{
Kind: KindLabel,
Key: string(key),
Value: string(value),
Expand All @@ -34,29 +34,29 @@ func ParseList(object unstructured.Unstructured) Object {

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

annotations := make([]*KeyValue, 0)
annotations := make([]*KubernetesKeyValue, 0)
_ = jsonparser.ObjectEach(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
annotations = append(annotations, &KeyValue{
annotations = append(annotations, &KubernetesKeyValue{
Kind: KindAnnotation,
Key: string(key),
Value: string(value),
})
return nil
}, "metadata", "annotations")
result.ObjectMeta.Annotations = annotations
result.KubernetesResourceMeta.Annotations = annotations

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

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

if ownerReferences, _, _, err := jsonparser.Get(data, "metadata", "ownerReferences"); err == nil {
result.ObjectMeta.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) Object {
return result
}

func IsObject(obj Object) bool {
return obj.ObjectMeta != nil
func IsObject(obj KubernetesResource) bool {
return obj.KubernetesResourceMeta != nil
}

func SetID(obj *Object) {
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.ObjectMeta.Namespace, obj.ObjectMeta.Name),
fmt.Sprintf("%s.%s.%s.%s.%s", obj.ClusterID, obj.Kind, obj.APIVersion, obj.KubernetesResourceMeta.Namespace, obj.KubernetesResourceMeta.Name),
))
obj.ID = id
obj.ObjectMeta.ID = id
obj.KubernetesResourceMeta.ID = id

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

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