Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Allow to add environment variable on builder instance
Browse files Browse the repository at this point in the history
Signed-off-by: disaster37 <linuxworkgroup@hotmail.com>
  • Loading branch information
disaster37 committed Aug 3, 2021
1 parent 2abb51e commit 9fb6685
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
21 changes: 20 additions & 1 deletion pkg/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package commands

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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*/)
Expand Down Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions pkg/driver/kubernetes/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"context"
"fmt"
"io/ioutil"
"regexp"
"strconv"
"strings"
"text/template"

"github.com/vmware-tanzu/buildkit-cli-for-kubectl/pkg/driver"
Expand Down Expand Up @@ -118,6 +120,7 @@ func (d *Driver) initDriverFromConfig() error {
ContainerdSockHostPath: DefaultContainerdSockPath,
ContainerdNamespace: DefaultContainerdNamespace,
DockerSockHostPath: DefaultDockerSockPath,
Environments: make(map[string]string),
}

imageOverride := ""
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/driver/kubernetes/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type DeploymentOpt struct {
DockerSockHostPath string
ContainerRuntime string
CustomConfig string
Environments map[string]string
}

const (
Expand All @@ -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
Expand Down Expand Up @@ -94,6 +105,7 @@ func NewDeployment(opt *DeploymentOpt) (*appsv1.Deployment, error) {
MountPath: "/etc/buildkit/",
},
},
Env: environments,
},
},
Volumes: []corev1.Volume{
Expand Down

0 comments on commit 9fb6685

Please sign in to comment.