From a31d94bbd692f11d238722aaf97354d916659fda Mon Sep 17 00:00:00 2001 From: ShotaKitazawa Date: Sun, 23 Jul 2023 14:48:30 +0900 Subject: [PATCH] add BuildArgs option --- cmd/weaver-kube/deploy.go | 2 +- internal/impl/docker.go | 17 ++++++++++------- internal/impl/kube.go | 3 +++ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/cmd/weaver-kube/deploy.go b/cmd/weaver-kube/deploy.go index b2f1ac9..3283e43 100644 --- a/cmd/weaver-kube/deploy.go +++ b/cmd/weaver-kube/deploy.go @@ -135,7 +135,7 @@ func deploy(ctx context.Context, args []string) error { } // Build the docker image for the deployment, and upload it to docker hub. - image, err := impl.BuildAndUploadDockerImage(ctx, dep, config.Image, *runInDevMode) + image, err := impl.BuildAndUploadDockerImage(ctx, dep, config, *runInDevMode) if err != nil { return err } diff --git a/internal/impl/docker.go b/internal/impl/docker.go index b7d9589..a9df1fc 100644 --- a/internal/impl/docker.go +++ b/internal/impl/docker.go @@ -52,12 +52,13 @@ type imageSpecs struct { name string // name is the name of the image to build files []string // files that should be copied to the container goInstall []string // binary targets that should be 'go install'-ed + buildArgs []string // arguments for docker build } // BuildAndUploadDockerImage builds a docker image and upload it to docker hub. -func BuildAndUploadDockerImage(ctx context.Context, dep *protos.Deployment, image string, runInDevMode bool) (string, error) { +func BuildAndUploadDockerImage(ctx context.Context, dep *protos.Deployment, config *KubeConfig, runInDevMode bool) (string, error) { // Create the docker image specifications. - specs, err := buildImageSpecs(dep, image, runInDevMode) + specs, err := buildImageSpecs(dep, config, runInDevMode) if err != nil { return "", fmt.Errorf("unable to build image specs: %w", err) } @@ -115,12 +116,13 @@ func buildImage(ctx context.Context, specs *imageSpecs) error { if err := dockerFile.Close(); err != nil { return err } - return dockerBuild(ctx, workDir, specs.name) + return dockerBuild(ctx, workDir, specs.name, specs.buildArgs...) } // Use docker-cli to build the docker image. -func dockerBuild(ctx context.Context, buildContext, tag string) error { - c := exec.CommandContext(ctx, "docker", "build", buildContext, "-t", tag) +func dockerBuild(ctx context.Context, buildContext, tag string, buildArgs ...string) error { + args := append([]string{"build", buildContext, "-t", tag}, buildArgs...) + c := exec.CommandContext(ctx, "docker", args...) c.Stdout = os.Stdout c.Stderr = os.Stderr return c.Run() @@ -137,7 +139,7 @@ func uploadImage(ctx context.Context, appImage string) error { } // buildImageSpecs build the docker image specs for an app deployment. -func buildImageSpecs(dep *protos.Deployment, image string, runInDevMode bool) (*imageSpecs, error) { +func buildImageSpecs(dep *protos.Deployment, config *KubeConfig, runInDevMode bool) (*imageSpecs, error) { files := []string{dep.App.Binary} goInstall := []string{"github.com/ServiceWeaver/weaver-kube/cmd/weaver-kube@latest"} @@ -154,8 +156,9 @@ func buildImageSpecs(dep *protos.Deployment, image string, runInDevMode bool) (* } return &imageSpecs{ - name: fmt.Sprintf("%s:%s", image, dep.Id[:8]), + name: fmt.Sprintf("%s:%s", config.Image, dep.Id[:8]), files: files, goInstall: goInstall, + buildArgs: config.BuildArgs, }, nil } diff --git a/internal/impl/kube.go b/internal/impl/kube.go index fb5a2e6..959625a 100644 --- a/internal/impl/kube.go +++ b/internal/impl/kube.go @@ -144,6 +144,9 @@ type KubeConfig struct { // Options for the application listeners, keyed by listener name. // If a listener isn't specified in the map, default options will be used. Listeners map[string]*ListenerOptions + + // Options for arguments of docker build + BuildArgs []string } // GenerateKubeDeployment generates the kubernetes deployment and service