Skip to content

Commit

Permalink
Merge pull request #430 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 e6160e5 + 24ffd57 commit 42fa4e7
Show file tree
Hide file tree
Showing 36 changed files with 686 additions and 375 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ replace (

require (
github.com/aspenmesh/istio-vet v0.0.0-20200806222806-9c8e9a962b9f
github.com/layer5io/meshery-adapter-library v0.5.4
github.com/layer5io/meshery-adapter-library v0.5.5
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
98 changes: 96 additions & 2 deletions go.sum

Large diffs are not rendered by default.

95 changes: 61 additions & 34 deletions istio/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"fmt"
"net/url"
"strings"
"sync"

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

mesherykube "github.com/layer5io/meshkit/utils/kubernetes"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
Expand All @@ -18,7 +19,7 @@ import (
//
// the template defines the manifest's link/location which needs to be used to
// install the addon
func (istio *Istio) installAddon(namespace string, del bool, service string, patches []string, templates []adapter.Template) (string, error) {
func (istio *Istio) installAddon(namespace string, del bool, service string, patches []string, templates []adapter.Template, kubeconfigs []string) (string, error) {
st := status.Installing

if del {
Expand All @@ -27,42 +28,68 @@ func (istio *Istio) installAddon(namespace string, del bool, service string, pat

istio.Log.Debug(fmt.Sprintf("Overidden namespace: %s", namespace))
namespace = "istio-system"

for _, template := range templates {
if istio.KubeClient == nil {
return st, ErrNilClient
}
err := istio.applyManifest([]byte(template.String()), del, namespace)
// Specifically choosing to ignore kiali dashboard's error.
// Referring to: https://github.com/kiali/kiali/issues/3112
if err != nil && !strings.Contains(err.Error(), "no matches for kind \"MonitoringDashboard\" in version \"monitoring.kiali.io/v1alpha1\"") {
if !strings.Contains(err.Error(), "clusterIP") {
return st, ErrAddonFromTemplate(err)
}
}
}

for _, patch := range patches {
if patch == "" {
continue //avoid throwing error when a given patch key didn't exist for a specific addon type in operations
}
if !del {
_, err := url.ParseRequestURI(patch)
var wg sync.WaitGroup
var errMx sync.Mutex
var errs []error
for _, k8sconfig := range kubeconfigs {
wg.Add(1)
go func(k8sconfig string) {
defer wg.Done()
mclient, err := mesherykube.New([]byte(k8sconfig))
if err != nil {
return st, ErrAddonFromTemplate(err)
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
return
}

content, err := utils.ReadFileSource(patch)
if err != nil {
return st, ErrAddonFromTemplate(err)
for _, template := range templates {
err := istio.applyManifestOnSingleCluster([]byte(template.String()), del, namespace, mclient)
// Specifically choosing to ignore kiali dashboard's error.
// Referring to: https://github.com/kiali/kiali/issues/3112
if err != nil && !strings.Contains(err.Error(), "no matches for kind \"MonitoringDashboard\" in version \"monitoring.kiali.io/v1alpha1\"") {
if !strings.Contains(err.Error(), "clusterIP") {
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
}
}
}

_, err = istio.KubeClient.CoreV1().Services(namespace).Patch(context.TODO(), service, types.MergePatchType, []byte(content), metav1.PatchOptions{})
if err != nil {
return st, ErrAddonFromTemplate(err)
for _, patch := range patches {
if patch == "" {
continue //avoid throwing error when a given patch key didn't exist for a specific addon type in operations
}
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 = mclient.KubeClient.CoreV1().Services(namespace).Patch(context.TODO(), service, types.MergePatchType, []byte(content), metav1.PatchOptions{})
if err != nil {
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
return
}
}
}
}
}(k8sconfig)
}

return status.Installed, nil
wg.Wait()
if len(errs) == 0 {
return status.Installed, nil
}
return st, ErrAddonFromTemplate(mergeErrors(errs))
}
20 changes: 10 additions & 10 deletions istio/addons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ func TestIstio_installAddon(t *testing.T) {
ch := make(chan interface{}, 10)
fs := fields{
Adapter: adapter.Adapter{
Config: getConfigHandler(t),
Log: getLoggerHandler(t),
KubeconfigHandler: getKubeconfigHandler(t),
Channel: &ch,
Config: getConfigHandler(t),
Log: getLoggerHandler(t),
Channel: &ch,
},
}

tests := []struct {
name string
fields fields
args args
want string
wantErr bool
name string
fields fields
args args
want string
kubeconfigs []string
wantErr bool
}{
// TODO: Add test cases.
{
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestIstio_installAddon(t *testing.T) {
istio := &Istio{
Adapter: tt.fields.Adapter,
}
got, err := istio.installAddon(tt.args.namespace, tt.args.del, tt.args.service, tt.args.patches, tt.args.templates)
got, err := istio.installAddon(tt.args.namespace, tt.args.del, tt.args.service, tt.args.patches, tt.args.templates, tt.kubeconfigs)
if (err != nil) != tt.wantErr {
t.Errorf("Istio.installAddon() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
7 changes: 2 additions & 5 deletions istio/custom_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import (
"github.com/layer5io/meshery-adapter-library/status"
)

func (istio *Istio) applyCustomOperation(namespace string, manifest string, isDel bool) (string, error) {
func (istio *Istio) applyCustomOperation(namespace string, manifest string, isDel bool, kubeconfigs []string) (string, error) {
st := status.Starting
if istio.KubeClient == nil {
return st, ErrNilClient
}

err := istio.applyManifest([]byte(manifest), isDel, namespace)
err := istio.applyManifest([]byte(manifest), isDel, namespace, kubeconfigs)
if err != nil {
return st, ErrCustomOperation(err)
}
Expand Down
20 changes: 10 additions & 10 deletions istio/custom_operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ func TestIstio_applyCustomOperation(t *testing.T) {
ch := make(chan interface{}, 10)
fs := fields{
Adapter: adapter.Adapter{
Config: getConfigHandler(t),
Log: getLoggerHandler(t),
KubeconfigHandler: getKubeconfigHandler(t),
Channel: &ch,
Config: getConfigHandler(t),
Log: getLoggerHandler(t),
Channel: &ch,
},
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
name string
fields fields
args args
kubeconfigs []string
want string
wantErr bool
}{
// TODO: Add test cases.
{
Expand All @@ -52,7 +52,7 @@ func TestIstio_applyCustomOperation(t *testing.T) {
istio := &Istio{
Adapter: tt.fields.Adapter,
}
got, err := istio.applyCustomOperation(tt.args.namespace, tt.args.manifest, tt.args.isDel)
got, err := istio.applyCustomOperation(tt.args.namespace, tt.args.manifest, tt.args.isDel, tt.kubeconfigs)
if (err != nil) != tt.wantErr {
t.Errorf("Istio.applyCustomOperation() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
Loading

0 comments on commit 42fa4e7

Please sign in to comment.