This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 327
docker: support for registry auth in deploy #2245
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,45 @@ func (r *Registry) pushWithDocker( | |
return status.Errorf(codes.Internal, "unable to parse image name: %s", err) | ||
} | ||
|
||
encodedAuth, err := r.findAuth(ref, cli, ctx, log) | ||
if err != nil { | ||
return status.Errorf(codes.Internal, "unable to extract registry credentials: %s", err) | ||
} | ||
|
||
|
||
step = sg.Add("Pushing Docker image...") | ||
|
||
options := types.ImagePushOptions{ | ||
RegistryAuth: encodedAuth, | ||
} | ||
|
||
responseBody, err := cli.ImagePush(ctx, reference.FamiliarString(ref), options) | ||
if err != nil { | ||
return status.Errorf(codes.Internal, "unable to push image to registry: %s", err) | ||
} | ||
|
||
defer responseBody.Close() | ||
|
||
var termFd uintptr | ||
if f, ok := stdout.(*os.File); ok { | ||
termFd = f.Fd() | ||
} | ||
|
||
err = jsonmessage.DisplayJSONMessagesStream(responseBody, step.TermOutput(), termFd, true, nil) | ||
if err != nil { | ||
return status.Errorf(codes.Internal, "unable to stream Docker logs to terminal: %s", err) | ||
} | ||
|
||
step.Done() | ||
return nil | ||
} | ||
|
||
func (r *Registry) findAuth( | ||
ref reference.Named, | ||
cli *client.Client, | ||
ctx context.Context, | ||
log hclog.Logger, | ||
) (string, error) { | ||
encodedAuth := r.config.EncodedAuth | ||
|
||
// If there was no explicit encoded auth but there is a password, make the username+password | ||
|
@@ -73,7 +112,7 @@ func (r *Registry) pushWithDocker( | |
|
||
buf, err := json.Marshal(authConfig) | ||
if err != nil { | ||
return status.Errorf(codes.Internal, "unable to generate authentication info for registry: %s", err) | ||
return "", status.Errorf(codes.Internal, "unable to generate authentication info for registry: %s", err) | ||
} | ||
encodedAuth = base64.URLEncoding.EncodeToString(buf) | ||
} | ||
|
@@ -83,7 +122,7 @@ func (r *Registry) pushWithDocker( | |
// Resolve the Repository name from fqn to RepositoryInfo | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this is the only part of the |
||
repoInfo, err := registry.ParseRepositoryInfo(ref) | ||
if err != nil { | ||
return status.Errorf(codes.Internal, "unable to parse repository info from image name: %s", err) | ||
return "", status.Errorf(codes.Internal, "unable to parse repository info from image name: %s", err) | ||
} | ||
|
||
var server string | ||
|
@@ -109,34 +148,9 @@ func (r *Registry) pushWithDocker( | |
authConfig, _ := cf.GetAuthConfig(server) | ||
buf, err := json.Marshal(authConfig) | ||
if err != nil { | ||
return status.Errorf(codes.Internal, "unable to generate authentication info for registry: %s", err) | ||
return "", status.Errorf(codes.Internal, "unable to generate authentication info for registry: %s", err) | ||
} | ||
encodedAuth = base64.URLEncoding.EncodeToString(buf) | ||
} | ||
|
||
step = sg.Add("Pushing Docker image...") | ||
|
||
options := types.ImagePushOptions{ | ||
RegistryAuth: encodedAuth, | ||
} | ||
|
||
responseBody, err := cli.ImagePush(ctx, reference.FamiliarString(ref), options) | ||
if err != nil { | ||
return status.Errorf(codes.Internal, "unable to push image to registry: %s", err) | ||
} | ||
|
||
defer responseBody.Close() | ||
|
||
var termFd uintptr | ||
if f, ok := stdout.(*os.File); ok { | ||
termFd = f.Fd() | ||
} | ||
|
||
err = jsonmessage.DisplayJSONMessagesStream(responseBody, step.TermOutput(), termFd, true, nil) | ||
if err != nil { | ||
return status.Errorf(codes.Internal, "unable to stream Docker logs to terminal: %s", err) | ||
} | ||
|
||
step.Done() | ||
return nil | ||
return encodedAuth, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could make
ctx
the first argument? That's our convention.