Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions api/model/model_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"`
}

Expand Down
10 changes: 4 additions & 6 deletions api/rest/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 6 additions & 2 deletions api/rest/execInit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions api/rest/execKubeConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"))
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion exec/exec-manager-provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
41 changes: 12 additions & 29 deletions exec/kubernetes_exec_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -318,42 +316,27 @@ 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 {
logrus.Debugf("Unable to get k8sAPI %s", err.Error())
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.
Expand Down
36 changes: 19 additions & 17 deletions mocks/ExecManager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.