diff --git a/api/model/model_types.go b/api/model/model_types.go index 73871dc5b..a9c52f9f7 100644 --- a/api/model/model_types.go +++ b/api/model/model_types.go @@ -32,15 +32,14 @@ type MachineIdentifier struct { } type ContainerInfo struct { - ContainerName string - PodName string + PodName string `json:"pod"` + ContainerName string `json:"container"` } //ResolvedExec holds info client might send to create exec type ResolvedExec struct { - PodName string `json:"pod"` - ContainerName string `json:"container"` - Cmd []string `json:"cmd"` + ContainerInfo + Cmd []string `json:"cmd"` } // Todo code Refactoring: MachineExec should be simple object for exec creation, without any business logic @@ -99,7 +98,7 @@ func (*ExecErrorEvent) Type() string { } type InitConfigParams struct { - ContainerName string `json:"container"` //optional, Will be first available if not set + ContainerName string `json:"container"` // optional, Will be first suitable container in pod if not set KubeConfigParams `json:"kubeconfig"` } diff --git a/api/rest/common.go b/api/rest/common.go index 24bfdced3..f0e626fa8 100644 --- a/api/rest/common.go +++ b/api/rest/common.go @@ -14,15 +14,13 @@ package rest import ( "github.com/eclipse/che-machine-exec/api/model" - "github.com/gin-gonic/gin" ) -func HandleKubeConfigCreation(c *gin.Context, initConfigParams *model.InitConfigParams, token string) error { - if initConfigParams.Username == "" { - initConfigParams.Username = "Developer" +func HandleKubeConfigCreation(kubeConfigParams *model.KubeConfigParams, containerInfo *model.ContainerInfo) error { + if kubeConfigParams.Username == "" { + kubeConfigParams.Username = "Developer" } - initConfigParams.BearerToken = token - err := execManager.CreateKubeConfig(initConfigParams) + err := execManager.CreateKubeConfig(kubeConfigParams, containerInfo) return err } diff --git a/api/rest/execInit.go b/api/rest/execInit.go index 271479a67..0ec1af496 100644 --- a/api/rest/execInit.go +++ b/api/rest/execInit.go @@ -15,9 +15,10 @@ package rest import ( "encoding/json" "fmt" + "net/http" + "github.com/eclipse/che-machine-exec/auth" "github.com/eclipse/che-machine-exec/common/rest" - "net/http" "github.com/eclipse/che-machine-exec/api/model" "github.com/eclipse/che-machine-exec/exec" @@ -46,13 +47,16 @@ func HandleInit(c *gin.Context) { return } + kubeConfigParams := initConfigParams.KubeConfigParams + kubeConfigParams.BearerToken = token + execRequest := handleContainerResolve(c, token, initConfigParams.ContainerName) if execRequest == nil { rest.WriteResponse(c, http.StatusInternalServerError, "Could not retrieve exec request") return } - err := HandleKubeConfigCreation(c, &initConfigParams, token) + err := HandleKubeConfigCreation(&kubeConfigParams, &execRequest.ContainerInfo) if err != nil { rest.WriteResponse(c, http.StatusInternalServerError, err.Error()) return diff --git a/api/rest/execKubeConfig.go b/api/rest/execKubeConfig.go index dc6593792..1f64213d3 100644 --- a/api/rest/execKubeConfig.go +++ b/api/rest/execKubeConfig.go @@ -13,9 +13,10 @@ package rest import ( + "net/http" + "github.com/eclipse/che-machine-exec/auth" "github.com/eclipse/che-machine-exec/common/rest" - "net/http" "github.com/eclipse/che-machine-exec/api/model" "github.com/gin-gonic/gin" @@ -39,6 +40,9 @@ func HandleKubeConfig(c *gin.Context) { return } + kubeConfigParams := initConfigParams.KubeConfigParams + kubeConfigParams.BearerToken = token + if initConfigParams.ContainerName == "" { c.Writer.WriteHeader(http.StatusBadRequest) _, err := c.Writer.Write([]byte("Container name is required")) @@ -48,7 +52,13 @@ func HandleKubeConfig(c *gin.Context) { return } - err := HandleKubeConfigCreation(c, &initConfigParams, token) + execRequest := handleContainerResolve(c, token, initConfigParams.ContainerName) + if execRequest == nil { + rest.WriteResponse(c, http.StatusInternalServerError, "Could not retrieve exec request") + return + } + + err := HandleKubeConfigCreation(&kubeConfigParams, &execRequest.ContainerInfo) if err != nil { logrus.Errorf("Unable to create kubeconfig. Cause: %s", err.Error()) diff --git a/exec/exec-manager-provider.go b/exec/exec-manager-provider.go index 143342bf0..457b398cb 100644 --- a/exec/exec-manager-provider.go +++ b/exec/exec-manager-provider.go @@ -46,7 +46,7 @@ type ExecManager interface { Resize(id int, cols uint, rows uint) error // Create a kubeconfig - CreateKubeConfig(kubeConfigParams *model.InitConfigParams) error + CreateKubeConfig(kubeConfigParams *model.KubeConfigParams, containerInfo *model.ContainerInfo) error } // CreateExecManager creates and returns new instance ExecManager. diff --git a/exec/kubernetes_exec_manager.go b/exec/kubernetes_exec_manager.go index fbd1064ec..5c5ff7909 100644 --- a/exec/kubernetes_exec_manager.go +++ b/exec/kubernetes_exec_manager.go @@ -94,8 +94,7 @@ func (manager *KubernetesExecManager) Resolve(container, token string) (*model.R logrus.Printf("%s is successfully resolved in auto discovered container %s/%s", resolvedCmd, containerInfo.PodName, containerInfo.ContainerName) return &model.ResolvedExec{ - PodName: containerInfo.PodName, - ContainerName: containerInfo.ContainerName, + ContainerInfo: *containerInfo, Cmd: resolvedCmd, }, nil } else { @@ -128,8 +127,7 @@ func (manager *KubernetesExecManager) findFirstAvailable(k8sAPI *client.K8sAPI, logrus.Printf("%s is successfully resolved in auto discovered container %s/%s", resolvedCmd, containerInfo.PodName, containerInfo.ContainerName) return &model.ResolvedExec{ - PodName: containerInfo.PodName, - ContainerName: containerInfo.ContainerName, + ContainerInfo: *containerInfo, Cmd: resolvedCmd, }, nil } @@ -318,9 +316,9 @@ func (*KubernetesExecManager) Resize(id int, cols uint, rows uint) error { return nil } -func (manager *KubernetesExecManager) CreateKubeConfig(initConfigParams *model.InitConfigParams) error { +func (manager *KubernetesExecManager) CreateKubeConfig(kubeConfigParams *model.KubeConfigParams, containerInfo *model.ContainerInfo) error { machineExec := &model.MachineExec{ - BearerToken: initConfigParams.BearerToken, + BearerToken: kubeConfigParams.BearerToken, } k8sAPI, err := manager.k8sAPIProvider.GetK8sAPI(machineExec) if err != nil { @@ -328,32 +326,17 @@ func (manager *KubernetesExecManager) CreateKubeConfig(initConfigParams *model.I return err } - containerFilter := filter.NewKubernetesContainerFilter(manager.namespace, k8sAPI.GetClient().CoreV1()) - containersInfo, err := containerFilter.GetContainerList() - if err != nil { - return err - } + currentNamespace := GetNamespace() + infoExecCreator := exec_info.NewKubernetesInfoExecCreator(currentNamespace, k8sAPI.GetClient().Core(), k8sAPI.GetConfig()) - if len(containersInfo) == 0 { - return errors.New("no containers found to exec") + if kubeConfigParams.Namespace == "" { + kubeConfigParams.Namespace = currentNamespace } - - for _, containerInfo := range containersInfo { - if containerInfo.ContainerName == initConfigParams.ContainerName || initConfigParams.ContainerName == "" { - currentNamespace := GetNamespace() - infoExecCreator := exec_info.NewKubernetesInfoExecCreator(currentNamespace, k8sAPI.GetClient().Core(), k8sAPI.GetConfig()) - - if initConfigParams.Namespace == "" { - initConfigParams.Namespace = currentNamespace - } - err = kubeconfig.CreateKubeConfig(infoExecCreator, &initConfigParams.KubeConfigParams, containerInfo) - if err != nil { - return err - } - return nil - } + err = kubeconfig.CreateKubeConfig(infoExecCreator, kubeConfigParams, containerInfo) + if err != nil { + return err } - return fmt.Errorf("No container with name %s found", initConfigParams.ContainerName) + return nil } // getByID return exec by id. diff --git a/mocks/ExecManager.go b/mocks/ExecManager.go index 2087f4dba..f135e861e 100644 --- a/mocks/ExecManager.go +++ b/mocks/ExecManager.go @@ -1,4 +1,4 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. +// Code generated by mockery v0.0.0-dev. DO NOT EDIT. package mocks @@ -69,6 +69,20 @@ func (_m *ExecManager) Create(machineExec *model.MachineExec) (int, error) { return r0, r1 } +// CreateKubeConfig provides a mock function with given fields: kubeConfigParams, containerInfo +func (_m *ExecManager) CreateKubeConfig(kubeConfigParams *model.KubeConfigParams, containerInfo *model.ContainerInfo) error { + ret := _m.Called(kubeConfigParams, containerInfo) + + var r0 error + if rf, ok := ret.Get(0).(func(*model.KubeConfigParams, *model.ContainerInfo) error); ok { + r0 = rf(kubeConfigParams, containerInfo) + } else { + r0 = ret.Error(0) + } + + return r0 +} + // Remove provides a mock function with given fields: execId func (_m *ExecManager) Remove(execId int) { _m.Called(execId) @@ -88,29 +102,17 @@ func (_m *ExecManager) Resize(id int, cols uint, rows uint) error { return r0 } -// Create provides a mock function with given fields: initConfigParams -func (_m *ExecManager) CreateKubeConfig(initConfigParams *model.InitConfigParams) error { - ret := _m.Called(initConfigParams) - - var r0 error - if rf, ok := ret.Get(0).(func(*model.InitConfigParams) error); ok { - r0 = rf(initConfigParams) - } else { - r0 = ret.Error(0) - } - - return r0 -} - // Resolve provides a mock function with given fields: container, token -func (_m *ExecManager) Resolve(container, token string) (*model.ResolvedExec, error) { +func (_m *ExecManager) Resolve(container string, token string) (*model.ResolvedExec, error) { ret := _m.Called(container, token) var r0 *model.ResolvedExec if rf, ok := ret.Get(0).(func(string, string) *model.ResolvedExec); ok { r0 = rf(container, token) } else { - r0 = ret.Get(0).(*model.ResolvedExec) + if ret.Get(0) != nil { + r0 = ret.Get(0).(*model.ResolvedExec) + } } var r1 error