Skip to content

Commit

Permalink
Merge pull request #243 from meshery/nithish/feat/meshmodel_relations…
Browse files Browse the repository at this point in the history
…hips

[Feat] Add support for `Relationships`
  • Loading branch information
humblenginr authored Dec 17, 2022
2 parents 690a1fc + 616ccf2 commit cafe42a
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 5 deletions.
5 changes: 3 additions & 2 deletions models/meshmodel/core/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package types
type CapabilityType string

const (
ComponentDefinition CapabilityType = "component"
PolicyDefinition CapabilityType = "policy"
ComponentDefinition CapabilityType = "component"
PolicyDefinition CapabilityType = "policy"
RelationshipDefinition CapabilityType = "relationship"
)

// Each entity will have it's own Filter implementation via which it exposes the nobs and dials to fetch entities
Expand Down
100 changes: 100 additions & 0 deletions models/meshmodel/core/v1alpha1/relationship.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package v1alpha1

import (
"encoding/json"
"time"

"github.com/google/uuid"
"github.com/layer5io/meshkit/database"
"github.com/layer5io/meshkit/models/meshmodel/core/types"
)

// https://docs.google.com/drawings/d/1_qzQ_YxvCWPYrOBcdqGMlMwfbsZx96SBuIkbn8TfKhU/edit?pli=1
// see RELATIONSHIPDEFINITIONS table in the diagram
// TODO: Add support for Model
type RelationshipDefinition struct {
ID uuid.UUID `json:"-"`
TypeMeta
Metadata map[string]interface{} `json:"metadata" yaml:"metadata"`
SubType string `json:"subType" yaml:"subType" gorm:"subType"`
Selectors map[string]interface{} `json:"selectors" yaml:"selectors"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}

type RelationshipDefinitionDB struct {
ID uuid.UUID `json:"-"`
TypeMeta
Metadata []byte `json:"metadata" yaml:"metadata"`
SubType string `json:"subType" yaml:"subType"`
Selectors []byte `json:"selectors" yaml:"selectors"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}

// For now, only filtering by Kind and SubType are allowed.
// In the future, we will add support to query using `selectors` (using CUE)
// TODO: Add support for Model
type RelationshipFilter struct {
Kind string
SubType string
}

// Create the filter from map[string]interface{}
func (rf *RelationshipFilter) Create(m map[string]interface{}) {
if m == nil {
return
}
}

func GetRelationships(db *database.Handler, f RelationshipFilter) (rs []RelationshipDefinition) {
var rdb []RelationshipDefinitionDB
// GORM takes care of drafting the correct SQL
// https://gorm.io/docs/query.html#Struct-amp-Map-Conditions
_ = db.Where(&RelationshipDefinitionDB{SubType: f.SubType, TypeMeta: TypeMeta{Kind: f.Kind}}).Find(&rdb)
for _, reldb := range rdb {
rel := reldb.GetRelationshipDefinition()
rs = append(rs, rel)
}
return
}

func (rdb *RelationshipDefinitionDB) GetRelationshipDefinition() (r RelationshipDefinition) {
r.ID = rdb.ID
r.TypeMeta = rdb.TypeMeta
if r.Metadata == nil {
r.Metadata = make(map[string]interface{})
}
_ = json.Unmarshal(rdb.Metadata, &r.Metadata)
if r.Selectors == nil {
r.Selectors = make(map[string]interface{})
}
_ = json.Unmarshal(rdb.Selectors, &r.Selectors)
r.SubType = rdb.SubType
r.Kind = rdb.Kind
return
}

func (r RelationshipDefinition) Type() types.CapabilityType {
return types.RelationshipDefinition
}

func CreateRelationship(db *database.Handler, r RelationshipDefinition) (uuid.UUID, error) {
r.ID = uuid.New()
rdb := r.GetRelationshipDefinitionDB()
err := db.Create(&rdb).Error
if err != nil {
return uuid.UUID{}, err
}
return r.ID, err
}

func (r *RelationshipDefinition) GetRelationshipDefinitionDB() (rdb RelationshipDefinitionDB) {
rdb.ID = r.ID
rdb.TypeMeta = r.TypeMeta
rdb.Metadata, _ = json.Marshal(r.Metadata)
rdb.Selectors, _ = json.Marshal(r.Selectors)
rdb.Kind = r.Kind
rdb.SubType = r.SubType
return
}
26 changes: 26 additions & 0 deletions models/meshmodel/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func NewRegistryManager(db *database.Handler) (*RegistryManager, error) {
&Registry{},
&Host{},
&v1alpha1.ComponentDefinitionDB{},
&v1alpha1.RelationshipDefinitionDB{},
&v1alpha1.Models{},
)
if err != nil {
Expand Down Expand Up @@ -104,6 +105,24 @@ func (rm *RegistryManager) RegisterEntity(h Host, en Entity) error {
UpdatedAt: time.Now(),
}
return rm.db.Create(&entry).Error
case v1alpha1.RelationshipDefinition:
relationshipID, err := v1alpha1.CreateRelationship(rm.db, entity)
if err != nil {
return err
}
registrantID, err := createHost(rm.db, h)
if err != nil {
return err
}
entry := Registry{
ID: uuid.New(),
RegistrantID: registrantID,
Entity: relationshipID,
Type: en.Type(),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
return rm.db.Create(&entry).Error
//Add logic for Policies and other entities below
default:
return nil
Expand All @@ -119,6 +138,13 @@ func (rm *RegistryManager) GetEntities(f types.Filter) []Entity {
en = append(en, comp)
}
return en
case *v1alpha1.RelationshipFilter:
en := make([]Entity, 1)
relationships := v1alpha1.GetRelationships(rm.db, *filter)
for _, rel := range relationships {
en = append(en, rel)
}
return en
default:
return nil
}
Expand Down
9 changes: 6 additions & 3 deletions utils/artifacthub/package_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package artifacthub

import "testing"
import (
"strings"
"testing"
)

func TestGetChartUrl(t *testing.T) {
var tests = []struct {
Expand All @@ -9,7 +12,7 @@ func TestGetChartUrl(t *testing.T) {
}{
// these might change in the future, so the tests have to be changed as well when the urls change
// because the urls will change with every new version update to the package
{AhPackage{Name: "consul", Repository: "bitnami", Organization: "", RepoUrl: "https://charts.bitnami.com/bitnami"}, "https://charts.bitnami.com/bitnami/consul-10.9.5.tgz"},
{AhPackage{Name: "consul", Repository: "bitnami", Organization: "", RepoUrl: "https://charts.bitnami.com/bitnami"}, "https://charts.bitnami.com/bitnami/consul"},
{AhPackage{Name: "crossplane-types", Repository: "crossplane", Organization: "", RepoUrl: "https://charts.crossplane.io/master"}, "https://charts.crossplane.io/master/crossplane-types-0.13.0-rc.191.g3a18fb7.tgz"},
}
for _, tt := range tests {
Expand All @@ -19,7 +22,7 @@ func TestGetChartUrl(t *testing.T) {
t.Errorf("error while updating package data = %v", err)
return
}
if tt.ahpkg.ChartUrl != tt.want {
if !strings.HasPrefix(tt.ahpkg.ChartUrl, tt.want) {
t.Errorf("got %v, want %v", tt.ahpkg.ChartUrl, tt.want)
}
})
Expand Down

0 comments on commit cafe42a

Please sign in to comment.