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

View File

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