Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes utils of docker adapter #3198

Merged
merged 1 commit into from
May 19, 2020
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
4 changes: 2 additions & 2 deletions pkg/devfile/adapters/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func GetVolumes(devfileObj devfileParser.DevfileObj) map[string][]DevfileVolume
}

// IsEnvPresent checks if the env variable is present in an array of env variables
func IsEnvPresent(envVars []common.Container, envVarName string) bool {
func IsEnvPresent(envVars []*common.Env, envVarName string) bool {
for _, envVar := range envVars {
if envVar.Name == envVarName {
return true
Expand All @@ -138,7 +138,7 @@ func IsEnvPresent(envVars []common.Container, envVarName string) bool {
}

// IsPortPresent checks if the port is present in the endpoints array
func IsPortPresent(endpoints []common.Endpoint, port int) bool {
func IsPortPresent(endpoints []*common.Endpoint, port int) bool {
for _, endpoint := range endpoints {
if endpoint.TargetPort == int32(port) {
return true
Expand Down
116 changes: 55 additions & 61 deletions pkg/devfile/adapters/docker/component/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (a Adapter) createComponent() (err error) {
// Loop over each component and start a container for it
for _, comp := range supportedComponents {
var dockerVolumeMounts []mount.Mount
for _, vol := range a.componentAliasToVolumes[*comp.Alias] {
for _, vol := range a.componentAliasToVolumes[comp.Container.Name] {
volMount := mount.Mount{
Type: mount.TypeVolume,
Source: a.volumeNameToDockerVolName[*vol.Name],
Expand All @@ -82,7 +82,7 @@ func (a Adapter) createComponent() (err error) {
}
err = a.pullAndStartContainer(dockerVolumeMounts, projectVolumeName, comp)
if err != nil {
return errors.Wrapf(err, "unable to pull and start container %s for component %s", *comp.Alias, componentName)
return errors.Wrapf(err, "unable to pull and start container %s for component %s", comp.Container.Name, componentName)
}
}
klog.V(3).Infof("Successfully created all containers for component %s", componentName)
Expand Down Expand Up @@ -120,13 +120,13 @@ func (a Adapter) updateComponent() (componentExists bool, err error) {
for _, comp := range supportedComponents {
// Check to see if this component is already running and if so, update it
// If component isn't running, re-create it, as it either may be new, or crashed.
containers, err := a.Client.GetContainersByComponentAndAlias(componentName, *comp.Alias)
containers, err := a.Client.GetContainersByComponentAndAlias(componentName, comp.Container.Name)
if err != nil {
return false, errors.Wrapf(err, "unable to list containers for component %s", componentName)
}

var dockerVolumeMounts []mount.Mount
for _, vol := range a.componentAliasToVolumes[*comp.Alias] {
for _, vol := range a.componentAliasToVolumes[comp.Container.Name] {
volMount := mount.Mount{
Type: mount.TypeVolume,
Source: a.volumeNameToDockerVolName[*vol.Name],
Expand All @@ -140,7 +140,7 @@ func (a Adapter) updateComponent() (componentExists bool, err error) {
// Container doesn't exist, so need to pull its image (to be safe) and start a new container
err = a.pullAndStartContainer(dockerVolumeMounts, projectVolumeName, comp)
if err != nil {
return false, errors.Wrapf(err, "unable to pull and start container %s for component %s", *comp.Alias, componentName)
return false, errors.Wrapf(err, "unable to pull and start container %s for component %s", comp.Container.Name, componentName)
}

// Update componentExists so that we re-sync project and initialize supervisord if required
Expand All @@ -155,7 +155,7 @@ func (a Adapter) updateComponent() (componentExists bool, err error) {
return componentExists, errors.Wrapf(err, "unable to get the container config for component %s", componentName)
}

portMap, namePortMapping, err := getPortMap(a.Context, comp.Endpoints, false)
portMap, namePortMapping, err := getPortMap(a.Context, comp.Container.Endpoints, false)
if err != nil {
return componentExists, errors.Wrapf(err, "unable to get the port map from env.yaml file for component %s", componentName)
}
Expand All @@ -167,22 +167,22 @@ func (a Adapter) updateComponent() (componentExists bool, err error) {
if utils.DoesContainerNeedUpdating(comp, containerConfig, hostConfig, dockerVolumeMounts, mounts, portMap) {
log.Infof("\nCreating Docker resources for component %s", a.ComponentName)

s := log.SpinnerNoSpin("Updating the component " + *comp.Alias)
s := log.SpinnerNoSpin("Updating the component " + comp.Container.Name)
defer s.End(false)

// Remove the container
err := a.Client.RemoveContainer(containerID)
if err != nil {
return componentExists, errors.Wrapf(err, "Unable to remove container %s for component %s", containerID, *comp.Alias)
return componentExists, errors.Wrapf(err, "Unable to remove container %s for component %s", containerID, comp.Container.Name)
}

// Start the container
err = a.startComponent(dockerVolumeMounts, projectVolumeName, comp)
if err != nil {
return false, errors.Wrapf(err, "Unable to start container for devfile component %s", *comp.Alias)
return false, errors.Wrapf(err, "Unable to start container for devfile component %s", comp.Container.Name)
}

klog.V(3).Infof("Successfully created container %s for component %s", *comp.Image, componentName)
klog.V(3).Infof("Successfully created container %s for component %s", comp.Container.Image, componentName)
s.End(true)

// Update componentExists so that we re-sync project and initialize supervisord if required
Expand All @@ -191,7 +191,7 @@ func (a Adapter) updateComponent() (componentExists bool, err error) {
} else {
// Multiple containers were returned with the specified label (which should be unique)
// Error out, as this isn't expected
return true, fmt.Errorf("Found multiple running containers for devfile component %s and cannot push changes", *comp.Alias)
return true, fmt.Errorf("Found multiple running containers for devfile component %s and cannot push changes", comp.Container.Name)
}
}

Expand All @@ -200,27 +200,27 @@ func (a Adapter) updateComponent() (componentExists bool, err error) {

func (a Adapter) pullAndStartContainer(mounts []mount.Mount, projectVolumeName string, comp versionsCommon.DevfileComponent) error {
// Container doesn't exist, so need to pull its image (to be safe) and start a new container
s := log.Spinnerf("Pulling image %s", *comp.Image)
s := log.Spinnerf("Pulling image %s", comp.Container.Image)

err := a.Client.PullImage(*comp.Image)
err := a.Client.PullImage(comp.Container.Image)
if err != nil {
s.End(false)
return errors.Wrapf(err, "Unable to pull %s image", *comp.Image)
return errors.Wrapf(err, "Unable to pull %s image", comp.Container.Image)
}
s.End(true)

// Start the component container
err = a.startComponent(mounts, projectVolumeName, comp)
if err != nil {
return errors.Wrapf(err, "Unable to start container for devfile component %s", *comp.Alias)
return errors.Wrapf(err, "Unable to start container for devfile component %s", comp.Container.Name)
}

klog.V(3).Infof("Successfully created container %s for component %s", *comp.Image, a.ComponentName)
klog.V(3).Infof("Successfully created container %s for component %s", comp.Container.Image, a.ComponentName)
return nil
}

func (a Adapter) startComponent(mounts []mount.Mount, projectVolumeName string, comp versionsCommon.DevfileComponent) error {
hostConfig, namePortMapping, err := a.generateAndGetHostConfig(comp.Endpoints)
hostConfig, namePortMapping, err := a.generateAndGetHostConfig(comp.Container.Endpoints)
hostConfig.Mounts = mounts
if err != nil {
return err
Expand All @@ -234,15 +234,15 @@ func (a Adapter) startComponent(mounts []mount.Mount, projectVolumeName string,
utils.UpdateComponentWithSupervisord(&comp, runCommand, a.supervisordVolumeName, &hostConfig)

// If the component set `mountSources` to true, add the source volume and env CHE_PROJECTS_ROOT to it
if comp.MountSources {
if comp.Container.MountSources {
utils.AddVolumeToContainer(projectVolumeName, lclient.OdoSourceVolumeMount, &hostConfig)

if !common.IsEnvPresent(comp.Env, common.EnvCheProjectsRoot) {
if !common.IsEnvPresent(comp.Container.Env, common.EnvCheProjectsRoot) {
envName := common.EnvCheProjectsRoot
envValue := lclient.OdoSourceVolumeMount
comp.Env = append(comp.Env, versionsCommon.DockerimageEnv{
Name: &envName,
Value: &envValue,
comp.Container.Env = append(comp.Container.Env, &versionsCommon.Env{
Name: envName,
Value: envValue,
})
}
}
Expand All @@ -254,7 +254,7 @@ func (a Adapter) startComponent(mounts []mount.Mount, projectVolumeName string,
}

// Create the docker container
s := log.Spinner("Starting container for " + *comp.Image)
s := log.Spinner("Starting container for " + comp.Container.Image)
defer s.End(false)
err = a.Client.StartContainer(&containerConfig, &hostConfig, nil)
if err != nil {
Expand All @@ -267,16 +267,16 @@ func (a Adapter) startComponent(mounts []mount.Mount, projectVolumeName string,

func (a Adapter) generateAndGetContainerConfig(componentName string, comp versionsCommon.DevfileComponent) container.Config {
// Convert the env vars in the Devfile to the format expected by Docker
envVars := utils.ConvertEnvs(comp.Env)
ports := utils.ConvertPorts(comp.Endpoints)
containerLabels := utils.GetContainerLabels(componentName, *comp.Alias)
envVars := utils.ConvertEnvs(comp.Container.Env)
ports := utils.ConvertPorts(comp.Container.Endpoints)
containerLabels := utils.GetContainerLabels(componentName, comp.Container.Name)

containerConfig := a.Client.GenerateContainerConfig(*comp.Image, comp.Command, comp.Args, envVars, containerLabels, ports)
containerConfig := a.Client.GenerateContainerConfig(comp.Container.Image, []string{}, []string{}, envVars, containerLabels, ports)

return containerConfig
}

func (a Adapter) generateAndGetHostConfig(endpoints []versionsCommon.DockerimageEndpoint) (container.HostConfig, map[nat.Port]string, error) {
func (a Adapter) generateAndGetHostConfig(endpoints []*versionsCommon.Endpoint) (container.HostConfig, map[nat.Port]string, error) {
// Convert the port bindings from env.yaml and generate docker host config
portMap, namePortMapping, err := getPortMap(a.Context, endpoints, true)
if err != nil {
Expand All @@ -291,7 +291,7 @@ func (a Adapter) generateAndGetHostConfig(endpoints []versionsCommon.Dockerimage
return hostConfig, namePortMapping, nil
}

func getPortMap(context string, endpoints []versionsCommon.DockerimageEndpoint, show bool) (nat.PortMap, map[nat.Port]string, error) {
func getPortMap(context string, endpoints []*versionsCommon.Endpoint, show bool) (nat.PortMap, map[nat.Port]string, error) {
// Convert the exposed and internal port pairs saved in env.yaml file to PortMap
// Todo: Use context to get the approraite envinfo after context is supported in experimental mode
portmap := nat.PortMap{}
Expand Down Expand Up @@ -368,57 +368,51 @@ func (a Adapter) execDevfile(pushDevfileCommands []versionsCommon.DevfileCommand
// Loop through each of the command given from the devfile
for _, command := range pushDevfileCommands {
// If the current command from the devfile is the currently expected command from the devfile
if command.Name == currentCommand.DefaultName || command.Name == currentCommand.AdapterName {
if command.Exec.Id == currentCommand.DefaultName || command.Exec.Id == currentCommand.AdapterName {
// If the current command is not the last command in the slice
// it is not expected to be the run command
if i < len(commandOrder)-1 {
// Any exec command such as "Init" and "Build"

for _, action := range command.Actions {
containerID := utils.GetContainerIDForAlias(containers, *action.Component)
compInfo := common.ComponentInfo{
ContainerName: containerID,
}
containerID := utils.GetContainerIDForAlias(containers, command.Exec.Component)
compInfo := common.ComponentInfo{
ContainerName: containerID,
}

err = exec.ExecuteDevfileBuildAction(&a.Client, action, command.Name, compInfo, show)
if err != nil {
return err
}
err = exec.ExecuteDevfileBuildAction(&a.Client, *command.Exec, command.Exec.Id, compInfo, show)
if err != nil {
return err
}

// If the current command is the last command in the slice
// it is expected to be the run command
} else {
// Last command is "Run"
klog.V(4).Infof("Executing devfile command %v", command.Name)
klog.V(4).Infof("Executing devfile command %v", command.Exec.Id)

for _, action := range command.Actions {

// Check if the devfile run component containers have supervisord as the entrypoint.
// Start the supervisord if the odo component does not exist
if !componentExists {
err = a.InitRunContainerSupervisord(*action.Component, containers)
if err != nil {
return
}
}

containerID := utils.GetContainerIDForAlias(containers, *action.Component)
compInfo := common.ComponentInfo{
ContainerName: containerID,
}

if componentExists && !common.IsRestartRequired(command) {
klog.V(4).Info("restart:false, Not restarting DevRun Command")
err = exec.ExecuteDevfileRunActionWithoutRestart(&a.Client, action, command.Name, compInfo, show)
// Check if the devfile run component containers have supervisord as the entrypoint.
// Start the supervisord if the odo component does not exist
if !componentExists {
err = a.InitRunContainerSupervisord(command.Exec.Component, containers)
if err != nil {
return
}
}

err = exec.ExecuteDevfileRunAction(&a.Client, action, command.Name, compInfo, show)
containerID := utils.GetContainerIDForAlias(containers, command.Exec.Component)
compInfo := common.ComponentInfo{
ContainerName: containerID,
}

if componentExists && !common.IsRestartRequired(command) {
klog.V(4).Info("restart:false, Not restarting DevRun Command")
err = exec.ExecuteDevfileRunActionWithoutRestart(&a.Client, *command.Exec, command.Exec.Id, compInfo, show)
return
}
}

err = exec.ExecuteDevfileRunAction(&a.Client, *command.Exec, command.Exec.Id, compInfo, show)

}
}
}
}
Expand Down
Loading