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

Commit ec8cb8d

Browse files
authored
Merge pull request #152 from Revolyssup/multicontext
Multi context refactor
2 parents bccaa43 + d5cf7be commit ec8cb8d

File tree

7 files changed

+226
-101
lines changed

7 files changed

+226
-101
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ replace (
99
)
1010

1111
require (
12-
github.com/layer5io/meshery-adapter-library v0.5.4
13-
github.com/layer5io/meshkit v0.5.19
12+
github.com/layer5io/meshery-adapter-library v0.5.6
13+
github.com/layer5io/meshkit v0.5.20
1414
github.com/layer5io/service-mesh-performance v0.3.4
1515
gopkg.in/yaml.v2 v2.4.0
1616
k8s.io/apimachinery v0.23.5

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -885,11 +885,11 @@ github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6Fm
885885
github.com/layer5io/kuttl v0.4.1-0.20200806180306-b7e46afd657f/go.mod h1:UmrVd7x+bNVKrpmKgTtfRiTKHZeNPcMjQproJ0vGwhE=
886886
github.com/layer5io/learn-layer5/smi-conformance v0.0.0-20210317075357-06b4f88b3e34 h1:QaViadDOBCMDUwYx78kfRvHMkzRVnh/GOhm3s2gxoP4=
887887
github.com/layer5io/learn-layer5/smi-conformance v0.0.0-20210317075357-06b4f88b3e34/go.mod h1:BQPLwdJt7v7y0fXIejI4whR9zMyX07Wjt5xrbgEmHLw=
888-
github.com/layer5io/meshery-adapter-library v0.5.4 h1:QQ+nVGHd7KhV58KhY40V00kC+IEM4+AlOhQcSHSbOUE=
889-
github.com/layer5io/meshery-adapter-library v0.5.4/go.mod h1:YmLV0w6ucBagrqUB0x9q8ZVXrhN1tJBP5j+Pu6LOY/M=
888+
github.com/layer5io/meshery-adapter-library v0.5.6 h1:pbZTMkWNcGWPk314K7WhO4UGVxSnKvGLmwQXBWZ05GI=
889+
github.com/layer5io/meshery-adapter-library v0.5.6/go.mod h1:YmLV0w6ucBagrqUB0x9q8ZVXrhN1tJBP5j+Pu6LOY/M=
890890
github.com/layer5io/meshkit v0.5.16/go.mod h1:tj5TAjty7T/WJ8YvlDfOZF94t4g3mhWuKBCc6MOUoNU=
891-
github.com/layer5io/meshkit v0.5.19 h1:pUCmbekifyOIvec1p1WjFa9hDrlqbwALKJ3VqpOXEKQ=
892-
github.com/layer5io/meshkit v0.5.19/go.mod h1:EUfXIcztap9Dh0Ao3Dmoxf3FMsm4h7zFHGwagj+5ra4=
891+
github.com/layer5io/meshkit v0.5.20 h1:QpN/SEepUZk+Jj2K4TBRZJCRr/pzuvHqDaUr30vWddI=
892+
github.com/layer5io/meshkit v0.5.20/go.mod h1:EUfXIcztap9Dh0Ao3Dmoxf3FMsm4h7zFHGwagj+5ra4=
893893
github.com/layer5io/service-mesh-performance v0.3.2-0.20210122142912-a94e0658b021/go.mod h1:W153amv8aHAeIWxO7b7d7Vibt9RhaEVh4Uh+RG+BumQ=
894894
github.com/layer5io/service-mesh-performance v0.3.4 h1:aw/elsx0wkry7SyiQRIj31wW7TPCP4YfhINdNOLXVg8=
895895
github.com/layer5io/service-mesh-performance v0.3.4/go.mod h1:W153amv8aHAeIWxO7b7d7Vibt9RhaEVh4Uh+RG+BumQ=

osm/install.go

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ package osm
1616

1717
import (
1818
"fmt"
19+
"sync"
1920

2021
"github.com/layer5io/meshery-adapter-library/adapter"
2122
"github.com/layer5io/meshery-adapter-library/status"
2223
mesherykube "github.com/layer5io/meshkit/utils/kubernetes"
2324
)
2425

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

4041
h.Log.Info("Installing...")
41-
err = h.applyHelmChart(del, version, ns)
42+
err = h.applyHelmChart(del, version, ns, kubeconfigs)
4243
if err != nil {
4344
return st, ErrApplyHelmChart(err)
4445
}
@@ -51,8 +52,7 @@ func (h *Handler) installOSM(del bool, version, ns string) (string, error) {
5152
return st, nil
5253
}
5354

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

5757
repo := "https://openservicemesh.github.io/osm/"
5858
chart := "osm"
@@ -62,14 +62,41 @@ func (h *Handler) applyHelmChart(del bool, version, namespace string) error {
6262
} else {
6363
act = mesherykube.INSTALL
6464
}
65-
return kClient.ApplyHelmChart(mesherykube.ApplyHelmChartConfig{
66-
ChartLocation: mesherykube.HelmChartLocation{
67-
Repository: repo,
68-
Chart: chart,
69-
Version: version,
70-
},
71-
Namespace: namespace,
72-
Action: act,
73-
CreateNamespace: true,
74-
})
65+
var wg sync.WaitGroup
66+
var errMx sync.Mutex
67+
var errs []error
68+
for _, kubeconfig := range kubeconfigs {
69+
wg.Add(1)
70+
go func(kubeconfig string) {
71+
defer wg.Done()
72+
kClient, err := mesherykube.New([]byte(kubeconfig))
73+
if err != nil {
74+
errMx.Lock()
75+
errs = append(errs, err)
76+
errMx.Unlock()
77+
return
78+
}
79+
err = kClient.ApplyHelmChart(mesherykube.ApplyHelmChartConfig{
80+
ChartLocation: mesherykube.HelmChartLocation{
81+
Repository: repo,
82+
Chart: chart,
83+
Version: version,
84+
},
85+
Namespace: namespace,
86+
Action: act,
87+
CreateNamespace: true,
88+
})
89+
if err != nil {
90+
errMx.Lock()
91+
errs = append(errs, err)
92+
errMx.Unlock()
93+
return
94+
}
95+
}(kubeconfig)
96+
}
97+
wg.Wait()
98+
if len(errs) != 0 {
99+
return mergeErrors(errs)
100+
}
101+
return nil
75102
}

