Skip to content

Commit

Permalink
#2 Image Push working
Browse files Browse the repository at this point in the history
  • Loading branch information
BrobotDubstep committed May 19, 2020
1 parent 245b99f commit 3a74cfb
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion deployments/registry/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
- 5000:5000
environment:
REGISTRY_AUTH_TOKEN_AUTOREDIRECT: "false"
REGISTRY_AUTH_TOKEN_REALM: http://192.168.0.10:5100/docker/auth/token
REGISTRY_AUTH_TOKEN_REALM: http://192.168.178.42:5100/docker/auth/token
REGISTRY_AUTH_TOKEN_SERVICE: http://localhost:5000
REGISTRY_AUTH_TOKEN_ISSUER: http://localhost:5100
REGISTRY_AUTH_TOKEN_ROOTCERTBUNDLE: /certs/registry-auth.crt
Expand Down
55 changes: 44 additions & 11 deletions pkg/harbourbuild/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,19 @@ func (b Builder) buildImage(job models.BuildJob) {
}

redisClient.HSet(job.BuildKey, "build_status", "Success", "logs", logs)
l.Trace("Image was built")
l.Tracef("Image %s was built", job.Request.Project)

imageString := b.getImageString(job.RegistryUrl, job.Request.Project)
if len(job.Request.Tags) > 0 {
imageString += ":" + job.Request.Tags[0]
}

if err = b.pushImage(imageString, job.RegistryToken); err != nil {
l.WithError(err).Error("Error while pushing image to registry")
return
}

l.Tracef("Image %s was pushed to registry %s", job.Request.Project, job.RegistryUrl)
}

func (b Builder) cleanUpAfterBuild(buildContext *os.File, logs io.ReadCloser) {
Expand All @@ -98,12 +110,6 @@ func (b Builder) cleanUpAfterBuild(buildContext *os.File, logs io.ReadCloser) {
}
}

//TODO Communicate with Harbour SCM in order to receive the path to the project-files
func (b Builder) getProjectPath(project string) (string, error) {
// Just returns a demo path
return fmt.Sprintf(b.repoPath+"%s", project), nil
}

func (b Builder) createBuildContext(project string) (*os.File, error) {
buildContext := fmt.Sprintf(b.ctxPath+"%s.tar", project)
projectPath, err := b.getProjectPath(project)
Expand All @@ -126,21 +132,48 @@ func (b Builder) createBuildContext(project string) (*os.File, error) {
}

func (b Builder) pushImage(image string, token string) error {
authConfig := types.AuthConfig{IdentityToken: token}
authConfig := types.AuthConfig{RegistryToken: token}
encodedJSON, err := json.Marshal(authConfig)
if err != nil {
return err
}

authStr := base64.URLEncoding.EncodeToString(encodedJSON)
options := types.ImagePushOptions{RegistryAuth: authStr}

out, err := b.cli.ImagePush(b.ctx, image, types.ImagePushOptions{RegistryAuth: authStr})
out, err := b.cli.ImagePush(b.ctx, image, options)
if err != nil {
return err
}

defer out.Close()
io.Copy(os.Stdout, out)
defer func() {
err = out.Close()
if err != nil {
l.WithError(err).Error("Error while closing file")
return
}
}()

buf := new(bytes.Buffer)
_, err = buf.ReadFrom(out)
if err != nil {
return err
}

logs := buf.String()
if strings.Contains(logs, "errorDetail") {
return fmt.Errorf("unable to push image: %s", logs)
}

return nil
}

func (b Builder) getImageString(registryUrl string, image string) string {
return fmt.Sprintf("%s/%s", strings.Split(registryUrl, "//")[1], image)
}

//TODO Communicate with Harbour SCM in order to receive the path to the project-files
func (b Builder) getProjectPath(project string) (string, error) {
// Just returns a demo path
return fmt.Sprintf(b.repoPath+"%s", project), nil
}
13 changes: 9 additions & 4 deletions pkg/harbourbuild/handler/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (b BuilderModel) BuildImage(w http.ResponseWriter, r *http.Request) {
return
}

registryToken, err := fetchRegistryToken(r.Context(), b.config)
registryToken, err := fetchRegistryToken(r.Context(), buildRequest.Project, b.config)
if err != nil {
return // Error is already logged in get
}
Expand All @@ -47,7 +47,12 @@ func (b BuilderModel) BuildImage(w http.ResponseWriter, r *http.Request) {
return
}

b.buildChan <- models.BuildJob{Request: buildRequest, BuildKey: buildKey, RegistryToken: registryToken}
b.buildChan <- models.BuildJob{
Request: buildRequest,
BuildKey: buildKey,
RegistryToken: registryToken,
RegistryUrl: b.config.DockerRegistry.RegistryUrl,
}

log.Trace("Build job enqueued")
w.WriteHeader(http.StatusAccepted)
Expand All @@ -74,9 +79,9 @@ func createBuildEntry(ctx context.Context, request models.BuildRequest) (string,
return buildKey, nil
}

func fetchRegistryToken(ctx context.Context, registry *configuration.Options) (string, error) {
func fetchRegistryToken(ctx context.Context, repository string, registry *configuration.Options) (string, error) {
oidcTokenStr := auth.GetOidcTokenStrCtx(ctx)
tokenUrl := registry.DockerRegistry.TokenURL("repository", "test", "push")
tokenUrl := registry.DockerRegistry.TokenURL("repository", repository, "push,pull")

var registryToken string
var tokenResponse registryModels.DockerTokenResponse
Expand Down
1 change: 1 addition & 0 deletions pkg/harbourbuild/models/buildjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ type BuildJob struct {
Request BuildRequest
BuildKey string
RegistryToken string
RegistryUrl string
}

0 comments on commit 3a74cfb

Please sign in to comment.