diff --git a/pkg/cmd/build.go b/pkg/cmd/build.go index b11d6700..32239fb7 100644 --- a/pkg/cmd/build.go +++ b/pkg/cmd/build.go @@ -4,6 +4,7 @@ package commands import ( "context" + "fmt" "os" "path/filepath" "strings" @@ -234,7 +235,25 @@ func buildTargets(ctx context.Context, kubeClientConfig clientcmd.ClientConfig, if driverName == "" { driverName = "buildkit" } - d, err := driver.GetDriver(ctx, driverName, nil, kubeClientConfig, []string{} /* TODO what BuildkitFlags are these? */, "" /* unused config file */, map[string]string{} /* DriverOpts unused */, contextPathHash) + + // Check if need to set proxy on builder + listEnvToCheck := []string{ + "http_proxy", + "https_proxy", + "no_proxy", + "HTTP_PROXY", + "HTTPS_PROXY", + "NO_PROXY", + } + envs := make([]string, 0, 0) + for _, env := range listEnvToCheck { + val, isSet := os.LookupEnv(env) + if isSet { + envs = append(envs, fmt.Sprintf("%s=%s", env, val)) + } + } + + d, err := driver.GetDriver(ctx, driverName, nil, kubeClientConfig, []string{} /* TODO what BuildkitFlags are these? */, "" /* unused config file */, map[string]string{"env": strings.Join(envs, ";")}, contextPathHash) if err != nil { return err } diff --git a/pkg/cmd/create.go b/pkg/cmd/create.go index 873cef58..3ef2ada7 100644 --- a/pkg/cmd/create.go +++ b/pkg/cmd/create.go @@ -42,6 +42,7 @@ type createOptions struct { configFile string progress string customConfig string + envs []string } func runCreate(streams genericclioptions.IOStreams, in createOptions, rootOpts *rootOptions) error { @@ -78,6 +79,7 @@ func runCreate(streams genericclioptions.IOStreams, in createOptions, rootOpts * "docker-sock": in.dockerSock, "runtime": in.runtime, "custom-config": in.customConfig, + "env": strings.Join(in.envs, ";"), } d, err := driver.GetDriver(ctx, in.name, driverFactory, rootOpts.KubeClientConfig, flags, in.configFile, driverOpts, "" /*contextPathHash*/) @@ -140,6 +142,7 @@ Driver Specific Usage: flags.StringVar(&options.loadbalance, "loadbalance", "random", "Load balancing strategy [random, sticky]") flags.StringVar(&options.worker, "worker", "auto", "Worker backend [auto, runc, containerd]") flags.StringVar(&options.customConfig, "custom-config", "", "Name of a ConfigMap containing custom files (e.g., certs), mounted in /etc/config/ - use 'kubectl create configmap ... --from-file=...'") + flags.StringArrayVar(&options.envs, "env", []string{}, "Environment variable to add when create builder, like http_proxy=http://my-proxy.com:8080") return cmd } diff --git a/pkg/driver/kubernetes/factory.go b/pkg/driver/kubernetes/factory.go index 6cad888c..7f5bb7d3 100644 --- a/pkg/driver/kubernetes/factory.go +++ b/pkg/driver/kubernetes/factory.go @@ -7,7 +7,9 @@ import ( "context" "fmt" "io/ioutil" + "regexp" "strconv" + "strings" "text/template" "github.com/vmware-tanzu/buildkit-cli-for-kubectl/pkg/driver" @@ -118,6 +120,7 @@ func (d *Driver) initDriverFromConfig() error { ContainerdSockHostPath: DefaultContainerdSockPath, ContainerdNamespace: DefaultContainerdNamespace, DockerSockHostPath: DefaultDockerSockPath, + Environments: make(map[string]string), } imageOverride := "" @@ -178,6 +181,15 @@ func (d *Driver) initDriverFromConfig() error { deploymentOpt.ContainerRuntime = v case "custom-config": deploymentOpt.CustomConfig = v + case "env": + // Split over comma for multiple key/value + re := regexp.MustCompile(`([^=]+)=([^=]+)`) + for _, item := range strings.Split(v, ";") { + m := re.FindStringSubmatch(strings.TrimSpace(item)) + if len(m) == 3 { + deploymentOpt.Environments[m[1]] = m[2] + } + } default: return errors.Errorf("invalid driver option %s for driver %s", k, DriverName) } diff --git a/pkg/driver/kubernetes/manifest/manifest.go b/pkg/driver/kubernetes/manifest/manifest.go index e6897b78..0cc757a8 100644 --- a/pkg/driver/kubernetes/manifest/manifest.go +++ b/pkg/driver/kubernetes/manifest/manifest.go @@ -24,6 +24,7 @@ type DeploymentOpt struct { DockerSockHostPath string ContainerRuntime string CustomConfig string + Environments map[string]string } const ( @@ -46,9 +47,19 @@ func annotations(opt *DeploymentOpt) map[string]string { } } +func environments(opt *DeploymentOpt) []corev1.EnvVar { + envs := make([]corev1.EnvVar, 0, len(opt.Environments)) + for name, value := range opt.Environments { + envs = append(envs, corev1.EnvVar{Name: name, Value: value}) + } + + return envs +} + func NewDeployment(opt *DeploymentOpt) (*appsv1.Deployment, error) { labels := labels(opt) annotations := annotations(opt) + environments := environments(opt) replicas := int32(opt.Replicas) privileged := true args := opt.BuildkitFlags @@ -94,6 +105,7 @@ func NewDeployment(opt *DeploymentOpt) (*appsv1.Deployment, error) { MountPath: "/etc/buildkit/", }, }, + Env: environments, }, }, Volumes: []corev1.Volume{