mirror of https://github.com/harness/drone.git
feat: [CDE-549]: Copy image using skopeo (#3127)
* feat: [CDE-549]: fix * feat: [CDE-549]: fix lint * feat: [CDE-549]: fix * feat: [CDE-549]: fix * feat: [CDE-549]: update command * feat: [CDE-549]: Copy image using skopeopull/3597/head
parent
9cd6480f9d
commit
df3342aefe
|
@ -15,12 +15,14 @@
|
||||||
package container
|
package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
goruntime "runtime"
|
goruntime "runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -43,6 +45,7 @@ import (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
catchAllIP = "0.0.0.0"
|
catchAllIP = "0.0.0.0"
|
||||||
|
imagePullRunArgMissing = "missing"
|
||||||
)
|
)
|
||||||
|
|
||||||
var containerStateMapping = map[string]State{
|
var containerStateMapping = map[string]State{
|
||||||
|
@ -338,6 +341,90 @@ func ExtractMetadataAndUserFromImage(
|
||||||
return metadataMap, imageUser, nil
|
return metadataMap, imageUser, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CopyImage(
|
||||||
|
ctx context.Context,
|
||||||
|
imageName string,
|
||||||
|
dockerClient *client.Client,
|
||||||
|
runArgsMap map[types.RunArg]*types.RunArgValue,
|
||||||
|
gitspaceLogger gitspaceTypes.GitspaceLogger,
|
||||||
|
imageAuthMap map[string]gitspaceTypes.DockerRegistryAuth,
|
||||||
|
httpProxyURL types.MaskSecret,
|
||||||
|
httpsProxyURL types.MaskSecret,
|
||||||
|
) error {
|
||||||
|
gitspaceLogger.Info("Copying image " + imageName + " to local")
|
||||||
|
imagePullRunArg := getImagePullPolicy(runArgsMap)
|
||||||
|
if imagePullRunArg == "never" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if imagePullRunArg == imagePullRunArgMissing {
|
||||||
|
imagePresentLocally, err := isImagePresentLocally(ctx, imageName, dockerClient)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if imagePresentLocally {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build skopeo command
|
||||||
|
platform := getPlatform(runArgsMap)
|
||||||
|
args := []string{"copy"}
|
||||||
|
|
||||||
|
if platform != "" {
|
||||||
|
args = append(args, "--override-os", platform)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add credentials if available
|
||||||
|
if auth, ok := imageAuthMap[imageName]; ok && auth.Password != nil {
|
||||||
|
gitspaceLogger.Info("Using credentials for registry: " + auth.RegistryURL)
|
||||||
|
args = append(args, "--src-creds", auth.Username.Value()+":"+auth.Password.Value())
|
||||||
|
} else {
|
||||||
|
gitspaceLogger.Warn("No credentials found for registry. Proceeding without authentication.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Source and destination
|
||||||
|
source := "docker://" + imageName
|
||||||
|
destination := "docker-daemon:" + imageName
|
||||||
|
args = append(args, source, destination)
|
||||||
|
|
||||||
|
cmd := exec.CommandContext(ctx, "skopeo", args...)
|
||||||
|
|
||||||
|
// Set proxy environment variables if provided
|
||||||
|
env := cmd.Env
|
||||||
|
if httpProxyURL.Value() != "" {
|
||||||
|
env = append(env, "HTTP_PROXY="+httpProxyURL.Value())
|
||||||
|
log.Info().Msg("HTTP_PROXY set in environment")
|
||||||
|
}
|
||||||
|
if httpsProxyURL.Value() != "" {
|
||||||
|
env = append(env, "HTTPS_PROXY="+httpsProxyURL.Value())
|
||||||
|
log.Info().Msg("HTTPS_PROXY set in environment")
|
||||||
|
}
|
||||||
|
cmd.Env = env
|
||||||
|
|
||||||
|
var outBuf, errBuf bytes.Buffer
|
||||||
|
cmd.Stdout = &outBuf
|
||||||
|
cmd.Stderr = &errBuf
|
||||||
|
|
||||||
|
gitspaceLogger.Info("Executing image copy command: " + cmd.String())
|
||||||
|
err := cmd.Run()
|
||||||
|
|
||||||
|
// Log command output
|
||||||
|
if outBuf.Len() > 0 {
|
||||||
|
gitspaceLogger.Info("Image copy output: " + outBuf.String())
|
||||||
|
}
|
||||||
|
if errBuf.Len() > 0 {
|
||||||
|
gitspaceLogger.Error("Image copy error output: "+errBuf.String(), nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return logStreamWrapError(gitspaceLogger, "Error while pulling image using skopeo", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
gitspaceLogger.Info("Image copy completed successfully using skopeo")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func PullImage(
|
func PullImage(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
imageName string,
|
imageName string,
|
||||||
|
@ -351,7 +438,7 @@ func PullImage(
|
||||||
if imagePullRunArg == "never" {
|
if imagePullRunArg == "never" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if imagePullRunArg == "missing" {
|
if imagePullRunArg == imagePullRunArgMissing {
|
||||||
gitspaceLogger.Info("Checking if image " + imageName + " is present locally")
|
gitspaceLogger.Info("Checking if image " + imageName + " is present locally")
|
||||||
imagePresentLocally, err := isImagePresentLocally(ctx, imageName, dockerClient)
|
imagePresentLocally, err := isImagePresentLocally(ctx, imageName, dockerClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -126,7 +126,7 @@ func (c PlatformConnectorAuthSpec) ExtractPasswordRef() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c PlatformConnectorAuthSpec) ExtractPassword() string {
|
func (c PlatformConnectorAuthSpec) ExtractPassword() string {
|
||||||
if c.AuthType == UserNamePasswordPlatformConnectorAuthType {
|
if c.AuthType == UserNamePasswordPlatformConnectorAuthType && c.Password != nil {
|
||||||
return c.Password.Value()
|
return c.Password.Value()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue