fix: [CDE-508]: use image auth map (#3058)

* use image auth map
This commit is contained in:
Deepak Bhatt 2024-11-28 07:39:06 +00:00 committed by Harness
parent ed0e904c59
commit b4baf364a9
2 changed files with 14 additions and 13 deletions

View File

@ -306,7 +306,7 @@ func PullImage(
dockerClient *client.Client,
runArgsMap map[types.RunArg]*types.RunArgValue,
gitspaceLogger gitspaceTypes.GitspaceLogger,
dockerRegistryAuth gitspaceTypes.DockerRegistryAuth,
imageAuthMap map[string]gitspaceTypes.DockerRegistryAuth,
) error {
imagePullRunArg := getImagePullPolicy(runArgsMap)
gitspaceLogger.Info("Image pull policy is: " + imagePullRunArg)
@ -331,7 +331,7 @@ func PullImage(
gitspaceLogger.Info("Pulling image: " + imageName)
pullOpts, err := buildImagePullOptions(getPlatform(runArgsMap), dockerRegistryAuth)
pullOpts, err := buildImagePullOptions(imageName, getPlatform(runArgsMap), imageAuthMap)
if err != nil {
return logStreamWrapError(gitspaceLogger, "Error building image pull options", err)
}
@ -400,15 +400,16 @@ func isImagePresentLocally(ctx context.Context, imageName string, dockerClient *
}
func buildImagePullOptions(
imageName,
platform string,
dockerRegistryAuth gitspaceTypes.DockerRegistryAuth,
imageAuthMap map[string]gitspaceTypes.DockerRegistryAuth,
) (image.PullOptions, error) {
pullOpts := image.PullOptions{Platform: platform}
if dockerRegistryAuth.RegistryURL != "" {
if imageAuth, ok := imageAuthMap[imageName]; ok {
authConfig := registry.AuthConfig{
Username: dockerRegistryAuth.Username.Value(),
Password: dockerRegistryAuth.Password.Value(),
ServerAddress: dockerRegistryAuth.RegistryURL,
Username: imageAuth.Username.Value(),
Password: imageAuth.Password.Value(),
ServerAddress: imageAuth.RegistryURL,
}
auth, err := encodeAuthToBase64(authConfig)
if err != nil {

View File

@ -116,7 +116,7 @@ func (e *EmbeddedDockerOrchestrator) CreateAndStartGitspace(
defer e.closeDockerClient(dockerClient)
// todo : update the code when private repository integration is supported in gitness
dockerRegistryAuth := gitspaceTypes.DockerRegistryAuth{}
imagAuthMap := make(map[string]gitspaceTypes.DockerRegistryAuth)
// Step 3: Check the current state of the container
state, err := e.checkContainerState(ctx, dockerClient, containerName)
@ -149,7 +149,7 @@ func (e *EmbeddedDockerOrchestrator) CreateAndStartGitspace(
infra,
defaultBaseImage,
ideService,
dockerRegistryAuth); err != nil {
imagAuthMap); err != nil {
return nil, err
}
case ContainerStatePaused, ContainerStateCreated, ContainerStateUnknown, ContainerStateDead:
@ -375,7 +375,7 @@ func (e *EmbeddedDockerOrchestrator) runGitspaceSetupSteps(
resolvedRepoDetails scm.ResolvedDetails,
defaultBaseImage string,
gitspaceLogger gitspaceTypes.GitspaceLogger,
dockerRegistryAuth gitspaceTypes.DockerRegistryAuth,
imageAuthMap map[string]gitspaceTypes.DockerRegistryAuth,
) error {
homeDir := GetUserHomeDir(gitspaceConfig.GitspaceUser.Identifier)
containerName := GetGitspaceContainerName(gitspaceConfig)
@ -393,7 +393,7 @@ func (e *EmbeddedDockerOrchestrator) runGitspaceSetupSteps(
}
// Pull the required image
if err := PullImage(ctx, imageName, dockerClient, runArgsMap, gitspaceLogger, dockerRegistryAuth); err != nil {
if err := PullImage(ctx, imageName, dockerClient, runArgsMap, gitspaceLogger, imageAuthMap); err != nil {
return err
}
portMappings := infrastructure.GitspacePortMappings
@ -683,7 +683,7 @@ func (e *EmbeddedDockerOrchestrator) createAndStartNewGitspace(
infrastructure types.Infrastructure,
defaultBaseImage string,
ideService ide.IDE,
dockerRegistryAuth gitspaceTypes.DockerRegistryAuth,
imageAuthMap map[string]gitspaceTypes.DockerRegistryAuth,
) error {
logStreamInstance, err := e.statefulLogger.CreateLogStream(ctx, gitspaceConfig.ID)
if err != nil {
@ -700,7 +700,7 @@ func (e *EmbeddedDockerOrchestrator) createAndStartNewGitspace(
resolvedRepoDetails,
defaultBaseImage,
logStreamInstance,
dockerRegistryAuth,
imageAuthMap,
)
if startErr != nil {
return fmt.Errorf("failed to start gitspace %s: %w", gitspaceConfig.Identifier, startErr)