Skip to content

Commit

Permalink
Fixes utils of k8s adapter (#3195)
Browse files Browse the repository at this point in the history
  • Loading branch information
mik-dass authored and Aditi Sharma committed May 28, 2020
1 parent 6aedd58 commit 39c58c4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 58 deletions.
8 changes: 4 additions & 4 deletions pkg/devfile/adapters/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ func GetVolumes(devfileObj devfileParser.DevfileObj) map[string][]DevfileVolume
size := volumeSize
for _, comp := range GetSupportedComponents(devfileObj.Data) {
if comp.Volume != nil {
for _, volume := range comp.Volumes {
for _, volume := range comp.Container.VolumeMounts {
vol := DevfileVolume{
Name: volume.Name,
ContainerPath: volume.ContainerPath,
Name: &volume.Name,
ContainerPath: &volume.Path,
Size: &size,
}
componentAliasToVolumes[*comp.Alias] = append(componentAliasToVolumes[*comp.Alias], vol)
componentAliasToVolumes[comp.Container.Name] = append(componentAliasToVolumes[comp.Container.Name], vol)
}
}
}
Expand Down
100 changes: 49 additions & 51 deletions pkg/devfile/adapters/kubernetes/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@ func ComponentExists(client kclient.Client, name string) bool {
}

// ConvertEnvs converts environment variables from the devfile structure to kubernetes structure
func ConvertEnvs(vars []common.DockerimageEnv) []corev1.EnvVar {
func ConvertEnvs(vars []*common.Env) []corev1.EnvVar {
kVars := []corev1.EnvVar{}
for _, env := range vars {
kVars = append(kVars, corev1.EnvVar{
Name: *env.Name,
Value: *env.Value,
Name: env.Name,
Value: env.Value,
})
}
return kVars
}

// ConvertPorts converts endpoint variables from the devfile structure to kubernetes ContainerPort
func ConvertPorts(endpoints []common.DockerimageEndpoint) ([]corev1.ContainerPort, error) {
func ConvertPorts(endpoints []*common.Endpoint) ([]corev1.ContainerPort, error) {
containerPorts := []corev1.ContainerPort{}
for _, endpoint := range endpoints {
name := strings.TrimSpace(util.GetDNS1123Name(strings.ToLower(*endpoint.Name)))
name := strings.TrimSpace(util.GetDNS1123Name(strings.ToLower(endpoint.Name)))
name = util.TruncateString(name, 15)
for _, c := range containerPorts {
if c.ContainerPort == *endpoint.Port {
return nil, fmt.Errorf("Devfile contains multiple identical ports: %v", *endpoint.Port)
if c.ContainerPort == endpoint.TargetPort {
return nil, fmt.Errorf("Devfile contains multiple identical ports: %v", endpoint.TargetPort)
}
}
containerPorts = append(containerPorts, corev1.ContainerPort{
Name: name,
ContainerPort: *endpoint.Port,
ContainerPort: endpoint.TargetPort,
})
}
return containerPorts, nil
Expand All @@ -56,13 +56,13 @@ func ConvertPorts(endpoints []common.DockerimageEndpoint) ([]corev1.ContainerPor
func GetContainers(devfileObj devfileParser.DevfileObj) ([]corev1.Container, error) {
var containers []corev1.Container
for _, comp := range adaptersCommon.GetSupportedComponents(devfileObj.Data) {
envVars := ConvertEnvs(comp.Env)
envVars := ConvertEnvs(comp.Container.Env)
resourceReqs := GetResourceReqs(comp)
ports, err := ConvertPorts(comp.Endpoints)
ports, err := ConvertPorts(comp.Container.Endpoints)
if err != nil {
return nil, err
}
container := kclient.GenerateContainer(*comp.Alias, *comp.Image, false, comp.Command, comp.Args, envVars, resourceReqs, ports)
container := kclient.GenerateContainer(comp.Container.Name, comp.Container.Image, false, envVars, resourceReqs, ports)
for _, c := range containers {
for _, containerPort := range c.Ports {
for _, curPort := range container.Ports {
Expand All @@ -74,7 +74,7 @@ func GetContainers(devfileObj devfileParser.DevfileObj) ([]corev1.Container, err
}

// If `mountSources: true` was set, add an empty dir volume to the container to sync the source to
if comp.MountSources {
if comp.Container.MountSources {
container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
Name: kclient.OdoSourceVolume,
MountPath: kclient.OdoSourceVolumeMount,
Expand Down Expand Up @@ -117,47 +117,45 @@ func UpdateContainersWithSupervisord(devfileObj devfileParser.DevfileObj, contai
}

for i, container := range containers {
for _, action := range runCommand.Actions {
// Check if the container belongs to a run command component
if container.Name == *action.Component {
// If the run component container has no entrypoint and arguments, override the entrypoint with supervisord
if len(container.Command) == 0 && len(container.Args) == 0 {
klog.V(3).Infof("Updating container %v entrypoint with supervisord", container.Name)
container.Command = append(container.Command, adaptersCommon.SupervisordBinaryPath)
container.Args = append(container.Args, "-c", adaptersCommon.SupervisordConfFile)
}
// Check if the container belongs to a run command component
if container.Name == runCommand.Exec.Component {
// If the run component container has no entrypoint and arguments, override the entrypoint with supervisord
if len(container.Command) == 0 && len(container.Args) == 0 {
klog.V(3).Infof("Updating container %v entrypoint with supervisord", container.Name)
container.Command = append(container.Command, adaptersCommon.SupervisordBinaryPath)
container.Args = append(container.Args, "-c", adaptersCommon.SupervisordConfFile)
}

// Always mount the supervisord volume in the run component container
klog.V(3).Infof("Updating container %v with supervisord volume mounts", container.Name)
container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
Name: adaptersCommon.SupervisordVolumeName,
MountPath: adaptersCommon.SupervisordMountPath,
})

// Update the run container's ENV for work dir and command
// only if the env var is not set in the devfile
// This is done, so supervisord can use it in it's program
if !isEnvPresent(container.Env, adaptersCommon.EnvOdoCommandRun) {
klog.V(3).Infof("Updating container %v env with run command", container.Name)
container.Env = append(container.Env,
corev1.EnvVar{
Name: adaptersCommon.EnvOdoCommandRun,
Value: *action.Command,
})
}
// Always mount the supervisord volume in the run component container
klog.V(3).Infof("Updating container %v with supervisord volume mounts", container.Name)
container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
Name: adaptersCommon.SupervisordVolumeName,
MountPath: adaptersCommon.SupervisordMountPath,
})

if !isEnvPresent(container.Env, adaptersCommon.EnvOdoCommandRunWorkingDir) && action.Workdir != nil {
klog.V(3).Infof("Updating container %v env with run command's workdir", container.Name)
container.Env = append(container.Env,
corev1.EnvVar{
Name: adaptersCommon.EnvOdoCommandRunWorkingDir,
Value: *action.Workdir,
})
}
// Update the run container's ENV for work dir and command
// only if the env var is not set in the devfile
// This is done, so supervisord can use it in it's program
if !isEnvPresent(container.Env, adaptersCommon.EnvOdoCommandRun) {
klog.V(3).Infof("Updating container %v env with run command", container.Name)
container.Env = append(container.Env,
corev1.EnvVar{
Name: adaptersCommon.EnvOdoCommandRun,
Value: runCommand.Exec.CommandLine,
})
}

// Update the containers array since the array is not a pointer to the container
containers[i] = container
if !isEnvPresent(container.Env, adaptersCommon.EnvOdoCommandRunWorkingDir) && runCommand.Exec.WorkingDir != nil {
klog.V(3).Infof("Updating container %v env with run command's workdir", container.Name)
container.Env = append(container.Env,
corev1.EnvVar{
Name: adaptersCommon.EnvOdoCommandRunWorkingDir,
Value: *runCommand.Exec.WorkingDir,
})
}

// Update the containers array since the array is not a pointer to the container
containers[i] = container
}
}

Expand All @@ -169,8 +167,8 @@ func UpdateContainersWithSupervisord(devfileObj devfileParser.DevfileObj, contai
func GetResourceReqs(comp common.DevfileComponent) corev1.ResourceRequirements {
reqs := corev1.ResourceRequirements{}
limits := make(corev1.ResourceList)
if comp.MemoryLimit != nil {
memoryLimit, err := resource.ParseQuantity(*comp.MemoryLimit)
if &comp.Container.MemoryLimit != nil {
memoryLimit, err := resource.ParseQuantity(comp.Container.MemoryLimit)
if err == nil {
limits[corev1.ResourceMemory] = memoryLimit
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/kclient/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ const (
)

// GenerateContainer creates a container spec that can be used when creating a pod
func GenerateContainer(name, image string, isPrivileged bool, command, args []string, envVars []corev1.EnvVar, resourceReqs corev1.ResourceRequirements, ports []corev1.ContainerPort) *corev1.Container {
func GenerateContainer(name, image string, isPrivileged bool, envVars []corev1.EnvVar, resourceReqs corev1.ResourceRequirements, ports []corev1.ContainerPort) *corev1.Container {
container := &corev1.Container{
Name: name,
Image: image,
ImagePullPolicy: corev1.PullAlways,
Resources: resourceReqs,
Command: command,
Args: args,
Env: envVars,
Ports: ports,
}
Expand Down

0 comments on commit 39c58c4

Please sign in to comment.