Skip to content

Commit

Permalink
Merge pull request #234 from utkarsh-pro/utkarsh-pro/feature/add-meta…
Browse files Browse the repository at this point in the history
…data

Refactor OAM handler and add metadata for new Meshery UI
  • Loading branch information
kumarabd authored Jun 18, 2021
2 parents c2272a2 + 14f1a32 commit 837b352
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 41 deletions.
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const (
StrictMTLSPolicyOperation = "strict-mtls-policy-operation"
MutualMTLSPolicyOperation = "mutual-mtls-policy-operation"
DisableMTLSPolicyOperation = "disable-mtls-policy-operation"

// OAM Metadata constants
OAMAdapterNameMetadataKey = "adapter.meshery.io/name"
OAMComponentCategoryMetadataKey = "ui.meshery.io/category"
)

var (
Expand Down
9 changes: 9 additions & 0 deletions istio/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ var (
// generated when virtual service parsing fails
ErrParseVirtualServiceCode = "istio_test_code"

// ErrInvalidOAMComponentTypeCode represents the error code which is
// generated when an invalid oam component is requested
ErrInvalidOAMComponentTypeCode = "istio_test_code"

// ErrOpInvalid represents the errors which are generated
// when an invalid operation is requested
ErrOpInvalid = errors.NewDefault(errors.ErrOpInvalid, "Invalid operation")
Expand Down Expand Up @@ -191,3 +195,8 @@ func ErrIstioVet(err error) error {
func ErrParseVirtualService(err error) error {
return errors.NewDefault(ErrParseVirtualServiceCode, err.Error())
}

// ErrInvalidOAMComponentType is the error when the OAM component name is not valid
func ErrInvalidOAMComponentType(compName string) error {
return errors.NewDefault(ErrInvalidOAMComponentTypeCode, "invalid OAM component name: ", compName)
}
77 changes: 36 additions & 41 deletions istio/oam.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,33 @@ import (
"gopkg.in/yaml.v2"
)

// CompHandler is the type for functions which can handle OAM components
type CompHandler func(*Istio, v1alpha1.Component, bool) (string, error)

// HandleComponents handles the processing of OAM components
func (istio *Istio) HandleComponents(comps []v1alpha1.Component, isDel bool) (string, error) {
var errs []error
var msgs []string
for _, comp := range comps {
if comp.Spec.Type == "IstioMesh" {
if err := handleComponentIstioMesh(istio, comp, isDel); err != nil {
errs = append(errs, err)
}

msg := "created service of type \"IstioMesh\""
if isDel {
msg = "deleted service of type \"IstioMesh\""
}

msgs = append(msgs, msg)
continue
}

if comp.Spec.Type == "VirtualService" {
if err := handleComponentVirtualService(istio, comp, isDel); err != nil {
errs = append(errs, err)
}

msg := fmt.Sprintf("created virtual service \"%s\" in namespace \"%s\"", comp.Name, comp.Namespace)
if isDel {
msg = fmt.Sprintf("deleted virtual service \"%s\" in namespace \"%s\"", comp.Name, comp.Namespace)
}
compFuncMap := map[string]CompHandler{
"IstioMesh": handleComponentIstioMesh,
"VirtualService": handleComponentVirtualService,
"GrafanaIstioAddon": handleComponentIstioAddon,
"PrometheusIstioAddon": handleComponentIstioAddon,
"ZipkinIstioAddon": handleComponentIstioAddon,
"JaegerIstioAddon": handleComponentIstioAddon,
}

msgs = append(msgs, msg)
continue
for _, comp := range comps {
fnc, ok := compFuncMap[comp.Spec.Type]
if !ok {
return "", ErrInvalidOAMComponentType(comp.Spec.Type)
}

if err := handleComponentIstioAddon(istio, comp, isDel); err != nil {
msg, err := fnc(istio, comp, isDel)
if err != nil {
errs = append(errs, err)
}

msg := fmt.Sprintf("created service of type \"%s\"", comp.Spec.Type)
if isDel {
msg = fmt.Sprintf("deleted service of type \"%s\"", comp.Spec.Type)
continue
}

msgs = append(msgs, msg)
Expand Down Expand Up @@ -120,18 +107,16 @@ func handleNamespaceLabel(istio *Istio, namespaces []string, isDel bool) error {
return mergeErrors(errs)
}

func handleComponentIstioMesh(istio *Istio, comp v1alpha1.Component, isDel bool) error {
func handleComponentIstioMesh(istio *Istio, comp v1alpha1.Component, isDel bool) (string, error) {
// Get the istio version from the settings
// we are sure that the version of istio would be present
// because the configuration is already validated against the schema
version := comp.Spec.Settings["version"].(string)

_, err := istio.installIstio(isDel, version, comp.Namespace)

return err
return istio.installIstio(isDel, version, comp.Namespace)
}

func handleComponentVirtualService(istio *Istio, comp v1alpha1.Component, isDel bool) error {
func handleComponentVirtualService(istio *Istio, comp v1alpha1.Component, isDel bool) (string, error) {
virtualSvc := map[string]interface{}{
"apiVersion": "networking.istio.io/v1beta1",
"kind": "VirtualService",
Expand All @@ -148,13 +133,18 @@ func handleComponentVirtualService(istio *Istio, comp v1alpha1.Component, isDel
if err != nil {
err = ErrParseVirtualService(err)
istio.Log.Error(err)
return err
return "", err
}

return istio.applyManifest(yamlByt, isDel, comp.Namespace)
msg := fmt.Sprintf("created virtual service \"%s\" in namespace \"%s\"", comp.Name, comp.Namespace)
if isDel {
msg = fmt.Sprintf("deleted virtual service \"%s\" in namespace \"%s\"", comp.Name, comp.Namespace)
}

return msg, istio.applyManifest(yamlByt, isDel, comp.Namespace)
}

func handleComponentIstioAddon(istio *Istio, comp v1alpha1.Component, isDel bool) error {
func handleComponentIstioAddon(istio *Istio, comp v1alpha1.Component, isDel bool) (string, error) {
var addonName string

switch comp.Spec.Type {
Expand All @@ -167,7 +157,7 @@ func handleComponentIstioAddon(istio *Istio, comp v1alpha1.Component, isDel bool
case "JaegerIstioAddon":
addonName = config.JaegerAddon
default:
return nil
return "", nil
}

// Get the service
Expand All @@ -184,7 +174,12 @@ func handleComponentIstioAddon(istio *Istio, comp v1alpha1.Component, isDel bool

_, err := istio.installAddon(comp.Namespace, isDel, svc, patches, templates)

return err
msg := fmt.Sprintf("created service of type \"%s\"", comp.Spec.Type)
if isDel {
msg = fmt.Sprintf("deleted service of type \"%s\"", comp.Spec.Type)
}

return msg, err
}

func castSliceInterfaceToSliceString(in []interface{}) []string {
Expand Down
14 changes: 14 additions & 0 deletions istio/oam/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/layer5io/meshery-adapter-library/adapter"
"github.com/layer5io/meshery-istio/internal/config"
)

var (
Expand Down Expand Up @@ -46,10 +48,19 @@ func RegisterWorkloads(runtime, host string) error {
for _, workload := range workloads {
defintionPath, schemaPath := generatePaths(workloadPath, workload)

metadata := map[string]string{
config.OAMAdapterNameMetadataKey: config.IstioOperation,
}

if strings.HasSuffix(workload, "addon") {
metadata[config.OAMComponentCategoryMetadataKey] = "addon"
}

oamRDP = append(oamRDP, adapter.OAMRegistrantDefinitionPath{
OAMDefintionPath: defintionPath,
OAMRefSchemaPath: schemaPath,
Host: host,
Metadata: metadata,
})
}

Expand Down Expand Up @@ -77,6 +88,9 @@ func RegisterTraits(runtime, host string) error {
OAMDefintionPath: defintionPath,
OAMRefSchemaPath: schemaPath,
Host: host,
Metadata: map[string]string{
config.OAMAdapterNameMetadataKey: config.IstioOperation,
},
})
}

Expand Down

0 comments on commit 837b352

Please sign in to comment.