Skip to content

Commit

Permalink
Merge pull request #96 from gr455/mcr
Browse files Browse the repository at this point in the history
Add Support for Multi-Context
  • Loading branch information
Revolyssup committed Jun 11, 2022
2 parents e7b29a0 + 6f6837c commit 6d47854
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 113 deletions.
77 changes: 58 additions & 19 deletions appmesh/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package appmesh
import (
"context"
"net/url"
"sync"

"github.com/layer5io/meshery-adapter-library/status"
"github.com/layer5io/meshkit/utils"
Expand All @@ -11,38 +12,76 @@ import (
"k8s.io/apimachinery/pkg/types"
)

func (appMesh *AppMesh) installAddon(ns string, del bool, svcName string, patches []string, helmChartURL string) (string, error) {
func (appMesh *AppMesh) installAddon(ns string, del bool, svcName string, patches []string, helmChartURL string, kubeconfigs []string) (string, error) {

st := status.Installing

if del {
st = status.Removing
}
err := appMesh.MesheryKubeclient.ApplyHelmChart(kubernetes.ApplyHelmChartConfig{
URL: helmChartURL,
Namespace: ns,
})
if err != nil {
return st, ErrAddonFromHelm(err)
}

for _, patch := range patches {
if !del {
_, err := url.ParseRequestURI(patch)
var wg sync.WaitGroup
var errs []error
var errMx sync.Mutex


for _, config := range kubeconfigs {
wg.Add(1);
go func(config string) {
defer wg.Done()
kClient, err := kubernetes.New([]byte(config))
if err != nil {
return st, ErrAddonFromTemplate(err)
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
return
}

content, err := utils.ReadFileSource(patch)
err = kClient.ApplyHelmChart(kubernetes.ApplyHelmChartConfig{
URL: helmChartURL,
Namespace: ns,
})
if err != nil {
return st, ErrAddonFromTemplate(err)
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
return
}

_, err = appMesh.KubeClient.CoreV1().Services(ns).Patch(context.TODO(), svcName, types.MergePatchType, []byte(content), metav1.PatchOptions{})
if err != nil {
return st, ErrAddonFromTemplate(err)
for _, patch := range patches {
if !del {
_, err := url.ParseRequestURI(patch)
if err != nil {
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
return
}

content, err := utils.ReadFileSource(patch)
if err != nil {
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
return
}

_, err = kClient.KubeClient.CoreV1().Services(ns).Patch(context.TODO(), svcName, types.MergePatchType, []byte(content), metav1.PatchOptions{})
if err != nil {
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
return
}
}
}
}
}(config)
}

wg.Wait()
if len(errs) == 0 {
return st, nil
}
return st, nil

return st, ErrAddonFromHelm(mergeErrors(errs))
}
28 changes: 16 additions & 12 deletions appmesh/app_mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ func New(c meshkitCfg.Handler, l logger.Handler, kc meshkitCfg.Handler) adapter.
Adapter: adapter.Adapter{
Config: c,
Log: l,
KubeconfigHandler: kc,
},
}
}

// ApplyOperation applies the requested operation on app-mesh
func (appMesh *AppMesh) ApplyOperation(ctx context.Context, opReq adapter.OperationRequest) error {
func (appMesh *AppMesh) ApplyOperation(ctx context.Context, opReq adapter.OperationRequest, hchan *chan interface{}) error {
kubeConfigs := opReq.K8sConfigs
appMesh.SetChannel(hchan);

operations := make(adapter.Operations)
err := appMesh.Config.GetObject(adapter.OperationsKey, &operations)
Expand All @@ -51,7 +52,7 @@ func (appMesh *AppMesh) ApplyOperation(ctx context.Context, opReq adapter.Operat
case internalconfig.AppMeshOperation:
go func(hh *AppMesh, ee *adapter.Event) {
version := string(operations[opReq.OperationName].Versions[0])
if stat, err = hh.installAppMesh(opReq.IsDeleteOperation, version, opReq.Namespace); err != nil {
if stat, err = hh.installAppMesh(opReq.IsDeleteOperation, version, opReq.Namespace, kubeConfigs); err != nil {
e.Summary = fmt.Sprintf("Error while %s AWS App mesh", stat)
e.Details = err.Error()
hh.StreamErr(e, err)
Expand All @@ -64,7 +65,7 @@ func (appMesh *AppMesh) ApplyOperation(ctx context.Context, opReq adapter.Operat

case internalconfig.LabelNamespace:
go func(hh *AppMesh, ee *adapter.Event) {
err := hh.LoadNamespaceToMesh(opReq.Namespace, opReq.IsDeleteOperation)
err := hh.LoadNamespaceToMesh(opReq.Namespace, opReq.IsDeleteOperation, kubeConfigs)
operation := "enabled"
if opReq.IsDeleteOperation {
operation = "removed"
Expand All @@ -85,7 +86,7 @@ func (appMesh *AppMesh) ApplyOperation(ctx context.Context, opReq adapter.Operat
helmChartURL := operations[opReq.OperationName].AdditionalProperties[internalconfig.HelmChartURL]
patches := make([]string, 0)
patches = append(patches, operations[opReq.OperationName].AdditionalProperties[internalconfig.ServicePatchFile])
_, err := hh.installAddon(opReq.Namespace, opReq.IsDeleteOperation, svcname, patches, helmChartURL)
_, err := hh.installAddon(opReq.Namespace, opReq.IsDeleteOperation, svcname, patches, helmChartURL, kubeConfigs)
operation := "install"
if opReq.IsDeleteOperation {
operation = "uninstall"
Expand All @@ -104,7 +105,7 @@ func (appMesh *AppMesh) ApplyOperation(ctx context.Context, opReq adapter.Operat
case common.BookInfoOperation, common.HTTPBinOperation, common.ImageHubOperation, common.EmojiVotoOperation:
go func(hh *AppMesh, ee *adapter.Event) {
appName := operations[opReq.OperationName].AdditionalProperties[common.ServiceName]
stat, err := hh.installSampleApp(opReq.Namespace, opReq.IsDeleteOperation, operations[opReq.OperationName].Templates)
stat, err := hh.installSampleApp(opReq.Namespace, opReq.IsDeleteOperation, operations[opReq.OperationName].Templates, kubeConfigs)
if err != nil {
e.Summary = fmt.Sprintf("Error while %s %s application", stat, appName)
e.Details = err.Error()
Expand All @@ -118,7 +119,7 @@ func (appMesh *AppMesh) ApplyOperation(ctx context.Context, opReq adapter.Operat

case common.CustomOperation:
go func(hh *AppMesh, ee *adapter.Event) {
stat, err := hh.applyCustomOperation(opReq.Namespace, opReq.CustomBody, opReq.IsDeleteOperation)
stat, err := hh.applyCustomOperation(opReq.Namespace, opReq.CustomBody, opReq.IsDeleteOperation, kubeConfigs)
if err != nil {
e.Summary = fmt.Sprintf("Error while %s custom operation", stat)
e.Details = err.Error()
Expand All @@ -137,7 +138,10 @@ func (appMesh *AppMesh) ApplyOperation(ctx context.Context, opReq adapter.Operat
}

// ProcessOAM handles the grpc invocation for handling OAM objects
func (appMesh *AppMesh) ProcessOAM(ctx context.Context, oamReq adapter.OAMRequest) (string, error) {
func (appMesh *AppMesh) ProcessOAM(ctx context.Context, oamReq adapter.OAMRequest, hchan *chan interface{}) (string, error) {
appMesh.SetChannel(hchan)
kubeConfigs := oamReq.K8sConfigs

var comps []v1alpha1.Component
for _, acomp := range oamReq.OamComps {
comp, err := oam.ParseApplicationComponent(acomp)
Expand All @@ -157,13 +161,13 @@ func (appMesh *AppMesh) ProcessOAM(ctx context.Context, oamReq adapter.OAMReques
// If operation is delete then first HandleConfiguration and then handle the deployment
if oamReq.DeleteOp {
// Process configuration
msg2, err := appMesh.HandleApplicationConfiguration(config, oamReq.DeleteOp)
msg2, err := appMesh.HandleApplicationConfiguration(config, oamReq.DeleteOp, kubeConfigs)
if err != nil {
return msg2, ErrProcessOAM(err)
}

// Process components
msg1, err := appMesh.HandleComponents(comps, oamReq.DeleteOp)
msg1, err := appMesh.HandleComponents(comps, oamReq.DeleteOp, kubeConfigs)
if err != nil {
return msg1 + "\n" + msg2, ErrProcessOAM(err)
}
Expand All @@ -172,13 +176,13 @@ func (appMesh *AppMesh) ProcessOAM(ctx context.Context, oamReq adapter.OAMReques
}

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

// Process configuration
msg2, err := appMesh.HandleApplicationConfiguration(config, oamReq.DeleteOp)
msg2, err := appMesh.HandleApplicationConfiguration(config, oamReq.DeleteOp, kubeConfigs)
if err != nil {
return msg1 + "\n" + msg2, ErrProcessOAM(err)
}
Expand Down
4 changes: 2 additions & 2 deletions appmesh/custom_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"github.com/layer5io/meshery-adapter-library/status"
)

func (appMesh *AppMesh) applyCustomOperation(namespace string, manifest string, isDel bool) (string, error) {
func (appMesh *AppMesh) applyCustomOperation(namespace string, manifest string, isDel bool, kubeclients []string) (string, error) {
st := status.Starting

err := appMesh.applyManifest([]byte(manifest), isDel, namespace)
err := appMesh.applyManifest([]byte(manifest), isDel, namespace, kubeclients)
if err != nil {
return st, ErrCustomOperation(err)
}
Expand Down
Loading

0 comments on commit 6d47854

Please sign in to comment.