@@ -12,7 +12,9 @@ import (
1212 internalconfig "github.com/layer5io/meshery-app-mesh/internal/config"
1313 meshkitCfg "github.com/layer5io/meshkit/config"
1414 "github.com/layer5io/meshkit/logger"
15+ "github.com/layer5io/meshkit/models"
1516 "github.com/layer5io/meshkit/models/oam/core/v1alpha1"
17+ "gopkg.in/yaml.v2"
1618)
1719
1820// 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.
2628 Adapter : adapter.Adapter {
2729 Config : c ,
2830 Log : l ,
31+ KubeconfigHandler : kc ,
2932 },
3033 }
3134}
3235
3336// ApplyOperation applies the requested operation on app-mesh
3437func (appMesh * AppMesh ) ApplyOperation (ctx context.Context , opReq adapter.OperationRequest , hchan * chan interface {}) error {
38+ err := appMesh .CreateKubeconfigs (opReq .K8sConfigs )
39+ if err != nil {
40+ return err
41+ }
3542 kubeConfigs := opReq .K8sConfigs
3643 appMesh .SetChannel (hchan );
3744
3845 operations := make (adapter.Operations )
39- err : = appMesh .Config .GetObject (adapter .OperationsKey , & operations )
46+ err = appMesh .Config .GetObject (adapter .OperationsKey , & operations )
4047 if err != nil {
4148 return err
4249 }
@@ -137,9 +144,58 @@ func (appMesh *AppMesh) ApplyOperation(ctx context.Context, opReq adapter.Operat
137144 return nil
138145}
139146
147+ //CreateKubeconfigs creates and writes passed kubeconfig onto the filesystem
148+ func (appMesh * AppMesh ) CreateKubeconfigs (kubeconfigs []string ) error {
149+ var errs = make ([]error , 0 )
150+ for _ , kubeconfig := range kubeconfigs {
151+ kconfig := models.Kubeconfig {}
152+ err := yaml .Unmarshal ([]byte (kubeconfig ), & kconfig )
153+ if err != nil {
154+ errs = append (errs , err )
155+ continue
156+ }
157+
158+ // To have control over what exactly to take in on kubeconfig
159+ appMesh .KubeconfigHandler .SetKey ("kind" , kconfig .Kind )
160+ appMesh .KubeconfigHandler .SetKey ("apiVersion" , kconfig .APIVersion )
161+ appMesh .KubeconfigHandler .SetKey ("current-context" , kconfig .CurrentContext )
162+ err = appMesh .KubeconfigHandler .SetObject ("preferences" , kconfig .Preferences )
163+ if err != nil {
164+ errs = append (errs , err )
165+ continue
166+ }
167+
168+ err = appMesh .KubeconfigHandler .SetObject ("clusters" , kconfig .Clusters )
169+ if err != nil {
170+ errs = append (errs , err )
171+ continue
172+ }
173+
174+ err = appMesh .KubeconfigHandler .SetObject ("users" , kconfig .Users )
175+ if err != nil {
176+ errs = append (errs , err )
177+ continue
178+ }
179+
180+ err = appMesh .KubeconfigHandler .SetObject ("contexts" , kconfig .Contexts )
181+ if err != nil {
182+ errs = append (errs , err )
183+ continue
184+ }
185+ }
186+ if len (errs ) == 0 {
187+ return nil
188+ }
189+ return mergeErrors (errs )
190+ }
191+
140192// ProcessOAM handles the grpc invocation for handling OAM objects
141193func (appMesh * AppMesh ) ProcessOAM (ctx context.Context , oamReq adapter.OAMRequest , hchan * chan interface {}) (string , error ) {
142194 appMesh .SetChannel (hchan )
195+ err := appMesh .CreateKubeconfigs (oamReq .K8sConfigs )
196+ if err != nil {
197+ return "" , err
198+ }
143199 kubeConfigs := oamReq .K8sConfigs
144200
145201 var comps []v1alpha1.Component
0 commit comments