osm/oam.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
)
1010

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

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

@@ -23,7 +23,7 @@ func (h *Handler) HandleComponents(comps []v1alpha1.Component, isDel bool) (stri
2323
for _, comp := range comps {
2424
fnc, ok := compFuncMap[comp.Spec.Type]
2525
if !ok {
26-
msg, err := handleOSMCoreComponent(h, comp, isDel, "", "")
26+
msg, err := handleOSMCoreComponent(h, comp, isDel, "", "", kubeconfigs)
2727
if err != nil {
2828
errs = append(errs, err)
2929
continue
@@ -33,7 +33,7 @@ func (h *Handler) HandleComponents(comps []v1alpha1.Component, isDel bool) (stri
3333
continue
3434
}
3535

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

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

7575
}
7676

77-
func handleNamespaceLabel(h *Handler, namespaces []string, isDel bool) error {
77+
func handleNamespaceLabel(h *Handler, namespaces []string, isDel bool, kubeconfigs []string) error {
7878
var errs []error
7979
for _, ns := range namespaces {
80-
if err := h.sidecarInjection(ns, isDel); err != nil {
80+
if err := h.sidecarInjection(ns, isDel, kubeconfigs); err != nil {
8181
errs = append(errs, err)
8282
}
8383
}
8484

8585
return mergeErrors(errs)
8686
}
8787

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

94-
msg, err := h.installOSM(isDel, version, comp.Namespace)
94+
msg, err := h.installOSM(isDel, version, comp.Namespace, kubeconfigs)
9595
if err != nil {
9696
return fmt.Sprintf("%s: %s", comp.Name, msg), err
9797
}
@@ -104,7 +104,8 @@ func handleOSMCoreComponent(
104104
comp v1alpha1.Component,
105105
isDel bool,
106106
apiVersion,
107-
kind string) (string, error) {
107+
kind string,
108+
kubeconfigs []string) (string, error) {
108109
if apiVersion == "" {
109110
apiVersion = getAPIVersionFromComponent(comp)
110111
if apiVersion == "" {
@@ -143,7 +144,7 @@ func handleOSMCoreComponent(
143144
msg = fmt.Sprintf("deleted %s config \"%s\" in namespace \"%s\"", kind, comp.Name, comp.Namespace)
144145
}
145146

146-
return msg, h.applyManifest(yamlByt, isDel, comp.Namespace)
147+
return msg, h.applyManifest(yamlByt, isDel, comp.Namespace, kubeconfigs)
147148
}
148149

149150
func getAPIVersionFromComponent(comp v1alpha1.Component) string {

osm/operations.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ import (
1111
)
1212

1313
// ApplyOperation function contains the operation handlers
14-
func (h *Handler) ApplyOperation(ctx context.Context, request adapter.OperationRequest) error {
14+
func (h *Handler) ApplyOperation(ctx context.Context, request adapter.OperationRequest, hchan *chan interface{}) error {
15+
err := h.CreateKubeconfigs(request.K8sConfigs)
16+
if err != nil {
17+
return err
18+
}
19+
h.SetChannel(hchan)
20+
kubeconfigs := request.K8sConfigs
1521
operations := make(adapter.Operations)
16-
err := h.Config.GetObject(adapter.OperationsKey, &operations)
22+
err = h.Config.GetObject(adapter.OperationsKey, &operations)
1723
if err != nil {
1824
return err
1925
}
@@ -29,7 +35,7 @@ func (h *Handler) ApplyOperation(ctx context.Context, request adapter.OperationR
2935
case internalconfig.OSMOperation:
3036
go func(hh *Handler, ee *adapter.Event) {
3137
version := string(operations[request.OperationName].Versions[0])
32-
stat, err := hh.installOSM(request.IsDeleteOperation, version, request.Namespace)
38+
stat, err := hh.installOSM(request.IsDeleteOperation, version, request.Namespace, kubeconfigs)
3339
if err != nil {
3440
e.Summary = fmt.Sprintf("Error while %s Open service mesh", stat)
3541
e.Details = err.Error()
@@ -47,7 +53,7 @@ func (h *Handler) ApplyOperation(ctx context.Context, request adapter.OperationR
4753
common.EmojiVotoOperation:
4854
go func(hh *Handler, ee *adapter.Event) {
4955
appName := operations[request.OperationName].AdditionalProperties[common.ServiceName]
50-
stat, err := hh.installSampleApp(request.IsDeleteOperation, request.Namespace, operations[request.OperationName].Templates)
56+
stat, err := hh.installSampleApp(request.IsDeleteOperation, request.Namespace, operations[request.OperationName].Templates, kubeconfigs)
5157
if err != nil {
5258
e.Summary = fmt.Sprintf("Error while %s %s application", stat, appName)
5359
e.Details = err.Error()
@@ -66,6 +72,7 @@ func (h *Handler) ApplyOperation(ctx context.Context, request adapter.OperationR
6672
request.IsDeleteOperation,
6773
version,
6874
operations[request.OperationName].Templates,
75+
kubeconfigs,
6976
)
7077
if err != nil {
7178
e.Summary = fmt.Sprintf("Error while %s %s application", stat, appName)

osm/osm.go

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import (
2121
"github.com/layer5io/meshery-osm/osm/oam"
2222
meshkitCfg "github.com/layer5io/meshkit/config"
2323
"github.com/layer5io/meshkit/logger"
24+
"github.com/layer5io/meshkit/models"
2425
"github.com/layer5io/meshkit/models/oam/core/v1alpha1"
26+
"gopkg.in/yaml.v2"
2527
)
2628

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

45+
//CreateKubeconfigs creates and writes passed kubeconfig onto the filesystem
46+
func (h *Handler) CreateKubeconfigs(kubeconfigs []string) error {
47+
var errs = make([]error, 0)
48+
for _, kubeconfig := range kubeconfigs {
49+
kconfig := models.Kubeconfig{}
50+
err := yaml.Unmarshal([]byte(kubeconfig), &kconfig)
51+
if err != nil {
52+
errs = append(errs, err)
53+
continue
54+
}
55+
56+
// To have control over what exactly to take in on kubeconfig
57+
h.KubeconfigHandler.SetKey("kind", kconfig.Kind)
58+
h.KubeconfigHandler.SetKey("apiVersion", kconfig.APIVersion)
59+
h.KubeconfigHandler.SetKey("current-context", kconfig.CurrentContext)
60+
err = h.KubeconfigHandler.SetObject("preferences", kconfig.Preferences)
61+
if err != nil {
62+
errs = append(errs, err)
63+
continue
64+
}
65+
66+
err = h.KubeconfigHandler.SetObject("clusters", kconfig.Clusters)
67+
if err != nil {
68+
errs = append(errs, err)
69+
continue
70+
}
71+
72+
err = h.KubeconfigHandler.SetObject("users", kconfig.Users)
73+
if err != nil {
74+
errs = append(errs, err)
75+
continue
76+
}
77+
78+
err = h.KubeconfigHandler.SetObject("contexts", kconfig.Contexts)
79+
if err != nil {
80+
errs = append(errs, err)
81+
continue
82+
}
83+
}
84+
if len(errs) == 0 {
85+
return nil
86+
}
87+
return mergeErrors(errs)
88+
}
89+
4390
// ProcessOAM will handles the grpc invocation for handling OAM objects
44-
func (h *Handler) ProcessOAM(ctx context.Context, oamReq adapter.OAMRequest) (string, error) {
91+
func (h *Handler) ProcessOAM(ctx context.Context, oamReq adapter.OAMRequest, hchan *chan interface{}) (string, error) {
92+
err := h.CreateKubeconfigs(oamReq.K8sConfigs)
93+
if err != nil {
94+
return "", err
95+
}
96+
kubeconfigs := oamReq.K8sConfigs
97+
h.SetChannel(hchan)
4598
var comps []v1alpha1.Component
4699
for _, acomp := range oamReq.OamComps {
47100
comp, err := oam.ParseApplicationComponent(acomp)
@@ -61,13 +114,13 @@ func (h *Handler) ProcessOAM(ctx context.Context, oamReq adapter.OAMRequest) (st
61114
// If operation is delete then first HandleConfiguration and then handle the deployment
62115
if oamReq.DeleteOp {
63116
// Process configuration
64-
msg2, err := h.HandleApplicationConfiguration(config, oamReq.DeleteOp)
117+
msg2, err := h.HandleApplicationConfiguration(config, oamReq.DeleteOp, kubeconfigs)
65118
if err != nil {
66119
return msg2, ErrProcessOAM(err)
67120
}
68121

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

78131
// Process components
79-
msg1, err := h.HandleComponents(comps, oamReq.DeleteOp)
132+
msg1, err := h.HandleComponents(comps, oamReq.DeleteOp, kubeconfigs)
80133
if err != nil {
81134
return msg1, ErrProcessOAM(err)
82135
}
83136

84137
// Process configuration
85-
msg2, err := h.HandleApplicationConfiguration(config, oamReq.DeleteOp)
138+
msg2, err := h.HandleApplicationConfiguration(config, oamReq.DeleteOp, kubeconfigs)
86139
if err != nil {
87140
return msg1 + "\n" + msg2, ErrProcessOAM(err)
88141
}

0 commit comments

Comments
 (0)