Skip to content

Commit

Permalink
Set common workspace environment variables for project clone as well
Browse files Browse the repository at this point in the history
Signed-off-by: Angel Misevski <amisevsk@redhat.com>
  • Loading branch information
amisevsk committed Nov 21, 2023
1 parent bfb9055 commit 37f485b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion controllers/workspace/devworkspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func (r *DevWorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request
// Add init container to clone projects
projectCloneOptions := projects.Options{
Image: workspace.Config.Workspace.ProjectCloneConfig.Image,
Env: workspace.Config.Workspace.ProjectCloneConfig.Env,
Env: env.GetEnvironmentVariablesForProjectClone(workspace),
Resources: workspace.Config.Workspace.ProjectCloneConfig.Resources,
}
if workspace.Config.Workspace.ProjectCloneConfig.ImagePullPolicy != "" {
Expand Down
27 changes: 19 additions & 8 deletions pkg/library/env/workspaceenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (

dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
devfileConstants "github.com/devfile/devworkspace-operator/pkg/library/constants"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"

"github.com/devfile/devworkspace-operator/pkg/common"
"github.com/devfile/devworkspace-operator/pkg/constants"
Expand All @@ -47,6 +47,17 @@ func AddCommonEnvironmentVariables(podAdditions *v1alpha1.PodAdditions, clusterD
return nil
}

func GetEnvironmentVariablesForProjectClone(workspace *common.DevWorkspaceWithConfig) []corev1.EnvVar {
var cloneEnv []corev1.EnvVar
cloneEnv = append(cloneEnv, workspace.Config.Workspace.ProjectCloneConfig.Env...)
cloneEnv = append(cloneEnv, commonEnvironmentVariables(workspace)...)
cloneEnv = append(cloneEnv, corev1.EnvVar{
Name: devfileConstants.ProjectsRootEnvVar,
Value: constants.DefaultProjectsSourcesRoot,
})
return cloneEnv
}

func commonEnvironmentVariables(workspaceWithConfig *common.DevWorkspaceWithConfig) []corev1.EnvVar {
envvars := []corev1.EnvVar{
{
Expand Down Expand Up @@ -88,20 +99,20 @@ func GetProxyEnvVars(proxyConfig *v1alpha1.Proxy) []corev1.EnvVar {

// Proxy env vars are defined by consensus rather than standard; most tools use the lower-snake-case version
// but some may only look at the upper-snake-case version, so we add both.
var env []v1.EnvVar
var env []corev1.EnvVar
if proxyConfig.HttpProxy != nil {
env = append(env, v1.EnvVar{Name: "http_proxy", Value: *proxyConfig.HttpProxy})
env = append(env, v1.EnvVar{Name: "HTTP_PROXY", Value: *proxyConfig.HttpProxy})
env = append(env, corev1.EnvVar{Name: "http_proxy", Value: *proxyConfig.HttpProxy})
env = append(env, corev1.EnvVar{Name: "HTTP_PROXY", Value: *proxyConfig.HttpProxy})
}
if proxyConfig.HttpsProxy != nil {
env = append(env, v1.EnvVar{Name: "https_proxy", Value: *proxyConfig.HttpsProxy})
env = append(env, v1.EnvVar{Name: "HTTPS_PROXY", Value: *proxyConfig.HttpsProxy})
env = append(env, corev1.EnvVar{Name: "https_proxy", Value: *proxyConfig.HttpsProxy})
env = append(env, corev1.EnvVar{Name: "HTTPS_PROXY", Value: *proxyConfig.HttpsProxy})
}
if proxyConfig.NoProxy != nil {
// Adding 'KUBERNETES_SERVICE_HOST' env var to the 'no_proxy / NO_PROXY' list. Hot Fix for https://issues.redhat.com/browse/CRW-2820
kubernetesServiceHost := os.Getenv("KUBERNETES_SERVICE_HOST")
env = append(env, v1.EnvVar{Name: "no_proxy", Value: *proxyConfig.NoProxy + "," + kubernetesServiceHost})
env = append(env, v1.EnvVar{Name: "NO_PROXY", Value: *proxyConfig.NoProxy + "," + kubernetesServiceHost})
env = append(env, corev1.EnvVar{Name: "no_proxy", Value: *proxyConfig.NoProxy + "," + kubernetesServiceHost})
env = append(env, corev1.EnvVar{Name: "NO_PROXY", Value: *proxyConfig.NoProxy + "," + kubernetesServiceHost})
}

return env
Expand Down
12 changes: 1 addition & 11 deletions pkg/library/projects/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
devfileConstants "github.com/devfile/devworkspace-operator/pkg/library/constants"
"github.com/devfile/devworkspace-operator/pkg/library/env"
dwResources "github.com/devfile/devworkspace-operator/pkg/library/resources"
corev1 "k8s.io/api/core/v1"

Expand Down Expand Up @@ -68,15 +67,6 @@ func GetProjectCloneInitContainer(workspace *dw.DevWorkspaceTemplateSpec, option
return nil, nil
}

cloneEnv := []corev1.EnvVar{
{
Name: devfileConstants.ProjectsRootEnvVar,
Value: constants.DefaultProjectsSourcesRoot,
},
}
cloneEnv = append(cloneEnv, env.GetProxyEnvVars(proxyConfig)...)
cloneEnv = append(cloneEnv, options.Env...)

resources := dwResources.FilterResources(options.Resources)
if err := dwResources.ValidateResources(resources); err != nil {
return nil, fmt.Errorf("invalid resources for project clone container: %w", err)
Expand All @@ -85,7 +75,7 @@ func GetProjectCloneInitContainer(workspace *dw.DevWorkspaceTemplateSpec, option
return &corev1.Container{
Name: projectClonerContainerName,
Image: cloneImage,
Env: cloneEnv,
Env: options.Env,
Resources: *resources,
VolumeMounts: []corev1.VolumeMount{
{
Expand Down

0 comments on commit 37f485b

Please sign in to comment.