Skip to content

Commit

Permalink
dockerfile: support ecr private repo as a base image
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyMarchuk committed Feb 12, 2019
1 parent 04c22d0 commit 03f42a6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
9 changes: 5 additions & 4 deletions common/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

// DockerImageBuilder for creating docker images
type DockerImageBuilder interface {
ImageBuild(contextDir string, serviceName string, relDockerfile string, tags []string, dockerOut io.Writer) error
ImageBuild(contextDir string, serviceName string, relDockerfile string, tags []string, registryAuthConfig map[string]types.AuthConfig, dockerOut io.Writer) error
}

// DockerImagePusher for pushing docker images
Expand Down Expand Up @@ -47,10 +47,11 @@ func newClientDockerManager() (DockerManager, error) {
}, nil
}

func (d *clientDockerManager) ImageBuild(contextDir string, serviceName string, relDockerfile string, tags []string, dockerOut io.Writer) error {
func (d *clientDockerManager) ImageBuild(contextDir string, serviceName string, relDockerfile string, tags []string, registryAuthConfig map[string]types.AuthConfig, dockerOut io.Writer) error {
options := types.ImageBuildOptions{
Tags: tags,
Labels: map[string]string{"SERVICE_NAME": serviceName},
Tags: tags,
Labels: map[string]string{"SERVICE_NAME": serviceName},
AuthConfigs: registryAuthConfig,
}

buildContext, err := createBuildContext(contextDir, relDockerfile)
Expand Down
15 changes: 15 additions & 0 deletions workflows/service_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"strings"

"github.com/docker/docker/api/types"
"github.com/pkg/errors"
"github.com/stelligent/mu/common"
)
Expand All @@ -18,6 +19,7 @@ type serviceWorkflow struct {
serviceTag string
serviceImage string
registryAuth string
registryAuthConfig map[string]types.AuthConfig
priority int
codeRevision string
repoName string
Expand Down Expand Up @@ -259,6 +261,19 @@ func (workflow *serviceWorkflow) serviceRegistryAuthenticator(authenticator comm
authParts := strings.Split(string(data), ":")

workflow.registryAuth = base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("{\"username\":\"%s\", \"password\":\"%s\"}", authParts[0], authParts[1])))

// ImageBuild pull auth
var authConfigs2 map[string]types.AuthConfig
authConfigs2 = make(map[string]types.AuthConfig)

authConfigs2[strings.Split(workflow.serviceImage, ":")[0]] = types.AuthConfig{
Username: authParts[0],
Password: authParts[1],
ServerAddress: fmt.Sprintf("https://%s", strings.Split(workflow.serviceImage, ":")[0]),
}

workflow.registryAuthConfig = authConfigs2

return nil
}
}
Expand Down
4 changes: 2 additions & 2 deletions workflows/service_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func NewServicePusher(ctx *common.Context, tag string, provider string, kmsKey s
newConditionalExecutor(workflow.isEcrProvider(),
newPipelineExecutor(
workflow.serviceRepoUpserter(ctx.Config.Namespace, &ctx.Config.Service, ctx.StackManager, ctx.StackManager),
workflow.serviceImageBuilder(ctx.DockerManager, &ctx.Config, dockerWriter),
workflow.serviceRegistryAuthenticator(ctx.ClusterManager),
workflow.serviceImageBuilder(ctx.DockerManager, &ctx.Config, dockerWriter),
workflow.serviceImagePusher(ctx.DockerManager, dockerWriter),
),
newPipelineExecutor(
Expand All @@ -36,7 +36,7 @@ func NewServicePusher(ctx *common.Context, tag string, provider string, kmsKey s
func (workflow *serviceWorkflow) serviceImageBuilder(imageBuilder common.DockerImageBuilder, config *common.Config, dockerWriter io.Writer) Executor {
return func() error {
log.Noticef("Building service:'%s' as image:%s'", workflow.serviceName, workflow.serviceImage)
return imageBuilder.ImageBuild(config.Basedir, workflow.serviceName, config.Service.Dockerfile, []string{workflow.serviceImage}, dockerWriter)
return imageBuilder.ImageBuild(config.Basedir, workflow.serviceName, config.Service.Dockerfile, []string{workflow.serviceImage}, workflow.registryAuthConfig, dockerWriter)
}
}

Expand Down

0 comments on commit 03f42a6

Please sign in to comment.