diff --git a/.gitignore b/.gitignore index 3976d1a3..a5b20a62 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,5 @@ **errorutil_errors_export.json .vscode/ -cover.html \ No newline at end of file +cover.html +.DS_Store \ No newline at end of file diff --git a/meshsync/handlers.go b/meshsync/handlers.go index e1c0e7b5..7f73326a 100644 --- a/meshsync/handlers.go +++ b/meshsync/handlers.go @@ -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))) } @@ -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) diff --git a/meshsync/handlers_test.go b/meshsync/handlers_test.go index 33071ef1..6cd79b6e 100644 --- a/meshsync/handlers_test.go +++ b/meshsync/handlers_test.go @@ -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", diff --git a/pkg/model/model.go b/pkg/model/model.go index d528fd4c..b161a83a 100644 --- a/pkg/model/model.go +++ b/pkg/model/model.go @@ -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"` @@ -28,7 +28,7 @@ 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"` @@ -36,48 +36,48 @@ type KeyValue struct { 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 } diff --git a/pkg/model/model_converter.go b/pkg/model/model_converter.go index c76d8204..5da3275a 100644 --- a/pkg/model/model_converter.go +++ b/pkg/model/model_converter.go @@ -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), @@ -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 { @@ -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() }