Merge pull request #7769 from devopswithnaman/fix/registry-error-handling
Improve error handling in docker operations
This commit is contained in:
@@ -2,7 +2,6 @@ package registry
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -96,14 +95,12 @@ func pushToRegistry(appName string, tag int, imageID string, imageRepo string) e
|
|||||||
fullImage := fmt.Sprintf("%s%s:%d", registryServer, imageRepo, tag)
|
fullImage := fmt.Sprintf("%s%s:%d", registryServer, imageRepo, tag)
|
||||||
|
|
||||||
common.LogVerboseQuiet(fmt.Sprintf("Tagging %s:%d in registry format", imageRepo, tag))
|
common.LogVerboseQuiet(fmt.Sprintf("Tagging %s:%d in registry format", imageRepo, tag))
|
||||||
if !dockerTag(imageID, fullImage) {
|
if err := dockerTag(imageID, fullImage); err != nil {
|
||||||
// TODO: better error
|
return fmt.Errorf("unable to tag image %s as %s: %w", imageID, fullImage, err)
|
||||||
return errors.New("Unable to tag image")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !dockerTag(imageID, fmt.Sprintf("%s:%d", imageRepo, tag)) {
|
if err := dockerTag(imageID, fmt.Sprintf("%s:%d", imageRepo, tag)); err != nil {
|
||||||
// TODO: better error
|
return fmt.Errorf("unable to tag image %s as %s:%d: %w", imageID, imageRepo, tag, err)
|
||||||
return errors.New("Unable to tag image")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extraTags := getRegistryPushExtraTagsForApp(appName)
|
extraTags := getRegistryPushExtraTagsForApp(appName)
|
||||||
@@ -112,26 +109,25 @@ func pushToRegistry(appName string, tag int, imageID string, imageRepo string) e
|
|||||||
for _, extraTag := range extraTagsArray {
|
for _, extraTag := range extraTagsArray {
|
||||||
extraTagImage := fmt.Sprintf("%s%s:%s", registryServer, imageRepo, extraTag)
|
extraTagImage := fmt.Sprintf("%s%s:%s", registryServer, imageRepo, extraTag)
|
||||||
common.LogVerboseQuiet(fmt.Sprintf("Tagging %s as %s in registry format", imageRepo, extraTag))
|
common.LogVerboseQuiet(fmt.Sprintf("Tagging %s as %s in registry format", imageRepo, extraTag))
|
||||||
if !dockerTag(imageID, extraTagImage) {
|
if err := dockerTag(imageID, extraTagImage); err != nil {
|
||||||
return errors.New(fmt.Sprintf("Unable to tag image as %s", extraTag))
|
return fmt.Errorf("unable to tag image %s as %s: %w", imageID, extraTagImage, err)
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
common.LogVerboseQuiet(fmt.Sprintf("Untagging extra tag %s", extraTag))
|
common.LogVerboseQuiet(fmt.Sprintf("Untagging extra tag %s", extraTag))
|
||||||
if err := common.RemoveImages([]string{extraTagImage}); err != nil {
|
if err := common.RemoveImages([]string{extraTagImage}); err != nil {
|
||||||
common.LogWarn(fmt.Sprintf("Unable to untag extra tag %s", extraTag, err.Error()))
|
common.LogWarn(fmt.Sprintf("Unable to untag extra tag %s: %s", extraTag, err.Error()))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
common.LogVerboseQuiet(fmt.Sprintf("Pushing %s", extraTagImage))
|
common.LogVerboseQuiet(fmt.Sprintf("Pushing %s", extraTagImage))
|
||||||
if !dockerPush(extraTagImage) {
|
if err := dockerPush(extraTagImage); err != nil {
|
||||||
return errors.New(fmt.Sprintf("Unable to push image with %s tag", extraTag))
|
return fmt.Errorf("unable to push image with %s tag: %w", extraTag, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
common.LogVerboseQuiet(fmt.Sprintf("Pushing %s", fullImage))
|
common.LogVerboseQuiet(fmt.Sprintf("Pushing %s", fullImage))
|
||||||
if !dockerPush(fullImage) {
|
if err := dockerPush(fullImage); err != nil {
|
||||||
// TODO: better error
|
return fmt.Errorf("unable to push image %s: %w", fullImage, err)
|
||||||
return errors.New("Unable to push image")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only clean up when the scheduler is not docker-local
|
// Only clean up when the scheduler is not docker-local
|
||||||
@@ -148,22 +144,34 @@ func pushToRegistry(appName string, tag int, imageID string, imageRepo string) e
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func dockerTag(imageID string, imageTag string) bool {
|
func dockerTag(imageID string, imageTag string) error {
|
||||||
result, err := common.CallExecCommand(common.ExecCommandInput{
|
result, err := common.CallExecCommand(common.ExecCommandInput{
|
||||||
Command: common.DockerBin(),
|
Command: common.DockerBin(),
|
||||||
Args: []string{"image", "tag", imageID, imageTag},
|
Args: []string{"image", "tag", imageID, imageTag},
|
||||||
StreamStdio: true,
|
StreamStdio: true,
|
||||||
})
|
})
|
||||||
return err == nil && result.ExitCode == 0
|
if err != nil {
|
||||||
|
return fmt.Errorf("docker tag command failed: %w", err)
|
||||||
|
}
|
||||||
|
if result.ExitCode != 0 {
|
||||||
|
return fmt.Errorf("docker tag command exited with code %d: %s", result.ExitCode, result.Stderr)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func dockerPush(imageTag string) bool {
|
func dockerPush(imageTag string) error {
|
||||||
result, err := common.CallExecCommand(common.ExecCommandInput{
|
result, err := common.CallExecCommand(common.ExecCommandInput{
|
||||||
Command: common.DockerBin(),
|
Command: common.DockerBin(),
|
||||||
Args: []string{"image", "push", imageTag},
|
Args: []string{"image", "push", imageTag},
|
||||||
StreamStdio: true,
|
StreamStdio: true,
|
||||||
})
|
})
|
||||||
return err == nil && result.ExitCode == 0
|
if err != nil {
|
||||||
|
return fmt.Errorf("docker push command failed: %w", err)
|
||||||
|
}
|
||||||
|
if result.ExitCode != 0 {
|
||||||
|
return fmt.Errorf("docker push command exited with code %d: %s", result.ExitCode, result.Stderr)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func imageCleanup(appName string, imageRepo string, imageTag string, tag int) {
|
func imageCleanup(appName string, imageRepo string, imageTag string, tag int) {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package registry
|
package registry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -108,9 +107,8 @@ func TriggerPostReleaseBuilder(appName string, image string) error {
|
|||||||
newImage := strings.Replace(image, imageRepo+":", computedImageRepo+":", 1)
|
newImage := strings.Replace(image, imageRepo+":", computedImageRepo+":", 1)
|
||||||
|
|
||||||
if computedImageRepo != imageRepo {
|
if computedImageRepo != imageRepo {
|
||||||
if !dockerTag(imageID, newImage) {
|
if err := dockerTag(imageID, newImage); err != nil {
|
||||||
// TODO: better error
|
return fmt.Errorf("unable to tag image %s as %s: %w", imageID, newImage, err)
|
||||||
return errors.New("Unable to tag image")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user