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

Add "service mesh" as category in meshmodel #100

Merged
merged 2 commits into from
Nov 29, 2022
Merged
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
43 changes: 29 additions & 14 deletions adapter/oam.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,23 @@ func (or *OAMRegistrant) Register() error {
return nil
}

type MeshModelConfig struct {
Category string
SubCategory string
Metadata map[string]interface{}
}

// StaticCompConfig is used to configure CreateComponents
type StaticCompConfig struct {
MeshModelName string //Used in Adding ModelName onto Core Meshmodel components. Pass it the same as meshName in OAM components
URL string //URL
Method string //Use the constants exported by package. Manifests or Helm
OAMPath string //Where to store the directory.(Each directory will have an array of definitions and schemas)
MeshModelPath string
DirName string //The directory's name. By convention, it should be the version name
Config manifests.Config //Filters required to create definition and schema
Force bool //When set to true, if the file with same name already exists, they will be overridden
MeshModelName string //Used in Adding ModelName onto Core Meshmodel components. Pass it the same as meshName in OAM components
URL string //URL
Method string //Use the constants exported by package. Manifests or Helm
OAMPath string //Where to store the directory.(Each directory will have an array of definitions and schemas)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps there's a more forward-looking descriptor and a more generic one than "OAMPath".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OAMPath is to be removed anyways.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will you open an issue to follow up on removal of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not just this, this along with a lot of other things will be stripped off. That is a part of last phase of MeshModel migration. So this will come under that legend.

MeshModelPath string
MeshModelConfig MeshModelConfig
DirName string //The directory's name. By convention, it should be the version name
Config manifests.Config //Filters required to create definition and schema
Force bool //When set to true, if the file with same name already exists, they will be overridden
}

// CreateComponents generates components for a given configuration and stores them.
Expand Down Expand Up @@ -235,7 +242,7 @@ func CreateComponents(scfg StaticCompConfig) error {
defFileName := name + "_definition.json"
schemaFileName := name + ".meshery.layer5io.schema.json"
meshmodelFileName := name + "_meshmodel.json"
err = createMeshModelComponentsFromLegacyOAMComponents([]byte(def), schema, filepath.Join(meshmodelDir, meshmodelFileName), scfg.MeshModelName)
err = createMeshModelComponentsFromLegacyOAMComponents([]byte(def), schema, filepath.Join(meshmodelDir, meshmodelFileName), scfg.MeshModelName, scfg.MeshModelConfig)
if err != nil {
return ErrCreatingComponents(err)
}
Expand All @@ -261,27 +268,35 @@ func CreateComponents(scfg StaticCompConfig) error {
}
return nil
}
func convertOAMtoMeshmodel(def []byte, schema string, isCore bool, meshmodelname string) ([]byte, error) {
func convertOAMtoMeshmodel(def []byte, schema string, isCore bool, meshmodelname string, mcfg MeshModelConfig) ([]byte, error) {
var oamdef v1alpha1.WorkloadDefinition
err := json.Unmarshal(def, &oamdef)
if err != nil {
return nil, err
}
var c meshmodel.ComponentDefinition
c.Metadata = make(map[string]interface{})
metaname := strings.Split(manifests.FormatToReadableString(oamdef.ObjectMeta.Name), ".")
var displayname string
if len(metaname) > 0 {
displayname = metaname[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love for this not to be a numeric index reference, but one that is named instead in order to avoid fragility (slightly).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The length of array is checked above so if code reaches at this line then it is guaranteed to have 0th element.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readability is also improved when named keys are used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually here I am just doing some string manipulation to extract Readable name from name with suffix like ".Istio". Nothing much to move into a map or struct.

}
c.DisplayName = displayname
c.Model.Category = mcfg.Category
c.Model.SubCategory = mcfg.SubCategory
c.Metadata = mcfg.Metadata
if isCore {
c.APIVersion = oamdef.APIVersion
c.Kind = oamdef.ObjectMeta.Name
c.Model.Version = oamdef.Spec.Metadata["version"]
c.Model.Name = meshmodelname
c.DisplayName = manifests.FormatToReadableString(oamdef.ObjectMeta.Name)
} else {
c.APIVersion = oamdef.Spec.Metadata["k8sAPIVersion"]
c.Kind = oamdef.Spec.Metadata["k8sKind"]
c.Model.Version = oamdef.Spec.Metadata["meshVersion"]
c.Model.Name = oamdef.Spec.Metadata["meshName"]
c.DisplayName = manifests.FormatToReadableString(oamdef.ObjectMeta.Name)
}
c.Model.DisplayName = manifests.FormatToReadableString(c.Model.Name)
c.Format = meshmodel.JSON
c.Schema = schema
byt, err := json.Marshal(c)
Expand All @@ -292,8 +307,8 @@ func convertOAMtoMeshmodel(def []byte, schema string, isCore bool, meshmodelname
}

// TODO: After OAM is completely removed from meshkit, replace this with fetching native meshmodel components. For now, reuse OAM functions
func createMeshModelComponentsFromLegacyOAMComponents(def []byte, schema string, path string, meshmodel string) (err error) {
byt, err := convertOAMtoMeshmodel(def, schema, false, meshmodel)
func createMeshModelComponentsFromLegacyOAMComponents(def []byte, schema string, path string, meshmodel string, mcfg MeshModelConfig) (err error) {
byt, err := convertOAMtoMeshmodel(def, schema, false, meshmodel, mcfg)
if err != nil {
return err
}
Expand Down