Skip to content

Commit

Permalink
Merge pull request #126 from gr455/mcr
Browse files Browse the repository at this point in the history
Multi Context Refactor
  • Loading branch information
Revolyssup authored Jun 12, 2022
2 parents 4a98ea7 + 9f4b709 commit ec61aab
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 88 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ replace (
)

require (
github.com/layer5io/meshery-adapter-library v0.5.4
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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -882,8 +882,8 @@ github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6Fm
github.com/layer5io/kuttl v0.4.1-0.20200723152044-916f10574334/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.20 h1:QpN/SEepUZk+Jj2K4TBRZJCRr/pzuvHqDaUr30vWddI=
github.com/layer5io/meshkit v0.5.20/go.mod h1:EUfXIcztap9Dh0Ao3Dmoxf3FMsm4h7zFHGwagj+5ra4=
Expand Down
4 changes: 2 additions & 2 deletions nginx/custom_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"github.com/layer5io/meshery-adapter-library/status"
)

func (nginx *Nginx) applyCustomOperation(namespace string, manifest string, isDel bool) (string, error) {
func (nginx *Nginx) applyCustomOperation(namespace string, manifest string, isDel bool, kubeconfigs []string) (string, error) {
st := status.Starting

err := nginx.applyManifest([]byte(manifest), isDel, namespace)
err := nginx.applyManifest([]byte(manifest), isDel, namespace, kubeconfigs)
if err != nil {
return st, ErrCustomOperation(err)
}
Expand Down
2 changes: 1 addition & 1 deletion nginx/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func ErrParseNginxCoreComponent(err error) error {
}

// ErrLoadNamespace is the occurend while applying namespace
func ErrLoadNamespace(err error, s string) error {
func ErrLoadNamespace(err error) error {
return errors.New(ErrLoadNamespaceCode, errors.Alert, []string{"Error occured while applying namespace "}, []string{err.Error()}, []string{"Trying to access a namespace which is not available"}, []string{"Verify presence of namespace. Confirm Meshery ServiceAccount permissions"})

}
94 changes: 72 additions & 22 deletions nginx/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nginx
import (
"fmt"
"strings"
"sync"

"github.com/layer5io/meshery-adapter-library/adapter"
"github.com/layer5io/meshery-adapter-library/status"
Expand All @@ -17,7 +18,7 @@ const (

// Installs NGINX service mesh using helm charts.
// Unlike other adapters, doesn't keep CLI as a fallback method
func (nginx *Nginx) installNginx(del bool, version, namespace string) (string, error) {
func (nginx *Nginx) installNginx(del bool, version, namespace string, kubeconfigs []string) (string, error) {
nginx.Log.Debug(fmt.Sprintf("Requested install of version: %s", version))
nginx.Log.Debug(fmt.Sprintf("Requested action is delete: %v", del))
nginx.Log.Debug(fmt.Sprintf("Requested action is in namespace: %s", namespace))
Expand All @@ -32,7 +33,7 @@ func (nginx *Nginx) installNginx(del bool, version, namespace string) (string, e
return st, ErrMeshConfig(err)
}

err = nginx.applyHelmChart(del, version, namespace)
err = nginx.applyHelmChart(del, version, namespace, kubeconfigs)
if err != nil {
nginx.Log.Error(ErrInstallNginx(err))
return st, ErrInstallNginx(err)
Expand All @@ -44,12 +45,7 @@ func (nginx *Nginx) installNginx(del bool, version, namespace string) (string, e
return status.Installed, nil
}

func (nginx *Nginx) applyHelmChart(del bool, version, namespace string) error {
kClient := nginx.MesheryKubeclient
if kClient == nil {
return ErrNilClient
}

func (nginx *Nginx) applyHelmChart(del bool, version, namespace string, kubeconfigs []string) error {
chartVersion, err := mesherykube.HelmAppVersionToChartVersion(repo, chart, version)
if err != nil {
version = strings.TrimPrefix(version, "v")
Expand Down Expand Up @@ -99,25 +95,79 @@ func (nginx *Nginx) applyHelmChart(del bool, version, namespace string) error {
OverrideValues: overrideVal,
}

// Install Helm chart.
nginx.Log.Info(fmt.Sprintf("Installing NGINX Service Mesh %s using Helm chart: %+v\n", version, c))
err = kClient.ApplyHelmChart(c)
if err != nil {
return ErrApplyHelmChart(err)

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 := mesherykube.New([]byte(config))
if err != nil {
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
return
}

// Install Helm chart.
err = kClient.ApplyHelmChart(c)
if err != nil {
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
return
}
}(config)
}
wg.Wait()
if len(errs) == 0 {
return nil
}

return nil
mergedErrors := mergeErrors(errs)
return ErrApplyHelmChart(mergedErrors)
}

func (nginx *Nginx) applyManifest(manifest []byte, isDel bool, namespace string) error {
err := nginx.MesheryKubeclient.ApplyManifest(manifest, mesherykube.ApplyOptions{
Namespace: namespace,
Update: true,
Delete: isDel,
})
if err != nil {
return err
func (nginx *Nginx) applyManifest(manifest []byte, isDel bool, namespace string, kubeconfigs []string) error {
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 := mesherykube.New([]byte(config))
if err != nil {
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
return
}

err = kClient.ApplyManifest(manifest, mesherykube.ApplyOptions{
Namespace: namespace,
Update: true,
Delete: isDel,
})
if err != nil {
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
return
}

}(config)
}

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

return nil
return mergeErrors(errs)
}
65 changes: 59 additions & 6 deletions nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/layer5io/meshery-adapter-library/status"
internalconfig "github.com/layer5io/meshery-nginx/internal/config"
"github.com/layer5io/meshkit/logger"
"github.com/layer5io/meshkit/models"
"gopkg.in/yaml.v2"
)

// Nginx defines a model for this adapter
Expand All @@ -29,10 +31,16 @@ func New(c adapterconfig.Handler, l logger.Handler, kc adapterconfig.Handler) ad
}

// ApplyOperation applies the operation on nginx
func (nginx *Nginx) ApplyOperation(ctx context.Context, opReq adapter.OperationRequest) error {
func (nginx *Nginx) ApplyOperation(ctx context.Context, opReq adapter.OperationRequest, hchan *chan interface{}) error {
err := nginx.CreateKubeconfigs(opReq.K8sConfigs)
if err != nil {
return err
}
kubeConfigs := opReq.K8sConfigs
nginx.SetChannel(hchan);

operations := make(adapter.Operations)
err := nginx.Config.GetObject(adapter.OperationsKey, &operations)
err = nginx.Config.GetObject(adapter.OperationsKey, &operations)
if err != nil {
return err
}
Expand All @@ -49,7 +57,7 @@ func (nginx *Nginx) ApplyOperation(ctx context.Context, opReq adapter.OperationR
case internalconfig.NginxOperation:
go func(hh *Nginx, ee *adapter.Event) {
version := string(operations[opReq.OperationName].Versions[0])
if stat, err = hh.installNginx(opReq.IsDeleteOperation, version, opReq.Namespace); err != nil {
if stat, err = hh.installNginx(opReq.IsDeleteOperation, version, opReq.Namespace, kubeConfigs); err != nil {
e.Summary = fmt.Sprintf("Error while %s NGINX Service Mesh", stat)
e.Details = err.Error()
hh.StreamErr(e, err)
Expand All @@ -61,7 +69,7 @@ func (nginx *Nginx) ApplyOperation(ctx context.Context, opReq adapter.OperationR
}(nginx, e)
case internalconfig.LabelNamespace:
go func(hh *Nginx, 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 Down Expand Up @@ -102,7 +110,7 @@ func (nginx *Nginx) ApplyOperation(ctx context.Context, opReq adapter.OperationR
case common.BookInfoOperation, common.HTTPBinOperation, common.ImageHubOperation, common.EmojiVotoOperation:
go func(hh *Nginx, 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 @@ -115,7 +123,7 @@ func (nginx *Nginx) ApplyOperation(ctx context.Context, opReq adapter.OperationR
}(nginx, e)
case common.CustomOperation:
go func(hh *Nginx, 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 @@ -132,3 +140,48 @@ func (nginx *Nginx) ApplyOperation(ctx context.Context, opReq adapter.OperationR

return nil
}

//CreateKubeconfigs creates and writes passed kubeconfig onto the filesystem
func (nginx *Nginx) 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
nginx.KubeconfigHandler.SetKey("kind", kconfig.Kind)
nginx.KubeconfigHandler.SetKey("apiVersion", kconfig.APIVersion)
nginx.KubeconfigHandler.SetKey("current-context", kconfig.CurrentContext)
err = nginx.KubeconfigHandler.SetObject("preferences", kconfig.Preferences)
if err != nil {
errs = append(errs, err)
continue
}

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

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

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

0 comments on commit ec61aab

Please sign in to comment.