diff --git a/appmesh/app_mesh.go b/appmesh/app_mesh.go index 5219d4d..6ff9f6b 100644 --- a/appmesh/app_mesh.go +++ b/appmesh/app_mesh.go @@ -12,7 +12,9 @@ import ( internalconfig "github.com/layer5io/meshery-app-mesh/internal/config" 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" ) // AppMesh is the app-mesh adapter. It embeds adapter.Adapter @@ -26,17 +28,22 @@ 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, hchan *chan interface{}) error { + err := appMesh.CreateKubeconfigs(opReq.K8sConfigs) + if err != nil { + return err + } kubeConfigs := opReq.K8sConfigs appMesh.SetChannel(hchan); operations := make(adapter.Operations) - err := appMesh.Config.GetObject(adapter.OperationsKey, &operations) + err = appMesh.Config.GetObject(adapter.OperationsKey, &operations) if err != nil { return err } @@ -137,9 +144,58 @@ func (appMesh *AppMesh) ApplyOperation(ctx context.Context, opReq adapter.Operat return nil } +//CreateKubeconfigs creates and writes passed kubeconfig onto the filesystem +func (appMesh *AppMesh) 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 + appMesh.KubeconfigHandler.SetKey("kind", kconfig.Kind) + appMesh.KubeconfigHandler.SetKey("apiVersion", kconfig.APIVersion) + appMesh.KubeconfigHandler.SetKey("current-context", kconfig.CurrentContext) + err = appMesh.KubeconfigHandler.SetObject("preferences", kconfig.Preferences) + if err != nil { + errs = append(errs, err) + continue + } + + err = appMesh.KubeconfigHandler.SetObject("clusters", kconfig.Clusters) + if err != nil { + errs = append(errs, err) + continue + } + + err = appMesh.KubeconfigHandler.SetObject("users", kconfig.Users) + if err != nil { + errs = append(errs, err) + continue + } + + err = appMesh.KubeconfigHandler.SetObject("contexts", kconfig.Contexts) + if err != nil { + errs = append(errs, err) + continue + } + } + if len(errs) == 0 { + return nil + } + return mergeErrors(errs) +} + // ProcessOAM handles the grpc invocation for handling OAM objects func (appMesh *AppMesh) ProcessOAM(ctx context.Context, oamReq adapter.OAMRequest, hchan *chan interface{}) (string, error) { appMesh.SetChannel(hchan) + err := appMesh.CreateKubeconfigs(oamReq.K8sConfigs) + if err != nil { + return "", err + } kubeConfigs := oamReq.K8sConfigs var comps []v1alpha1.Component diff --git a/go.mod b/go.mod index b044a2c..22ad9de 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ replace ( ) require ( - github.com/layer5io/meshery-adapter-library v0.5.5 + github.com/layer5io/meshery-adapter-library v0.5.6 github.com/layer5io/meshkit v0.5.17 github.com/layer5io/service-mesh-performance v0.3.4 gopkg.in/yaml.v2 v2.4.0 diff --git a/go.sum b/go.sum index fd23637..ebf6989 100644 --- a/go.sum +++ b/go.sum @@ -878,8 +878,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.5 h1:4dGsHBDCLnkOOA/RaUM5n4bPdEdySREnkd3raU9LSDI= -github.com/layer5io/meshery-adapter-library v0.5.5/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.17 h1:QnNuIj5CVLfaZyznEAMrwt51QImpVpQ8BTcOZOIOWhI= github.com/layer5io/meshkit v0.5.17/go.mod h1:tj5TAjty7T/WJ8YvlDfOZF94t4g3mhWuKBCc6MOUoNU=