Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add BuildArgs option #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/weaver-kube/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 10 additions & 7 deletions internal/impl/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()
Expand All @@ -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"}

Expand All @@ -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
}
3 changes: 3 additions & 0 deletions internal/impl/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading