Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #152 from Revolyssup/multicontext
Browse files Browse the repository at this point in the history
Multi context refactor
  • Loading branch information
Revolyssup authored Jun 11, 2022
2 parents bccaa43 + d5cf7be commit ec8cb8d
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 101 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ replace (
)

require (
github.com/layer5io/meshery-adapter-library v0.5.4
github.com/layer5io/meshkit v0.5.19
github.com/layer5io/meshery-adapter-library v0.5.6
github.com/layer5io/meshkit v0.5.20
github.com/layer5io/service-mesh-performance v0.3.4
gopkg.in/yaml.v2 v2.4.0
k8s.io/apimachinery v0.23.5
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -885,11 +885,11 @@ github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6Fm
github.com/layer5io/kuttl v0.4.1-0.20200806180306-b7e46afd657f/go.mod h1:UmrVd7x+bNVKrpmKgTtfRiTKHZeNPcMjQproJ0vGwhE=
github.com/layer5io/learn-layer5/smi-conformance v0.0.0-20210317075357-06b4f88b3e34 h1:QaViadDOBCMDUwYx78kfRvHMkzRVnh/GOhm3s2gxoP4=
github.com/layer5io/learn-layer5/smi-conformance v0.0.0-20210317075357-06b4f88b3e34/go.mod h1:BQPLwdJt7v7y0fXIejI4whR9zMyX07Wjt5xrbgEmHLw=
github.com/layer5io/meshery-adapter-library v0.5.4 h1:QQ+nVGHd7KhV58KhY40V00kC+IEM4+AlOhQcSHSbOUE=
github.com/layer5io/meshery-adapter-library v0.5.4/go.mod h1:YmLV0w6ucBagrqUB0x9q8ZVXrhN1tJBP5j+Pu6LOY/M=
github.com/layer5io/meshery-adapter-library v0.5.6 h1:pbZTMkWNcGWPk314K7WhO4UGVxSnKvGLmwQXBWZ05GI=
github.com/layer5io/meshery-adapter-library v0.5.6/go.mod h1:YmLV0w6ucBagrqUB0x9q8ZVXrhN1tJBP5j+Pu6LOY/M=
github.com/layer5io/meshkit v0.5.16/go.mod h1:tj5TAjty7T/WJ8YvlDfOZF94t4g3mhWuKBCc6MOUoNU=
github.com/layer5io/meshkit v0.5.19 h1:pUCmbekifyOIvec1p1WjFa9hDrlqbwALKJ3VqpOXEKQ=
github.com/layer5io/meshkit v0.5.19/go.mod h1:EUfXIcztap9Dh0Ao3Dmoxf3FMsm4h7zFHGwagj+5ra4=
github.com/layer5io/meshkit v0.5.20 h1:QpN/SEepUZk+Jj2K4TBRZJCRr/pzuvHqDaUr30vWddI=
github.com/layer5io/meshkit v0.5.20/go.mod h1:EUfXIcztap9Dh0Ao3Dmoxf3FMsm4h7zFHGwagj+5ra4=
github.com/layer5io/service-mesh-performance v0.3.2-0.20210122142912-a94e0658b021/go.mod h1:W153amv8aHAeIWxO7b7d7Vibt9RhaEVh4Uh+RG+BumQ=
github.com/layer5io/service-mesh-performance v0.3.4 h1:aw/elsx0wkry7SyiQRIj31wW7TPCP4YfhINdNOLXVg8=
github.com/layer5io/service-mesh-performance v0.3.4/go.mod h1:W153amv8aHAeIWxO7b7d7Vibt9RhaEVh4Uh+RG+BumQ=
Expand Down
55 changes: 41 additions & 14 deletions osm/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ package osm

import (
"fmt"
"sync"

"github.com/layer5io/meshery-adapter-library/adapter"
"github.com/layer5io/meshery-adapter-library/status"
mesherykube "github.com/layer5io/meshkit/utils/kubernetes"
)

func (h *Handler) installOSM(del bool, version, ns string) (string, error) {
func (h *Handler) installOSM(del bool, version, ns string, kubeconfigs []string) (string, error) {
h.Log.Debug(fmt.Sprintf("Requested install of version: %s", version))
h.Log.Debug(fmt.Sprintf("Requested action is delete: %v", del))
h.Log.Debug(fmt.Sprintf("Requested action is in namespace: %s", ns))
Expand All @@ -38,7 +39,7 @@ func (h *Handler) installOSM(del bool, version, ns string) (string, error) {
}

h.Log.Info("Installing...")
err = h.applyHelmChart(del, version, ns)
err = h.applyHelmChart(del, version, ns, kubeconfigs)
if err != nil {
return st, ErrApplyHelmChart(err)
}
Expand All @@ -51,8 +52,7 @@ func (h *Handler) installOSM(del bool, version, ns string) (string, error) {
return st, nil
}

func (h *Handler) applyHelmChart(del bool, version, namespace string) error {
kClient := h.MesheryKubeclient
func (h *Handler) applyHelmChart(del bool, version, namespace string, kubeconfigs []string) error {

repo := "https://openservicemesh.github.io/osm/"
chart := "osm"
Expand All @@ -62,14 +62,41 @@ func (h *Handler) applyHelmChart(del bool, version, namespace string) error {
} else {
act = mesherykube.INSTALL
}
return kClient.ApplyHelmChart(mesherykube.ApplyHelmChartConfig{
ChartLocation: mesherykube.HelmChartLocation{
Repository: repo,
Chart: chart,
Version: version,
},
Namespace: namespace,
Action: act,
CreateNamespace: true,
})
var wg sync.WaitGroup
var errMx sync.Mutex
var errs []error
for _, kubeconfig := range kubeconfigs {
wg.Add(1)
go func(kubeconfig string) {
defer wg.Done()
kClient, err := mesherykube.New([]byte(kubeconfig))
if err != nil {
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
return
}
err = kClient.ApplyHelmChart(mesherykube.ApplyHelmChartConfig{
ChartLocation: mesherykube.HelmChartLocation{
Repository: repo,
Chart: chart,
Version: version,
},
Namespace: namespace,
Action: act,
CreateNamespace: true,
})
if err != nil {
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
return
}
}(kubeconfig)
}
wg.Wait()
if len(errs) != 0 {
return mergeErrors(errs)
}
return nil
}
25 changes: 13 additions & 12 deletions osm/oam.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
)

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

// HandleComponents handles the processing of OAM components
func (h *Handler) HandleComponents(comps []v1alpha1.Component, isDel bool) (string, error) {
func (h *Handler) HandleComponents(comps []v1alpha1.Component, isDel bool, kubeconfigs []string) (string, error) {
var errs []error
var msgs []string

Expand All @@ -23,7 +23,7 @@ func (h *Handler) HandleComponents(comps []v1alpha1.Component, isDel bool) (stri
for _, comp := range comps {
fnc, ok := compFuncMap[comp.Spec.Type]
if !ok {
msg, err := handleOSMCoreComponent(h, comp, isDel, "", "")
msg, err := handleOSMCoreComponent(h, comp, isDel, "", "", kubeconfigs)
if err != nil {
errs = append(errs, err)
continue
Expand All @@ -33,7 +33,7 @@ func (h *Handler) HandleComponents(comps []v1alpha1.Component, isDel bool) (stri
continue
}

msg, err := fnc(h, comp, isDel)
msg, err := fnc(h, comp, isDel, kubeconfigs)
if err != nil {
errs = append(errs, err)
continue
Expand All @@ -50,14 +50,14 @@ func (h *Handler) HandleComponents(comps []v1alpha1.Component, isDel bool) (stri
}

// HandleApplicationConfiguration handles the processing of OAM application configuration
func (h *Handler) HandleApplicationConfiguration(config v1alpha1.Configuration, isDel bool) (string, error) {
func (h *Handler) HandleApplicationConfiguration(config v1alpha1.Configuration, isDel bool, kubeconfigs []string) (string, error) {
var errs []error
var msgs []string
for _, comp := range config.Spec.Components {
for _, trait := range comp.Traits {
if trait.Name == "automaticSidecarInjection.OSM" {
namespaces := castSliceInterfaceToSliceString(trait.Properties["namespaces"].([]interface{}))
if err := handleNamespaceLabel(h, namespaces, isDel); err != nil {
if err := handleNamespaceLabel(h, namespaces, isDel, kubeconfigs); err != nil {
errs = append(errs, err)
}
}
Expand All @@ -74,24 +74,24 @@ func (h *Handler) HandleApplicationConfiguration(config v1alpha1.Configuration,

}

func handleNamespaceLabel(h *Handler, namespaces []string, isDel bool) error {
func handleNamespaceLabel(h *Handler, namespaces []string, isDel bool, kubeconfigs []string) error {
var errs []error
for _, ns := range namespaces {
if err := h.sidecarInjection(ns, isDel); err != nil {
if err := h.sidecarInjection(ns, isDel, kubeconfigs); err != nil {
errs = append(errs, err)
}
}

return mergeErrors(errs)
}

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

msg, err := h.installOSM(isDel, version, comp.Namespace)
msg, err := h.installOSM(isDel, version, comp.Namespace, kubeconfigs)
if err != nil {
return fmt.Sprintf("%s: %s", comp.Name, msg), err
}
Expand All @@ -104,7 +104,8 @@ func handleOSMCoreComponent(
comp v1alpha1.Component,
isDel bool,
apiVersion,
kind string) (string, error) {
kind string,
kubeconfigs []string) (string, error) {
if apiVersion == "" {
apiVersion = getAPIVersionFromComponent(comp)
if apiVersion == "" {
Expand Down Expand Up @@ -143,7 +144,7 @@ func handleOSMCoreComponent(
msg = fmt.Sprintf("deleted %s config \"%s\" in namespace \"%s\"", kind, comp.Name, comp.Namespace)
}

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

func getAPIVersionFromComponent(comp v1alpha1.Component) string {
Expand Down
15 changes: 11 additions & 4 deletions osm/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ import (
)

// ApplyOperation function contains the operation handlers
func (h *Handler) ApplyOperation(ctx context.Context, request adapter.OperationRequest) error {
func (h *Handler) ApplyOperation(ctx context.Context, request adapter.OperationRequest, hchan *chan interface{}) error {
err := h.CreateKubeconfigs(request.K8sConfigs)
if err != nil {
return err
}
h.SetChannel(hchan)
kubeconfigs := request.K8sConfigs
operations := make(adapter.Operations)
err := h.Config.GetObject(adapter.OperationsKey, &operations)
err = h.Config.GetObject(adapter.OperationsKey, &operations)
if err != nil {
return err
}
Expand All @@ -29,7 +35,7 @@ func (h *Handler) ApplyOperation(ctx context.Context, request adapter.OperationR
case internalconfig.OSMOperation:
go func(hh *Handler, ee *adapter.Event) {
version := string(operations[request.OperationName].Versions[0])
stat, err := hh.installOSM(request.IsDeleteOperation, version, request.Namespace)
stat, err := hh.installOSM(request.IsDeleteOperation, version, request.Namespace, kubeconfigs)
if err != nil {
e.Summary = fmt.Sprintf("Error while %s Open service mesh", stat)
e.Details = err.Error()
Expand All @@ -47,7 +53,7 @@ func (h *Handler) ApplyOperation(ctx context.Context, request adapter.OperationR
common.EmojiVotoOperation:
go func(hh *Handler, ee *adapter.Event) {
appName := operations[request.OperationName].AdditionalProperties[common.ServiceName]
stat, err := hh.installSampleApp(request.IsDeleteOperation, request.Namespace, operations[request.OperationName].Templates)
stat, err := hh.installSampleApp(request.IsDeleteOperation, request.Namespace, operations[request.OperationName].Templates, kubeconfigs)
if err != nil {
e.Summary = fmt.Sprintf("Error while %s %s application", stat, appName)
e.Details = err.Error()
Expand All @@ -66,6 +72,7 @@ func (h *Handler) ApplyOperation(ctx context.Context, request adapter.OperationR
request.IsDeleteOperation,
version,
operations[request.OperationName].Templates,
kubeconfigs,
)
if err != nil {
e.Summary = fmt.Sprintf("Error while %s %s application", stat, appName)
Expand Down
63 changes: 58 additions & 5 deletions osm/osm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"github.com/layer5io/meshery-osm/osm/oam"
meshkitCfg "github.com/layer5io/meshkit/config"
"github.com/layer5io/meshkit/logger"
"github.com/layer5io/meshkit/models"
"github.com/layer5io/meshkit/models/oam/core/v1alpha1"
"gopkg.in/yaml.v2"
)

// Handler instance for this adapter
Expand All @@ -40,8 +42,59 @@ func New(config meshkitCfg.Handler, log logger.Handler, kc meshkitCfg.Handler) a
}
}

//CreateKubeconfigs creates and writes passed kubeconfig onto the filesystem
func (h *Handler) CreateKubeconfigs(kubeconfigs []string) error {
var errs = make([]error, 0)
for _, kubeconfig := range kubeconfigs {
kconfig := models.Kubeconfig{}
err := yaml.Unmarshal([]byte(kubeconfig), &kconfig)
if err != nil {
errs = append(errs, err)
continue
}

// To have control over what exactly to take in on kubeconfig
h.KubeconfigHandler.SetKey("kind", kconfig.Kind)
h.KubeconfigHandler.SetKey("apiVersion", kconfig.APIVersion)
h.KubeconfigHandler.SetKey("current-context", kconfig.CurrentContext)
err = h.KubeconfigHandler.SetObject("preferences", kconfig.Preferences)
if err != nil {
errs = append(errs, err)
continue
}

err = h.KubeconfigHandler.SetObject("clusters", kconfig.Clusters)
if err != nil {
errs = append(errs, err)
continue
}

err = h.KubeconfigHandler.SetObject("users", kconfig.Users)
if err != nil {
errs = append(errs, err)
continue
}

err = h.KubeconfigHandler.SetObject("contexts", kconfig.Contexts)
if err != nil {
errs = append(errs, err)
continue
}
}
if len(errs) == 0 {
return nil
}
return mergeErrors(errs)
}

// ProcessOAM will handles the grpc invocation for handling OAM objects
func (h *Handler) ProcessOAM(ctx context.Context, oamReq adapter.OAMRequest) (string, error) {
func (h *Handler) ProcessOAM(ctx context.Context, oamReq adapter.OAMRequest, hchan *chan interface{}) (string, error) {
err := h.CreateKubeconfigs(oamReq.K8sConfigs)
if err != nil {
return "", err
}
kubeconfigs := oamReq.K8sConfigs
h.SetChannel(hchan)
var comps []v1alpha1.Component
for _, acomp := range oamReq.OamComps {
comp, err := oam.ParseApplicationComponent(acomp)
Expand All @@ -61,13 +114,13 @@ func (h *Handler) ProcessOAM(ctx context.Context, oamReq adapter.OAMRequest) (st
// If operation is delete then first HandleConfiguration and then handle the deployment
if oamReq.DeleteOp {
// Process configuration
msg2, err := h.HandleApplicationConfiguration(config, oamReq.DeleteOp)
msg2, err := h.HandleApplicationConfiguration(config, oamReq.DeleteOp, kubeconfigs)
if err != nil {
return msg2, ErrProcessOAM(err)
}

// Process components
msg1, err := h.HandleComponents(comps, oamReq.DeleteOp)
msg1, err := h.HandleComponents(comps, oamReq.DeleteOp, kubeconfigs)
if err != nil {
return msg1 + "\n" + msg2, ErrProcessOAM(err)
}
Expand All @@ -76,13 +129,13 @@ func (h *Handler) ProcessOAM(ctx context.Context, oamReq adapter.OAMRequest) (st
}

// Process components
msg1, err := h.HandleComponents(comps, oamReq.DeleteOp)
msg1, err := h.HandleComponents(comps, oamReq.DeleteOp, kubeconfigs)
if err != nil {
return msg1, ErrProcessOAM(err)
}

// Process configuration
msg2, err := h.HandleApplicationConfiguration(config, oamReq.DeleteOp)
msg2, err := h.HandleApplicationConfiguration(config, oamReq.DeleteOp, kubeconfigs)
if err != nil {
return msg1 + "\n" + msg2, ErrProcessOAM(err)
}
Expand Down
Loading

0 comments on commit ec8cb8d

Please sign in to comment.